Skip to content

Commit 5b9cbd4

Browse files
authored
test: add tests for npm/util (#1303)
1 parent 95155ca commit 5b9cbd4

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

npm/util/util_test.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,199 @@ func TestCompareSlices(t *testing.T) {
323323
t.Errorf("TestCompareSlices failed @ slice comparison 4")
324324
}
325325
}
326+
327+
func TestExists(t *testing.T) {
328+
type args struct {
329+
filePath string
330+
}
331+
dir := t.TempDir()
332+
tests := []struct {
333+
name string
334+
args args
335+
want bool
336+
}{
337+
{
338+
name: "Test for filepath exists",
339+
args: args{
340+
dir,
341+
},
342+
want: true,
343+
},
344+
{
345+
name: "Test for directory/file not exist",
346+
args: args{
347+
"unknown_directory",
348+
},
349+
want: false,
350+
},
351+
}
352+
353+
for _, tt := range tests {
354+
t.Run(tt.name, func(t *testing.T) {
355+
if got := Exists(tt.args.filePath); got != tt.want {
356+
t.Errorf("Exists() = %v, want %v", got, tt.want)
357+
}
358+
})
359+
}
360+
}
361+
362+
func TestGetClusterID(t *testing.T) {
363+
type args struct {
364+
nodeName string
365+
}
366+
tests := []struct {
367+
name string
368+
args args
369+
want string
370+
}{
371+
{
372+
name: "Test to get cluster id for invalid azure node name",
373+
args: args{
374+
"nodename-test111",
375+
},
376+
want: "",
377+
},
378+
{
379+
name: "Test to get cluster id for valid azure node name",
380+
args: args{
381+
"aks-agentpool-vmss000000",
382+
},
383+
want: "vmss000000",
384+
},
385+
}
386+
for _, tt := range tests {
387+
t.Run(tt.name, func(t *testing.T) {
388+
if got := GetClusterID(tt.args.nodeName); got != tt.want {
389+
t.Errorf("GetClusterID() = %v, want %v", got, tt.want)
390+
}
391+
})
392+
}
393+
}
394+
395+
func TestGetIPSetListFromLabels(t *testing.T) {
396+
labels := make(map[string]string)
397+
labels["test-key"] = "test-val"
398+
expected := []string{
399+
"test-key",
400+
"test-key:test-val",
401+
}
402+
got := GetIPSetListFromLabels(labels)
403+
if len(got) != 2 || expected[0] != got[0] || expected[1] != got[1] {
404+
t.Errorf("GetIPSetListFromLabels(labels map[string]string) = %v, want %v", got, expected)
405+
}
406+
}
407+
408+
func TestClearAndAppendMap(t *testing.T) {
409+
base := map[string]string{
410+
"base-key": "base-val",
411+
}
412+
newmap := map[string]string{
413+
"one": "uno",
414+
"two": "dos",
415+
}
416+
if got := ClearAndAppendMap(base, newmap); !reflect.DeepEqual(got, newmap) {
417+
t.Errorf("ClearAndAppendMap() = %v, want %v", got, newmap)
418+
}
419+
}
420+
421+
func TestAppendMap(t *testing.T) {
422+
base := map[string]string{
423+
"one": "uno",
424+
}
425+
mapAppend := map[string]string{
426+
"two": "two",
427+
}
428+
result := map[string]string{
429+
"one": "uno",
430+
"two": "two",
431+
}
432+
if got := AppendMap(base, mapAppend); !reflect.DeepEqual(got, result) {
433+
t.Errorf("AppendMap() = %v, want %v", got, result)
434+
}
435+
}
436+
437+
func TestGetOperatorAndLabel(t *testing.T) {
438+
type args struct {
439+
label string
440+
}
441+
tests := []struct {
442+
name string
443+
args args
444+
want0 string
445+
want1 string
446+
}{
447+
{
448+
name: "Test for empty input",
449+
args: args{
450+
"",
451+
},
452+
want0: "",
453+
want1: "",
454+
},
455+
{
456+
name: "Test for iptables not flag",
457+
args: args{
458+
"!test",
459+
},
460+
want0: "!",
461+
want1: "test",
462+
},
463+
{
464+
name: "Test for normal label",
465+
args: args{
466+
"test",
467+
},
468+
want0: "",
469+
want1: "test",
470+
},
471+
}
472+
for _, tt := range tests {
473+
t.Run(tt.name, func(t *testing.T) {
474+
got, got1 := GetOperatorAndLabel(tt.args.label)
475+
if got != tt.want0 {
476+
t.Errorf("GetOperatorAndLabel() got = %v, want %v", got, tt.want0)
477+
}
478+
if got1 != tt.want1 {
479+
t.Errorf("GetOperatorAndLabel() got1 = %v, want %v", got1, tt.want1)
480+
}
481+
})
482+
}
483+
}
484+
485+
func TestGetLabelsWithoutOperators(t *testing.T) {
486+
want := []string{
487+
"res",
488+
"res2",
489+
}
490+
labels := []string{
491+
"!res",
492+
"res2",
493+
}
494+
if got := GetLabelsWithoutOperators(labels); !reflect.DeepEqual(want, got) {
495+
t.Errorf("GetLabelsWithoutOperators() got = %v, want %v", got, want)
496+
}
497+
}
498+
499+
func TestGetSetsFromLabels(t *testing.T) {
500+
labels := map[string]string{
501+
"key": "val",
502+
}
503+
want := []string{
504+
"key",
505+
"key:val",
506+
}
507+
if got := GetSetsFromLabels(labels); !reflect.DeepEqual(want, got) {
508+
t.Errorf("GetSetsFromLabels() got = %v, want %v", got, want)
509+
}
510+
}
511+
512+
func TestSliceToString(t *testing.T) {
513+
want := "test,test2"
514+
list := []string{
515+
"test",
516+
"test2",
517+
}
518+
if got := SliceToString(list); want != got {
519+
t.Errorf("SliceToString() got = %v, want %v, using delimiter %v", got, want, SetPolicyDelimiter)
520+
}
521+
}

0 commit comments

Comments
 (0)