Skip to content

Commit

Permalink
tests: Add unit tests for update resource options
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmarshall committed Sep 26, 2019
1 parent 79fbe7c commit ac9424f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
6 changes: 2 additions & 4 deletions pkg/plugins/execplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
98 changes: 98 additions & 0 deletions pkg/plugins/execplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package plugins

import (
"fmt"
"strings"
"testing"

"sigs.k8s.io/kustomize/v3/internal/loadertest"
"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) {
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
# Skip the config file name argument.
shift

echo "
cat <<EOF
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap-test
annotations:
kustomize.config.k8s.io/needs-hash: ""
data:
username: $1
password: $2
"
EOF
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main_test
import (
"testing"

"sigs.k8s.io/kustomize/v3/pkg/kusttest"
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
)

Expand Down Expand Up @@ -35,4 +35,7 @@ kind: ConfigMap
metadata:
name: example-configmap-test
`)
if m.Resources()[0].NeedHashSuffix() != true {
t.Errorf("expected resource to need hashing")
}
}

0 comments on commit ac9424f

Please sign in to comment.