chore: change error_matcher.go to use test interface instead of importing testing pkg

This commit is contained in:
Aaron Prindle
2025-03-11 05:24:07 +00:00
parent 82667879bb
commit cd9df2f115
4 changed files with 23 additions and 21 deletions

View File

@@ -38,7 +38,6 @@ import (
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
fldtest "k8s.io/apimachinery/pkg/util/validation/field/testing"
"k8s.io/apimachinery/pkg/util/version" "k8s.io/apimachinery/pkg/util/version"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/component-base/featuregate" "k8s.io/component-base/featuregate"
@@ -16714,7 +16713,7 @@ func TestValidateReplicationControllerUpdate(t *testing.T) {
tc.old.ObjectMeta.ResourceVersion = "1" tc.old.ObjectMeta.ResourceVersion = "1"
tc.update.ObjectMeta.ResourceVersion = "1" tc.update.ObjectMeta.ResourceVersion = "1"
errs := ValidateReplicationControllerUpdate(&tc.update, &tc.old, PodValidationOptions{}) errs := ValidateReplicationControllerUpdate(&tc.update, &tc.old, PodValidationOptions{})
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring() matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring()
matcher.Test(t, tc.expectedErrs, errs) matcher.Test(t, tc.expectedErrs, errs)
}) })
} }
@@ -16864,7 +16863,7 @@ func TestValidateReplicationController(t *testing.T) {
for k, tc := range errorCases { for k, tc := range errorCases {
t.Run(k, func(t *testing.T) { t.Run(k, func(t *testing.T) {
errs := ValidateReplicationController(&tc.input, PodValidationOptions{}) errs := ValidateReplicationController(&tc.input, PodValidationOptions{})
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring() matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().ByDetailSubstring()
matcher.Test(t, tc.expectedErrs, errs) matcher.Test(t, tc.expectedErrs, errs)
}) })
} }
@@ -20770,7 +20769,7 @@ func TestValidateEndpointsCreate(t *testing.T) {
t.Run(k, func(t *testing.T) { t.Run(k, func(t *testing.T) {
errs := ValidateEndpointsCreate(&v.endpoints) errs := ValidateEndpointsCreate(&v.endpoints)
// TODO: set .RequireOriginWhenInvalid() once metadata is done // TODO: set .RequireOriginWhenInvalid() once metadata is done
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin() matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
matcher.Test(t, v.expectedErrs, errs) matcher.Test(t, v.expectedErrs, errs)
}) })
} }
@@ -22767,7 +22766,7 @@ func TestValidateTopologySpreadConstraints(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
errs := validateTopologySpreadConstraints(tc.constraints, fieldPath, tc.opts) errs := validateTopologySpreadConstraints(tc.constraints, fieldPath, tc.opts)
matcher := fldtest.ErrorMatcher{}.ByType().ByField().ByOrigin().RequireOriginWhenInvalid() matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin().RequireOriginWhenInvalid()
matcher.Test(t, tc.wantFieldErrors, errs) matcher.Test(t, tc.wantFieldErrors, errs)
}) })
} }

View File

@@ -35,7 +35,6 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
fieldtesting "k8s.io/apimachinery/pkg/util/validation/field/testing"
) )
type testConversions struct { type testConversions struct {
@@ -1089,7 +1088,7 @@ func TestRegisterValidate(t *testing.T) {
} else { } else {
results = s.ValidateUpdate(ctx, tc.options, tc.object, tc.oldObject, tc.subresource...) results = s.ValidateUpdate(ctx, tc.options, tc.object, tc.oldObject, tc.subresource...)
} }
matcher := fieldtesting.ErrorMatcher{}.ByType().ByField().ByOrigin() matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
matcher.Test(t, tc.expected, results) matcher.Test(t, tc.expected, results)
}) })
} }

View File

