Debian Codegraph @

pkg/apis/meta/v1/helpers_test.go

raw
1 /*
2 Copyright 2016 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 v1
18
19 import (
20 "fmt"
21 "reflect"
22 "strings"
23 "testing"
24
25 "github.com/google/go-cmp/cmp"
26 "k8s.io/apimachinery/pkg/labels"
27 "k8s.io/apimachinery/pkg/types"
28 "sigs.k8s.io/randfill"
29 )
30
31 func TestLabelSelectorAsSelector(t *testing.T) {
32 matchLabels := map[string]string{"foo": "bar"}
33 matchExpressions := []LabelSelectorRequirement{{
34 Key: "baz",
35 Operator: LabelSelectorOpIn,
36 Values: []string{"qux", "norf"},
37 }}
38 mustParse := func(s string) labels.Selector {
39 out, e := labels.Parse(s)
40 if e != nil {
41 panic(e)
42 }
43 return out
44 }
45 tc := []struct {
46 in *LabelSelector
47 out labels.Selector
48 expectErr bool
49 }{
50 {in: nil, out: labels.Nothing()},
51 {in: &LabelSelector{}, out: labels.Everything()},
52 {
53 in: &LabelSelector{MatchLabels: matchLabels},
54 out: mustParse("foo=bar"),
55 },
56 {
57 in: &LabelSelector{MatchExpressions: matchExpressions},
58 out: mustParse("baz in (norf,qux)"),
59 },
60 {
61 in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions},
62 out: mustParse("baz in (norf,qux),foo=bar"),
63 },
64 {
65 in: &LabelSelector{
66 MatchExpressions: []LabelSelectorRequirement{{
67 Key: "baz",
68 Operator: LabelSelectorOpExists,
69 Values: []string{"qux", "norf"},
70 }},
71 },
72 expectErr: true,
73 },
74 }
75
76 for i, tc := range tc {
77 inCopy := tc.in.DeepCopy()
78 out, err := LabelSelectorAsSelector(tc.in)
79 // after calling LabelSelectorAsSelector, tc.in shouldn't be modified
80 if !reflect.DeepEqual(inCopy, tc.in) {
81 t.Errorf("[%v]expected:\n\t%#v\nbut got:\n\t%#v", i, inCopy, tc.in)
82 }
83 if err == nil && tc.expectErr {
84 t.Errorf("[%v]expected error but got none.", i)
85 }
86 if err != nil && !tc.expectErr {
87 t.Errorf("[%v]did not expect error but got: %v", i, err)
88 }
89 // fmt.Sprint() over String() as nil.String() will panic
90 if fmt.Sprint(out) != fmt.Sprint(tc.out) {
91 t.Errorf("[%v]expected:\n\t%s\nbut got:\n\t%s", i, fmt.Sprint(tc.out), fmt.Sprint(out))
92 }
93 }
94 }
95
96 func BenchmarkLabelSelectorAsSelector(b *testing.B) {
97 selector := &LabelSelector{
98 MatchLabels: map[string]string{
99 "foo": "foo",
100 "bar": "bar",
101 },
102 MatchExpressions: []LabelSelectorRequirement{{
103 Key: "baz",
104 Operator: LabelSelectorOpExists,
105 }},
106 }
107 b.StartTimer()
108 for i := 0; i < b.N; i++ {
109 _, err := LabelSelectorAsSelector(selector)
110 if err != nil {
111 b.Fatal(err)
112 }
113 }
114 }
115
116 func TestLabelSelectorAsMap(t *testing.T) {
117 matchLabels := map[string]string{"foo": "bar"}
118 matchExpressions := func(operator LabelSelectorOperator, values []string) []LabelSelectorRequirement {
119 return []LabelSelectorRequirement{{
120 Key: "baz",
121 Operator: operator,
122 Values: values,
123 }}
124 }
125
126 tests := []struct {
127 in *LabelSelector
128 out map[string]string
129 errString string
130 }{
131 {in: nil, out: nil},
132 {
133 in: &LabelSelector{MatchLabels: matchLabels},
134 out: map[string]string{"foo": "bar"},
135 },
136 {
137 in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions(LabelSelectorOpIn, []string{"norf"})},
138 out: map[string]string{"foo": "bar", "baz": "norf"},
139 },
140 {
141 in: &LabelSelector{MatchExpressions: matchExpressions(LabelSelectorOpIn, []string{"norf"})},
142 out: map[string]string{"baz": "norf"},
143 },
144 {
145 in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions(LabelSelectorOpIn, []string{"norf", "qux"})},
146 out: map[string]string{"foo": "bar"},
147 errString: "without a single value cannot be converted",
148 },
149 {
150 in: &LabelSelector{MatchExpressions: matchExpressions(LabelSelectorOpNotIn, []string{"norf", "qux"})},
151 out: map[string]string{},
152 errString: "cannot be converted",
153 },
154 {
155 in: &LabelSelector{MatchLabels: matchLabels, MatchExpressions: matchExpressions(LabelSelectorOpExists, []string{})},
156 out: map[string]string{"foo": "bar"},
157 errString: "cannot be converted",
158 },
159 {
160 in: &LabelSelector{MatchExpressions: matchExpressions(LabelSelectorOpDoesNotExist, []string{})},
161 out: map[string]string{},
162 errString: "cannot be converted",
163 },
164 }
165
166 for i, tc := range tests {
167 out, err := LabelSelectorAsMap(tc.in)
168 if err == nil && len(tc.errString) > 0 {
169 t.Errorf("[%v]expected error but got none.", i)
170 continue
171 }
172 if err != nil && len(tc.errString) == 0 {
173 t.Errorf("[%v]did not expect error but got: %v", i, err)
174 continue
175 }
176 if err != nil && len(tc.errString) > 0 && !strings.Contains(err.Error(), tc.errString) {
177 t.Errorf("[%v]expected error with %q but got: %v", i, tc.errString, err)
178 continue
179 }
180 if !reflect.DeepEqual(out, tc.out) {
181 t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)
182 }
183 }
184 }
185
186 func TestResetObjectMetaForStatus(t *testing.T) {
187 meta := &ObjectMeta{}
188 existingMeta := &ObjectMeta{}
189
190 // fuzz the existingMeta to set every field, no nils
191 f := randfill.New().NilChance(0).NumElements(1, 1).MaxDepth(10)
192 f.Fill(existingMeta)
193 ResetObjectMetaForStatus(meta, existingMeta)
194
195 // not all fields are stomped during the reset. These fields should not have been set. False
196 // set them all to their zero values. Before you add anything to this list, consider whether or not
197 // you're enforcing immutability (those are fine) and whether /status should be able to update
198 // these values (these are usually not fine).
199
200 // generateName doesn't do anything after create
201 existingMeta.SetGenerateName("")
202 // resourceVersion is enforced in validation and used during the storage update
203 existingMeta.SetResourceVersion("")
204 // fields made immutable in validation
205 existingMeta.SetUID(types.UID(""))
206 existingMeta.SetName("")
207 existingMeta.SetNamespace("")
208 existingMeta.SetCreationTimestamp(Time{})
209 existingMeta.SetDeletionTimestamp(nil)
210 existingMeta.SetDeletionGracePeriodSeconds(nil)
211 existingMeta.SetManagedFields(nil)
212
213 if !reflect.DeepEqual(meta, existingMeta) {
214 t.Error(cmp.Diff(meta, existingMeta))
215 }
216 }
217
218 func TestSetMetaDataLabel(t *testing.T) {
219 tests := []struct {
220 obj *ObjectMeta
221 label string
222 value string
223 want map[string]string
224 }{
225 {
226 obj: &ObjectMeta{},
227 label: "foo",
228 value: "bar",
229 want: map[string]string{"foo": "bar"},
230 },
231 {
232 obj: &ObjectMeta{Labels: map[string]string{"foo": "bar"}},
233 label: "foo",
234 value: "baz",
235 want: map[string]string{"foo": "baz"},
236 },
237 {
238 obj: &ObjectMeta{Labels: map[string]string{"foo": "bar"}},
239 label: "version",
240 value: "1.0.0",
241 want: map[string]string{"foo": "bar", "version": "1.0.0"},
242 },
243 }
244
245 for _, tc := range tests {
246 SetMetaDataLabel(tc.obj, tc.label, tc.value)
247 if !reflect.DeepEqual(tc.obj.Labels, tc.want) {
248 t.Errorf("got %v, want %v", tc.obj.Labels, tc.want)
249 }
250 }
251 }
252
253 func TestFieldsV1MarshalJSON(t *testing.T) {
254 for _, tc := range []struct {
255 Name string
256 FieldsV1 FieldsV1
257 Want []byte
258 Error string
259 }{
260 {
261 Name: "nil encodes as json null",
262 FieldsV1: FieldsV1{},
263 Want: []byte(`null`),
264 },
265 {
266 Name: "empty invalid json is returned as-is",
267 FieldsV1: FieldsV1{Raw: []byte{}},
268 Want: []byte{},
269 },
270 {
271 Name: "cbor null is transcoded to json null",
272 FieldsV1: FieldsV1{Raw: []byte{0xf6}}, // null
273 Want: []byte(`null`),
274 },
275 {
276 Name: "valid non-map cbor and valid non-object json is returned as-is",
277 FieldsV1: FieldsV1{Raw: []byte{0x30}},
278 Want: []byte{0x30}, // Valid CBOR encoding of -17 and JSON encoding of 0!
279 },
280 {
281 Name: "self-described cbor map is transcoded to json map",
282 FieldsV1: FieldsV1{Raw: []byte{0xd9, 0xd9, 0xf7, 0xa1, 0x43, 'f', 'o', 'o', 0x43, 'b', 'a', 'r'}}, // 55799({"foo":"bar"})
283 Want: []byte(`{"foo":"bar"}`),
284 },
285 {
286 Name: "json object is returned as-is",
287 FieldsV1: FieldsV1{Raw: []byte(" \t\r\n{\"foo\":\"bar\"}")},
288 Want: []byte(" \t\r\n{\"foo\":\"bar\"}"),
289 },
290 {
291 Name: "invalid json is returned as-is",
292 FieldsV1: FieldsV1{Raw: []byte(`{{`)},
293 Want: []byte(`{{`),
294 },
295 {
296 Name: "invalid cbor fails to transcode to json",
297 FieldsV1: FieldsV1{Raw: []byte{0xa1}},
298 Error: "metav1.FieldsV1 cbor invalid: unexpected EOF",
299 },
300 } {
301 t.Run(tc.Name, func(t *testing.T) {
302 got, err := tc.FieldsV1.MarshalJSON()
303 if err != nil {
304 if tc.Error == "" {
305 t.Fatalf("unexpected error: %v", err)
306 }
307 if msg := err.Error(); msg != tc.Error {
308 t.Fatalf("expected error %q, got %q", tc.Error, msg)
309 }
310 } else if tc.Error != "" {
311 t.Fatalf("expected error %q, got nil", tc.Error)
312 }
313 if diff := cmp.Diff(tc.Want, got); diff != "" {
314 t.Errorf("unexpected diff:\n%s", diff)
315 }
316 })
317 }
318 }
319
320 func TestFieldsV1MarshalCBOR(t *testing.T) {
321 for _, tc := range []struct {
322 Name string
323 FieldsV1 FieldsV1
324 Want []byte
325 Error string
326 }{
327 {
328 Name: "nil encodes as cbor null",
329 FieldsV1: FieldsV1{},
330 Want: []byte{0xf6}, // null
331 },
332 {
333 Name: "empty invalid cbor is returned as-is",
334 FieldsV1: FieldsV1{Raw: []byte{}},
335 Want: []byte{},
336 },
337 {
338 Name: "json null is transcoded to cbor null",
339 FieldsV1: FieldsV1{Raw: []byte(`null`)},
340 Want: []byte{0xf6}, // null
341 },
342 {
343 Name: "valid non-map cbor and valid non-object json is returned as-is",
344 FieldsV1: FieldsV1{Raw: []byte{0x30}},
345 Want: []byte{0x30}, // Valid CBOR encoding of -17 and JSON encoding of 0!
346 },
347 {
348 Name: "json object is transcoded to cbor map",
349 FieldsV1: FieldsV1{Raw: []byte(" \t\r\n{\"foo\":\"bar\"}")},
350 Want: []byte{0xa1, 0x43, 'f', 'o', 'o', 0x43, 'b', 'a', 'r'},
351 },
352 {
353 Name: "self-described cbor map is returned as-is",
354 FieldsV1: FieldsV1{Raw: []byte{0xd9, 0xd9, 0xf7, 0xa1, 0x43, 'f', 'o', 'o', 0x43, 'b', 'a', 'r'}}, // 55799({"foo":"bar"})
355 Want: []byte{0xd9, 0xd9, 0xf7, 0xa1, 0x43, 'f', 'o', 'o', 0x43, 'b', 'a', 'r'}, // 55799({"foo":"bar"})
356 },
357 {
358 Name: "invalid json fails to transcode to cbor",
359 FieldsV1: FieldsV1{Raw: []byte(`{{`)},
360 Error: "metav1.FieldsV1 json invalid: invalid character '{' looking for beginning of object key string",
361 },
362 {
363 Name: "invalid cbor is returned as-is",
364 FieldsV1: FieldsV1{Raw: []byte{0xa1}},
365 Want: []byte{0xa1},
366 },
367 } {
368 t.Run(tc.Name, func(t *testing.T) {
369 got, err := tc.FieldsV1.MarshalCBOR()
370 if err != nil {
371 if tc.Error == "" {
372 t.Fatalf("unexpected error: %v", err)
373 }
374 if msg := err.Error(); msg != tc.Error {
375 t.Fatalf("expected error %q, got %q", tc.Error, msg)
376 }
377 } else if tc.Error != "" {
378 t.Fatalf("expected error %q, got nil", tc.Error)
379 }
380
381 if diff := cmp.Diff(tc.Want, got); diff != "" {
382 t.Errorf("unexpected diff:\n%s", diff)
383 }
384 })
385 }
386 }
387
388 func TestFieldsV1UnmarshalJSON(t *testing.T) {
389 for _, tc := range []struct {
390 Name string
391 JSON []byte
392 Into *FieldsV1
393 Want *FieldsV1
394 Error string
395 }{
396 {
397 Name: "nil receiver returns error",
398 Into: nil,
399 Error: "metav1.FieldsV1: UnmarshalJSON on nil pointer",
400 },
401 {
402 Name: "json null does not modify receiver", // conventional for json.Unmarshaler
403 JSON: []byte(`null`),
404 Into: &FieldsV1{Raw: []byte(`unmodified`)},
405 Want: &FieldsV1{Raw: []byte(`unmodified`)},
406 },
407 {
408 Name: "valid input is copied verbatim",
409 JSON: []byte("{\"foo\":\"bar\"} \t\r\n"),
410 Into: &FieldsV1{},
411 Want: &FieldsV1{Raw: []byte("{\"foo\":\"bar\"} \t\r\n")},
412 },
413 {
414 Name: "invalid input is copied verbatim",
415 JSON: []byte("{{"),
416 Into: &FieldsV1{},
417 Want: &FieldsV1{Raw: []byte("{{")},
418 },
419 } {
420 t.Run(tc.Name, func(t *testing.T) {
421 got := tc.Into.DeepCopy()
422 err := got.UnmarshalJSON(tc.JSON)
423 if err != nil {
424 if tc.Error == "" {
425 t.Fatalf("unexpected error: %v", err)
426 }
427 if msg := err.Error(); msg != tc.Error {
428 t.Fatalf("expected error %q, got %q", tc.Error, msg)
429 }
430 } else if tc.Error != "" {
431 t.Fatalf("expected error %q, got nil", tc.Error)
432 }
433
434 if diff := cmp.Diff(tc.Want, got); diff != "" {
435 t.Errorf("unexpected diff:\n%s", diff)
436 }
437 })
438 }
439 }
440
441 func TestFieldsV1UnmarshalCBOR(t *testing.T) {
442 for _, tc := range []struct {
443 Name string
444 CBOR []byte
445 Into *FieldsV1
446 Want *FieldsV1
447 Error string
448 }{
449 {
450 Name: "nil receiver returns error",
451 Into: nil,
452 Want: nil,
453 Error: "metav1.FieldsV1: UnmarshalCBOR on nil pointer",
454 },
455 {
456 Name: "cbor null does not modify receiver",
457 CBOR: []byte{0xf6},
458 Into: &FieldsV1{Raw: []byte(`unmodified`)},
459 Want: &FieldsV1{Raw: []byte(`unmodified`)},
460 },
461 {
462 Name: "valid input is copied verbatim",
463 CBOR: []byte{0xa1, 0x43, 'f', 'o', 'o', 0x43, 'b', 'a', 'r'},
464 Into: &FieldsV1{},
465 Want: &FieldsV1{Raw: []byte{0xa1, 0x43, 'f', 'o', 'o', 0x43, 'b', 'a', 'r'}},
466 },
467 {
468 Name: "invalid input is copied verbatim",
469 CBOR: []byte{0xff}, // UnmarshalCBOR should never be called with malformed input, testing anyway.
470 Into: &FieldsV1{},
471 Want: &FieldsV1{Raw: []byte{0xff}},
472 },
473 } {
474 t.Run(tc.Name, func(t *testing.T) {
475 got := tc.Into.DeepCopy()
476 err := got.UnmarshalCBOR(tc.CBOR)
477 if err != nil {
478 if tc.Error == "" {
479 t.Fatalf("unexpected error: %v", err)
480 }
481 if msg := err.Error(); msg != tc.Error {
482 t.Fatalf("expected error %q, got %q", tc.Error, msg)
483 }
484 } else if tc.Error != "" {
485 t.Fatalf("expected error %q, got nil", tc.Error)
486 }
487
488 if diff := cmp.Diff(tc.Want, got); diff != "" {
489 t.Errorf("unexpected diff:\n%s", diff)
490 }
491 })
492 }
493 }