Skip to content

Commit 8ba2ea9

Browse files
committed
Add test for mutatefield
1 parent ef51cce commit 8ba2ea9

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
File renamed without changes.

pkg/transformers/mutatefield_test.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package transformers
18+
19+
import (
20+
"fmt"
21+
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
22+
"sigs.k8s.io/kustomize/pkg/ifc"
23+
"testing"
24+
)
25+
26+
type noopMutator struct {
27+
wasCalled bool
28+
errorToReturn error
29+
}
30+
31+
var errExpected = fmt.Errorf("oops")
32+
33+
const originalValue = "tomato"
34+
const newValue = "notThe" + originalValue
35+
36+
func (m *noopMutator) mutate(in interface{}) (interface{}, error) {
37+
m.wasCalled = true
38+
return newValue, m.errorToReturn
39+
}
40+
41+
func makeTestDeployment() ifc.Kunstructured {
42+
factory := kunstruct.NewKunstructuredFactoryImpl()
43+
return factory.FromMap(
44+
map[string]interface{}{
45+
"group": "apps",
46+
"apiVersion": "v1",
47+
"kind": "Deployment",
48+
"metadata": map[string]interface{}{
49+
"name": originalValue,
50+
},
51+
"spec": map[string]interface{}{
52+
"template": map[string]interface{}{
53+
"env": []interface{}{
54+
map[string]interface{}{
55+
"name": "HELLO",
56+
"value": "hi there",
57+
},
58+
map[string]interface{}{
59+
"name": "GOODBYE",
60+
"value": "adios!",
61+
},
62+
},
63+
"metadata": map[string]interface{}{
64+
"labels": map[string]interface{}{
65+
"vegetable": originalValue,
66+
},
67+
},
68+
"spec": map[string]interface{}{
69+
"containers": []interface{}{
70+
map[string]interface{}{
71+
"name": "tangerine",
72+
"image": originalValue,
73+
},
74+
},
75+
},
76+
},
77+
},
78+
})
79+
}
80+
81+
func getFieldValue(t *testing.T, obj ifc.Kunstructured, fieldName string) string {
82+
v, err := obj.GetFieldValue(fieldName)
83+
if err != nil {
84+
t.Fatalf("unexpected field error: %v", err)
85+
}
86+
return v
87+
}
88+
89+
func TestNoPath(t *testing.T) {
90+
obj := makeTestDeployment()
91+
m := &noopMutator{}
92+
err := mutateField(
93+
obj.Map(), []string{}, false, m.mutate)
94+
if m.wasCalled {
95+
t.Fatalf("mutator should not have been called.")
96+
}
97+
if err != nil {
98+
t.Fatalf("unexpected error: %v", err)
99+
}
100+
}
101+
102+
func TestHappyPath(t *testing.T) {
103+
obj := makeTestDeployment()
104+
v := getFieldValue(t, obj, "metadata.name")
105+
if v != originalValue {
106+
t.Fatalf("unexpected original value: %v", v)
107+
}
108+
v = getFieldValue(t, obj, "spec.template.metadata.labels.vegetable")
109+
if v != originalValue {
110+
t.Fatalf("unexpected original value: %v", v)
111+
}
112+
113+
m := &noopMutator{}
114+
err := mutateField(
115+
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
116+
if !m.wasCalled {
117+
t.Fatalf("mutator should have been called.")
118+
}
119+
if err != nil {
120+
t.Fatalf("unexpected mutate error: %v", err)
121+
}
122+
v = getFieldValue(t, obj, "metadata.name")
123+
if v != newValue {
124+
t.Fatalf("unexpected new value: %v", v)
125+
}
126+
127+
m = &noopMutator{}
128+
err = mutateField(
129+
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
130+
if !m.wasCalled {
131+
t.Fatalf("mutator should have been called.")
132+
}
133+
if err != nil {
134+
t.Fatalf("unexpected mutate error: %v", err)
135+
}
136+
v = getFieldValue(t, obj, "spec.template.metadata.labels.vegetable")
137+
if v != newValue {
138+
t.Fatalf("unexpected new value: %v", v)
139+
}
140+
}
141+
142+
func TestWithError(t *testing.T) {
143+
obj := makeTestDeployment()
144+
m := noopMutator{errorToReturn: errExpected}
145+
err := mutateField(
146+
obj.Map(), []string{"metadata", "name"}, false, m.mutate)
147+
if !m.wasCalled {
148+
t.Fatalf("mutator was not called!")
149+
}
150+
if err != errExpected {
151+
t.Fatalf("unexpected error: %v", err)
152+
}
153+
}
154+
155+
func TestWithNil(t *testing.T) {
156+
obj := makeTestDeployment()
157+
foo := obj.Map()["spec"]
158+
foo = foo.(map[string]interface{})["template"]
159+
foo = foo.(map[string]interface{})["metadata"]
160+
foo.(map[string]interface{})["labels"] = nil
161+
162+
m := &noopMutator{}
163+
err := mutateField(
164+
obj.Map(), []string{"spec", "template", "metadata", "labels", "vegetable"}, false, m.mutate)
165+
if err == nil {
166+
t.Fatalf("Expected error due to nil field.")
167+
}
168+
if err.Error() != "<nil> is not expected to be a primitive type" {
169+
t.Fatalf("unexpected error: %v", err)
170+
}
171+
}

0 commit comments

Comments
 (0)