Add output_tests

This commit is contained in:
Joe Betz
2025-05-19 11:23:14 -04:00
parent 6284a0f50b
commit c2eef42cf4
9 changed files with 207 additions and 27 deletions

View File

@@ -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 {

View File

@@ -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"})),
)
}

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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"})),
)
}

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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"})),
)
}

View File

@@ -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 }))...)