mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 16:42:51 +00:00
Add validator interface
This commit is contained in:
@@ -1,38 +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 validators defines a FakeValidator that can be used in tests
|
||||
package validators
|
||||
|
||||
import (
|
||||
"errors"
|
||||
apivalidation "k8s.io/apimachinery/pkg/api/validation"
|
||||
v1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// MapValidatorFunc returns an error if a map contains errors.
|
||||
type MapValidatorFunc func(map[string]string) error
|
||||
|
||||
// MakeAnnotationValidator returns a MapValidatorFunc using apimachinery.
|
||||
func MakeAnnotationValidator() MapValidatorFunc {
|
||||
return func(x map[string]string) error {
|
||||
errs := apivalidation.ValidateAnnotations(x, field.NewPath("field"))
|
||||
if len(errs) > 0 {
|
||||
return errors.New(errs.ToAggregate().Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MakeLabelValidator returns a MapValidatorFunc using apimachinery.
|
||||
func MakeLabelValidator() MapValidatorFunc {
|
||||
return func(x map[string]string) error {
|
||||
errs := v1validation.ValidateLabels(x, field.NewPath("field"))
|
||||
if len(errs) > 0 {
|
||||
return errors.New(errs.ToAggregate().Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// FakeValidator can be used in tests.
|
||||
type FakeValidator struct {
|
||||
happy bool
|
||||
@@ -53,6 +43,30 @@ func MakeSadMapValidator(t *testing.T) *FakeValidator {
|
||||
return &FakeValidator{happy: false, t: t}
|
||||
}
|
||||
|
||||
// MakeFakeValidator makes an empty Fake Validator.
|
||||
func MakeFakeValidator() *FakeValidator {
|
||||
return &FakeValidator{}
|
||||
}
|
||||
|
||||
// MakeAnnotationValidator returns a nil function
|
||||
func (v *FakeValidator) MakeAnnotationValidator() func(map[string]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeLabelValidator returns a nil function
|
||||
func (v *FakeValidator) MakeLabelValidator() func(map[string]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateNamespace validates namespace by regexp
|
||||
func (v *FakeValidator) ValidateNamespace(s string) []string {
|
||||
pattern := regexp.MustCompile(`^[a-zA-Z].*`)
|
||||
if pattern.MatchString(s) {
|
||||
return nil
|
||||
}
|
||||
return []string{"doesn't match"}
|
||||
}
|
||||
|
||||
// Validator replaces apimachinery validation in tests.
|
||||
// Can be set to fail or succeed to test error handling.
|
||||
// Can confirm if run or not run by surrounding code.
|
||||
|
||||
Reference in New Issue
Block a user