@@ -14,19 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package testing package field
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
"testing"
field "k8s.io/apimachinery/pkg/util/validation/field"
) )
// ErrorMatcher is a helper for comparing field.Error objects. // ErrorMatcher is a helper for comparing Error objects.
type ErrorMatcher struct { type ErrorMatcher struct {
// TODO(thockin): consider whether type is ever NOT required, maybe just // TODO(thockin): consider whether type is ever NOT required, maybe just
// assume it. // assume it.
@@ -42,9 +39,9 @@ type ErrorMatcher struct {
requireOriginWhenInvalid bool requireOriginWhenInvalid bool
} }
// Matches returns true if the two field.Error objects match according to the // Matches returns true if the two Error objects match according to the
// configured criteria. // configured criteria.
func (m ErrorMatcher) Matches(want, got *field.Error) bool { func (m ErrorMatcher) Matches(want, got *Error) bool {
if m.matchType && want.Type != got.Type { if m.matchType && want.Type != got.Type {
return false return false
} }
@@ -58,7 +55,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool {
if want.Origin != got.Origin { if want.Origin != got.Origin {
return false return false
} }
if m.requireOriginWhenInvalid && want.Type == field.ErrorTypeInvalid { if m.requireOriginWhenInvalid && want.Type == ErrorTypeInvalid {
if want.Origin == "" || got.Origin == "" { if want.Origin == "" || got.Origin == "" {
return false return false
} }
@@ -72,7 +69,7 @@ func (m ErrorMatcher) Matches(want, got *field.Error) bool {
// Render returns a string representation of the specified Error object, // Render returns a string representation of the specified Error object,
// according to the criteria configured in the ErrorMatcher. // according to the criteria configured in the ErrorMatcher.
func (m ErrorMatcher) Render(e *field.Error) string { func (m ErrorMatcher) Render(e *Error) string {
buf := strings.Builder{} buf := strings.Builder{}
comma := func() { comma := func() {
@@ -93,7 +90,7 @@ func (m ErrorMatcher) Render(e *field.Error) string {
comma() comma()
buf.WriteString(fmt.Sprintf("Value=%v", e.BadValue)) buf.WriteString(fmt.Sprintf("Value=%v", e.BadValue))
} }
if m.matchOrigin || m.requireOriginWhenInvalid && e.Type == field.ErrorTypeInvalid { if m.matchOrigin || m.requireOriginWhenInvalid && e.Type == ErrorTypeInvalid {
comma() comma()
buf.WriteString(fmt.Sprintf("Origin=%q", e.Origin)) buf.WriteString(fmt.Sprintf("Origin=%q", e.Origin))
} }
@@ -170,17 +167,25 @@ func (m ErrorMatcher) ByDetailRegexp() ErrorMatcher {
return m return m
} }
// TestIntf lets users pass a testing.T while not coupling this package to Go's
// testing package.
type TestIntf interface {
Helper()
Errorf(format string, args ...any)
Logf(format string, args ...any)
}
// Test compares two ErrorLists by the criteria configured in this matcher, and // Test compares two ErrorLists by the criteria configured in this matcher, and
// fails the test if they don't match. If a given "want" error matches multiple // fails the test if they don't match. If a given "want" error matches multiple
// "got" errors, they will all be consumed. This might be OK (e.g. if there are // "got" errors, they will all be consumed. This might be OK (e.g. if there are
// multiple errors on the same field from the same origin) or it might be an // multiple errors on the same field from the same origin) or it might be an
// insufficiently specific matcher, so these will be logged. // insufficiently specific matcher, so these will be logged.
func (m ErrorMatcher) Test(tb testing.TB, want, got field.ErrorList) { func (m ErrorMatcher) Test(tb TestIntf, want, got ErrorList) {
tb.Helper() tb.Helper()
remaining := got remaining := got
for _, w := range want { for _, w := range want {
tmp := make(field.ErrorList, 0, len(remaining)) tmp := make(ErrorList, 0, len(remaining))
n := 0 n := 0
for _, g := range remaining { for _, g := range remaining {
if m.Matches(w, g) { if m.Matches(w, g) {

View File

@@ -29,7 +29,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
fieldtesting "k8s.io/apimachinery/pkg/util/validation/field/testing"
genericapirequest "k8s.io/apiserver/pkg/endpoints/request" genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
) )
@@ -154,7 +153,7 @@ func TestValidateDeclaratively(t *testing.T) {
} else { } else {
results = ValidateUpdateDeclaratively(ctx, tc.options, scheme, tc.object, tc.oldObject) results = ValidateUpdateDeclaratively(ctx, tc.options, scheme, tc.object, tc.oldObject)
} }
matcher := fieldtesting.ErrorMatcher{}.ByType().ByField().ByOrigin() matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
matcher.Test(t, tc.expected, results) matcher.Test(t, tc.expected, results)
}) })
} }