github.com/kruglovmax/helm@v3.0.0-beta.3+incompatible/pkg/strvals/parser_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package strvals 17 18 import ( 19 "testing" 20 21 "sigs.k8s.io/yaml" 22 ) 23 24 func TestSetIndex(t *testing.T) { 25 tests := []struct { 26 name string 27 initial []interface{} 28 expect []interface{} 29 add int 30 val int 31 }{ 32 { 33 name: "short", 34 initial: []interface{}{0, 1}, 35 expect: []interface{}{0, 1, 2}, 36 add: 2, 37 val: 2, 38 }, 39 { 40 name: "equal", 41 initial: []interface{}{0, 1}, 42 expect: []interface{}{0, 2}, 43 add: 1, 44 val: 2, 45 }, 46 { 47 name: "long", 48 initial: []interface{}{0, 1, 2, 3, 4, 5}, 49 expect: []interface{}{0, 1, 2, 4, 4, 5}, 50 add: 3, 51 val: 4, 52 }, 53 } 54 55 for _, tt := range tests { 56 got := setIndex(tt.initial, tt.add, tt.val) 57 if len(got) != len(tt.expect) { 58 t.Fatalf("%s: Expected length %d, got %d", tt.name, len(tt.expect), len(got)) 59 } 60 61 if gg := got[tt.add].(int); gg != tt.val { 62 t.Errorf("%s, Expected value %d, got %d", tt.name, tt.val, gg) 63 } 64 } 65 } 66 67 func TestParseSet(t *testing.T) { 68 testsString := []struct { 69 str string 70 expect map[string]interface{} 71 err bool 72 }{ 73 { 74 str: "long_int_string=1234567890", 75 expect: map[string]interface{}{"long_int_string": "1234567890"}, 76 err: false, 77 }, 78 } 79 tests := []struct { 80 str string 81 expect map[string]interface{} 82 err bool 83 }{ 84 { 85 "name1=value1", 86 map[string]interface{}{"name1": "value1"}, 87 false, 88 }, 89 { 90 "name1=value1,name2=value2", 91 map[string]interface{}{"name1": "value1", "name2": "value2"}, 92 false, 93 }, 94 { 95 "name1=value1,name2=value2,", 96 map[string]interface{}{"name1": "value1", "name2": "value2"}, 97 false, 98 }, 99 { 100 str: "name1=value1,,,,name2=value2,", 101 err: true, 102 }, 103 { 104 str: "name1=,name2=value2", 105 expect: map[string]interface{}{"name1": "", "name2": "value2"}, 106 }, 107 { 108 str: "leading_zeros=00009", 109 expect: map[string]interface{}{"leading_zeros": "00009"}, 110 }, 111 { 112 str: "long_int=1234567890", 113 expect: map[string]interface{}{"long_int": 1234567890}, 114 }, 115 { 116 str: "name1,name2=", 117 err: true, 118 }, 119 { 120 str: "name1,name2=value2", 121 err: true, 122 }, 123 { 124 str: "name1,name2=value2\\", 125 err: true, 126 }, 127 { 128 str: "name1,name2", 129 err: true, 130 }, 131 { 132 "name1=one\\,two,name2=three\\,four", 133 map[string]interface{}{"name1": "one,two", "name2": "three,four"}, 134 false, 135 }, 136 { 137 "name1=one\\=two,name2=three\\=four", 138 map[string]interface{}{"name1": "one=two", "name2": "three=four"}, 139 false, 140 }, 141 { 142 "name1=one two three,name2=three two one", 143 map[string]interface{}{"name1": "one two three", "name2": "three two one"}, 144 false, 145 }, 146 { 147 "outer.inner=value", 148 map[string]interface{}{"outer": map[string]interface{}{"inner": "value"}}, 149 false, 150 }, 151 { 152 "outer.middle.inner=value", 153 map[string]interface{}{"outer": map[string]interface{}{"middle": map[string]interface{}{"inner": "value"}}}, 154 false, 155 }, 156 { 157 "outer.inner1=value,outer.inner2=value2", 158 map[string]interface{}{"outer": map[string]interface{}{"inner1": "value", "inner2": "value2"}}, 159 false, 160 }, 161 { 162 "outer.inner1=value,outer.middle.inner=value", 163 map[string]interface{}{ 164 "outer": map[string]interface{}{ 165 "inner1": "value", 166 "middle": map[string]interface{}{ 167 "inner": "value", 168 }, 169 }, 170 }, 171 false, 172 }, 173 { 174 str: "name1.name2", 175 err: true, 176 }, 177 { 178 str: "name1.name2,name1.name3", 179 err: true, 180 }, 181 { 182 str: "name1.name2=", 183 expect: map[string]interface{}{"name1": map[string]interface{}{"name2": ""}}, 184 }, 185 { 186 str: "name1.=name2", 187 err: true, 188 }, 189 { 190 str: "name1.,name2", 191 err: true, 192 }, 193 { 194 "name1={value1,value2}", 195 map[string]interface{}{"name1": []string{"value1", "value2"}}, 196 false, 197 }, 198 { 199 "name1={value1,value2},name2={value1,value2}", 200 map[string]interface{}{ 201 "name1": []string{"value1", "value2"}, 202 "name2": []string{"value1", "value2"}, 203 }, 204 false, 205 }, 206 { 207 "name1={1021,902}", 208 map[string]interface{}{"name1": []int{1021, 902}}, 209 false, 210 }, 211 { 212 "name1.name2={value1,value2}", 213 map[string]interface{}{"name1": map[string]interface{}{"name2": []string{"value1", "value2"}}}, 214 false, 215 }, 216 { 217 str: "name1={1021,902", 218 err: true, 219 }, 220 // List support 221 { 222 str: "list[0]=foo", 223 expect: map[string]interface{}{"list": []string{"foo"}}, 224 }, 225 { 226 str: "list[0].foo=bar", 227 expect: map[string]interface{}{ 228 "list": []interface{}{ 229 map[string]interface{}{"foo": "bar"}, 230 }, 231 }, 232 }, 233 { 234 str: "list[0].foo=bar,list[0].hello=world", 235 expect: map[string]interface{}{ 236 "list": []interface{}{ 237 map[string]interface{}{"foo": "bar", "hello": "world"}, 238 }, 239 }, 240 }, 241 { 242 str: "list[0]=foo,list[1]=bar", 243 expect: map[string]interface{}{"list": []string{"foo", "bar"}}, 244 }, 245 { 246 str: "list[0]=foo,list[1]=bar,", 247 expect: map[string]interface{}{"list": []string{"foo", "bar"}}, 248 }, 249 { 250 str: "list[0]=foo,list[3]=bar", 251 expect: map[string]interface{}{"list": []interface{}{"foo", nil, nil, "bar"}}, 252 }, 253 { 254 str: "illegal[0]name.foo=bar", 255 err: true, 256 }, 257 { 258 str: "noval[0]", 259 expect: map[string]interface{}{"noval": []interface{}{}}, 260 }, 261 { 262 str: "noval[0]=", 263 expect: map[string]interface{}{"noval": []interface{}{""}}, 264 }, 265 { 266 str: "nested[0][0]=1", 267 expect: map[string]interface{}{"nested": []interface{}{[]interface{}{1}}}, 268 }, 269 { 270 str: "nested[1][1]=1", 271 expect: map[string]interface{}{"nested": []interface{}{nil, []interface{}{nil, 1}}}, 272 }, 273 } 274 275 for _, tt := range tests { 276 got, err := Parse(tt.str) 277 if err != nil { 278 if tt.err { 279 continue 280 } 281 t.Fatalf("%s: %s", tt.str, err) 282 } 283 if tt.err { 284 t.Errorf("%s: Expected error. Got nil", tt.str) 285 } 286 287 y1, err := yaml.Marshal(tt.expect) 288 if err != nil { 289 t.Fatal(err) 290 } 291 y2, err := yaml.Marshal(got) 292 if err != nil { 293 t.Fatalf("Error serializing parsed value: %s", err) 294 } 295 296 if string(y1) != string(y2) { 297 t.Errorf("%s: Expected:\n%s\nGot:\n%s", tt.str, y1, y2) 298 } 299 } 300 for _, tt := range testsString { 301 got, err := ParseString(tt.str) 302 if err != nil { 303 if tt.err { 304 continue 305 } 306 t.Fatalf("%s: %s", tt.str, err) 307 } 308 if tt.err { 309 t.Errorf("%s: Expected error. Got nil", tt.str) 310 } 311 312 y1, err := yaml.Marshal(tt.expect) 313 if err != nil { 314 t.Fatal(err) 315 } 316 y2, err := yaml.Marshal(got) 317 if err != nil { 318 t.Fatalf("Error serializing parsed value: %s", err) 319 } 320 321 if string(y1) != string(y2) { 322 t.Errorf("%s: Expected:\n%s\nGot:\n%s", tt.str, y1, y2) 323 } 324 } 325 } 326 327 func TestParseInto(t *testing.T) { 328 got := map[string]interface{}{ 329 "outer": map[string]interface{}{ 330 "inner1": "overwrite", 331 "inner2": "value2", 332 }, 333 } 334 input := "outer.inner1=value1,outer.inner3=value3" 335 expect := map[string]interface{}{ 336 "outer": map[string]interface{}{ 337 "inner1": "value1", 338 "inner2": "value2", 339 "inner3": "value3", 340 }, 341 } 342 343 if err := ParseInto(input, got); err != nil { 344 t.Fatal(err) 345 } 346 347 y1, err := yaml.Marshal(expect) 348 if err != nil { 349 t.Fatal(err) 350 } 351 y2, err := yaml.Marshal(got) 352 if err != nil { 353 t.Fatalf("Error serializing parsed value: %s", err) 354 } 355 356 if string(y1) != string(y2) { 357 t.Errorf("%s: Expected:\n%s\nGot:\n%s", input, y1, y2) 358 } 359 } 360 361 func TestToYAML(t *testing.T) { 362 // The TestParse does the hard part. We just verify that YAML formatting is 363 // happening. 364 o, err := ToYAML("name=value") 365 if err != nil { 366 t.Fatal(err) 367 } 368 expect := "name: value" 369 if o != expect { 370 t.Errorf("Expected %q, got %q", expect, o) 371 } 372 }