@@ -297,85 +297,153 @@ func TestKeyToVolumeID(t *testing.T) {
297
297
}
298
298
299
299
func TestConvertLabelsStringToMap (t * testing.T ) {
300
- testCases := []struct {
301
- name string
302
- labels string
303
- expectedOutput map [string ]string
304
- expectedError bool
305
- }{
306
- {
307
- name : "should return empty map when labels string is empty" ,
308
- labels : "" ,
309
- expectedOutput : map [string ]string {},
310
- expectedError : false ,
311
- },
312
- {
313
- name : "single label string" ,
314
- labels : "key=value" ,
315
- expectedOutput : map [string ]string {
316
- "key" : "value" ,
300
+ t .Run ("parsing labels string into map" , func (t * testing.T ) {
301
+ testCases := []struct {
302
+ name string
303
+ labels string
304
+ expectedOutput map [string ]string
305
+ expectedError bool
306
+ }{
307
+ {
308
+ name : "should return empty map when labels string is empty" ,
309
+ labels : "" ,
310
+ expectedOutput : map [string ]string {},
311
+ expectedError : false ,
317
312
},
318
- expectedError : false ,
319
- },
320
- {
321
- name : "multiple label string" ,
322
- labels : "key1=value1,key2=value2" ,
323
- expectedOutput : map [string ]string {
324
- "key1" : "value1" ,
325
- "key2" : "value2" ,
313
+ {
314
+ name : "single label string" ,
315
+ labels : "key=value" ,
316
+ expectedOutput : map [string ]string {
317
+ "key" : "value" ,
318
+ },
319
+ expectedError : false ,
326
320
},
327
- expectedError : false ,
328
- } ,
329
- {
330
- name : "multiple labels string with whitespaces gets trimmed" ,
331
- labels : "key1= value1, key2=value2 " ,
332
- expectedOutput : map [ string ] string {
333
- "key1" : "value1" ,
334
- "key2" : "value2" ,
321
+ {
322
+ name : "multiple label string" ,
323
+ labels : "key1=value1,key2=value2" ,
324
+ expectedOutput : map [ string ] string {
325
+ "key1" : "value1" ,
326
+ "key2" : "value2" ,
327
+ } ,
328
+ expectedError : false ,
335
329
},
336
- expectedError : false ,
337
- },
338
- {
339
- name : "malformed labels string (no keys and values)" ,
340
- labels : ",," ,
341
- expectedOutput : nil ,
342
- expectedError : true ,
343
- },
344
- {
345
- name : "malformed labels string (incorrect format)" ,
346
- labels : "foo,bar" ,
347
- expectedOutput : nil ,
348
- expectedError : true ,
349
- },
350
- {
351
- name : "malformed labels string (missing key)" ,
352
- labels : "key1=value1,=bar" ,
353
- expectedOutput : nil ,
354
- expectedError : true ,
355
- },
356
- {
357
- name : "malformed labels string (missing key and value)" ,
358
- labels : "key1=value1,=bar,=" ,
359
- expectedOutput : nil ,
360
- expectedError : true ,
361
- },
362
- }
330
+ {
331
+ name : "multiple labels string with whitespaces gets trimmed" ,
332
+ labels : "key1=value1, key2=value2" ,
333
+ expectedOutput : map [string ]string {
334
+ "key1" : "value1" ,
335
+ "key2" : "value2" ,
336
+ },
337
+ expectedError : false ,
338
+ },
339
+ {
340
+ name : "malformed labels string (no keys and values)" ,
341
+ labels : ",," ,
342
+ expectedOutput : nil ,
343
+ expectedError : true ,
344
+ },
345
+ {
346
+ name : "malformed labels string (incorrect format)" ,
347
+ labels : "foo,bar" ,
348
+ expectedOutput : nil ,
349
+ expectedError : true ,
350
+ },
351
+ {
352
+ name : "malformed labels string (missing key)" ,
353
+ labels : "key1=value1,=bar" ,
354
+ expectedOutput : nil ,
355
+ expectedError : true ,
356
+ },
357
+ {
358
+ name : "malformed labels string (missing key and value)" ,
359
+ labels : "key1=value1,=bar,=" ,
360
+ expectedOutput : nil ,
361
+ expectedError : true ,
362
+ },
363
+ }
363
364
364
- for _ , tc := range testCases {
365
- t .Logf ("test case: %s" , tc .name )
366
- output , err := ConvertLabelsStringToMap (tc .labels )
367
- if err == nil && tc .expectedError {
368
- t .Errorf ("Expected error but got none" )
365
+ for _ , tc := range testCases {
366
+ t .Logf ("test case: %s" , tc .name )
367
+ output , err := ConvertLabelsStringToMap (tc .labels )
368
+ if tc .expectedError && err == nil {
369
+ t .Errorf ("Expected error but got none" )
370
+ }
371
+ if err != nil {
372
+ if ! tc .expectedError {
373
+ t .Errorf ("Did not expect error but got: %v" , err )
374
+ }
375
+ continue
376
+ }
377
+
378
+ if ! reflect .DeepEqual (output , tc .expectedOutput ) {
379
+ t .Errorf ("Got labels %v, but expected %v" , output , tc .expectedOutput )
380
+ }
369
381
}
370
- if err != nil {
371
- if ! tc .expectedError {
382
+ })
383
+
384
+ t .Run ("checking google requirements" , func (t * testing.T ) {
385
+ testCases := []struct {
386
+ name string
387
+ labels string
388
+ expectedError bool
389
+ }{
390
+ {
391
+ name : "64 labels at most" ,
392
+ labels : `k1=v,k2=v,k3=v,k4=v,k5=v,k6=v,k7=v,k8=v,k9=v,k10=v,k11=v,k12=v,k13=v,k14=v,k15=v,k16=v,k17=v,k18=v,k19=v,k20=v,
393
+ k21=v,k22=v,k23=v,k24=v,k25=v,k26=v,k27=v,k28=v,k29=v,k30=v,k31=v,k32=v,k33=v,k34=v,k35=v,k36=v,k37=v,k38=v,k39=v,k40=v,
394
+ k41=v,k42=v,k43=v,k44=v,k45=v,k46=v,k47=v,k48=v,k49=v,k50=v,k51=v,k52=v,k53=v,k54=v,k55=v,k56=v,k57=v,k58=v,k59=v,k60=v,
395
+ k61=v,k62=v,k63=v,k64=v,k65=v` ,
396
+ expectedError : true ,
397
+ },
398
+ {
399
+ name : "label key must start with lowercase char" ,
400
+ labels : "#k=v" ,
401
+ expectedError : true ,
402
+ },
403
+ {
404
+ name : "label key can only contain lowercase chars, digits, _ and -)" ,
405
+ labels : "k*=v" ,
406
+ expectedError : true ,
407
+ },
408
+ {
409
+ name : "label key may not have over 63 characters" ,
410
+ labels : "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij1234=v" ,
411
+ expectedError : true ,
412
+ },
413
+ {
414
+ name : "label key can have up to 63 characters" ,
415
+ labels : "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij123=v" ,
416
+ expectedError : false ,
417
+ },
418
+ {
419
+ name : "label value can only contain lowercase chars, digits, _ and -)" ,
420
+ labels : "k1=###" ,
421
+ expectedError : true ,
422
+ },
423
+ {
424
+ name : "label value may not have over 63 characters" ,
425
+ labels : "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij1234=v" ,
426
+ expectedError : true ,
427
+ },
428
+ {
429
+ name : "label value can have up to 63 characters" ,
430
+ labels : "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij123=v" ,
431
+ expectedError : false ,
432
+ },
433
+ }
434
+
435
+ for _ , tc := range testCases {
436
+ t .Logf ("test case: %s" , tc .name )
437
+ _ , err := ConvertLabelsStringToMap (tc .labels )
438
+
439
+ if tc .expectedError && err == nil {
440
+ t .Errorf ("Expected error but got none" )
441
+ }
442
+
443
+ if ! tc .expectedError && err != nil {
372
444
t .Errorf ("Did not expect error but got: %v" , err )
373
445
}
374
- continue
375
446
}
447
+ })
376
448
377
- if ! reflect .DeepEqual (output , tc .expectedOutput ) {
378
- t .Errorf ("Got labels %v, but expected %v" , output , tc .expectedOutput )
379
- }
380
- }
381
449
}
0 commit comments