Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
bb208a02
Commit
bb208a02
authored
Jan 28, 2016
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make IsValidPercent return error strings
parent
87c1fc50
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
6 deletions
+48
-6
validation.go
pkg/apis/extensions/validation/validation.go
+6
-3
validation_test.go
pkg/apis/extensions/validation/validation_test.go
+1
-1
validation.go
pkg/util/validation/validation.go
+5
-2
validation_test.go
pkg/util/validation/validation_test.go
+36
-0
No files found.
pkg/apis/extensions/validation/validation.go
View file @
bb208a02
...
...
@@ -149,8 +149,8 @@ func ValidatePositiveIntOrPercent(intOrPercent intstr.IntOrString, fldPath *fiel
allErrs
:=
field
.
ErrorList
{}
switch
intOrPercent
.
Type
{
case
intstr
.
String
:
if
!
validation
.
IsValidPercent
(
intOrPercent
.
StrVal
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
,
intOrPercent
,
"must be an integer or percentage (e.g '5%%')"
))
for
_
,
msg
:=
range
validation
.
IsValidPercent
(
intOrPercent
.
StrVal
)
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
,
intOrPercent
,
msg
))
}
case
intstr
.
Int
:
allErrs
=
append
(
allErrs
,
apivalidation
.
ValidateNonnegativeField
(
int64
(
intOrPercent
.
IntValue
()),
fldPath
)
...
)
...
...
@@ -161,7 +161,10 @@ func ValidatePositiveIntOrPercent(intOrPercent intstr.IntOrString, fldPath *fiel
}
func
getPercentValue
(
intOrStringValue
intstr
.
IntOrString
)
(
int
,
bool
)
{
if
intOrStringValue
.
Type
!=
intstr
.
String
||
!
validation
.
IsValidPercent
(
intOrStringValue
.
StrVal
)
{
if
intOrStringValue
.
Type
!=
intstr
.
String
{
return
0
,
false
}
if
len
(
validation
.
IsValidPercent
(
intOrStringValue
.
StrVal
))
!=
0
{
return
0
,
false
}
value
,
_
:=
strconv
.
Atoi
(
intOrStringValue
.
StrVal
[
:
len
(
intOrStringValue
.
StrVal
)
-
1
])
...
...
pkg/apis/extensions/validation/validation_test.go
View file @
bb208a02
...
...
@@ -632,7 +632,7 @@ func TestValidateDeployment(t *testing.T) {
MaxSurge
:
intstr
.
FromString
(
"20Percent"
),
},
}
errorCases
[
"must
be an integer or percentage
"
]
=
invalidMaxSurgeDeployment
errorCases
[
"must
match the regex
"
]
=
invalidMaxSurgeDeployment
// MaxSurge and MaxUnavailable cannot both be zero.
invalidRollingUpdateDeployment
:=
validDeployment
()
...
...
pkg/util/validation/validation.go
View file @
bb208a02
...
...
@@ -221,8 +221,11 @@ const percentFmt string = "[0-9]+%"
var
percentRegexp
=
regexp
.
MustCompile
(
"^"
+
percentFmt
+
"$"
)
func
IsValidPercent
(
percent
string
)
bool
{
return
percentRegexp
.
MatchString
(
percent
)
func
IsValidPercent
(
percent
string
)
[]
string
{
if
!
percentRegexp
.
MatchString
(
percent
)
{
return
[]
string
{
RegexError
(
percentFmt
,
"1%"
,
"93%"
)}
}
return
nil
}
const
HTTPHeaderNameFmt
string
=
"[-A-Za-z0-9]+"
...
...
pkg/util/validation/validation_test.go
View file @
bb208a02
...
...
@@ -338,3 +338,39 @@ func TestIsHTTPHeaderName(t *testing.T) {
}
}
}
func
TestIsValidPercent
(
t
*
testing
.
T
)
{
goodValues
:=
[]
string
{
"0%"
,
"00000%"
,
"1%"
,
"01%"
,
"99%"
,
"100%"
,
"101%"
,
}
for
_
,
val
:=
range
goodValues
{
if
msgs
:=
IsValidPercent
(
val
);
len
(
msgs
)
!=
0
{
t
.
Errorf
(
"expected true for %q: %v"
,
val
,
msgs
)
}
}
badValues
:=
[]
string
{
""
,
"0"
,
"100"
,
"0.0%"
,
"99.9%"
,
"hundred"
,
" 1%"
,
"1% "
,
"-0%"
,
"-1%"
,
"+1%"
,
}
for
_
,
val
:=
range
badValues
{
if
msgs
:=
IsValidPercent
(
val
);
len
(
msgs
)
==
0
{
t
.
Errorf
(
"expected false for %q"
,
val
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment