Skip to content

Commit

Permalink
Adding unit tests for utils.go
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Cole <[email protected]>
  • Loading branch information
preflightsiren committed Oct 1, 2021
1 parent ffab2f5 commit 931b500
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions controllers/providers/kubernetes/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package kubernetes

import (
"testing"
)

func TestHasAnnotation(t *testing.T) {
annotations := map[string]string{
"foo": "bar",
"baz": "",
}
tests := []struct {
name string
annotations map[string]string
key string
expected bool
}{
{
name: "present with value",
annotations: annotations,
key: "foo",
expected: true,
},
{
name: "present with no value",
annotations: annotations,
key: "baz",
expected: true,
},
{
name: "absent",
annotations: annotations,
key: "missing",
expected: false,
},
}

for _, tc := range tests {
result := HasAnnotation(tc.annotations, tc.key)
if result != tc.expected {
t.Fail()
}
}

}

func TestHasAnnotationWithValue(t *testing.T) {
annotations := map[string]string{
"foo": "bar",
"baz": "",
}
tests := []struct {
name string
annotations map[string]string
key string
value string
expected bool
}{
{
name: "present with value expecting value",
annotations: annotations,
key: "foo",
value: "bar",
expected: true,
},
{
name: "present with value expecting no value",
annotations: annotations,
key: "foo",
value: "",
expected: false,
},
{
name: "present with no value expecting no value",
annotations: annotations,
key: "baz",
value: "",
expected: true,
},
{
name: "present with no value expecting value",
annotations: annotations,
key: "baz",
value: "boop",
expected: false,
},
{
name: "absent",
annotations: annotations,
key: "missing",
value: "",
expected: false,
},
}

for _, tc := range tests {
result := HasAnnotationWithValue(tc.annotations, tc.key, tc.value)
if result != tc.expected {
t.Fatalf("Unexpected result %v. expected %v from %s", result, tc.expected, tc.name)
}
}

}

0 comments on commit 931b500

Please sign in to comment.