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 24, 2019
1 parent 32f3fe4 commit 7a90d30
Show file tree
Hide file tree
Showing 4 changed files with 79 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 @@ -191,10 +191,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
69 changes: 69 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,70 @@ metadata:
t.Fatalf("unexpected arg array: %v", p.args)
}
}

func makeConfigMap(rf *resource.Factory, name, behavior string, hash bool) *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 hash {
annotations[hashAnnotation] = ""
}
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 TestUpdateResourceOptions(t *testing.T) {
p := NewExecPlugin("")
rf := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
in := resmap.New()
expected := resmap.New()
cases := []struct {
behavior string
hash bool
}{
{"", false},
{"", true},
{"replace", false},
{"merge", false},
{"create", false},
{"nonsense", false},
{"merge", true},
}
for i, c := range cases {
name := fmt.Sprintf("test%d", i)
in.Append(makeConfigMap(rf, name, c.behavior, c.hash))
expected.Append(makeConfigMapOptions(rf, name, c.behavior, !c.hash))
}
actual := p.updateResourceOptions(in)
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())
}
}
}
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 7a90d30

Please sign in to comment.