mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
add tests for field validation verifier
This commit is contained in:
parent
d6c83281bc
commit
bc68466b34
File diff suppressed because it is too large
Load Diff
@ -25,16 +25,19 @@ import (
|
|||||||
openapitesting "k8s.io/kube-openapi/pkg/util/proto/testing"
|
openapitesting "k8s.io/kube-openapi/pkg/util/proto/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSupportsDryRun(t *testing.T) {
|
var fakeSchema = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger.json")}
|
||||||
|
|
||||||
|
func TestSupportsQueryParam(t *testing.T) {
|
||||||
doc, err := fakeSchema.OpenAPISchema()
|
doc, err := fakeSchema.OpenAPISchema()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to get OpenAPI Schema: %v", err)
|
t.Fatalf("Failed to get OpenAPI Schema: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
gvk schema.GroupVersionKind
|
gvk schema.GroupVersionKind
|
||||||
success bool
|
success bool
|
||||||
supports bool
|
supports bool
|
||||||
|
queryParam VerifiableQueryParam
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
gvk: schema.GroupVersionKind{
|
gvk: schema.GroupVersionKind{
|
||||||
@ -42,8 +45,19 @@ func TestSupportsDryRun(t *testing.T) {
|
|||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "Pod",
|
Kind: "Pod",
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
supports: true,
|
supports: true,
|
||||||
|
queryParam: QueryParamDryRun,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
gvk: schema.GroupVersionKind{
|
||||||
|
Group: "",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "Pod",
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
supports: true,
|
||||||
|
queryParam: QueryParamFieldValidation,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
gvk: schema.GroupVersionKind{
|
gvk: schema.GroupVersionKind{
|
||||||
@ -51,8 +65,19 @@ func TestSupportsDryRun(t *testing.T) {
|
|||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "UnknownKind",
|
Kind: "UnknownKind",
|
||||||
},
|
},
|
||||||
success: false,
|
success: false,
|
||||||
supports: false,
|
supports: false,
|
||||||
|
queryParam: QueryParamDryRun,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
gvk: schema.GroupVersionKind{
|
||||||
|
Group: "",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "UnknownKind",
|
||||||
|
},
|
||||||
|
success: false,
|
||||||
|
supports: false,
|
||||||
|
queryParam: QueryParamFieldValidation,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
gvk: schema.GroupVersionKind{
|
gvk: schema.GroupVersionKind{
|
||||||
@ -60,20 +85,31 @@ func TestSupportsDryRun(t *testing.T) {
|
|||||||
Version: "v1",
|
Version: "v1",
|
||||||
Kind: "NodeProxyOptions",
|
Kind: "NodeProxyOptions",
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
supports: false,
|
supports: false,
|
||||||
|
queryParam: QueryParamDryRun,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
gvk: schema.GroupVersionKind{
|
||||||
|
Group: "",
|
||||||
|
Version: "v1",
|
||||||
|
Kind: "NodeProxyOptions",
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
supports: false,
|
||||||
|
queryParam: QueryParamFieldValidation,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
supports, err := supportsQueryParam(doc, test.gvk, QueryParamDryRun)
|
supports, err := supportsQueryParam(doc, test.gvk, test.queryParam)
|
||||||
if supports != test.supports || ((err == nil) != test.success) {
|
if supports != test.supports || ((err == nil) != test.success) {
|
||||||
errStr := "nil"
|
errStr := "nil"
|
||||||
if test.success == false {
|
if test.success == false {
|
||||||
errStr = "err"
|
errStr = "err"
|
||||||
}
|
}
|
||||||
t.Errorf("SupportsDryRun(doc, %v) = (%v, %v), expected (%v, %v)",
|
t.Errorf("SupportsQueryParam(doc, %v, %v) = (%v, %v), expected (%v, %v)",
|
||||||
test.gvk,
|
test.gvk, test.queryParam,
|
||||||
supports, err,
|
supports, err,
|
||||||
test.supports, errStr,
|
test.supports, errStr,
|
||||||
)
|
)
|
||||||
@ -81,8 +117,6 @@ func TestSupportsDryRun(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var fakeSchema = openapitesting.Fake{Path: filepath.Join("..", "..", "artifacts", "openapi", "swagger.json")}
|
|
||||||
|
|
||||||
func TestDryRunVerifier(t *testing.T) {
|
func TestDryRunVerifier(t *testing.T) {
|
||||||
dryRunVerifier := QueryParamVerifier{
|
dryRunVerifier := QueryParamVerifier{
|
||||||
finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
|
finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
|
||||||
@ -122,6 +156,45 @@ func TestDryRunVerifier(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFieldValidationVerifier(t *testing.T) {
|
||||||
|
fieldValidationVerifier := QueryParamVerifier{
|
||||||
|
finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
|
||||||
|
return []schema.GroupKind{
|
||||||
|
{
|
||||||
|
Group: "crd.com",
|
||||||
|
Kind: "MyCRD",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Group: "crd.com",
|
||||||
|
Kind: "MyNewCRD",
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}),
|
||||||
|
openAPIGetter: &fakeSchema,
|
||||||
|
queryParam: QueryParamFieldValidation,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "NodeProxyOptions"})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("NodeProxyOptions doesn't support fieldValidation, yet no error found")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Pod should support fieldValidation: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("MyCRD should support fieldValidation: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "Random"})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Random doesn't support fieldValidation, yet no error found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type EmptyOpenAPI struct{}
|
type EmptyOpenAPI struct{}
|
||||||
|
|
||||||
func (EmptyOpenAPI) OpenAPISchema() (*openapi_v2.Document, error) {
|
func (EmptyOpenAPI) OpenAPISchema() (*openapi_v2.Document, error) {
|
||||||
@ -156,3 +229,32 @@ func TestDryRunVerifierNoOpenAPI(t *testing.T) {
|
|||||||
t.Fatalf("MyCRD doesn't support dry-run, yet no error found")
|
t.Fatalf("MyCRD doesn't support dry-run, yet no error found")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFieldValidationVerifierNoOpenAPI(t *testing.T) {
|
||||||
|
fieldValidationVerifier := QueryParamVerifier{
|
||||||
|
finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
|
||||||
|
return []schema.GroupKind{
|
||||||
|
{
|
||||||
|
Group: "crd.com",
|
||||||
|
Kind: "MyCRD",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Group: "crd.com",
|
||||||
|
Kind: "MyNewCRD",
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}),
|
||||||
|
openAPIGetter: EmptyOpenAPI{},
|
||||||
|
queryParam: QueryParamFieldValidation,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Pod doesn't support fieldValidation, yet no error found")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fieldValidationVerifier.HasSupport(schema.GroupVersionKind{Group: "crd.com", Version: "v1", Kind: "MyCRD"})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("MyCRD doesn't support fieldValidation, yet no error found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user