github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/duration_test.go (about) 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 "testing" 22 "time" 23 24 "sigs.k8s.io/yaml" 25 ) 26 27 type DurationHolder struct { 28 D Duration `json:"d"` 29 } 30 31 func TestDurationMarshalYAML(t *testing.T) { 32 cases := []struct { 33 input Duration 34 result string 35 }{ 36 {Duration{5 * time.Second}, "d: 5s\n"}, 37 {Duration{2 * time.Minute}, "d: 2m0s\n"}, 38 {Duration{time.Hour + 3*time.Millisecond}, "d: 1h0m0.003s\n"}, 39 } 40 41 for _, c := range cases { 42 input := DurationHolder{c.input} 43 result, err := yaml.Marshal(&input) 44 if err != nil { 45 t.Errorf("Failed to marshal input: %q: %v", input, err) 46 } 47 if string(result) != c.result { 48 t.Errorf("Failed to marshal input: %q: expected %q, got %q", input, c.result, string(result)) 49 } 50 } 51 } 52 53 func TestDurationUnmarshalYAML(t *testing.T) { 54 cases := []struct { 55 input string 56 result Duration 57 }{ 58 {"d: 0s\n", Duration{}}, 59 {"d: 5s\n", Duration{5 * time.Second}}, 60 {"d: 2m0s\n", Duration{2 * time.Minute}}, 61 {"d: 1h0m0.003s\n", Duration{time.Hour + 3*time.Millisecond}}, 62 63 // Units with zero values can optionally be dropped 64 {"d: 2m\n", Duration{2 * time.Minute}}, 65 {"d: 1h0.003s\n", Duration{time.Hour + 3*time.Millisecond}}, 66 } 67 68 for _, c := range cases { 69 var result DurationHolder 70 if err := yaml.Unmarshal([]byte(c.input), &result); err != nil { 71 t.Errorf("Failed to unmarshal input %q: %v", c.input, err) 72 } 73 if result.D != c.result { 74 t.Errorf("Failed to unmarshal input %q: expected %q, got %q", c.input, c.result, result) 75 } 76 } 77 } 78 79 func TestDurationMarshalJSON(t *testing.T) { 80 cases := []struct { 81 input Duration 82 result string 83 }{ 84 {Duration{5 * time.Second}, `{"d":"5s"}`}, 85 {Duration{2 * time.Minute}, `{"d":"2m0s"}`}, 86 {Duration{time.Hour + 3*time.Millisecond}, `{"d":"1h0m0.003s"}`}, 87 } 88 89 for _, c := range cases { 90 input := DurationHolder{c.input} 91 result, err := json.Marshal(&input) 92 if err != nil { 93 t.Errorf("Failed to marshal input: %q: %v", input, err) 94 } 95 if string(result) != c.result { 96 t.Errorf("Failed to marshal input: %q: expected %q, got %q", input, c.result, string(result)) 97 } 98 } 99 } 100 101 func TestDurationUnmarshalJSON(t *testing.T) { 102 cases := []struct { 103 input string 104 result Duration 105 }{ 106 {`{"d":"0s"}`, Duration{}}, 107 {`{"d":"5s"}`, Duration{5 * time.Second}}, 108 {`{"d":"2m0s"}`, Duration{2 * time.Minute}}, 109 {`{"d":"1h0m0.003s"}`, Duration{time.Hour + 3*time.Millisecond}}, 110 111 // Units with zero values can optionally be dropped 112 {`{"d":"2m"}`, Duration{2 * time.Minute}}, 113 {`{"d":"1h0.003s"}`, Duration{time.Hour + 3*time.Millisecond}}, 114 } 115 116 for _, c := range cases { 117 var result DurationHolder 118 if err := json.Unmarshal([]byte(c.input), &result); err != nil { 119 t.Errorf("Failed to unmarshal input %q: %v", c.input, err) 120 } 121 if result.D != c.result { 122 t.Errorf("Failed to unmarshal input %q: expected %q, got %q", c.input, c.result, result) 123 } 124 } 125 } 126 127 func TestDurationMarshalJSONUnmarshalYAML(t *testing.T) { 128 cases := []struct { 129 input Duration 130 }{ 131 {Duration{}}, 132 {Duration{5 * time.Second}}, 133 {Duration{2 * time.Minute}}, 134 {Duration{time.Hour + 3*time.Millisecond}}, 135 } 136 137 for i, c := range cases { 138 input := DurationHolder{c.input} 139 jsonMarshalled, err := json.Marshal(&input) 140 if err != nil { 141 t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err) 142 } 143 144 var result DurationHolder 145 if err := yaml.Unmarshal(jsonMarshalled, &result); err != nil { 146 t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err) 147 } 148 149 if input.D != result.D { 150 t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result) 151 } 152 } 153 }