mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 18:10:59 +00:00
Isolated content of pkg/kustomize
This commit is contained in:
28
pkg/internal/error/configmaperror.go
Normal file
28
pkg/internal/error/configmaperror.go
Normal file
@@ -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 error
|
||||
|
||||
import "fmt"
|
||||
|
||||
type ConfigmapError struct {
|
||||
Path string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (e ConfigmapError) Error() string {
|
||||
return fmt.Sprintf("Kustomization file [%s] encounters a configmap error: %s\n", e.Path, e.ErrorMsg)
|
||||
}
|
||||
40
pkg/internal/error/configmaperror_test.go
Normal file
40
pkg/internal/error/configmaperror_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
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 error
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
)
|
||||
|
||||
func TestConfigmapError_Error(t *testing.T) {
|
||||
filepath := "/path/to/" + constants.KustomizationFileName
|
||||
errorMsg := "configmap name is missing"
|
||||
me := ConfigmapError{Path: 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)
|
||||
}
|
||||
}
|
||||
57
pkg/internal/error/kustomizationerror.go
Normal file
57
pkg/internal/error/kustomizationerror.go
Normal file
@@ -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 error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// First pass to encapsulate fields for more informative error messages.
|
||||
type KustomizationError struct {
|
||||
KustomizationPath string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (ke KustomizationError) Error() string {
|
||||
return fmt.Sprintf("Kustomization File [%s]: %s\n", ke.KustomizationPath, ke.ErrorMsg)
|
||||
}
|
||||
|
||||
type KustomizationErrors struct {
|
||||
kErrors []error
|
||||
}
|
||||
|
||||
func (ke *KustomizationErrors) Error() string {
|
||||
errormsg := ""
|
||||
for _, e := range ke.kErrors {
|
||||
errormsg += e.Error() + "\n"
|
||||
}
|
||||
return errormsg
|
||||
}
|
||||
|
||||
func (ke *KustomizationErrors) Append(e error) {
|
||||
ke.kErrors = append(ke.kErrors, e)
|
||||
}
|
||||
|
||||
func (ke *KustomizationErrors) Get() []error {
|
||||
return ke.kErrors
|
||||
}
|
||||
|
||||
func (ke *KustomizationErrors) BatchAppend(e KustomizationErrors) {
|
||||
for _, err := range e.Get() {
|
||||
ke.kErrors = append(ke.kErrors, err)
|
||||
}
|
||||
}
|
||||
94
pkg/internal/error/kustomizationerror_test.go
Normal file
94
pkg/internal/error/kustomizationerror_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
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 error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
)
|
||||
|
||||
func TestKustomizationError_Error(t *testing.T) {
|
||||
filepath := "/path/to/" + constants.KustomizationFileName
|
||||
errorMsg := "Kustomization not found"
|
||||
|
||||
me := KustomizationError{KustomizationPath: filepath, ErrorMsg: errorMsg}
|
||||
|
||||
if !strings.Contains(me.Error(), filepath) {
|
||||
t.Errorf("Incorrect KustomizationError.Error() message \n")
|
||||
t.Errorf("Expected filepath %s, but unfound\n", filepath)
|
||||
}
|
||||
|
||||
if !strings.Contains(me.Error(), errorMsg) {
|
||||
t.Errorf("Incorrect KustomizationError.Error() message \n")
|
||||
t.Errorf("Expected errorMsg %s, but unfound\n", errorMsg)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestKustomizationErrors_Error(t *testing.T) {
|
||||
filepath := "/path/to/kustomize"
|
||||
me := KustomizationError{KustomizationPath: filepath, ErrorMsg: "Kustomization not found"}
|
||||
ce := ConfigmapError{Path: filepath, ErrorMsg: "can't find configmap name"}
|
||||
pe := PatchError{KustomizationPath: filepath, PatchFilepath: filepath, ErrorMsg: "can't find patch file"}
|
||||
re := ResourceError{KustomizationPath: filepath, ResourceFilepath: filepath, ErrorMsg: "can't find resource file"}
|
||||
se := SecretError{KustomizationPath: filepath, ErrorMsg: "can't find secret name"}
|
||||
mes := KustomizationErrors{kErrors: []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 KustomizationErrors.Error() message\n")
|
||||
t.Errorf(" Expected: %s\n", expectedErrorMsg)
|
||||
t.Errorf(" Got: %s\n", mes.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestKustomizationErrors_Get(t *testing.T) {
|
||||
ce := ConfigmapError{Path: "kustomization/filepath", ErrorMsg: "can't find configmap name"}
|
||||
mes := KustomizationErrors{kErrors: []error{ce}}
|
||||
if len(mes.Get()) != 1 {
|
||||
t.Errorf("Incorrect KustomizationErrors.Get()\n")
|
||||
t.Errorf(" Expected: %v\n", []error{ce})
|
||||
t.Errorf(" Got: %s\n", mes.Get())
|
||||
}
|
||||
}
|
||||
|
||||
func TestKustomizationErrors_Append(t *testing.T) {
|
||||
ce := ConfigmapError{Path: "kustomization/filepath", ErrorMsg: "can't find configmap name"}
|
||||
pe := PatchError{KustomizationPath: "kustomization/filepath", PatchFilepath: "patch/path", ErrorMsg: "can't find patch file"}
|
||||
mes := KustomizationErrors{kErrors: []error{ce}}
|
||||
mes.Append(pe)
|
||||
if len(mes.Get()) != 2 {
|
||||
t.Errorf("Incorrect KustomizationErrors.Append()\n")
|
||||
t.Errorf(" Expected: %d error\n%v/n", 2, []error{ce, pe})
|
||||
t.Errorf(" Got: %d error\n%v\n", len(mes.Get()), mes.Get())
|
||||
}
|
||||
}
|
||||
|
||||
func TestKustomizationErrors_BatchAppend(t *testing.T) {
|
||||
ce := ConfigmapError{Path: "kustomization/filepath", ErrorMsg: "can't find configmap name"}
|
||||
pe := PatchError{KustomizationPath: "kustomization/filepath", PatchFilepath: "patch/path", ErrorMsg: "can't find patch file"}
|
||||
mes := KustomizationErrors{kErrors: []error{ce}}
|
||||
me := KustomizationErrors{kErrors: []error{pe}}
|
||||
mes.BatchAppend(me)
|
||||
if len(mes.Get()) != 2 {
|
||||
t.Errorf("Incorrect KustomizationErrors.Append()\n")
|
||||
t.Errorf(" Expected: %d error\n%v/n", 2, []error{ce, pe})
|
||||
t.Errorf(" Got: %d error\n%v\n", len(mes.Get()), mes.Get())
|
||||
}
|
||||
}
|
||||
31
pkg/internal/error/patcherror.go
Normal file
31
pkg/internal/error/patcherror.go
Normal file
@@ -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 error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PatchError struct {
|
||||
KustomizationPath string
|
||||
PatchFilepath string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (e PatchError) Error() string {
|
||||
return fmt.Sprintf("Kustomization file [%s] encounters a patch error for [%s]: %s\n", e.KustomizationPath, e.PatchFilepath, e.ErrorMsg)
|
||||
}
|
||||
43
pkg/internal/error/patcherror_test.go
Normal file
43
pkg/internal/error/patcherror_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
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 error
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
)
|
||||
|
||||
func TestPatchError_Error(t *testing.T) {
|
||||
filepath := "/path/to/" + constants.KustomizationFileName
|
||||
patchfilepath := "/path/to/patch/patch.yaml"
|
||||
errorMsg := "file not found"
|
||||
me := PatchError{KustomizationPath: 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)
|
||||
}
|
||||
}
|
||||
30
pkg/internal/error/resourceerror.go
Normal file
30
pkg/internal/error/resourceerror.go
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
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 error
|
||||
|
||||
import "fmt"
|
||||
|
||||
// First pass to encapsulate fields for more informative error messages.
|
||||
type ResourceError struct {
|
||||
KustomizationPath string
|
||||
ResourceFilepath string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (e ResourceError) Error() string {
|
||||
return fmt.Sprintf("Kustomization file [%s] encounters a resource error for [%s]: %s\n", e.KustomizationPath, e.ResourceFilepath, e.ErrorMsg)
|
||||
}
|
||||
43
pkg/internal/error/resourceerror_test.go
Normal file
43
pkg/internal/error/resourceerror_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
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 error
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
)
|
||||
|
||||
func TestResourceError_Error(t *testing.T) {
|
||||
filepath := "/path/to/" + constants.KustomizationFileName
|
||||
resourcefilepath := "/path/to/resource/deployment.yaml"
|
||||
errorMsg := "file not found"
|
||||
me := ResourceError{KustomizationPath: 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)
|
||||
}
|
||||
}
|
||||
28
pkg/internal/error/secreterror.go
Normal file
28
pkg/internal/error/secreterror.go
Normal file
@@ -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 error
|
||||
|
||||
import "fmt"
|
||||
|
||||
type SecretError struct {
|
||||
KustomizationPath string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (e SecretError) Error() string {
|
||||
return fmt.Sprintf("Kustomization file [%s] encounters a secret error: %s\n", e.KustomizationPath, e.ErrorMsg)
|
||||
}
|
||||
37
pkg/internal/error/secreterror_test.go
Normal file
37
pkg/internal/error/secreterror_test.go
Normal file
@@ -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 error
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSecretError_Error(t *testing.T) {
|
||||
filepath := "/path/to/secret.yaml"
|
||||
errorMsg := "missing a command"
|
||||
me := SecretError{KustomizationPath: 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user