| 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 |
"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 MicroTimeHolder struct { |
| 34 |
T MicroTime `json:"t"` |
| 35 |
} |
| 36 |
|
| 37 |
func TestMicroTimeMarshalYAML(t *testing.T) { |
| 38 |
cases := []struct { |
| 39 |
input MicroTime |
| 40 |
result string |
| 41 |
}{ |
| 42 |
{MicroTime{}, "t: null\n"}, |
| 43 |
{DateMicro(1998, time.May, 5, 1, 5, 5, 50, time.FixedZone("test", -4*60*60)), "t: \"1998-05-05T05:05:05.000000Z\"\n"}, |
| 44 |
{DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: \"1998-05-05T05:05:05.000000Z\"\n"}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, c := range cases { |
| 48 |
input := MicroTimeHolder{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 TestMicroTimeUnmarshalYAML(t *testing.T) { |
| 60 |
cases := []struct { |
| 61 |
input string |
| 62 |
result MicroTime |
| 63 |
}{ |
| 64 |
{"t: null\n", MicroTime{}}, |
| 65 |
{"t: 1998-05-05T05:05:05.000000Z\n", MicroTime{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}}, |
| 66 |
} |
| 67 |
|
| 68 |
for _, c := range cases { |
| 69 |
var result MicroTimeHolder |
| 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 TestMicroTimeMarshalJSON(t *testing.T) { |
| 80 |
cases := []struct { |
| 81 |
input MicroTime |
| 82 |
result string |
| 83 |
}{ |
| 84 |
{MicroTime{}, "{\"t\":null}"}, |
| 85 |
{DateMicro(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "{\"t\":\"1998-05-05T05:05:05.000000Z\"}"}, |
| 86 |
{DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "{\"t\":\"1998-05-05T05:05:05.000000Z\"}"}, |
| 87 |
} |
| 88 |
|
| 89 |
for _, c := range cases { |
| 90 |
input := MicroTimeHolder{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 TestMicroTimeUnmarshalJSON(t *testing.T) { |
| 102 |
cases := []struct { |
| 103 |
input string |
| 104 |
result MicroTime |
| 105 |
}{ |
| 106 |
{"{\"t\":null}", MicroTime{}}, |
| 107 |
{"{\"t\":\"1998-05-05T05:05:05.000000Z\"}", MicroTime{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}}, |
| 108 |
} |
| 109 |
|
| 110 |
for _, c := range cases { |
| 111 |
var result MicroTimeHolder |
| 112 |
if err := json.Unmarshal([]byte(c.input), &result); err != nil { |
| 113 |
t.Errorf("Failed to unmarshal input '%v': %v", c.input, err) |
| 114 |
} |
| 115 |
if result.T != c.result { |
| 116 |
t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestMicroTimeMarshalCBOR(t *testing.T) { |
| 122 |
for _, tc := range []struct { |
| 123 |
name string |
| 124 |
in MicroTime |
| 125 |
out []byte |
| 126 |
}{ |
| 127 |
{name: "zero value", in: MicroTime{}, out: []byte{0xf6}}, // null |
| 128 |
{name: "no fractional seconds", in: DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.UTC), out: []byte("\x58\x1b1998-05-05T05:05:05.000000Z")}, // '1998-05-05T05:05:05.000000Z' |
| 129 |
{name: "nanoseconds truncated", in: DateMicro(1998, time.May, 5, 5, 5, 5, 5050, time.UTC), out: []byte("\x58\x1b1998-05-05T05:05:05.000005Z")}, // '1998-05-05T05:05:05.000005Z' |
| 130 |
} { |
| 131 |
t.Run(fmt.Sprintf("%+v", tc.in), func(t *testing.T) { |
| 132 |
got, err := tc.in.MarshalCBOR() |
| 133 |
if err != nil { |
| 134 |
t.Fatal(err) |
| 135 |
} |
| 136 |
if diff := cmp.Diff(tc.out, got); diff != "" { |
| 137 |
t.Errorf("unexpected output:\n%s", diff) |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestMicroTimeUnmarshalCBOR(t *testing.T) { |
| 144 |
for _, tc := range []struct { |
| 145 |
name string |
| 146 |
in []byte |
| 147 |
out MicroTime |
| 148 |
errMessage string |
| 149 |
}{ |
| 150 |
{name: "null", in: []byte{0xf6}, out: MicroTime{}}, // null |
| 151 |
{name: "valid", in: []byte("\x58\x1b1998-05-05T05:05:05.000000Z"), out: MicroTime{Time: Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}}, // '1998-05-05T05:05:05.000000Z' |
| 152 |
{name: "invalid cbor type", in: []byte{0x07}, out: MicroTime{}, errMessage: "cbor: cannot unmarshal positive integer into Go value of type string"}, // 7 |
| 153 |
{name: "malformed timestamp", in: []byte("\x45hello"), out: MicroTime{}, errMessage: `parsing time "hello" as "2006-01-02T15:04:05.000000Z07:00": cannot parse "hello" as "2006"`}, // 'hello' |
| 154 |
} { |
| 155 |
t.Run(tc.name, func(t *testing.T) { |
| 156 |
var got MicroTime |
| 157 |
err := got.UnmarshalCBOR(tc.in) |
| 158 |
if err != nil { |
| 159 |
if tc.errMessage == "" { |
| 160 |
t.Fatalf("want nil error, got: %v", err) |
| 161 |
} else if gotMessage := err.Error(); tc.errMessage != gotMessage { |
| 162 |
t.Fatalf("want error: %q, got: %q", tc.errMessage, gotMessage) |
| 163 |
} |
| 164 |
} else if tc.errMessage != "" { |
| 165 |
t.Fatalf("got nil error, want: %s", tc.errMessage) |
| 166 |
} |
| 167 |
if diff := cmp.Diff(tc.out, got); diff != "" { |
| 168 |
t.Errorf("unexpected output:\n%s", diff) |
| 169 |
} |
| 170 |
}) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func TestMicroTimeProto(t *testing.T) { |
| 175 |
cases := []struct { |
| 176 |
input MicroTime |
| 177 |
}{ |
| 178 |
{MicroTime{}}, |
| 179 |
{DateMicro(1998, time.May, 5, 1, 5, 5, 1000, time.Local)}, |
| 180 |
{DateMicro(1998, time.May, 5, 5, 5, 5, 0, time.Local)}, |
| 181 |
} |
| 182 |
|
| 183 |
for _, c := range cases { |
| 184 |
input := c.input |
| 185 |
data, err := input.Marshal() |
| 186 |
if err != nil { |
| 187 |
t.Fatalf("Failed to marshal input: '%v': %v", input, err) |
| 188 |
} |
| 189 |
time := MicroTime{} |
| 190 |
if err := time.Unmarshal(data); err != nil { |
| 191 |
t.Fatalf("Failed to unmarshal output: '%v': %v", input, err) |
| 192 |
} |
| 193 |
if !reflect.DeepEqual(input, time) { |
| 194 |
t.Errorf("Marshal->Unmarshal is not idempotent: '%v' vs '%v'", input, time) |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestMicroTimeEqual(t *testing.T) { |
| 200 |
t1 := NewMicroTime(time.Now()) |
| 201 |
cases := []struct { |
| 202 |
name string |
| 203 |
x *MicroTime |
| 204 |
y *MicroTime |
| 205 |
result bool |
| 206 |
}{ |
| 207 |
{"nil =? nil", nil, nil, true}, |
| 208 |
{"!nil =? !nil", &t1, &t1, true}, |
| 209 |
{"nil =? !nil", nil, &t1, false}, |
| 210 |
{"!nil =? nil", &t1, nil, false}, |
| 211 |
} |
| 212 |
|
| 213 |
for _, c := range cases { |
| 214 |
t.Run(c.name, func(t *testing.T) { |
| 215 |
result := c.x.Equal(c.y) |
| 216 |
if result != c.result { |
| 217 |
t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result) |
| 218 |
} |
| 219 |
}) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestMicroTimeEqualTime(t *testing.T) { |
| 224 |
t1 := NewMicroTime(time.Now()) |
| 225 |
t2 := NewTime(t1.Time) |
| 226 |
cases := []struct { |
| 227 |
name string |
| 228 |
x *MicroTime |
| 229 |
y *Time |
| 230 |
result bool |
| 231 |
}{ |
| 232 |
{"nil =? nil", nil, nil, true}, |
| 233 |
{"!nil =? !nil", &t1, &t2, true}, |
| 234 |
{"nil =? !nil", nil, &t2, false}, |
| 235 |
{"!nil =? nil", &t1, nil, false}, |
| 236 |
} |
| 237 |
|
| 238 |
for _, c := range cases { |
| 239 |
t.Run(c.name, func(t *testing.T) { |
| 240 |
result := c.x.EqualTime(c.y) |
| 241 |
if result != c.result { |
| 242 |
t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result) |
| 243 |
} |
| 244 |
}) |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func TestMicroTimeBefore(t *testing.T) { |
| 249 |
t1 := NewMicroTime(time.Now()) |
| 250 |
cases := []struct { |
| 251 |
name string |
| 252 |
x *MicroTime |
| 253 |
y *MicroTime |
| 254 |
}{ |
| 255 |
{"nil <? nil", nil, nil}, |
| 256 |
{"!nil <? !nil", &t1, &t1}, |
| 257 |
{"nil <? !nil", nil, &t1}, |
| 258 |
{"!nil <? nil", &t1, nil}, |
| 259 |
} |
| 260 |
|
| 261 |
for _, c := range cases { |
| 262 |
t.Run(c.name, func(t *testing.T) { |
| 263 |
result := c.x.Before(c.y) |
| 264 |
if result { |
| 265 |
t.Errorf("Failed before test for '%v', '%v': expected false, got %+v", c.x, c.y, result) |
| 266 |
} |
| 267 |
}) |
| 268 |
} |
| 269 |
} |
| 270 |
func TestMicroTimeBeforeTime(t *testing.T) { |
| 271 |
t1 := NewMicroTime(time.Now()) |
| 272 |
t2 := NewTime(t1.Time) |
| 273 |
cases := []struct { |
| 274 |
name string |
| 275 |
x *MicroTime |
| 276 |
y *Time |
| 277 |
}{ |
| 278 |
{"nil <? nil", nil, nil}, |
| 279 |
{"!nil <? !nil", &t1, &t2}, |
| 280 |
{"nil <? !nil", nil, &t2}, |
| 281 |
{"!nil <? nil", &t1, nil}, |
| 282 |
} |
| 283 |
|
| 284 |
for _, c := range cases { |
| 285 |
t.Run(c.name, func(t *testing.T) { |
| 286 |
result := c.x.BeforeTime(c.y) |
| 287 |
if result { |
| 288 |
t.Errorf("Failed before test for '%v', '%v': expected false, got %+v", c.x, c.y, result) |
| 289 |
} |
| 290 |
}) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
func TestMicroTimeIsZero(t *testing.T) { |
| 295 |
t1 := NewMicroTime(time.Now()) |
| 296 |
cases := []struct { |
| 297 |
name string |
| 298 |
x *MicroTime |
| 299 |
result bool |
| 300 |
}{ |
| 301 |
{"nil =? 0", nil, true}, |
| 302 |
{"!nil =? 0", &t1, false}, |
| 303 |
} |
| 304 |
|
| 305 |
for _, c := range cases { |
| 306 |
t.Run(c.name, func(t *testing.T) { |
| 307 |
result := c.x.IsZero() |
| 308 |
if result != c.result { |
| 309 |
t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.x, c.result, result) |
| 310 |
} |
| 311 |
}) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func TestMicroTimeUnmarshalJSONAndProtoEqual(t *testing.T) { |
| 316 |
cases := []struct { |
| 317 |
name string |
| 318 |
input MicroTime |
| 319 |
result bool |
| 320 |
}{ |
| 321 |
{"nanosecond level precision", UnixMicro(123, 123123123), true}, |
| 322 |
{"microsecond level precision", UnixMicro(123, 123123000), true}, |
| 323 |
} |
| 324 |
|
| 325 |
for _, c := range cases { |
| 326 |
t.Run(c.name, func(t *testing.T) { |
| 327 |
jsonData, err := c.input.MarshalJSON() |
| 328 |
if err != nil { |
| 329 |
t.Fatalf("Failed to marshal input to JSON: '%v': %v", c.input, err) |
| 330 |
} |
| 331 |
|
| 332 |
protoData, err := c.input.Marshal() |
| 333 |
if err != nil { |
| 334 |
t.Fatalf("Failed to marshal input to proto: '%v': %v", c.input, err) |
| 335 |
} |
| 336 |
|
| 337 |
var tJSON, tProto MicroTime |
| 338 |
if err = tJSON.UnmarshalJSON(jsonData); err != nil { |
| 339 |
t.Fatalf("Failed to unmarshal JSON: '%v': %v", jsonData, err) |
| 340 |
} |
| 341 |
if err = tProto.Unmarshal(protoData); err != nil { |
| 342 |
t.Fatalf("Failed to unmarshal proto: '%v': %v", protoData, err) |
| 343 |
} |
| 344 |
|
| 345 |
result := tJSON.Equal(&tProto) |
| 346 |
if result != c.result { |
| 347 |
t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.input, c.result, result) |
| 348 |
} |
| 349 |
}) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
func TestMicroTimeProtoUnmarshalRaw(t *testing.T) { |
| 354 |
cases := []struct { |
| 355 |
name string |
| 356 |
input []byte |
| 357 |
expected MicroTime |
| 358 |
}{ |
| 359 |
// input is generated by Timestamp{123, 123123123}.Marshal() |
| 360 |
{"nanosecond level precision", []byte{8, 123, 16, 179, 235, 218, 58}, UnixMicro(123, 123123000)}, |
| 361 |
// input is generated by Timestamp{123, 123123000}.Marshal() |
| 362 |
{"microsecond level precision", []byte{8, 123, 16, 184, 234, 218, 58}, UnixMicro(123, 123123000)}, |
| 363 |
} |
| 364 |
|
| 365 |
for _, c := range cases { |
| 366 |
t.Run(c.name, func(t *testing.T) { |
| 367 |
var actual MicroTime |
| 368 |
if err := actual.Unmarshal(c.input); err != nil { |
| 369 |
t.Fatalf("Failed to unmarshal proto: '%v': %v", c.input, err) |
| 370 |
} |
| 371 |
|
| 372 |
if !actual.Equal(&c.expected) { |
| 373 |
t.Errorf("Failed unmarshal from nanosecond-precise raw for '%v': expected %+v, got %+v", c.input, c.expected, actual) |
| 374 |
} |
| 375 |
}) |
| 376 |
} |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
func TestMicroTimeRoundtripCBOR(t *testing.T) { |
| 381 |
fuzzer := randfill.New() |
| 382 |
for i := 0; i < 500; i++ { |
| 383 |
var initial, final MicroTime |
| 384 |
fuzzer.Fill(&initial) |
| 385 |
b, err := cbor.Marshal(initial) |
| 386 |
if err != nil { |
| 387 |
t.Errorf("error encoding %v: %v", initial, err) |
| 388 |
continue |
| 389 |
} |
| 390 |
err = cbor.Unmarshal(b, &final) |
| 391 |
if err != nil { |
| 392 |
t.Errorf("%v: error decoding %v: %v", initial, string(b), err) |
| 393 |
} |
| 394 |
if !final.Equal(&initial) { |
| 395 |
diag, err := cbor.Diagnose(b) |
| 396 |
if err != nil { |
| 397 |
t.Logf("failed to produce diagnostic encoding of 0x%x: %v", b, err) |
| 398 |
} |
| 399 |
t.Errorf("expected equal: %v, %v (cbor was '%s')", initial, final, diag) |
| 400 |
} |
| 401 |
} |
| 402 |
} |