1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resource
import (
"strings"
"testing"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/openapi/cached"
"k8s.io/client-go/openapi/openapitest"
"k8s.io/client-go/openapi3"
"k8s.io/kube-openapi/pkg/spec3"
)
func TestV3SupportsQueryParamBatchV1(t *testing.T) {
tests := map[string]struct {
crds []schema.GroupKind // CRDFinder returns these CRD's
gvk schema.GroupVersionKind // GVK whose OpenAPI V3 spec is checked
queryParam VerifiableQueryParam // Usually "fieldValidation"
expectedSupports bool
}{
"Field validation query param is supported for batch/v1/Job": {
crds: []schema.GroupKind{},
gvk: schema.GroupVersionKind{
Group: "batch",
Version: "v1",
Kind: "Job",
},
queryParam: QueryParamFieldValidation,
expectedSupports: true,
},
"Field validation query param supported for core/v1/Namespace": {
crds: []schema.GroupKind{},
gvk: schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Namespace",
},
queryParam: QueryParamFieldValidation,
expectedSupports: true,
},
"Field validation unsupported for unknown GVK": {
crds: []schema.GroupKind{},
gvk: schema.GroupVersionKind{
Group: "bad",
Version: "v1",
Kind: "Uknown",
},
queryParam: QueryParamFieldValidation,
expectedSupports: false,
},
"Unknown query param unsupported (for all GVK's)": {
crds: []schema.GroupKind{},
gvk: schema.GroupVersionKind{
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
queryParam: "UnknownQueryParam",
expectedSupports: false,
},
"Field validation query param supported for found CRD": {
crds: []schema.GroupKind{
{
Group: "example.com",
Kind: "ExampleCRD",
},
},
// GVK matches above CRD GroupKind
gvk: schema.GroupVersionKind{
Group: "example.com",
Version: "v1",
Kind: "ExampleCRD",
},
queryParam: QueryParamFieldValidation,
expectedSupports: true,
},
"Field validation query param unsupported for missing CRD": {
crds: []schema.GroupKind{
{
Group: "different.com",
Kind: "DifferentCRD",
},
},
// GVK does NOT match above CRD GroupKind
gvk: schema.GroupVersionKind{
Group: "example.com",
Version: "v1",
Kind: "ExampleCRD",
},
queryParam: QueryParamFieldValidation,
expectedSupports: false,
},
"List GVK is specifically unsupported": {
crds: []schema.GroupKind{},
gvk: schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "List",
},
queryParam: QueryParamFieldValidation,
expectedSupports: false,
},
}
root := openapi3.NewRoot(cached.NewClient(openapitest.NewEmbeddedFileClient()))
for tn, tc := range tests {
t.Run(tn, func(t *testing.T) {
verifier := &queryParamVerifierV3{
finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
return tc.crds, nil
}),
root: root,
queryParam: tc.queryParam,
}
err := verifier.HasSupport(tc.gvk)
if tc.expectedSupports && err != nil {
t.Errorf("Expected supports, but returned err for GVK (%s)", tc.gvk)
} else if !tc.expectedSupports && err == nil {
t.Errorf("Expected not supports, but returned no err for GVK (%s)", tc.gvk)
}
})
}
}
func TestInvalidOpenAPIV3Document(t *testing.T) {
tests := map[string]struct {
spec *spec3.OpenAPI
err string
}{
"nil document returns error": {
spec: nil,
err: "Invalid OpenAPI V3 document",
},
"empty document returns error": {
spec: &spec3.OpenAPI{},
err: "Invalid OpenAPI V3 document",
},
"minimal document returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: nil,
},
err: "Invalid OpenAPI V3 document",
},
"empty Paths returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: &spec3.Paths{},
},
err: "Path not found for GVK",
},
"nil Path returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: &spec3.Paths{Paths: map[string]*spec3.Path{"/version": nil}},
},
err: "Path not found for GVK",
},
"empty Path returns error": {
spec: &spec3.OpenAPI{
Version: "openapi 3.0.0",
Paths: &spec3.Paths{Paths: map[string]*spec3.Path{"/version": {}}},
},
err: "Path not found for GVK",
},
}
gvk := schema.GroupVersionKind{
Group: "batch",
Version: "v1",
Kind: "Job",
}
for tn, tc := range tests {
t.Run(tn, func(t *testing.T) {
verifier := &queryParamVerifierV3{
finder: NewCRDFinder(func() ([]schema.GroupKind, error) {
return []schema.GroupKind{}, nil
}),
root: &fakeRoot{tc.spec},
queryParam: QueryParamFieldValidation,
}
err := verifier.HasSupport(gvk)
if !strings.Contains(err.Error(), tc.err) {
t.Errorf("Expected error (%s), but received (%s)", tc.err, err.Error())
}
})
}
}
// fakeRoot implements Root interface; manually specifies the returned OpenAPI V3 document.
type fakeRoot struct {
spec *spec3.OpenAPI
}
func (f *fakeRoot) GroupVersions() ([]schema.GroupVersion, error) {
// Unused
return nil, nil
}
// GVSpec returns hard-coded OpenAPI V3 document.
func (f *fakeRoot) GVSpec(gv schema.GroupVersion) (*spec3.OpenAPI, error) {
return f.spec, nil
}
func (f *fakeRoot) GVSpecAsMap(gv schema.GroupVersion) (map[string]interface{}, error) {
// Unused
return nil, nil
}
|