Skip to content

Commit

Permalink
Merge pull request #728 from reetasingh/master
Browse files Browse the repository at this point in the history
Adding unit test and fixing typo
  • Loading branch information
k8s-ci-robot committed Apr 23, 2019
2 parents 5bcff06 + d06a5e0 commit 1d5b808
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/options/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions pkg/options/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
44 changes: 44 additions & 0 deletions pkg/whiteblacklist/whiteblacklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package whiteblacklist

import (
"regexp"
"testing"
)

Expand Down Expand Up @@ -118,3 +119,46 @@ func TestExclude(t *testing.T) {
}
})
}

func TestStatus(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 %q but got %q", expectedStatusString, actualStatusString)
}
})
t.Run("status when whitelist has multiple items", func(t *testing.T) {
item1 := "item1"
item2 := "item2"
whitelist, _ := New(map[string]struct{}{item1: {}, item2: {}}, map[string]struct{}{})
actualStatusString := whitelist.Status()
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) {
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 %q but got %q", expectedStatusString, actualStatusString)
}
})
t.Run("status when blacklist has multiple items", func(t *testing.T) {
item1 := "item1"
item2 := "item2"
blacklist, _ := New(map[string]struct{}{}, map[string]struct{}{item1: {}, item2: {}})
actualStatusString := blacklist.Status()
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)
}
})
}

0 comments on commit 1d5b808

Please sign in to comment.