Skip to content

Commit

Permalink
Add unit tests for common.go in pkg/util
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Aug 8, 2024
1 parent 5a10d75 commit 13993ee
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions pkg/util/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,22 @@ func TestDiffKey(t *testing.T) {
t.Errorf("removed = %v, want []string{\"kubefed\"}", removed)
}
})
t.Run("no old, no new", func(t *testing.T) {
var previous map[string]sigMultiCluster = nil

Check failure on line 73 in pkg/util/common_test.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var previous; it is the zero value (revive)
var current map[string]string = nil

Check failure on line 74 in pkg/util/common_test.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var current; it is the zero value (revive)
added, removed := DiffKey(previous, current)
if len(added) != 0 && len(removed) != 0 {
t.Errorf("added = %v want nil, removed = %v want nil", added, removed)
}
if len(added) != 0 {
t.Errorf("added = %v, want nil", added)
}
if len(removed) != 0 {
t.Errorf("removed = %v, want nil", removed)
}
})
t.Run("no old, all new added", func(t *testing.T) {
previous := map[string]sigMultiCluster{}
var previous map[string]sigMultiCluster = nil

Check failure on line 87 in pkg/util/common_test.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var previous; it is the zero value (revive)
current := map[string]string{
"karmada": "v1.6.0",
}
Expand All @@ -82,6 +96,19 @@ func TestDiffKey(t *testing.T) {
t.Errorf("removed = %v, want nil", removed)
}
})
t.Run("all old removed, no new", func(t *testing.T) {
previous := map[string]sigMultiCluster{
"kubefed": kubefed,
}
var current map[string]string = nil

Check failure on line 103 in pkg/util/common_test.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var current; it is the zero value (revive)
added, removed := DiffKey(previous, current)
if len(added) != 0 {
t.Errorf("added = %v, want nil", added)
}
if len(removed) != 1 || removed[0] != "kubefed" {
t.Errorf("removed = %v, want []string{\"kubefed\"}", removed)
}
})
t.Run("removed, added and remained", func(t *testing.T) {
previous := map[string]sigMultiCluster{
"kubefed": kubefed,
Expand Down Expand Up @@ -111,15 +138,26 @@ func TestStringerJoin(t *testing.T) {
}

func TestKeys(t *testing.T) {
mcs := map[string]sigMultiCluster{
kubefed.Name: kubefed,
karmada.Name: karmada,
}
got := Keys(mcs)
expect := []string{kubefed.Name, karmada.Name}
sort.Strings(got)
sort.Strings(expect)
if !reflect.DeepEqual(got, expect) {
t.Errorf("got = %v, want %v", got, expect)
}
t.Run("keys exist", func(t *testing.T) {
mcs := map[string]sigMultiCluster{
kubefed.Name: kubefed,
karmada.Name: karmada,
}
got := Keys(mcs)
expect := []string{kubefed.Name, karmada.Name}
sort.Strings(got)
sort.Strings(expect)
if !reflect.DeepEqual(got, expect) {
t.Errorf("got = %v, want %v", got, expect)
}
})
t.Run("empty keys", func(t *testing.T) {
var mcs map[string]sigMultiCluster = nil

Check failure on line 155 in pkg/util/common_test.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var mcs; it is the zero value (revive)
got := Keys(mcs)
var expect []string = nil

Check failure on line 157 in pkg/util/common_test.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should drop = nil from declaration of var expect; it is the zero value (revive)
sort.Strings(got)
if !reflect.DeepEqual(got, expect) {
t.Errorf("got = %v, want %v", got, expect)
}
})
}

0 comments on commit 13993ee

Please sign in to comment.