From c2eef42cf4d1d3c223ca8ad9179d5f8a8f7b341a Mon Sep 17 00:00:00 2001 From: Joe Betz Date: Mon, 19 May 2025 11:23:14 -0400 Subject: [PATCH] Add output_tests --- .../supported_resources/is_subresource/doc.go | 6 +- .../is_subresource/doc_test.go | 13 ++--- .../zz_generated.validations.go | 58 +++++++++++++++++++ .../supported_resources/root/doc.go | 15 +++-- .../supported_resources/root/doc_test.go | 25 +++++++- .../root/zz_generated.validations.go | 58 +++++++++++++++++++ .../supported_resources/subresource/doc.go | 9 ++- .../subresource/doc_test.go | 44 +++++++++++++- .../subresource/zz_generated.validations.go | 6 +- 9 files changed, 207 insertions(+), 27 deletions(-) create mode 100644 staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/zz_generated.validations.go create mode 100644 staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/zz_generated.validations.go diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc.go index c164f71809b..15c9cdfd07f 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc.go @@ -19,7 +19,7 @@ limitations under the License. // +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme // This is a test package. -package subresource +package is_subresource import "k8s.io/code-generator/cmd/validation-gen/testscheme" @@ -27,9 +27,7 @@ var localSchemeBuilder = testscheme.New() // Root resource is supported by default -// +k8s:supportsSubresource=/status -// +k8s:supportsSubresource=/scale -// +k8s:supportsSubresource=/x/y +// +k8s:isSubresource=/scale // T1 is a test type type T1 struct { diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc_test.go index f58e55f8294..f745fff73b6 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/doc_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package subresource +package is_subresource import ( "fmt" @@ -27,17 +27,14 @@ func TestRegisterValidations(t *testing.T) { st := localSchemeBuilder.Test(t) t1 := &T1{} + + st.Value(t1).ExpectInvalid( + field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{})), + ) - st.Value(t1).ExpectValid() - - st.Value(t1).Subresources([]string{"status"}).ExpectValid() st.Value(t1).Subresources([]string{"scale"}).ExpectValid() - st.Value(t1).Subresources([]string{"x", "y"}).ExpectValid() st.Value(t1).Subresources([]string{"unknown"}).ExpectInvalid( field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{"unknown"})), ) - st.Value(t1).Subresources([]string{"x", "unknown"}).ExpectInvalid( - field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{"x", "unknown"})), - ) } diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/zz_generated.validations.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/zz_generated.validations.go new file mode 100644 index 00000000000..b56f68b3aaa --- /dev/null +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/is_subresource/zz_generated.validations.go @@ -0,0 +1,58 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright 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. +*/ + +// Code generated by validation-gen. DO NOT EDIT. + +package is_subresource + +import ( + context "context" + fmt "fmt" + + operation "k8s.io/apimachinery/pkg/api/operation" + safe "k8s.io/apimachinery/pkg/api/safe" + validate "k8s.io/apimachinery/pkg/api/validate" + field "k8s.io/apimachinery/pkg/util/validation/field" + testscheme "k8s.io/code-generator/cmd/validation-gen/testscheme" +) + +func init() { localSchemeBuilder.Register(RegisterValidations) } + +// RegisterValidations adds validation functions to the given scheme. +// Public to allow building arbitrary schemes. +func RegisterValidations(scheme *testscheme.Scheme) error { + scheme.AddValidationFunc((*T1)(nil), func(ctx context.Context, op operation.Operation, obj, oldObj interface{}) field.ErrorList { + if op.Request.SubresourceIn([]string{"/scale"}) { + return Validate_T1(ctx, op, nil /* fldPath */, obj.(*T1), safe.Cast[*T1](oldObj)) + } + return field.ErrorList{field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", obj, op.Request.Subresources))} + }) + return nil +} + +func Validate_T1(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *T1) (errs field.ErrorList) { + // field T1.S + errs = append(errs, + func(fldPath *field.Path, obj, oldObj *string) (errs field.ErrorList) { + errs = append(errs, validate.FixedResult(ctx, op, fldPath, obj, oldObj, true, "field T1.S")...) + return + }(fldPath.Child("s"), &obj.S, safe.Field(oldObj, func(oldObj *T1) *string { return &oldObj.S }))...) + + return errs +} diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc.go index 561f1e31e40..9c426808820 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc.go @@ -17,19 +17,18 @@ limitations under the License. // Note: this selects all types in the package. // +k8s:validation-gen=* // +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme -// +k8s:validation-gen-test-fixture=validateFalse // This is a test package. -package trivial +package root import "k8s.io/code-generator/cmd/validation-gen/testscheme" var localSchemeBuilder = testscheme.New() -type T1 struct{} +// Root resource is supported by default -type T2 struct{} - -type E1 string - -type E2 string +// T1 is a test type +type T1 struct { + // +k8s:validateTrue="field T1.S" + S string `json:"s"` +} diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc_test.go index 754d3d260a7..4d146adcce6 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/doc_test.go @@ -14,4 +14,27 @@ See the License for the specific language governing permissions and limitations under the License. */ -package subresource +package root + +import ( + "fmt" + "testing" + + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestRegisterValidations(t *testing.T) { + st := localSchemeBuilder.Test(t) + + t1 := &T1{} + + st.Value(t1).ExpectValid() + + st.Value(t1).Subresources([]string{"scale"}).ExpectInvalid( + field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{"scale"})), + ) + + st.Value(t1).Subresources([]string{"x", "y"}).ExpectInvalid( + field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{"x", "y"})), + ) +} diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/zz_generated.validations.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/zz_generated.validations.go new file mode 100644 index 00000000000..7555844cab4 --- /dev/null +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/root/zz_generated.validations.go @@ -0,0 +1,58 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright 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. +*/ + +// Code generated by validation-gen. DO NOT EDIT. + +package root + +import ( + context "context" + fmt "fmt" + + operation "k8s.io/apimachinery/pkg/api/operation" + safe "k8s.io/apimachinery/pkg/api/safe" + validate "k8s.io/apimachinery/pkg/api/validate" + field "k8s.io/apimachinery/pkg/util/validation/field" + testscheme "k8s.io/code-generator/cmd/validation-gen/testscheme" +) + +func init() { localSchemeBuilder.Register(RegisterValidations) } + +// RegisterValidations adds validation functions to the given scheme. +// Public to allow building arbitrary schemes. +func RegisterValidations(scheme *testscheme.Scheme) error { + scheme.AddValidationFunc((*T1)(nil), func(ctx context.Context, op operation.Operation, obj, oldObj interface{}) field.ErrorList { + if op.Request.SubresourceIn([]string{"/"}) { + return Validate_T1(ctx, op, nil /* fldPath */, obj.(*T1), safe.Cast[*T1](oldObj)) + } + return field.ErrorList{field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", obj, op.Request.Subresources))} + }) + return nil +} + +func Validate_T1(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *T1) (errs field.ErrorList) { + // field T1.S + errs = append(errs, + func(fldPath *field.Path, obj, oldObj *string) (errs field.ErrorList) { + errs = append(errs, validate.FixedResult(ctx, op, fldPath, obj, oldObj, true, "field T1.S")...) + return + }(fldPath.Child("s"), &obj.S, safe.Field(oldObj, func(oldObj *T1) *string { return &oldObj.S }))...) + + return errs +} diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc.go index 4ae53556c99..c164f71809b 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc.go @@ -19,7 +19,7 @@ limitations under the License. // +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme // This is a test package. -package root +package subresource import "k8s.io/code-generator/cmd/validation-gen/testscheme" @@ -27,7 +27,12 @@ var localSchemeBuilder = testscheme.New() // Root resource is supported by default +// +k8s:supportsSubresource=/status +// +k8s:supportsSubresource=/scale +// +k8s:supportsSubresource=/x/y + +// T1 is a test type type T1 struct { - // +k8s:validateFalse="field T1.S" + // +k8s:validateTrue="field T1.S" S string `json:"s"` } diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc_test.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc_test.go index c8040311d47..f58e55f8294 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc_test.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/doc_test.go @@ -1 +1,43 @@ -package root +/* +Copyright 2024 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 subresource + +import ( + "fmt" + "testing" + + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestRegisterValidations(t *testing.T) { + st := localSchemeBuilder.Test(t) + + t1 := &T1{} + + st.Value(t1).ExpectValid() + + st.Value(t1).Subresources([]string{"status"}).ExpectValid() + st.Value(t1).Subresources([]string{"scale"}).ExpectValid() + st.Value(t1).Subresources([]string{"x", "y"}).ExpectValid() + + st.Value(t1).Subresources([]string{"unknown"}).ExpectInvalid( + field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{"unknown"})), + ) + st.Value(t1).Subresources([]string{"x", "unknown"}).ExpectInvalid( + field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", t1, []string{"x", "unknown"})), + ) +} diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/zz_generated.validations.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/zz_generated.validations.go index cf3da29f80a..8d3713ef995 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/zz_generated.validations.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/supported_resources/subresource/zz_generated.validations.go @@ -19,7 +19,7 @@ limitations under the License. // Code generated by validation-gen. DO NOT EDIT. -package root +package subresource import ( context "context" @@ -38,7 +38,7 @@ func init() { localSchemeBuilder.Register(RegisterValidations) } // Public to allow building arbitrary schemes. func RegisterValidations(scheme *testscheme.Scheme) error { scheme.AddValidationFunc((*T1)(nil), func(ctx context.Context, op operation.Operation, obj, oldObj interface{}) field.ErrorList { - if op.Request.ResourceIn([]string{"/"}) { + if op.Request.SubresourceIn([]string{"/", "/scale", "/status", "/x/y"}) { return Validate_T1(ctx, op, nil /* fldPath */, obj.(*T1), safe.Cast[*T1](oldObj)) } return field.ErrorList{field.InternalError(nil, fmt.Errorf("no validation found for %T, subresources: %v", obj, op.Request.Subresources))} @@ -50,7 +50,7 @@ func Validate_T1(ctx context.Context, op operation.Operation, fldPath *field.Pat // field T1.S errs = append(errs, func(fldPath *field.Path, obj, oldObj *string) (errs field.ErrorList) { - errs = append(errs, validate.FixedResult(ctx, op, fldPath, obj, oldObj, false, "field T1.S")...) + errs = append(errs, validate.FixedResult(ctx, op, fldPath, obj, oldObj, true, "field T1.S")...) return }(fldPath.Child("s"), &obj.S, safe.Field(oldObj, func(oldObj *T1) *string { return &oldObj.S }))...)