Debian Codegraph @

pkg/apis/meta/v1/types_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 gojson "encoding/json"
21 "reflect"
22 "testing"
23
24 utiljson "k8s.io/apimachinery/pkg/util/json"
25 )
26
27 func TestVerbsMarshalJSON(t *testing.T) {
28 cases := []struct {
29 input APIResource
30 result string
31 }{
32 {APIResource{}, `{"name":"","singularName":"","namespaced":false,"kind":"","verbs":null}`},
33 {APIResource{Verbs: Verbs([]string{})}, `{"name":"","singularName":"","namespaced":false,"kind":"","verbs":[]}`},
34 {APIResource{Verbs: Verbs([]string{"delete"})}, `{"name":"","singularName":"","namespaced":false,"kind":"","verbs":["delete"]}`},
35 }
36
37 for i, c := range cases {
38 result, err := gojson.Marshal(&c.input)
39 if err != nil {
40 t.Errorf("[%d] Failed to marshal input: '%v': %v", i, c.input, err)
41 }
42 if string(result) != c.result {
43 t.Errorf("[%d] Failed to marshal input: '%v': expected '%v', got '%v'", i, c.input, c.result, string(result))
44 }
45 }
46 }
47
48 func TestVerbsJsonIterUnmarshalJSON(t *testing.T) {
49 cases := []struct {
50 input string
51 result APIResource
52 }{
53 {`{}`, APIResource{}},
54 {`{"verbs":null}`, APIResource{}},
55 {`{"verbs":[]}`, APIResource{Verbs: Verbs([]string{})}},
56 {`{"verbs":["delete"]}`, APIResource{Verbs: Verbs([]string{"delete"})}},
57 }
58
59 for i, c := range cases {
60 var result APIResource
61 if err := utiljson.Unmarshal([]byte(c.input), &result); err != nil {
62 t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err)
63 }
64 if !reflect.DeepEqual(result, c.result) {
65 t.Errorf("[%d] Failed to unmarshal input '%v': expected %+v, got %+v", i, c.input, c.result, result)
66 }
67 }
68 }
69
70 // TestMarshalJSONWithOmit tests that we don't have regressions regarding nil and empty slices with "omit"
71 func TestMarshalJSONWithOmit(t *testing.T) {
72 cases := []struct {
73 input LabelSelector
74 result string
75 }{
76 {LabelSelector{}, `{}`},
77 {LabelSelector{MatchExpressions: []LabelSelectorRequirement{}}, `{}`},
78 {LabelSelector{MatchExpressions: []LabelSelectorRequirement{{}}}, `{"matchExpressions":[{"key":"","operator":""}]}`},
79 }
80
81 for i, c := range cases {
82 result, err := gojson.Marshal(&c.input)
83 if err != nil {
84 t.Errorf("[%d] Failed to marshal input: '%v': %v", i, c.input, err)
85 }
86 if string(result) != c.result {
87 t.Errorf("[%d] Failed to marshal input: '%v': expected '%v', got '%v'", i, c.input, c.result, string(result))
88 }
89 }
90 }
91
92 func TestVerbsUnmarshalJSON(t *testing.T) {
93 cases := []struct {
94 input string
95 result APIResource
96 }{
97 {`{}`, APIResource{}},
98 {`{"verbs":null}`, APIResource{}},
99 {`{"verbs":[]}`, APIResource{Verbs: Verbs([]string{})}},
100 {`{"verbs":["delete"]}`, APIResource{Verbs: Verbs([]string{"delete"})}},
101 }
102
103 for i, c := range cases {
104 var result APIResource
105 if err := gojson.Unmarshal([]byte(c.input), &result); err != nil {
106 t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err)
107 }
108 if !reflect.DeepEqual(result, c.result) {
109 t.Errorf("[%d] Failed to unmarshal input '%v': expected %+v, got %+v", i, c.input, c.result, result)
110 }
111 }
112 }
113
114 func TestVerbsProto(t *testing.T) {
115 cases := []APIResource{
116 {},
117 {Verbs: Verbs([]string{})},
118 {Verbs: Verbs([]string{"delete"})},
119 }
120
121 for _, input := range cases {
122 data, err := input.Marshal()
123 if err != nil {
124 t.Fatalf("Failed to marshal input: '%v': %v", input, err)
125 }
126 resource := APIResource{}
127 if err := resource.Unmarshal(data); err != nil {
128 t.Fatalf("Failed to unmarshal output: '%v': %v", input, err)
129 }
130 if !reflect.DeepEqual(input, resource) {
131 t.Errorf("Marshal->Unmarshal is not idempotent: '%v' vs '%v'", input, resource)
132 }
133 }
134 }