@@ -295,3 +295,155 @@ func TestKeyToVolumeID(t *testing.T) {
295
295
}
296
296
297
297
}
298
+
299
+ func TestConvertLabelsStringToMap (t * testing.T ) {
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 ,
312
+ },
313
+ {
314
+ name : "single label string" ,
315
+ labels : "key=value" ,
316
+ expectedOutput : map [string ]string {
317
+ "key" : "value" ,
318
+ },
319
+ expectedError : false ,
320
+ },
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 ,
329
+ },
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
+ }
364
+
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
+ }
381
+ }
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 {
444
+ t .Errorf ("Did not expect error but got: %v" , err )
445
+ }
446
+ }
447
+ })
448
+
449
+ }
0 commit comments