-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de8f0b3
commit 907a8a5
Showing
1 changed file
with
51 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,60 @@ | ||
package utils | ||
|
||
import ( | ||
"k8s.io/utils/diff" | ||
"reflect" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// Please, one func per test for readability | ||
|
||
func TestParseCustomLabels(t *testing.T) { | ||
testCases := []struct { | ||
given string | ||
expect map[string]string | ||
}{ | ||
{ | ||
given: "test=123,123=test", | ||
expect: map[string]string{"test": "123", "123": "test"}, | ||
//description: "", | ||
|
||
}, | ||
{ | ||
given: "test=123123=test", | ||
expect: map[string]string{"test": "123123=test"}, | ||
}, | ||
{ | ||
given: "test123,123test", | ||
expect: map[string]string{}, | ||
}, | ||
{ | ||
given: "customer=ca,creator=kubi,test=123123=test", | ||
expect: map[string]string{"test": "123123=test"}, | ||
}, | ||
{ | ||
given: "creator=kubi", | ||
expect: map[string]string{}, | ||
}, | ||
} | ||
|
||
for index, testCase := range testCases { | ||
result := parseCustomLabels(testCase.given) | ||
if !reflect.DeepEqual(testCase.expect, result) { | ||
t.Errorf("Test Case %d, Unexpected result\nDiff:\n %s", | ||
index, | ||
diff.ObjectGoPrintSideBySide(testCase.expect, result)) | ||
} | ||
|
||
} | ||
|
||
} | ||
t.Run("properly formatted k/v string should return two labels", func(t *testing.T) { | ||
result := parseCustomLabels("test=123,123=test") | ||
assert.Equal(t, result, map[string]string{"test": "123", "123": "test"}) | ||
}) | ||
|
||
t.Run("two equal without a coma should return a single label containing an equal", func(t *testing.T) { | ||
result := parseCustomLabels("test=123123=test") | ||
assert.Equal(t, result, map[string]string{"test": "123123=test"}) | ||
}) | ||
|
||
t.Run("two separated strings without equals should not return labels", func(t *testing.T) { | ||
result := parseCustomLabels("test123,123test") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
t.Run("three k/v containing two illegals key should return a single label", func(t *testing.T) { | ||
result := parseCustomLabels("customer=ca,creator=kubi,test=123123") | ||
assert.Equal(t, result, map[string]string{"test": "123123"}) | ||
}) | ||
|
||
t.Run("a single illegal label should not return label", func(t *testing.T) { | ||
result := parseCustomLabels("creator=kubi") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
t.Run("a single illegal label should not return label", func(t *testing.T) { | ||
result := parseCustomLabels("creator=kubi") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
t.Run("an illegal key should not return label", func(t *testing.T) { | ||
result := parseCustomLabels("creator=") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
t.Run("a single key should not return label", func(t *testing.T) { | ||
result := parseCustomLabels("test=") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
t.Run("a value should not return label", func(t *testing.T) { | ||
result := parseCustomLabels("=test") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
t.Run("a string should not return label", func(t *testing.T) { | ||
result := parseCustomLabels("test") | ||
assert.Equal(t, result, map[string]string{}) | ||
}) | ||
|
||
// Use more combination of failure | ||
//given: "customer==ca", | ||
//given: "cust=omer=ca", | ||
//given: "customer=", | ||
//given: "customer=,", | ||
//given: "=ca", | ||
} |