Skip to content

Commit

Permalink
validateTypeStringNullableIntOrPercent should tolerate 100% value (#…
Browse files Browse the repository at this point in the history
…1107)

* validateTypeStringNullableIntOrPercent should tolerate `100%` value

* Add unit test for validateTypeStringNullableIntOrPercent

Co-authored-by: John Houston <[email protected]>
  • Loading branch information
dee-kryvenko and jrhouston authored Mar 31, 2021
1 parent 1039c8f commit ddc3070
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kubernetes/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func validateTypeStringNullableIntOrPercent(v interface{}, key string) (ws []str
if err != nil {
es = append(es, fmt.Errorf("%s: cannot parse '%s' as percent: %s", key, value, err))
}
if percent < 0 || percent >= 100 {
if percent < 0 || percent > 100 {
es = append(es, fmt.Errorf("%s: '%s' is not between 0%% and 100%%", key, value))
}
} else if _, err := strconv.ParseInt(value, 10, 32); err != nil {
Expand Down
30 changes: 30 additions & 0 deletions kubernetes/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,33 @@ func TestValidateNonNegativeInteger(t *testing.T) {
}
}
}

func TestValidateTypeStringNullableIntOrPercent(t *testing.T) {
validCases := []string{
"",
"1",
"100",
"1%",
"100%",
}
for _, data := range validCases {
_, es := validateTypeStringNullableIntOrPercent(data, "replicas")
if len(es) > 0 {
t.Fatalf("Expected %q to be valid: %#v", data, es)
}
}
invalidCases := []string{
" ",
"0.1",
"test",
"!@@#$",
"💣",
"%",
}
for _, data := range invalidCases {
_, es := validateTypeStringNullableIntOrPercent(data, "replicas")
if len(es) == 0 {
t.Fatalf("Expected %q to be invalid", data)
}
}
}

0 comments on commit ddc3070

Please sign in to comment.