From a07208518f1747d9ee9d7e586b707302e7bb77df Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Tue, 20 Feb 2018 10:41:26 -0800 Subject: [PATCH] Create different internal error types --- pkg/kinflate/internal/configmaperror.go | 28 ++++++ .../configmaperror_test.go} | 24 +++-- pkg/kinflate/internal/manifesterror.go | 57 ++++++++++++ pkg/kinflate/internal/manifesterror_test.go | 92 +++++++++++++++++++ pkg/kinflate/internal/patcherror.go | 31 +++++++ pkg/kinflate/internal/patcherror_test.go | 41 +++++++++ .../resourceerror.go} | 9 +- pkg/kinflate/internal/resourceerror_test.go | 41 +++++++++ pkg/kinflate/internal/secreterror.go | 28 ++++++ pkg/kinflate/internal/secreterror_test.go | 37 ++++++++ 10 files changed, 374 insertions(+), 14 deletions(-) create mode 100644 pkg/kinflate/internal/configmaperror.go rename pkg/kinflate/{util/manifesterror_test.go => internal/configmaperror_test.go} (55%) create mode 100644 pkg/kinflate/internal/manifesterror.go create mode 100644 pkg/kinflate/internal/manifesterror_test.go create mode 100644 pkg/kinflate/internal/patcherror.go create mode 100644 pkg/kinflate/internal/patcherror_test.go rename pkg/kinflate/{util/manifesterror.go => internal/resourceerror.go} (74%) create mode 100644 pkg/kinflate/internal/resourceerror_test.go create mode 100644 pkg/kinflate/internal/secreterror.go create mode 100644 pkg/kinflate/internal/secreterror_test.go diff --git a/pkg/kinflate/internal/configmaperror.go b/pkg/kinflate/internal/configmaperror.go new file mode 100644 index 000000000..786929b4c --- /dev/null +++ b/pkg/kinflate/internal/configmaperror.go @@ -0,0 +1,28 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import "fmt" + +type ConfigmapError struct { + ManifestFilepath string + ErrorMsg string +} + +func (e ConfigmapError) Error() string { + return fmt.Sprintf("Manifest file [%s] encounters a configmap error: %s\n", e.ManifestFilepath, e.ErrorMsg) +} diff --git a/pkg/kinflate/util/manifesterror_test.go b/pkg/kinflate/internal/configmaperror_test.go similarity index 55% rename from pkg/kinflate/util/manifesterror_test.go rename to pkg/kinflate/internal/configmaperror_test.go index 0687f4292..57e0a2996 100644 --- a/pkg/kinflate/util/manifesterror_test.go +++ b/pkg/kinflate/internal/configmaperror_test.go @@ -14,21 +14,25 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package internal import ( - "fmt" + "strings" "testing" ) -func TestManifestError_Error(t *testing.T) { +func TestConfigmapError_Error(t *testing.T) { filepath := "/path/to/Kube-manifest.yaml" - errorMsg := "Manifest not found" - expectedErrorMsg := fmt.Sprintf("Manifest File [%s]: %s\n", filepath, errorMsg) - me := ManifestError{ManifestFilepath: filepath, ErrorMsg: errorMsg} - if me.Error() != expectedErrorMsg { - t.Errorf("Incorrect ManifestError.Error() message\n") - t.Errorf(" Expected: %s\n", expectedErrorMsg) - t.Errorf(" Got: %s\n", me.Error()) + errorMsg := "configmap name is missing" + me := ConfigmapError{ManifestFilepath: filepath, ErrorMsg: errorMsg} + + if !strings.Contains(me.Error(), filepath) { + t.Errorf("Incorrect ConfigmapError.Error() message \n") + t.Errorf("Expected filepath %s, but unfound\n", filepath) + } + + if !strings.Contains(me.Error(), errorMsg) { + t.Errorf("Incorrect ConfigmapError.Error() message \n") + t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg) } } diff --git a/pkg/kinflate/internal/manifesterror.go b/pkg/kinflate/internal/manifesterror.go new file mode 100644 index 000000000..21e112ea3 --- /dev/null +++ b/pkg/kinflate/internal/manifesterror.go @@ -0,0 +1,57 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "fmt" +) + +// First pass to encapsulate fields for more informative error messages. +type ManifestError struct { + ManifestFilepath string + ErrorMsg string +} + +func (me ManifestError) Error() string { + return fmt.Sprintf("Manifest File [%s]: %s\n", me.ManifestFilepath, me.ErrorMsg) +} + +type ManifestErrors struct { + merrors []error +} + +func (me *ManifestErrors) Error() string { + errormsg := "" + for _, e := range me.merrors { + errormsg += e.Error() + "\n" + } + return errormsg +} + +func (me *ManifestErrors) Append(e error) { + me.merrors = append(me.merrors, e) +} + +func (me *ManifestErrors) Get() []error { + return me.merrors +} + +func (me *ManifestErrors) BatchAppend(e ManifestErrors) { + for _, err := range e.Get() { + me.merrors = append(me.merrors, err) + } +} diff --git a/pkg/kinflate/internal/manifesterror_test.go b/pkg/kinflate/internal/manifesterror_test.go new file mode 100644 index 000000000..460cd2103 --- /dev/null +++ b/pkg/kinflate/internal/manifesterror_test.go @@ -0,0 +1,92 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "fmt" + "strings" + "testing" +) + +func TestManifestError_Error(t *testing.T) { + filepath := "/path/to/Kube-manifest.yaml" + errorMsg := "Manifest not found" + + me := ManifestError{ManifestFilepath: filepath, ErrorMsg: errorMsg} + + if !strings.Contains(me.Error(), filepath) { + t.Errorf("Incorrect ManifestError.Error() message \n") + t.Errorf("Expected filepath %s, but unfound\n", filepath) + } + + if !strings.Contains(me.Error(), errorMsg) { + t.Errorf("Incorrect ManifestError.Error() message \n") + t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg) + } + +} + +func TestManifestErrors_Error(t *testing.T) { + filepath := "/path/to/Kube-manifest.yaml" + me := ManifestError{ManifestFilepath: filepath, ErrorMsg: "Manifest not found"} + ce := ConfigmapError{ManifestFilepath: filepath, ErrorMsg: "can't find configmap name"} + pe := PatchError{ManifestFilepath: filepath, PatchFilepath: filepath, ErrorMsg: "can't find patch file"} + re := ResourceError{ManifestFilepath: filepath, ResourceFilepath: filepath, ErrorMsg: "can't find resource file"} + se := SecretError{ManifestFilepath: filepath, ErrorMsg: "can't find secret name"} + mes := ManifestErrors{merrors: []error{me, ce, pe, re, se}} + expectedErrorMsg := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n", me.Error(), ce.Error(), pe.Error(), re.Error(), se.Error()) + if mes.Error() != expectedErrorMsg { + t.Errorf("Incorrect ManifestErrors.Error() message\n") + t.Errorf(" Expected: %s\n", expectedErrorMsg) + t.Errorf(" Got: %s\n", mes.Error()) + } +} + +func TestManifestErrors_Get(t *testing.T) { + ce := ConfigmapError{ManifestFilepath: "manifest/filepath", ErrorMsg: "can't find configmap name"} + mes := ManifestErrors{merrors: []error{ce}} + if len(mes.Get()) != 1 { + t.Errorf("Incorrect ManifestErrors.Get()\n") + t.Errorf(" Expected: %v\n", []error{ce}) + t.Errorf(" Got: %s\n", mes.Get()) + } +} + +func TestManifestErrors_Append(t *testing.T) { + ce := ConfigmapError{ManifestFilepath: "manifest/filepath", ErrorMsg: "can't find configmap name"} + pe := PatchError{ManifestFilepath: "manifest/filepath", PatchFilepath: "patch/path", ErrorMsg: "can't find patch file"} + mes := ManifestErrors{merrors: []error{ce}} + mes.Append(pe) + if len(mes.Get()) != 2 { + t.Errorf("Incorrect ManifestErrors.Append()\n") + t.Errorf(" Expected: %d errors\n%v/n", 2, []error{ce, pe}) + t.Errorf(" Got: %d errors\n%v\n", len(mes.Get()), mes.Get()) + } +} + +func TestManifestErrors_BatchAppend(t *testing.T) { + ce := ConfigmapError{ManifestFilepath: "manifest/filepath", ErrorMsg: "can't find configmap name"} + pe := PatchError{ManifestFilepath: "manifest/filepath", PatchFilepath: "patch/path", ErrorMsg: "can't find patch file"} + mes := ManifestErrors{merrors: []error{ce}} + me := ManifestErrors{merrors: []error{pe}} + mes.BatchAppend(me) + if len(mes.Get()) != 2 { + t.Errorf("Incorrect ManifestErrors.Append()\n") + t.Errorf(" Expected: %d errors\n%v/n", 2, []error{ce, pe}) + t.Errorf(" Got: %d errors\n%v\n", len(mes.Get()), mes.Get()) + } +} diff --git a/pkg/kinflate/internal/patcherror.go b/pkg/kinflate/internal/patcherror.go new file mode 100644 index 000000000..6321d5f1d --- /dev/null +++ b/pkg/kinflate/internal/patcherror.go @@ -0,0 +1,31 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "fmt" +) + +type PatchError struct { + ManifestFilepath string + PatchFilepath string + ErrorMsg string +} + +func (e PatchError) Error() string { + return fmt.Sprintf("Manifest file [%s] encounters a patch error for [%s]: %s\n", e.ManifestFilepath, e.PatchFilepath, e.ErrorMsg) +} diff --git a/pkg/kinflate/internal/patcherror_test.go b/pkg/kinflate/internal/patcherror_test.go new file mode 100644 index 000000000..7078ffa4c --- /dev/null +++ b/pkg/kinflate/internal/patcherror_test.go @@ -0,0 +1,41 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "strings" + "testing" +) + +func TestPatchError_Error(t *testing.T) { + filepath := "/path/to/Kube-manifest.yaml" + patchfilepath := "/path/to/patch/patch.yaml" + errorMsg := "file not found" + me := PatchError{ManifestFilepath: filepath, PatchFilepath: patchfilepath, ErrorMsg: errorMsg} + if !strings.Contains(me.Error(), filepath) { + t.Errorf("Incorrect PatchError.Error() message \n") + t.Errorf("Expected filepath %s, but unfound\n", filepath) + } + if !strings.Contains(me.Error(), patchfilepath) { + t.Errorf("Incorrect PatchError.Error() message \n") + t.Errorf("Expected patchfilepath %s, but unfound\n", patchfilepath) + } + if !strings.Contains(me.Error(), errorMsg) { + t.Errorf("Incorrect PatchError.Error() message \n") + t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg) + } +} diff --git a/pkg/kinflate/util/manifesterror.go b/pkg/kinflate/internal/resourceerror.go similarity index 74% rename from pkg/kinflate/util/manifesterror.go rename to pkg/kinflate/internal/resourceerror.go index b63278a35..753268dbf 100644 --- a/pkg/kinflate/util/manifesterror.go +++ b/pkg/kinflate/internal/resourceerror.go @@ -14,16 +14,17 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package internal import "fmt" // First pass to encapsulate fields for more informative error messages. -type ManifestError struct { +type ResourceError struct { ManifestFilepath string + ResourceFilepath string ErrorMsg string } -func (me ManifestError) Error() string { - return fmt.Sprintf("Manifest File [%s]: %s\n", me.ManifestFilepath, me.ErrorMsg) +func (e ResourceError) Error() string { + return fmt.Sprintf("Manifest file [%s] encounters a resource error for [%s]: %s\n", e.ManifestFilepath, e.ResourceFilepath, e.ErrorMsg) } diff --git a/pkg/kinflate/internal/resourceerror_test.go b/pkg/kinflate/internal/resourceerror_test.go new file mode 100644 index 000000000..d83fcac86 --- /dev/null +++ b/pkg/kinflate/internal/resourceerror_test.go @@ -0,0 +1,41 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "strings" + "testing" +) + +func TestResourceError_Error(t *testing.T) { + filepath := "/path/to/Kube-manifest.yaml" + resourcefilepath := "/path/to/resource/deployment.yaml" + errorMsg := "file not found" + me := ResourceError{ManifestFilepath: filepath, ResourceFilepath: resourcefilepath, ErrorMsg: errorMsg} + if !strings.Contains(me.Error(), filepath) { + t.Errorf("Incorrect ResourceError.Error() message \n") + t.Errorf("Expected filepath %s, but unfound\n", filepath) + } + if !strings.Contains(me.Error(), resourcefilepath) { + t.Errorf("Incorrect ResourceError.Error() message \n") + t.Errorf("Expected resourcefilepath %s, but unfound\n", resourcefilepath) + } + if !strings.Contains(me.Error(), errorMsg) { + t.Errorf("Incorrect ResourceError.Error() message \n") + t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg) + } +} diff --git a/pkg/kinflate/internal/secreterror.go b/pkg/kinflate/internal/secreterror.go new file mode 100644 index 000000000..1fd2b77e3 --- /dev/null +++ b/pkg/kinflate/internal/secreterror.go @@ -0,0 +1,28 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import "fmt" + +type SecretError struct { + ManifestFilepath string + ErrorMsg string +} + +func (e SecretError) Error() string { + return fmt.Sprintf("Manifest file [%s] encounters a secret error: %s\n", e.ManifestFilepath, e.ErrorMsg) +} diff --git a/pkg/kinflate/internal/secreterror_test.go b/pkg/kinflate/internal/secreterror_test.go new file mode 100644 index 000000000..8dddd10a6 --- /dev/null +++ b/pkg/kinflate/internal/secreterror_test.go @@ -0,0 +1,37 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "strings" + "testing" +) + +func TestSecretError_Error(t *testing.T) { + filepath := "/path/to/secret.yaml" + errorMsg := "missing a command" + me := SecretError{ManifestFilepath: filepath, ErrorMsg: errorMsg} + if !strings.Contains(me.Error(), filepath) { + t.Errorf("Incorrect SecretError.Error() message \n") + t.Errorf("Expected filepath %s, but unfound\n", filepath) + } + + if !strings.Contains(me.Error(), errorMsg) { + t.Errorf("Incorrect SecretError.Error() message \n") + t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg) + } +}