From ac9424fa3ecf03c485c8c6653d4dd28b61618456 Mon Sep 17 00:00:00 2001 From: Richard Marshall Date: Mon, 2 Sep 2019 08:46:09 -0700 Subject: [PATCH] tests: Add unit tests for update resource options --- pkg/plugins/execplugin.go | 6 +- pkg/plugins/execplugin_test.go | 98 +++++++++++++++++++ .../v1/bashedconfigmap/BashedConfigMap | 6 +- .../bashedconfigmap/BashedConfigMap_test.go | 5 +- 4 files changed, 108 insertions(+), 7 deletions(-) diff --git a/pkg/plugins/execplugin.go b/pkg/plugins/execplugin.go index 061ee8693e..03b324b7a5 100644 --- a/pkg/plugins/execplugin.go +++ b/pkg/plugins/execplugin.go @@ -192,10 +192,8 @@ func (p *ExecPlugin) getResMapWithIdAnnotation(rm resmap.ResMap) (resmap.ResMap, return inputRM, nil } -/* -updateResMapValues updates the Resource value in the given ResMap -with the emitted Resource values in output. -*/ +// updateResMapValues updates the Resource value in the given ResMap +// with the emitted Resource values in output. func (p *ExecPlugin) updateResMapValues(output []byte, rm resmap.ResMap) error { outputRM, err := p.rf.NewResMapFromBytes(output) if err != nil { diff --git a/pkg/plugins/execplugin_test.go b/pkg/plugins/execplugin_test.go index 7dc7708d20..2375f16e8b 100644 --- a/pkg/plugins/execplugin_test.go +++ b/pkg/plugins/execplugin_test.go @@ -17,6 +17,7 @@ limitations under the License. package plugins import ( + "fmt" "strings" "testing" @@ -24,6 +25,7 @@ import ( "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resource" + "sigs.k8s.io/kustomize/v3/pkg/types" ) func TestExecPluginConfig(t *testing.T) { @@ -87,3 +89,99 @@ metadata: t.Fatalf("unexpected arg array: %v", p.args) } } + +func makeConfigMap(rf *resource.Factory, name, behavior string, hashValue *string) *resource.Resource { + r := rf.FromMap(map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{"name": name}, + }) + annotations := map[string]string{} + if behavior != "" { + annotations[behaviorAnnotation] = behavior + } + if hashValue != nil { + annotations[hashAnnotation] = *hashValue + } + if len(annotations) > 0 { + r.SetAnnotations(annotations) + } + return r +} + +func makeConfigMapOptions(rf *resource.Factory, name, behavior string, disableHash bool) *resource.Resource { + return rf.FromMapAndOption(map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{"name": name}, + }, &types.GeneratorArgs{Behavior: behavior}, &types.GeneratorOptions{DisableNameSuffixHash: disableHash}) +} + +func strptr(s string) *string { + return &s +} + +func TestUpdateResourceOptions(t *testing.T) { + p := NewExecPlugin("") + rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()) + in := resmap.New() + expected := resmap.New() + cases := []struct { + behavior string + needsHash bool + hashValue *string + }{ + {hashValue: strptr("false")}, + {hashValue: strptr("true"), needsHash: true}, + {behavior: "replace"}, + {behavior: "merge"}, + {behavior: "create"}, + {behavior: "nonsense"}, + {behavior: "merge", hashValue: strptr("false")}, + {behavior: "merge", hashValue: strptr("true"), needsHash: true}, + } + for i, c := range cases { + name := fmt.Sprintf("test%d", i) + in.Append(makeConfigMap(rf, name, c.behavior, c.hashValue)) + expected.Append(makeConfigMapOptions(rf, name, c.behavior, !c.needsHash)) + } + actual, err := p.updateResourceOptions(in) + if err != nil { + t.Fatalf("unexpected error: %v", err.Error()) + } + for i, a := range expected.Resources() { + b := actual.GetByIndex(i) + if b == nil { + t.Fatalf("resource %d missing from processed map", i) + } + if !a.Equals(b) { + t.Errorf("expected %v got %v", a, b) + } + if a.NeedHashSuffix() != b.NeedHashSuffix() { + t.Errorf("") + } + if a.Behavior() != b.Behavior() { + t.Errorf("expected %v got %v", a.Behavior(), b.Behavior()) + } + } +} + +func TestUpdateResourceOptionsWithInvalidHashAnnotationValues(t *testing.T) { + p := NewExecPlugin("") + rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()) + cases := []string{ + "", + "FaLsE", + "TrUe", + "potato", + } + for i, c := range cases { + name := fmt.Sprintf("test%d", i) + in := resmap.New() + in.Append(makeConfigMap(rf, name, "", &c)) + _, err := p.updateResourceOptions(in) + if err == nil { + t.Errorf("expected error from value %q", c) + } + } +} diff --git a/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap b/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap index 8904238073..5aca2a0ffa 100755 --- a/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap +++ b/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap @@ -3,12 +3,14 @@ # Skip the config file name argument. shift -echo " +cat <