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
b476ee4b
Commit
b476ee4b
authored
Apr 08, 2016
by
nikhiljindal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allowing type object in kubectl swagger validation
parent
ce988c8d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
9 deletions
+55
-9
schema.go
pkg/api/validation/schema.go
+3
-0
schema_test.go
pkg/api/validation/schema_test.go
+52
-9
No files found.
pkg/api/validation/schema.go
View file @
b476ee4b
...
@@ -282,7 +282,10 @@ func (s *SwaggerSchema) validateField(value interface{}, fieldName, fieldType st
...
@@ -282,7 +282,10 @@ func (s *SwaggerSchema) validateField(value interface{}, fieldName, fieldType st
if
_
,
ok
:=
value
.
(
bool
);
!
ok
{
if
_
,
ok
:=
value
.
(
bool
);
!
ok
{
return
append
(
allErrs
,
NewInvalidTypeError
(
reflect
.
Bool
,
reflect
.
TypeOf
(
value
)
.
Kind
(),
fieldName
))
return
append
(
allErrs
,
NewInvalidTypeError
(
reflect
.
Bool
,
reflect
.
TypeOf
(
value
)
.
Kind
(),
fieldName
))
}
}
// API servers before release 1.3 produce swagger spec with `type: "any"` as the fallback type, while newer servers produce spec with `type: "object"`.
// We have both here so that kubectl can work with both old and new api servers.
case
"any"
:
case
"any"
:
case
"object"
:
default
:
default
:
return
append
(
allErrs
,
fmt
.
Errorf
(
"unexpected type: %v"
,
fieldType
))
return
append
(
allErrs
,
fmt
.
Errorf
(
"unexpected type: %v"
,
fieldType
))
}
}
...
...
pkg/api/validation/schema_test.go
View file @
b476ee4b
...
@@ -19,26 +19,33 @@ package validation
...
@@ -19,26 +19,33 @@ package validation
import
(
import
(
"io/ioutil"
"io/ioutil"
"math/rand"
"math/rand"
"strings"
"testing"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/testapi"
apitesting
"k8s.io/kubernetes/pkg/api/testing"
apitesting
"k8s.io/kubernetes/pkg/api/testing"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/runtime"
"github.com/ghodss/yaml"
)
)
func
readPod
(
filename
string
)
(
string
,
error
)
{
func
readPod
(
filename
string
)
(
[]
byte
,
error
)
{
data
,
err
:=
ioutil
.
ReadFile
(
"testdata/"
+
testapi
.
Default
.
GroupVersion
()
.
Version
+
"/"
+
filename
)
data
,
err
:=
ioutil
.
ReadFile
(
"testdata/"
+
testapi
.
Default
.
GroupVersion
()
.
Version
+
"/"
+
filename
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
nil
,
err
}
}
return
string
(
data
)
,
nil
return
data
,
nil
}
}
func
loadSchemaForTest
()
(
Schema
,
error
)
{
func
readSwaggerFile
()
([]
byte
,
error
)
{
// TODO this path is broken
// TODO this path is broken
pathToSwaggerSpec
:=
"../../../api/swagger-spec/"
+
testapi
.
Default
.
GroupVersion
()
.
Version
+
".json"
pathToSwaggerSpec
:=
"../../../api/swagger-spec/"
+
testapi
.
Default
.
GroupVersion
()
.
Version
+
".json"
data
,
err
:=
ioutil
.
ReadFile
(
pathToSwaggerSpec
)
return
ioutil
.
ReadFile
(
pathToSwaggerSpec
)
}
func
loadSchemaForTest
()
(
Schema
,
error
)
{
data
,
err
:=
readSwaggerFile
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -98,9 +105,9 @@ func TestInvalid(t *testing.T) {
...
@@ -98,9 +105,9 @@ func TestInvalid(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
pod
,
err
:=
readPod
(
test
)
pod
,
err
:=
readPod
(
test
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"could not read file: %s
"
,
pod
)
t
.
Errorf
(
"could not read file: %s
, err: %v"
,
test
,
err
)
}
}
err
=
schema
.
ValidateBytes
(
[]
byte
(
pod
)
)
err
=
schema
.
ValidateBytes
(
pod
)
if
err
==
nil
{
if
err
==
nil
{
t
.
Errorf
(
"unexpected non-error, err: %s for pod: %s"
,
err
,
pod
)
t
.
Errorf
(
"unexpected non-error, err: %s for pod: %s"
,
err
,
pod
)
}
}
...
@@ -118,9 +125,9 @@ func TestValid(t *testing.T) {
...
@@ -118,9 +125,9 @@ func TestValid(t *testing.T) {
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
pod
,
err
:=
readPod
(
test
)
pod
,
err
:=
readPod
(
test
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"could not read file: %s
"
,
test
)
t
.
Errorf
(
"could not read file: %s
, err: %v"
,
test
,
err
)
}
}
err
=
schema
.
ValidateBytes
(
[]
byte
(
pod
)
)
err
=
schema
.
ValidateBytes
(
pod
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error %s, for pod %s"
,
err
,
pod
)
t
.
Errorf
(
"unexpected error %s, for pod %s"
,
err
,
pod
)
}
}
...
@@ -154,3 +161,39 @@ func TestVersionRegex(t *testing.T) {
...
@@ -154,3 +161,39 @@ func TestVersionRegex(t *testing.T) {
}
}
}
}
}
}
// Tests that validation works fine when spec contains "type": "object" instead of "type": "any"
func
TestTypeObject
(
t
*
testing
.
T
)
{
data
,
err
:=
readSwaggerFile
()
if
err
!=
nil
{
t
.
Errorf
(
"failed to read swagger file: %v"
,
err
)
}
// Replace type: "any" in the spec by type: "object" and verify that the validation still passes.
newData
:=
strings
.
Replace
(
string
(
data
),
`"type": "any"`
,
`"type": "object"`
,
-
1
)
schema
,
err
:=
NewSwaggerSchemaFromBytes
([]
byte
(
newData
))
if
err
!=
nil
{
t
.
Errorf
(
"Failed to load: %v"
,
err
)
}
tests
:=
[]
string
{
"validPod.yaml"
,
}
for
_
,
test
:=
range
tests
{
podBytes
,
err
:=
readPod
(
test
)
if
err
!=
nil
{
t
.
Errorf
(
"could not read file: %s, err: %v"
,
test
,
err
)
}
// Verify that pod has at least one label (labels are type "any")
var
pod
api
.
Pod
err
=
yaml
.
Unmarshal
(
podBytes
,
&
pod
)
if
err
!=
nil
{
t
.
Errorf
(
"error in unmarshalling pod: %v"
,
err
)
}
if
len
(
pod
.
Labels
)
==
0
{
t
.
Errorf
(
"invalid test input: the pod should have at least one label"
)
}
err
=
schema
.
ValidateBytes
(
podBytes
)
if
err
!=
nil
{
t
.
Errorf
(
"unexpected error %s, for pod %s"
,
err
,
string
(
podBytes
))
}
}
}
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