@@ -16,21 +16,31 @@ import (
16
16
"k8s.io/utils/diff"
17
17
)
18
18
19
- func getObject (version , kind , name , namespace string ) * unstructured.Unstructured {
19
+ func getObject (version , kind , name , namespace string , withManagedFields bool ) * unstructured.Unstructured {
20
+ metadata := map [string ]interface {}{
21
+ "name" : name ,
22
+ "namespace" : namespace ,
23
+ }
24
+
25
+ if withManagedFields {
26
+ // []metav1.FieldsV1{} can't be deep copied by fake client so using
27
+ // string as example value
28
+ metadata ["managedFields" ] = "set"
29
+ }
30
+
31
+ object := map [string ]interface {}{
32
+ "apiVersion" : version ,
33
+ "kind" : kind ,
34
+ "metadata" : metadata ,
35
+ }
36
+
20
37
return & unstructured.Unstructured {
21
- Object : map [string ]interface {}{
22
- "apiVersion" : version ,
23
- "kind" : kind ,
24
- "metadata" : map [string ]interface {}{
25
- "name" : name ,
26
- "namespace" : namespace ,
27
- },
28
- },
38
+ Object : object ,
29
39
}
30
40
}
31
41
32
42
func getSecret (name , namespace string , data map [string ]interface {}, isTLS bool , withLastApplied bool ) * unstructured.Unstructured {
33
- object := getObject ("v1" , "Secret" , name , namespace )
43
+ object := getObject ("v1" , "Secret" , name , namespace , false )
34
44
object .Object ["data" ] = data
35
45
36
46
object .Object ["type" ] = "Opaque"
@@ -113,34 +123,34 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
113
123
"only a Foo should be returned if GVR selects foos" : {
114
124
gvr : schema.GroupVersionResource {Group : "foobar" , Version : "v1" , Resource : "foos" },
115
125
objects : []runtime.Object {
116
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" ),
117
- getObject ("v1" , "Service" , "testservice" , "testns" ),
118
- getObject ("foobar/v1" , "NotFoo" , "notfoo" , "testns" ),
126
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" , false ),
127
+ getObject ("v1" , "Service" , "testservice" , "testns" , false ),
128
+ getObject ("foobar/v1" , "NotFoo" , "notfoo" , "testns" , false ),
119
129
},
120
130
expected : asUnstructuredList (
121
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" ),
131
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" , false ),
122
132
),
123
133
},
124
134
"only Foos in the specified namespace should be returned" : {
125
135
gvr : schema.GroupVersionResource {Group : "foobar" , Version : "v1" , Resource : "foos" },
126
136
namespaces : []string {"testns" },
127
137
objects : []runtime.Object {
128
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" ),
129
- getObject ("foobar/v1" , "Foo" , "testfoo" , "nottestns" ),
138
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" , false ),
139
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "nottestns" , false ),
130
140
},
131
141
expected : asUnstructuredList (
132
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" ),
142
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" , false ),
133
143
),
134
144
},
135
145
"Foos in different namespaces should be returned if no namespace field is set" : {
136
146
gvr : schema.GroupVersionResource {Group : "foobar" , Version : "v1" , Resource : "foos" },
137
147
objects : []runtime.Object {
138
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns1" ),
139
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns2" ),
148
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns1" , false ),
149
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns2" , false ),
140
150
},
141
151
expected : asUnstructuredList (
142
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns1" ),
143
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns2" ),
152
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns1" , false ),
153
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns2" , false ),
144
154
),
145
155
},
146
156
"Secret resources should have data removed" : {
@@ -185,13 +195,24 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
185
195
gvr : schema.GroupVersionResource {Group : "foobar" , Version : "v1" , Resource : "foos" },
186
196
namespaces : []string {"testns" , "testns2" },
187
197
objects : []runtime.Object {
188
- getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" ),
189
- getObject ("foobar/v1" , "Foo" , "testfoo2" , "testns2" ),
190
- getObject ("foobar/v1" , "Foo" , "testfoo3" , "nottestns" ),
198
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" , false ),
199
+ getObject ("foobar/v1" , "Foo" , "testfoo2" , "testns2" , false ),
200
+ getObject ("foobar/v1" , "Foo" , "testfoo3" , "nottestns" , false ),
201
+ },
202
+ expected : asUnstructuredList (
203
+ getObject ("foobar/v1" , "Foo" , "testfoo" , "testns" , false ),
204
+ getObject ("foobar/v1" , "Foo" , "testfoo2" , "testns2" , false ),
205
+ ),
206
+ },
207
+ "Resources should have managed fields removed" : {
208
+ gvr : schema.GroupVersionResource {Group : "apps" , Version : "v1" , Resource : "deployments" },
209
+ objects : []runtime.Object {
210
+ getObject ("apps/v1" , "Deployment" , "foo1" , "testns" , false ),
211
+ getObject ("apps/v1" , "Deployment" , "foo2" , "testns" , true ),
191
212
},
192
213
expected : asUnstructuredList (
193
- getObject ("foobar /v1" , "Foo " , "testfoo " , "testns" ),
194
- getObject ("foobar /v1" , "Foo " , "testfoo2 " , "testns2" ),
214
+ getObject ("apps /v1" , "Deployment " , "foo1 " , "testns" , false ),
215
+ getObject ("apps /v1" , "Deployment " , "foo2 " , "testns" , false ),
195
216
),
196
217
},
197
218
// Note that we can't test use of fieldSelector to exclude namespaces
@@ -217,7 +238,7 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
217
238
if err == nil && test .err {
218
239
t .Errorf ("expected to get an error but didn't get one" )
219
240
}
220
- if diff , equal := messagediff .PrettyDiff (res , test .expected ); ! equal {
241
+ if diff , equal := messagediff .PrettyDiff (test .expected , res ); ! equal {
221
242
t .Errorf ("\n %s" , diff )
222
243
}
223
244
})
0 commit comments