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
825dc470
Commit
825dc470
authored
Feb 12, 2016
by
Eric Tune
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a validation helper function.
I will use this in a subsequent PR as part of #12298
parent
468d0db8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
0 deletions
+51
-0
validation.go
pkg/api/validation/validation.go
+14
-0
validation_test.go
pkg/api/validation/validation_test.go
+37
-0
No files found.
pkg/api/validation/validation.go
View file @
825dc470
...
@@ -84,6 +84,20 @@ func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorLi
...
@@ -84,6 +84,20 @@ func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorLi
return
allErrs
return
allErrs
}
}
// ValidateHasLabel requires that api.ObjectMeta has a Label with key and expectedValue
func
ValidateHasLabel
(
meta
api
.
ObjectMeta
,
fldPath
*
field
.
Path
,
key
,
expectedValue
string
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
actualValue
,
found
:=
meta
.
Labels
[
key
]
if
!
found
{
allErrs
=
append
(
allErrs
,
field
.
Required
(
fldPath
.
Child
(
"labels"
),
key
+
"="
+
expectedValue
))
return
allErrs
}
if
actualValue
!=
expectedValue
{
allErrs
=
append
(
allErrs
,
field
.
Invalid
(
fldPath
.
Child
(
"labels"
),
meta
.
Labels
,
"expected "
+
key
+
"="
+
expectedValue
))
}
return
allErrs
}
// ValidateAnnotations validates that a set of annotations are correctly defined.
// ValidateAnnotations validates that a set of annotations are correctly defined.
func
ValidateAnnotations
(
annotations
map
[
string
]
string
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
func
ValidateAnnotations
(
annotations
map
[
string
]
string
,
fldPath
*
field
.
Path
)
field
.
ErrorList
{
allErrs
:=
field
.
ErrorList
{}
allErrs
:=
field
.
ErrorList
{}
...
...
pkg/api/validation/validation_test.go
View file @
825dc470
...
@@ -5009,3 +5009,40 @@ func TestValidateConfigMapUpdate(t *testing.T) {
...
@@ -5009,3 +5009,40 @@ func TestValidateConfigMapUpdate(t *testing.T) {
}
}
}
}
}
}
func
TestValidateHasLabel
(
t
*
testing
.
T
)
{
successCase
:=
api
.
ObjectMeta
{
Name
:
"123"
,
Namespace
:
"ns"
,
Labels
:
map
[
string
]
string
{
"other"
:
"blah"
,
"foo"
:
"bar"
,
},
}
if
errs
:=
ValidateHasLabel
(
successCase
,
field
.
NewPath
(
"field"
),
"foo"
,
"bar"
);
len
(
errs
)
!=
0
{
t
.
Errorf
(
"expected success: %v"
,
errs
)
}
missingCase
:=
api
.
ObjectMeta
{
Name
:
"123"
,
Namespace
:
"ns"
,
Labels
:
map
[
string
]
string
{
"other"
:
"blah"
,
},
}
if
errs
:=
ValidateHasLabel
(
missingCase
,
field
.
NewPath
(
"field"
),
"foo"
,
"bar"
);
len
(
errs
)
==
0
{
t
.
Errorf
(
"expected failure"
)
}
wrongValueCase
:=
api
.
ObjectMeta
{
Name
:
"123"
,
Namespace
:
"ns"
,
Labels
:
map
[
string
]
string
{
"other"
:
"blah"
,
"foo"
:
"notbar"
,
},
}
if
errs
:=
ValidateHasLabel
(
wrongValueCase
,
field
.
NewPath
(
"field"
),
"foo"
,
"bar"
);
len
(
errs
)
==
0
{
t
.
Errorf
(
"expected failure"
)
}
}
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