Skip to content

Commit

Permalink
cleanup Put/Post CRD tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindelgado committed Nov 2, 2021
1 parent d8a9b3a commit 22904a6
Showing 1 changed file with 126 additions and 92 deletions.
218 changes: 126 additions & 92 deletions test/integration/apiserver/field_validation_test.go
Expand Up @@ -562,6 +562,63 @@ func TestFieldValidationSMP(t *testing.T) {
}
}

var crdSchemaBase = `
{
"openAPIV3Schema": {
"type": "object",
"properties": {
"spec": {
"type": "object",
%s
"properties": {
"cronSpec": {
"type": "string",
"pattern": "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$"
},
"knownField1": {
"type": "string"
},
"ports": {
"type": "array",
"x-kubernetes-list-map-keys": [
"containerPort",
"protocol"
],
"x-kubernetes-list-type": "map",
"items": {
"properties": {
"containerPort": {
"format": "int32",
"type": "integer"
},
"hostIP": {
"type": "string"
},
"hostPort": {
"format": "int32",
"type": "integer"
},
"name": {
"type": "string"
},
"protocol": {
"type": "string"
}
},
"required": [
"containerPort",
"protocol"
],
"type": "object"
}
}
}
}
}
}
}
`

// TestFieldValidationPatchCRD tests that server-side schema validation
// works for jsonpatch and mergepatch requests.
func TestFieldValidationPatchCRD(t *testing.T) {
Expand Down Expand Up @@ -830,10 +887,10 @@ func TestFieldValidationPostCRD(t *testing.T) {
}
`
var testcases = []struct {
name string
opts metav1.PatchOptions
body string
//preserveUnknownFields bool
name string
opts metav1.PatchOptions
body string
preserveUnknownFields string
strictDecodingErrors []string
strictDecodingWarnings []string
}{
Expand Down Expand Up @@ -886,6 +943,59 @@ func TestFieldValidationPostCRD(t *testing.T) {
name: "crd-post-no-validation",
body: postBody,
},
{
name: "schemaless-crd-post-strict-validation",
opts: metav1.PatchOptions{
FieldValidation: "Strict",
},
body: postBody,
preserveUnknownFields: `"x-kubernetes-preserve-unknown-fields": true,`,
strictDecodingErrors: []string{
// TODO: there is a bug in kjson.StrictUnmarshall
// where it does not recognize duplicate fields
// when unmarshalling unstructured objects.
// See TODO in serializer/json/json.go:176
//`duplicate field "hostPort"`,
//`duplicate field "knownField1"`,
//`duplicate field "unknownDupe"`,
`unknown field "unknown1"`,
`unknown field "unknownDupe"`,
`unknown field "unknownNested"`,
},
},
{
name: "schemaless-crd-post-warn-validation",
opts: metav1.PatchOptions{
FieldValidation: "Warn",
},
body: postBody,
preserveUnknownFields: `"x-kubernetes-preserve-unknown-fields": true,`,
strictDecodingWarnings: []string{
// TODO: there is a bug in kjson.StrictUnmarshall
// where it does not recognize duplicate fields
// when unmarshalling unstructured objects.
// See TODO in serializer/json/json.go:176
//`duplicate field "hostPort"`,
//`duplicate field "knownField1"`,
//`duplicate field "unknownDupe"`,
`unknown field "unknown1"`,
`unknown field "unknownDupe"`,
`unknown field "unknownNested"`,
},
},
{
name: "schemaless-crd-post-ignore-validation",
opts: metav1.PatchOptions{
FieldValidation: "Ignore",
},
body: postBody,
preserveUnknownFields: `"x-kubernetes-preserve-unknown-fields": true,`,
},
{
name: "schemaless-crd-post-no-validation",
body: postBody,
preserveUnknownFields: `"x-kubernetes-preserve-unknown-fields": true,`,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -906,20 +1016,17 @@ func TestFieldValidationPostCRD(t *testing.T) {
if err != nil {
t.Fatal(err)
}
crdSchema, err := os.ReadFile("./testdata/crd-schema.json")
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
crdSchema := fmt.Sprintf(crdSchemaBase, tc.preserveUnknownFields)
klog.Warningf("crdSchema: %s", crdSchema)

// create the CRD
noxuDefinition := fixtures.NewNoxuV1CustomResourceDefinition(apiextensionsv1.ClusterScoped)
var c apiextensionsv1.CustomResourceValidation
err = json.Unmarshal(crdSchema, &c)
err = json.Unmarshal([]byte(crdSchema), &c)
if err != nil {
t.Fatal(err)
}
// set the CRD schema
noxuDefinition.Spec.PreserveUnknownFields = false
for i := range noxuDefinition.Spec.Versions {
noxuDefinition.Spec.Versions[i].Schema = &c
}
Expand All @@ -935,17 +1042,21 @@ func TestFieldValidationPostCRD(t *testing.T) {
// create the CR as specified by the test case
rest := apiExtensionClient.Discovery().RESTClient()
jsonBody := []byte(fmt.Sprintf(tc.body, apiVersion, kind, tc.name))
klog.Warningf("jsonBody: %s\n", jsonBody)
req := rest.Post().
AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
VersionedParams(&tc.opts, metav1.ParameterCodec)
result := req.Body([]byte(jsonBody)).Do(context.TODO())

if result.Error() != nil && len(tc.strictDecodingErrors) == 0 {
t.Fatalf("unexpected post err: %v", result.Error())
}

if result.Error() == nil && len(tc.strictDecodingErrors) > 0 {
resObj, _ := result.Get()
klog.Warningf("resObj: %v", resObj)
t.Fatalf("unexpected patch succeeded")
}
raw, _ := result.Raw()
// TODO: this is what we need to check if err isn't nil
klog.Warningf("post raw res: %s\n", string(raw))
for _, strictErr := range tc.strictDecodingErrors {
if !strings.Contains(result.Error().Error(), strictErr) {
t.Fatalf("missing strict decoding error: %s from error: %s", strictErr, result.Error().Error())
Expand All @@ -962,7 +1073,6 @@ func TestFieldValidationPostCRD(t *testing.T) {
return result.Warnings()[i].Text < result.Warnings()[j].Text

})
klog.Warningf("warns: %v", result.Warnings)

for i, strictWarn := range tc.strictDecodingWarnings {
if strictWarn != result.Warnings()[i].Text {
Expand All @@ -979,64 +1089,6 @@ func TestFieldValidationPostCRD(t *testing.T) {
func TestFieldValidationPutCRD(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.StrictFieldValidation, true)()

crdSchemaBase :=
`
{
"openAPIV3Schema": {
"type": "object",
"properties": {
"spec": {
"type": "object",
%s
"properties": {
"cronSpec": {
"type": "string",
"pattern": "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$"
},
"knownField1": {
"type": "string"
},
"ports": {
"type": "array",
"x-kubernetes-list-map-keys": [
"containerPort",
"protocol"
],
"x-kubernetes-list-type": "map",
"items": {
"properties": {
"containerPort": {
"format": "int32",
"type": "integer"
},
"hostIP": {
"type": "string"
},
"hostPort": {
"format": "int32",
"type": "integer"
},
"name": {
"type": "string"
},
"protocol": {
"type": "string"
}
},
"required": [
"containerPort",
"protocol"
],
"type": "object"
}
}
}
}
}
}
}
`

postBody := `
{
"apiVersion": "%s",
Expand Down Expand Up @@ -1219,10 +1271,6 @@ func TestFieldValidationPutCRD(t *testing.T) {
if err != nil {
t.Fatal(err)
}
//crdSchema, err := os.ReadFile("./testdata/crd-schema.json")
//if err != nil {
// t.Fatalf("failed to read file: %v", err)
//}

crdSchema := fmt.Sprintf(crdSchemaBase, tc.preserveUnknownFields)
klog.Warningf("crdSchema: %s", crdSchema)
Expand All @@ -1235,7 +1283,6 @@ func TestFieldValidationPutCRD(t *testing.T) {
t.Fatal(err)
}
// set the CRD schema
//noxuDefinition.Spec.PreserveUnknownFields = tc.preserveUnknownFields
for i := range noxuDefinition.Spec.Versions {
noxuDefinition.Spec.Versions[i].Schema = &c
}
Expand All @@ -1252,7 +1299,6 @@ func TestFieldValidationPutCRD(t *testing.T) {
rest := apiExtensionClient.Discovery().RESTClient()

jsonPostBody := []byte(fmt.Sprintf(tc.postBody, apiVersion, kind, tc.name))
klog.Warningf("jsonBody: %s\n", jsonPostBody)
postReq := rest.Post().
AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
VersionedParams(&tc.opts, metav1.ParameterCodec)
Expand All @@ -1264,30 +1310,22 @@ func TestFieldValidationPutCRD(t *testing.T) {
if err := postUnstructured.UnmarshalJSON(postResult); err != nil {
t.Fatalf("unexpeted error unmarshalling created CR: %v", err)
}
klog.Warningf("resourceVersion: %s", postUnstructured.GetResourceVersion())

//resObj, resErr := postResult.Get()
//klog.Warningf("resRaw: %v\n", string(resRaw))
//klog.Warningf("resErr: %v\n", resErr)

// update the CR as specified by the test case
jsonPutBody := []byte(fmt.Sprintf(tc.putBody, apiVersion, kind, tc.name, postUnstructured.GetResourceVersion()))
klog.Warningf("jsonPutBody: %s\n", jsonPutBody)
putReq := rest.Put().
AbsPath("/apis", noxuDefinition.Spec.Group, noxuDefinition.Spec.Versions[0].Name, noxuDefinition.Spec.Names.Plural).
Name(tc.name).
VersionedParams(&tc.opts, metav1.ParameterCodec)
result := putReq.Body([]byte(jsonPutBody)).Do(context.TODO())
raw, _ := result.Raw()
klog.Warningf("put raw res: %s\n", string(raw))
if result.Error() != nil && len(tc.strictDecodingErrors) == 0 {
t.Fatalf("unexpected put err: %v", result.Error())
}
if result.Error() == nil && len(tc.strictDecodingErrors) > 0 {
resObj, _ := result.Get()
klog.Warningf("resObj: %v", resObj)
t.Fatalf("unexpected patch succeeded")
}
raw, _ := result.Raw()
klog.Warningf("put raw res: %s\n", string(raw))
for _, strictErr := range tc.strictDecodingErrors {
if !strings.Contains(result.Error().Error(), strictErr) {
t.Fatalf("missing strict decoding error: %s from error: %s", strictErr, result.Error().Error())
Expand All @@ -1304,10 +1342,6 @@ func TestFieldValidationPutCRD(t *testing.T) {
return result.Warnings()[i].Text < result.Warnings()[j].Text

})
klog.Warningf("warns: %v", result.Warnings)
for _, w := range result.Warnings() {
klog.Warning("warn: %v\n", w.Text)
}

for i, strictWarn := range tc.strictDecodingWarnings {
if strictWarn != result.Warnings()[i].Text {
Expand Down

0 comments on commit 22904a6

Please sign in to comment.