Skip to content

Commit ddc3070

Browse files
validateTypeStringNullableIntOrPercent should tolerate 100% value (#1107)
* validateTypeStringNullableIntOrPercent should tolerate `100%` value * Add unit test for validateTypeStringNullableIntOrPercent Co-authored-by: John Houston <[email protected]>
1 parent 1039c8f commit ddc3070

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

kubernetes/validators.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func validateTypeStringNullableIntOrPercent(v interface{}, key string) (ws []str
281281
if err != nil {
282282
es = append(es, fmt.Errorf("%s: cannot parse '%s' as percent: %s", key, value, err))
283283
}
284-
if percent < 0 || percent >= 100 {
284+
if percent < 0 || percent > 100 {
285285
es = append(es, fmt.Errorf("%s: '%s' is not between 0%% and 100%%", key, value))
286286
}
287287
} else if _, err := strconv.ParseInt(value, 10, 32); err != nil {

kubernetes/validators_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,33 @@ func TestValidateNonNegativeInteger(t *testing.T) {
131131
}
132132
}
133133
}
134+
135+
func TestValidateTypeStringNullableIntOrPercent(t *testing.T) {
136+
validCases := []string{
137+
"",
138+
"1",
139+
"100",
140+
"1%",
141+
"100%",
142+
}
143+
for _, data := range validCases {
144+
_, es := validateTypeStringNullableIntOrPercent(data, "replicas")
145+
if len(es) > 0 {
146+
t.Fatalf("Expected %q to be valid: %#v", data, es)
147+
}
148+
}
149+
invalidCases := []string{
150+
" ",
151+
"0.1",
152+
"test",
153+
"!@@#$",
154+
"💣",
155+
"%",
156+
}
157+
for _, data := range invalidCases {
158+
_, es := validateTypeStringNullableIntOrPercent(data, "replicas")
159+
if len(es) == 0 {
160+
t.Fatalf("Expected %q to be invalid", data)
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)