mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
tests: Add unit tests for update resource options
This commit is contained in:
@@ -192,10 +192,8 @@ func (p *ExecPlugin) getResMapWithIdAnnotation(rm resmap.ResMap) (resmap.ResMap,
|
|||||||
return inputRM, nil
|
return inputRM, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// updateResMapValues updates the Resource value in the given ResMap
|
||||||
updateResMapValues updates the Resource value in the given ResMap
|
// with the emitted Resource values in output.
|
||||||
with the emitted Resource values in output.
|
|
||||||
*/
|
|
||||||
func (p *ExecPlugin) updateResMapValues(output []byte, rm resmap.ResMap) error {
|
func (p *ExecPlugin) updateResMapValues(output []byte, rm resmap.ResMap) error {
|
||||||
outputRM, err := p.rf.NewResMapFromBytes(output)
|
outputRM, err := p.rf.NewResMapFromBytes(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
|
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/resmap"
|
"sigs.k8s.io/kustomize/v3/pkg/resmap"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/resource"
|
"sigs.k8s.io/kustomize/v3/pkg/resource"
|
||||||
|
"sigs.k8s.io/kustomize/v3/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExecPluginConfig(t *testing.T) {
|
func TestExecPluginConfig(t *testing.T) {
|
||||||
@@ -87,3 +89,99 @@ metadata:
|
|||||||
t.Fatalf("unexpected arg array: %v", p.args)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
# Skip the config file name argument.
|
# Skip the config file name argument.
|
||||||
shift
|
shift
|
||||||
|
|
||||||
echo "
|
cat <<EOF
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
metadata:
|
metadata:
|
||||||
name: example-configmap-test
|
name: example-configmap-test
|
||||||
|
annotations:
|
||||||
|
kustomize.config.k8s.io/needs-hash: ""
|
||||||
data:
|
data:
|
||||||
username: $1
|
username: $1
|
||||||
password: $2
|
password: $2
|
||||||
"
|
EOF
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ package main_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"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"
|
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,4 +35,7 @@ kind: ConfigMap
|
|||||||
metadata:
|
metadata:
|
||||||
name: example-configmap-test
|
name: example-configmap-test
|
||||||
`)
|
`)
|
||||||
|
if m.Resources()[0].NeedHashSuffix() != true {
|
||||||
|
t.Errorf("expected resource to need hashing")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user