Debian Codegraph @

pkg/apis/meta/v1/time_test.go

raw
1 /*
2 Copyright 2014 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 "encoding/json"
21 "fmt"
22 "reflect"
23 "testing"
24 "time"
25
26 cbor "k8s.io/apimachinery/pkg/runtime/serializer/cbor/direct"
27 "sigs.k8s.io/yaml"
28
29 "github.com/google/go-cmp/cmp"
30 "sigs.k8s.io/randfill"
31 )
32
33 type TimeHolder struct {
34 T Time `json:"t"`
35 }
36
37 func TestTimeMarshalYAML(t *testing.T) {
38 cases := []struct {
39 input Time
40 result string
41 }{
42 {Time{}, "t: null\n"},
43 {Date(1998, time.May, 5, 1, 5, 5, 50, time.FixedZone("test", -4*60*60)), "t: \"1998-05-05T05:05:05Z\"\n"},
44 {Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: \"1998-05-05T05:05:05Z\"\n"},
45 }
46
47 for _, c := range cases {
48 input := TimeHolder{c.input}
49 result, err := yaml.Marshal(&input)
50 if err != nil {
51 t.Errorf("Failed to marshal input: '%v': %v", input, err)
52 }
53 if string(result) != c.result {
54 t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
55 }
56 }
57 }
58
59 func TestTimeUnmarshalYAML(t *testing.T) {
60 cases := []struct {
61 input string
62 result Time
63 }{
64 {"t: null\n", Time{}},
65 {"t: 1998-05-05T05:05:05Z\n", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
66 }
67
68 for _, c := range cases {
69 var result TimeHolder
70 if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
71 t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
72 }
73 if result.T != c.result {
74 t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
75 }
76 }
77 }
78
79 func TestTimeMarshalJSON(t *testing.T) {
80 cases := []struct {
81 input Time
82 result string
83 }{
84 {Time{}, "{\"t\":null}"},
85 {Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"},
86 {Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"},
87 }
88
89 for _, c := range cases {
90 input := TimeHolder{c.input}
91 result, err := json.Marshal(&input)
92 if err != nil {
93 t.Errorf("Failed to marshal input: '%v': %v", input, err)
94 }
95 if string(result) != c.result {
96 t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
97 }
98 }
99 }
100
101 func TestTimeUnmarshalJSON(t *testing.T) {
102 cases := []struct {
103 input string
104 result Time
105 }{
106 {"{\"t\":null}", Time{}},
107 {"{\"t\":\"1998-05-05T05:05:05Z\"}", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
108 {"{\"t\":\"1998-05-05T05:05:05.123456789Z\"}", Time{Date(1998, time.May, 5, 5, 5, 5, 123456789, time.UTC).Local()}},
109 }
110
111 for _, c := range cases {
112 var result TimeHolder
113 if err := json.Unmarshal([]byte(c.input), &result); err != nil {
114 t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
115 }
116 if result.T != c.result {
117 t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
118 }
119 }
120 }
121
122 func TestTimeMarshalJSONUnmarshalYAML(t *testing.T) {
123 cases := []struct {
124 input Time
125 }{
126 {Time{}},
127 {Date(1998, time.May, 5, 5, 5, 5, 50, time.Local).Rfc3339Copy()},
128 {Date(1998, time.May, 5, 5, 5, 5, 0, time.Local).Rfc3339Copy()},
129 }
130
131 for i, c := range cases {
132 input := TimeHolder{c.input}
133 jsonMarshalled, err := json.Marshal(&input)
134 if err != nil {
135 t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err)
136 }
137
138 var result TimeHolder
139 err = yaml.Unmarshal(jsonMarshalled, &result)
140 if err != nil {
141 t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err)
142 }
143
144 iN, iO := input.T.Zone()
145 oN, oO := result.T.Zone()
146 if iN != oN || iO != oO {
147 t.Errorf("%d-3: Time zones differ before and after serialization %s:%d %s:%d", i, iN, iO, oN, oO)
148 }
149
150 if input.T.UnixNano() != result.T.UnixNano() {
151 t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result)
152 }
153 }
154 }
155
156 func TestTimeMarshalCBOR(t *testing.T) {
157 for _, tc := range []struct {
158 name string
159 in Time
160 out []byte
161 }{
162 {name: "zero value", in: Time{}, out: []byte{0xf6}}, // null
163 {name: "no fractional seconds", in: Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), out: []byte("\x541998-05-05T05:05:05Z")}, // '1998-05-05T05:05:05Z'
164 {name: "fractional seconds truncated", in: Date(1998, time.May, 5, 5, 5, 5, 123456789, time.UTC), out: []byte("\x541998-05-05T05:05:05Z")}, // '1998-05-05T05:05:05Z'
165 {name: "epoch", in: Time{Time: time.Unix(0, 0)}, out: []byte("\x541970-01-01T00:00:00Z")}, // '1970-01-01T00:00:00Z'
166 {name: "pre-epoch", in: Date(1960, time.January, 1, 0, 0, 0, 0, time.UTC), out: []byte("\x541960-01-01T00:00:00Z")}, // '1960-01-01T00:00:00Z'
167 } {
168 t.Run(fmt.Sprintf("%+v", tc.in), func(t *testing.T) {
169 got, err := tc.in.MarshalCBOR()
170 if err != nil {
171 t.Fatal(err)
172 }
173 if diff := cmp.Diff(tc.out, got); diff != "" {
174 t.Errorf("unexpected output:\n%s", diff)
175 }
176 })
177 }
178 }
179
180 func TestTimeUnmarshalCBOR(t *testing.T) {
181 for _, tc := range []struct {
182 name string
183 in []byte
184 out Time
185 errMessage string
186 }{
187 {name: "null", in: []byte{0xf6}, out: Time{}}, // null
188 {name: "no fractional seconds", in: []byte("\x58\x141998-05-05T05:05:05Z"), out: Time{Time: Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}}, // '1998-05-05T05:05:05Z'
189 {name: "fractional seconds", in: []byte("\x58\x1e1998-05-05T05:05:05.123456789Z"), out: Time{Time: Date(1998, time.May, 5, 5, 5, 5, 123456789, time.UTC).Local()}}, // '1998-05-05T05:05:05.123456789Z'
190 {name: "invalid cbor type", in: []byte{0x07}, out: Time{}, errMessage: "cbor: cannot unmarshal positive integer into Go value of type string"}, // 7
191 {name: "malformed timestamp", in: []byte("\x45hello"), out: Time{}, errMessage: `parsing time "hello" as "2006-01-02T15:04:05Z07:00": cannot parse "hello" as "2006"`}, // 'hello'
192 } {
193 t.Run(tc.name, func(t *testing.T) {
194 var got Time
195 err := got.UnmarshalCBOR(tc.in)
196 if err != nil {
197 if tc.errMessage == "" {
198 t.Fatalf("want nil error, got: %v", err)
199 } else if gotMessage := err.Error(); tc.errMessage != gotMessage {
200 t.Fatalf("want error: %q, got: %q", tc.errMessage, gotMessage)
201 }
202 } else if tc.errMessage != "" {
203 t.Fatalf("got nil error, want: %s", tc.errMessage)
204 }
205 if diff := cmp.Diff(tc.out, got); diff != "" {
206 t.Errorf("unexpected output:\n%s", diff)
207 }
208 })
209 }
210 }
211
212 func TestTimeProto(t *testing.T) {
213 cases := []struct {
214 input Time
215 }{
216 {Time{}},
217 {Date(1998, time.May, 5, 1, 5, 5, 0, time.Local)},
218 {Date(1998, time.May, 5, 5, 5, 5, 0, time.Local)},
219 }
220
221 for _, c := range cases {
222 input := c.input
223 data, err := input.Marshal()
224 if err != nil {
225 t.Fatalf("Failed to marshal input: '%v': %v", input, err)
226 }
227 time := Time{}
228 if err := time.Unmarshal(data); err != nil {
229 t.Fatalf("Failed to unmarshal output: '%v': %v", input, err)
230 }
231 if !reflect.DeepEqual(input, time) {
232 t.Errorf("Marshal->Unmarshal is not idempotent: '%v' vs '%v'", input, time)
233 }
234 }
235 }
236
237 func TestTimeEqual(t *testing.T) {
238 t1 := NewTime(time.Now())
239 cases := []struct {
240 name string
241 x *Time
242 y *Time
243 result bool
244 }{
245 {"nil =? nil", nil, nil, true},
246 {"!nil =? !nil", &t1, &t1, true},
247 {"nil =? !nil", nil, &t1, false},
248 {"!nil =? nil", &t1, nil, false},
249 }
250
251 for _, c := range cases {
252 t.Run(c.name, func(t *testing.T) {
253 result := c.x.Equal(c.y)
254 if result != c.result {
255 t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result)
256 }
257 })
258 }
259 }
260
261 func TestTimeBefore(t *testing.T) {
262 t1 := NewTime(time.Now())
263 cases := []struct {
264 name string
265 x *Time
266 y *Time
267 }{
268 {"nil <? nil", nil, nil},
269 {"!nil <? !nil", &t1, &t1},
270 {"nil <? !nil", nil, &t1},
271 {"!nil <? nil", &t1, nil},
272 }
273
274 for _, c := range cases {
275 t.Run(c.name, func(t *testing.T) {
276 result := c.x.Before(c.y)
277 if result {
278 t.Errorf("Failed equality test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
279 }
280 })
281 }
282 }
283
284 func TestTimeIsZero(t *testing.T) {
285 t1 := NewTime(time.Now())
286 cases := []struct {
287 name string
288 x *Time
289 result bool
290 }{
291 {"nil =? 0", nil, true},
292 {"!nil =? 0", &t1, false},
293 }
294
295 for _, c := range cases {
296 t.Run(c.name, func(t *testing.T) {
297 result := c.x.IsZero()
298 if result != c.result {
299 t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.x, c.result, result)
300 }
301 })
302 }
303 }
304
305 func TestTimeRoundtripCBOR(t *testing.T) {
306 fuzzer := randfill.New()
307 for i := 0; i < 500; i++ {
308 var initial, final Time
309 fuzzer.Fill(&initial)
310 b, err := cbor.Marshal(initial)
311 if err != nil {
312 t.Errorf("error encoding %v: %v", initial, err)
313 continue
314 }
315 err = cbor.Unmarshal(b, &final)
316 if err != nil {
317 t.Errorf("%v: error decoding %v: %v", initial, string(b), err)
318 }
319 if !final.Equal(&initial) {
320 diag, err := cbor.Diagnose(b)
321 if err != nil {
322 t.Logf("failed to produce diagnostic encoding of 0x%x: %v", b, err)
323 }
324 t.Errorf("expected equal: %v, %v (cbor was '%s')", initial, final, diag)
325 }
326 }
327 }