From 2720645a94bee1b23178189192c7f8f8362a03b4 Mon Sep 17 00:00:00 2001 From: reetasingh Date: Sun, 21 Apr 2019 03:14:12 -0400 Subject: [PATCH 1/5] Adding unit test and fixing typo --- pkg/options/types.go | 2 +- pkg/options/types_test.go | 30 +++++++++++++++++++++++ pkg/whiteblacklist/whiteblacklist_test.go | 21 ++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/pkg/options/types.go b/pkg/options/types.go index 322be8f947..2f263827d9 100644 --- a/pkg/options/types.go +++ b/pkg/options/types.go @@ -113,7 +113,7 @@ func (c *CollectorSet) Type() string { return "string" } -// NamespaceList represents a list of namespaces to query forom. +// NamespaceList represents a list of namespaces to query from. type NamespaceList []string func (n *NamespaceList) String() string { diff --git a/pkg/options/types_test.go b/pkg/options/types_test.go index f6de0c7db6..638293d305 100644 --- a/pkg/options/types_test.go +++ b/pkg/options/types_test.go @@ -61,3 +61,33 @@ func TestCollectorSetSet(t *testing.T) { } } } + +func TestNamespaceListSet(t *testing.T) { + tests := []struct { + Desc string + Value string + Wanted NamespaceList + }{ + { + Desc: "empty namespacelist", + Value: "", + Wanted: NamespaceList{}, + }, + { + Desc: "normal namespacelist", + Value: "default, kube-system", + Wanted: NamespaceList([]string{ + "default", + "kube-system", + }), + }, + } + + for _, test := range tests { + ns := &NamespaceList{} + gotError := ns.Set(test.Value) + if gotError != nil || !reflect.DeepEqual(*ns, test.Wanted) { + t.Errorf("Test error for Desc: %s. Want: %+v. Got: %+v. Got Error: %v", test.Desc, test.Wanted, *ns, gotError) + } + } +} diff --git a/pkg/whiteblacklist/whiteblacklist_test.go b/pkg/whiteblacklist/whiteblacklist_test.go index be1d2a7f99..01a2d5d442 100644 --- a/pkg/whiteblacklist/whiteblacklist_test.go +++ b/pkg/whiteblacklist/whiteblacklist_test.go @@ -118,3 +118,24 @@ func TestExclude(t *testing.T) { } }) } + +func TestStatus(t *testing.T) { + t.Run("status when whitelist", func(t *testing.T) { + item1 := "item1" + whitelist, _ := New(map[string]struct{}{item1: {}}, map[string]struct{}{}) + actualStatusString := whitelist.Status() + expectedStatusString := "whitelisting the following items: " + item1 + if actualStatusString != expectedStatusString { + t.Errorf("expected status %s but got %s", expectedStatusString, actualStatusString) + } + }) + t.Run("status when blacklist", func(t *testing.T) { + item1 := "not-empty" + blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {}}) + actualStatusString := blacklist.Status() + expectedStatusString := "blacklisting the following items: " + item1 + if actualStatusString != expectedStatusString { + t.Errorf("expected status %s but got %s", expectedStatusString, actualStatusString) + } + }) +} From c9502a2d7bccf249d555b6816e2b9923e16f2a27 Mon Sep 17 00:00:00 2001 From: reetasingh Date: Tue, 23 Apr 2019 00:36:27 -0400 Subject: [PATCH 2/5] Adding test + modifying based on review comments --- pkg/whiteblacklist/whiteblacklist_test.go | 33 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/pkg/whiteblacklist/whiteblacklist_test.go b/pkg/whiteblacklist/whiteblacklist_test.go index 01a2d5d442..5b6d9b4040 100644 --- a/pkg/whiteblacklist/whiteblacklist_test.go +++ b/pkg/whiteblacklist/whiteblacklist_test.go @@ -17,6 +17,7 @@ limitations under the License. package whiteblacklist import ( + "strings" "testing" ) @@ -120,22 +121,44 @@ func TestExclude(t *testing.T) { } func TestStatus(t *testing.T) { - t.Run("status when whitelist", func(t *testing.T) { + t.Run("status when whitelist has single item", func(t *testing.T) { item1 := "item1" whitelist, _ := New(map[string]struct{}{item1: {}}, map[string]struct{}{}) actualStatusString := whitelist.Status() expectedStatusString := "whitelisting the following items: " + item1 if actualStatusString != expectedStatusString { - t.Errorf("expected status %s but got %s", expectedStatusString, actualStatusString) + t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) } }) - t.Run("status when blacklist", func(t *testing.T) { + t.Run("status when whitelist has multiple items", func(t *testing.T) { + item1 := "item1" + item2 := "item2" + items := []string{item1, item2} + whitelist, _ := New(map[string]struct{}{item1: {}, item2: {}}, map[string]struct{}{}) + actualStatusString := whitelist.Status() + expectedStatusString := "whitelisting the following items: " + strings.Join(items, ",") + if actualStatusString != expectedStatusString { + t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) + } + }) + t.Run("status when blacklist has single item", func(t *testing.T) { item1 := "not-empty" blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {}}) actualStatusString := blacklist.Status() expectedStatusString := "blacklisting the following items: " + item1 if actualStatusString != expectedStatusString { - t.Errorf("expected status %s but got %s", expectedStatusString, actualStatusString) + t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) } }) -} + t.Run("status when blacklist has multiple items", func(t *testing.T) { + item1 := "not-empty" + item2 := "item2" + items := []string{item1, item2} + blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {} , item2: {}}) + actualStatusString := blacklist.Status() + expectedStatusString := "blacklisting the following items: " + strings.Join(items, ",") + if actualStatusString != expectedStatusString { + t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) + } + }) +} \ No newline at end of file From 44bff055317c7804bb6483e36373c0ba9754ca88 Mon Sep 17 00:00:00 2001 From: reetasingh Date: Tue, 23 Apr 2019 00:45:40 -0400 Subject: [PATCH 3/5] fixing formatting --- pkg/whiteblacklist/whiteblacklist_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/whiteblacklist/whiteblacklist_test.go b/pkg/whiteblacklist/whiteblacklist_test.go index 5b6d9b4040..83cc60260a 100644 --- a/pkg/whiteblacklist/whiteblacklist_test.go +++ b/pkg/whiteblacklist/whiteblacklist_test.go @@ -154,11 +154,11 @@ func TestStatus(t *testing.T) { item1 := "not-empty" item2 := "item2" items := []string{item1, item2} - blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {} , item2: {}}) + blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {}, item2: {}}) actualStatusString := blacklist.Status() expectedStatusString := "blacklisting the following items: " + strings.Join(items, ",") if actualStatusString != expectedStatusString { t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) } }) -} \ No newline at end of file +} From 368bd9d2916ddaac6d71963b52a12fdec408ed01 Mon Sep 17 00:00:00 2001 From: reetasingh Date: Tue, 23 Apr 2019 02:32:27 -0400 Subject: [PATCH 4/5] fixing formatting --- pkg/whiteblacklist/whiteblacklist_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/whiteblacklist/whiteblacklist_test.go b/pkg/whiteblacklist/whiteblacklist_test.go index 83cc60260a..5f89105660 100644 --- a/pkg/whiteblacklist/whiteblacklist_test.go +++ b/pkg/whiteblacklist/whiteblacklist_test.go @@ -17,7 +17,7 @@ limitations under the License. package whiteblacklist import ( - "strings" + "regexp" "testing" ) @@ -133,12 +133,12 @@ func TestStatus(t *testing.T) { t.Run("status when whitelist has multiple items", func(t *testing.T) { item1 := "item1" item2 := "item2" - items := []string{item1, item2} whitelist, _ := New(map[string]struct{}{item1: {}, item2: {}}, map[string]struct{}{}) actualStatusString := whitelist.Status() - expectedStatusString := "whitelisting the following items: " + strings.Join(items, ",") - if actualStatusString != expectedStatusString { - t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) + expectedRegexPattern := `^whitelisting the following items: item1|item2, item2|item1$` + matched, _ := regexp.MatchString(expectedRegexPattern, actualStatusString) + if !matched { + t.Errorf("expected status %q but got %q", expectedRegexPattern, actualStatusString) } }) t.Run("status when blacklist has single item", func(t *testing.T) { @@ -151,14 +151,14 @@ func TestStatus(t *testing.T) { } }) t.Run("status when blacklist has multiple items", func(t *testing.T) { - item1 := "not-empty" + item1 := "item1" item2 := "item2" - items := []string{item1, item2} blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {}, item2: {}}) actualStatusString := blacklist.Status() - expectedStatusString := "blacklisting the following items: " + strings.Join(items, ",") - if actualStatusString != expectedStatusString { - t.Errorf("expected status %q but got %q", expectedStatusString, actualStatusString) + expectedRegexPattern := `^blacklisting the following items: item1|item2, item2|item1$` + matched, _ := regexp.MatchString(expectedRegexPattern, actualStatusString) + if !matched { + t.Errorf("expected status %q but got %q", expectedRegexPattern, actualStatusString) } }) } From d06a5e0452ad9f9a94d8a881177e23dc867e62bf Mon Sep 17 00:00:00 2001 From: reetasingh Date: Tue, 23 Apr 2019 03:50:48 -0400 Subject: [PATCH 5/5] Adding parenthesis in the regex --- pkg/whiteblacklist/whiteblacklist_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/whiteblacklist/whiteblacklist_test.go b/pkg/whiteblacklist/whiteblacklist_test.go index 5f89105660..9ee0444507 100644 --- a/pkg/whiteblacklist/whiteblacklist_test.go +++ b/pkg/whiteblacklist/whiteblacklist_test.go @@ -135,7 +135,7 @@ func TestStatus(t *testing.T) { item2 := "item2" whitelist, _ := New(map[string]struct{}{item1: {}, item2: {}}, map[string]struct{}{}) actualStatusString := whitelist.Status() - expectedRegexPattern := `^whitelisting the following items: item1|item2, item2|item1$` + expectedRegexPattern := `^whitelisting the following items: (item1|item2), (item2|item1)$` matched, _ := regexp.MatchString(expectedRegexPattern, actualStatusString) if !matched { t.Errorf("expected status %q but got %q", expectedRegexPattern, actualStatusString) @@ -155,7 +155,7 @@ func TestStatus(t *testing.T) { item2 := "item2" blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {}, item2: {}}) actualStatusString := blacklist.Status() - expectedRegexPattern := `^blacklisting the following items: item1|item2, item2|item1$` + expectedRegexPattern := `^blacklisting the following items: (item1|item2), (item2|item1)$` matched, _ := regexp.MatchString(expectedRegexPattern, actualStatusString) if !matched { t.Errorf("expected status %q but got %q", expectedRegexPattern, actualStatusString)