mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-29 17:41:13 +00:00
Make resource, resmap public.
This commit is contained in:
84
api/testutils/resmaptest/rmbuilder.go
Normal file
84
api/testutils/resmaptest/rmbuilder.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package resmaptest_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/v3/api/resid"
|
||||
"sigs.k8s.io/kustomize/v3/api/resmap"
|
||||
"sigs.k8s.io/kustomize/v3/api/resource"
|
||||
)
|
||||
|
||||
// Builds ResMaps for tests, with test-aware error handling.
|
||||
type rmBuilder struct {
|
||||
t *testing.T
|
||||
m resmap.ResMap
|
||||
rf *resource.Factory
|
||||
}
|
||||
|
||||
func NewSeededRmBuilder(t *testing.T, rf *resource.Factory, m resmap.ResMap) *rmBuilder {
|
||||
return &rmBuilder{t: t, rf: rf, m: m}
|
||||
}
|
||||
|
||||
func NewRmBuilder(t *testing.T, rf *resource.Factory) *rmBuilder {
|
||||
return NewSeededRmBuilder(t, rf, resmap.New())
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) Add(m map[string]interface{}) *rmBuilder {
|
||||
return rm.AddR(rm.rf.FromMap(m))
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) AddR(r *resource.Resource) *rmBuilder {
|
||||
err := rm.m.Append(r)
|
||||
if err != nil {
|
||||
rm.t.Fatalf("test setup failure: %v", err)
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) AddWithId(id resid.ResId, m map[string]interface{}) *rmBuilder {
|
||||
err := rm.m.Append(rm.rf.FromMap(m))
|
||||
if err != nil {
|
||||
rm.t.Fatalf("test setup failure: %v", err)
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) AddWithName(n string, m map[string]interface{}) *rmBuilder {
|
||||
err := rm.m.Append(rm.rf.FromMapWithName(n, m))
|
||||
if err != nil {
|
||||
rm.t.Fatalf("test setup failure: %v", err)
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) AddWithNs(ns string, m map[string]interface{}) *rmBuilder {
|
||||
err := rm.m.Append(rm.rf.FromMapWithNamespace(ns, m))
|
||||
if err != nil {
|
||||
rm.t.Fatalf("test setup failure: %v", err)
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) AddWithNsAndName(ns string, n string, m map[string]interface{}) *rmBuilder {
|
||||
err := rm.m.Append(rm.rf.FromMapWithNamespaceAndName(ns, n, m))
|
||||
if err != nil {
|
||||
rm.t.Fatalf("test setup failure: %v", err)
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) ReplaceResource(m map[string]interface{}) *rmBuilder {
|
||||
r := rm.rf.FromMap(m)
|
||||
_, err := rm.m.Replace(r)
|
||||
if err != nil {
|
||||
rm.t.Fatalf("test setup failure: %v", err)
|
||||
}
|
||||
return rm
|
||||
}
|
||||
|
||||
func (rm *rmBuilder) ResMap() resmap.ResMap {
|
||||
return rm.m
|
||||
}
|
||||
108
api/testutils/valtest/fakevalidator.go
Normal file
108
api/testutils/valtest/fakevalidator.go
Normal file
@@ -0,0 +1,108 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package valtest_test defines a fakeValidator that can be used in tests
|
||||
package valtest_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// fakeValidator can be used in tests.
|
||||
type fakeValidator struct {
|
||||
happy bool
|
||||
called bool
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
// SAD is an error string.
|
||||
const SAD = "i'm not happy Bob, NOT HAPPY"
|
||||
|
||||
// MakeHappyMapValidator makes a fakeValidator that always passes.
|
||||
func MakeHappyMapValidator(t *testing.T) *fakeValidator {
|
||||
return &fakeValidator{happy: true, t: t}
|
||||
}
|
||||
|
||||
// MakeSadMapValidator makes a fakeValidator that always fails.
|
||||
func MakeSadMapValidator(t *testing.T) *fakeValidator {
|
||||
return &fakeValidator{happy: false, t: t}
|
||||
}
|
||||
|
||||
// MakeFakeValidator makes an empty Fake Validator.
|
||||
func MakeFakeValidator() *fakeValidator {
|
||||
return &fakeValidator{}
|
||||
}
|
||||
|
||||
// ErrIfInvalidKey returns nil
|
||||
func (v *fakeValidator) ErrIfInvalidKey(k string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsEnvVarName returns nil
|
||||
func (v *fakeValidator) IsEnvVarName(k string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeAnnotationValidator returns a nil function
|
||||
func (v *fakeValidator) MakeAnnotationValidator() func(map[string]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeAnnotationNameValidator returns a nil function
|
||||
func (v *fakeValidator) MakeAnnotationNameValidator() func([]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeLabelValidator returns a nil function
|
||||
func (v *fakeValidator) MakeLabelValidator() func(map[string]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeLabelNameValidator returns a nil function
|
||||
func (v *fakeValidator) MakeLabelNameValidator() func([]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.
|
||||
func (v *fakeValidator) Validator(_ map[string]string) error {
|
||||
v.called = true
|
||||
if v.happy {
|
||||
return nil
|
||||
}
|
||||
return errors.New(SAD)
|
||||
}
|
||||
|
||||
func (v *fakeValidator) ValidatorArray(_ []string) error {
|
||||
v.called = true
|
||||
if v.happy {
|
||||
return nil
|
||||
}
|
||||
return errors.New(SAD)
|
||||
}
|
||||
|
||||
// VerifyCall returns true if Validator was used.
|
||||
func (v *fakeValidator) VerifyCall() {
|
||||
if !v.called {
|
||||
v.t.Errorf("should have called Validator")
|
||||
}
|
||||
}
|
||||
|
||||
// VerifyNoCall returns true if Validator was not used.
|
||||
func (v *fakeValidator) VerifyNoCall() {
|
||||
if v.called {
|
||||
v.t.Errorf("should not have called Validator")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user