github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/intstr/intstr_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 intstr 18 19 import ( 20 "encoding/json" 21 "reflect" 22 "testing" 23 24 "sigs.k8s.io/yaml" 25 ) 26 27 func TestFromInt(t *testing.T) { 28 i := FromInt(93) 29 if i.Type != Int || i.IntVal != 93 { 30 t.Errorf("Expected IntVal=93, got %+v", i) 31 } 32 } 33 34 func TestFromString(t *testing.T) { 35 i := FromString("76") 36 if i.Type != String || i.StrVal != "76" { 37 t.Errorf("Expected StrVal=\"76\", got %+v", i) 38 } 39 } 40 41 type IntOrStringHolder struct { 42 IOrS IntOrString `json:"val"` 43 } 44 45 func TestIntOrStringUnmarshalJSON(t *testing.T) { 46 cases := []struct { 47 input string 48 result IntOrString 49 }{ 50 {"{\"val\": 123}", FromInt(123)}, 51 {"{\"val\": \"123\"}", FromString("123")}, 52 } 53 54 for _, c := range cases { 55 var result IntOrStringHolder 56 if err := json.Unmarshal([]byte(c.input), &result); err != nil { 57 t.Errorf("Failed to unmarshal input '%v': %v", c.input, err) 58 } 59 if result.IOrS != c.result { 60 t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result) 61 } 62 } 63 } 64 65 func TestIntOrStringMarshalJSON(t *testing.T) { 66 cases := []struct { 67 input IntOrString 68 result string 69 }{ 70 {FromInt(123), "{\"val\":123}"}, 71 {FromString("123"), "{\"val\":\"123\"}"}, 72 } 73 74 for _, c := range cases { 75 input := IntOrStringHolder{c.input} 76 result, err := json.Marshal(&input) 77 if err != nil { 78 t.Errorf("Failed to marshal input '%v': %v", input, err) 79 } 80 if string(result) != c.result { 81 t.Errorf("Failed to marshal input '%v': expected: %+v, got %q", input, c.result, string(result)) 82 } 83 } 84 } 85 86 func TestIntOrStringMarshalJSONUnmarshalYAML(t *testing.T) { 87 cases := []struct { 88 input IntOrString 89 }{ 90 {FromInt(123)}, 91 {FromString("123")}, 92 } 93 94 for _, c := range cases { 95 input := IntOrStringHolder{c.input} 96 jsonMarshalled, err := json.Marshal(&input) 97 if err != nil { 98 t.Errorf("1: Failed to marshal input: '%v': %v", input, err) 99 } 100 101 var result IntOrStringHolder 102 err = yaml.Unmarshal(jsonMarshalled, &result) 103 if err != nil { 104 t.Errorf("2: Failed to unmarshal '%+v': %v", string(jsonMarshalled), err) 105 } 106 107 if !reflect.DeepEqual(input, result) { 108 t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result) 109 } 110 } 111 } 112 113 func TestGetIntFromIntOrString(t *testing.T) { 114 tests := []struct { 115 input IntOrString 116 expectErr bool 117 expectVal int 118 expectPerc bool 119 }{ 120 { 121 input: FromInt(200), 122 expectErr: false, 123 expectVal: 200, 124 expectPerc: false, 125 }, 126 { 127 input: FromString("200"), 128 expectErr: true, 129 expectPerc: false, 130 }, 131 { 132 input: FromString("30%0"), 133 expectErr: true, 134 expectPerc: false, 135 }, 136 { 137 input: FromString("40%"), 138 expectErr: false, 139 expectVal: 40, 140 expectPerc: true, 141 }, 142 { 143 input: FromString("%"), 144 expectErr: true, 145 expectPerc: false, 146 }, 147 { 148 input: FromString("a%"), 149 expectErr: true, 150 expectPerc: false, 151 }, 152 { 153 input: FromString("a"), 154 expectErr: true, 155 expectPerc: false, 156 }, 157 { 158 input: FromString("40#"), 159 expectErr: true, 160 expectPerc: false, 161 }, 162 { 163 input: FromString("40%%"), 164 expectErr: true, 165 expectPerc: false, 166 }, 167 } 168 for _, test := range tests { 169 t.Run("", func(t *testing.T) { 170 value, isPercent, err := getIntOrPercentValueSafely(&test.input) 171 if test.expectVal != value { 172 t.Fatalf("expected value does not match, expected: %d, got: %d", test.expectVal, value) 173 } 174 if test.expectPerc != isPercent { 175 t.Fatalf("expected percent does not match, expected: %t, got: %t", test.expectPerc, isPercent) 176 } 177 if test.expectErr != (err != nil) { 178 t.Fatalf("expected error does not match, expected error: %v, got: %v", test.expectErr, err) 179 } 180 }) 181 } 182 183 } 184 185 func TestGetIntFromIntOrPercent(t *testing.T) { 186 tests := []struct { 187 input IntOrString 188 total int 189 roundUp bool 190 expectErr bool 191 expectVal int 192 }{ 193 { 194 input: FromInt(123), 195 expectErr: false, 196 expectVal: 123, 197 }, 198 { 199 input: FromString("90%"), 200 total: 100, 201 roundUp: true, 202 expectErr: false, 203 expectVal: 90, 204 }, 205 { 206 input: FromString("90%"), 207 total: 95, 208 roundUp: true, 209 expectErr: false, 210 expectVal: 86, 211 }, 212 { 213 input: FromString("90%"), 214 total: 95, 215 roundUp: false, 216 expectErr: false, 217 expectVal: 85, 218 }, 219 { 220 input: FromString("%"), 221 expectErr: true, 222 }, 223 { 224 input: FromString("90#"), 225 expectErr: true, 226 }, 227 { 228 input: FromString("#%"), 229 expectErr: true, 230 }, 231 { 232 input: FromString("90"), 233 expectErr: true, 234 }, 235 } 236 237 for i, test := range tests { 238 t.Logf("test case %d", i) 239 value, err := GetScaledValueFromIntOrPercent(&test.input, test.total, test.roundUp) 240 if test.expectErr && err == nil { 241 t.Errorf("expected error, but got none") 242 continue 243 } 244 if !test.expectErr && err != nil { 245 t.Errorf("unexpected err: %v", err) 246 continue 247 } 248 if test.expectVal != value { 249 t.Errorf("expected %v, but got %v", test.expectVal, value) 250 } 251 } 252 } 253 254 func TestGetValueFromIntOrPercentNil(t *testing.T) { 255 _, err := GetScaledValueFromIntOrPercent(nil, 0, false) 256 if err == nil { 257 t.Errorf("expected error got none") 258 } 259 }