Drop direct use of cmp in component-helpers

This commit is contained in:
Jordan Liggitt
2026-02-20 12:05:25 -05:00
parent d56e985fa4
commit 7de12a329f
5 changed files with 37 additions and 41 deletions

View File

@@ -23,7 +23,6 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -253,7 +252,7 @@ func TestNewNodeLease(t *testing.T) {
t.Fatalf("the new lease must be newly allocated, but got same address as base")
}
if !apiequality.Semantic.DeepEqual(tc.expect, newLease) {
t.Errorf("unexpected result from newLease: %s", cmp.Diff(tc.expect, newLease))
t.Errorf("expected %#v, got %#v", tc.expect, newLease)
}
})
}

View File

@@ -19,7 +19,6 @@ package reconciliation
import (
"testing"
"github.com/google/go-cmp/cmp"
rbacv1 "k8s.io/api/rbac/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -392,7 +391,7 @@ func TestComputeReconciledRoleAggregationRules(t *testing.T) {
continue
}
if reconciliationNeeded && !apiequality.Semantic.DeepEqual(result.Role.(ClusterRoleRuleOwner).ClusterRole, tc.expectedReconciledRole) {
t.Errorf("%s: %v", k, cmp.Diff(tc.expectedReconciledRole, result.Role.(ClusterRoleRuleOwner).ClusterRole))
t.Errorf("%s: expected %#v, got %#v", k, tc.expectedReconciledRole, result.Role.(ClusterRoleRuleOwner).ClusterRole)
}
}
}

View File

@@ -7,7 +7,6 @@ go 1.25.0
godebug default=go1.25
require (
github.com/google/go-cmp v0.7.0
github.com/stretchr/testify v1.11.1
k8s.io/api v0.0.0
k8s.io/apimachinery v0.0.0

View File

@@ -19,10 +19,10 @@ package resource
import (
"testing"
"github.com/google/go-cmp/cmp"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/utils/ptr"
)
@@ -884,8 +884,8 @@ func TestPodResourceRequests(t *testing.T) {
},
}
request := PodRequests(p, tc.options)
if diff := cmp.Diff(request, tc.expectedRequests); diff != "" {
t.Errorf("got=%v, want=%v, diff=%s", request, tc.expectedRequests, diff)
if !equality.Semantic.DeepEqual(request, tc.expectedRequests) {
t.Errorf("got=%v, want=%v, diff=%s", request, tc.expectedRequests, diff.Diff(request, tc.expectedRequests))
}
})
}
@@ -914,13 +914,13 @@ func TestPodResourceRequestsReuse(t *testing.T) {
}
requests := PodRequests(p, opts)
if diff := cmp.Diff(requests, expectedRequests); diff != "" {
t.Errorf("got=%v, want=%v, diff=%s", requests, expectedRequests, diff)
if !equality.Semantic.DeepEqual(requests, expectedRequests) {
t.Errorf("got=%v, want=%v, diff=%s", requests, expectedRequests, diff.Diff(requests, expectedRequests))
}
// should re-use the maps we passed in
if diff := cmp.Diff(opts.Reuse, expectedRequests); diff != "" {
t.Errorf("got=%v, want=%v, diff=%s", requests, expectedRequests, diff)
if !equality.Semantic.DeepEqual(opts.Reuse, expectedRequests) {
t.Errorf("got=%v, want=%v, diff=%s", requests, expectedRequests, diff.Diff(opts.Reuse, expectedRequests))
}
}
@@ -1486,8 +1486,8 @@ func TestPodResourceLimits(t *testing.T) {
},
}
limits := PodLimits(p, tc.options)
if diff := cmp.Diff(limits, tc.expectedLimits); diff != "" {
t.Errorf("got=%v, want=%v, diff=%s", limits, tc.expectedLimits, diff)
if !equality.Semantic.DeepEqual(limits, tc.expectedLimits) {
t.Errorf("got=%v, want=%v, diff=%s", limits, tc.expectedLimits, diff.Diff(limits, tc.expectedLimits))
}
})
}
@@ -1969,8 +1969,8 @@ func TestPodLevelResourceRequests(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
podReqs := PodRequests(getPodLevelResourcesPod(tc.podResources, tc.overhead, tc.containers, tc.initContainers), tc.opts)
if diff := cmp.Diff(podReqs, tc.expectedRequests); diff != "" {
t.Errorf("got=%v, want=%v, diff=%s", podReqs, tc.expectedRequests, diff)
if !equality.Semantic.DeepEqual(podReqs, tc.expectedRequests) {
t.Errorf("got=%v, want=%v, diff=%s", podReqs, tc.expectedRequests, diff.Diff(podReqs, tc.expectedRequests))
}
})
}

View File

@@ -20,18 +20,13 @@ import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/validation/field"
)
var (
ignoreBadValue = cmpopts.IgnoreFields(field.Error{}, "BadValue")
)
func TestNodeSelectorMatch(t *testing.T) {
tests := []struct {
name string
@@ -76,14 +71,16 @@ func TestNodeSelectorMatch(t *testing.T) {
}},
wantErr: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "nodeSelectorTerms[0].matchFields[0].values",
Detail: "must have one element",
Type: field.ErrorTypeInvalid,
Field: "nodeSelectorTerms[0].matchFields[0].values",
Detail: "must have one element",
BadValue: []string{"host_1", "host_2"},
},
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "nodeSelectorTerms[2].matchExpressions[0].key",
Detail: `name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
Type: field.ErrorTypeInvalid,
Field: "nodeSelectorTerms[2].matchExpressions[0].key",
Detail: `name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
BadValue: "invalid key",
},
}.ToAggregate(),
},
@@ -149,8 +146,8 @@ func TestNodeSelectorMatch(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nodeSelector, err := NewNodeSelector(&tt.nodeSelector)
if diff := cmp.Diff(tt.wantErr, err, ignoreBadValue); diff != "" {
t.Errorf("NewNodeSelector returned unexpected error (-want,+got):\n%s", diff)
if !reflect.DeepEqual(tt.wantErr, err) {
t.Errorf("NewNodeSelector returned unexpected error (-want,+got):\n%s", diff.Diff(tt.wantErr, err))
}
if tt.wantErr != nil {
return
@@ -212,14 +209,16 @@ func TestPreferredSchedulingTermsScore(t *testing.T) {
},
wantErr: field.ErrorList{
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "[0].matchFields[0].values",
Detail: "must have one element",
Type: field.ErrorTypeInvalid,
Field: "[0].matchFields[0].values",
Detail: "must have one element",
BadValue: []string{"host_1", "host_2"},
},
&field.Error{
Type: field.ErrorTypeInvalid,
Field: "[2].matchExpressions[0].key",
Detail: `name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
Type: field.ErrorTypeInvalid,
Field: "[2].matchExpressions[0].key",
Detail: `name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`,
BadValue: "invalid key",
},
}.ToAggregate(),
},
@@ -280,8 +279,8 @@ func TestPreferredSchedulingTermsScore(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prefSchedTerms, err := NewPreferredSchedulingTerms(tt.prefSchedTerms)
if diff := cmp.Diff(tt.wantErr, err, ignoreBadValue); diff != "" {
t.Errorf("NewPreferredSchedulingTerms returned unexpected error (-want,+got):\n%s", diff)
if !reflect.DeepEqual(tt.wantErr, err) {
t.Errorf("NewPreferredSchedulingTerms returned unexpected error (-want,+got):\n%s", diff.Diff(tt.wantErr, err))
}
if tt.wantErr != nil {
return
@@ -326,7 +325,7 @@ func TestNodeSelectorRequirementsAsSelector(t *testing.T) {
}},
wantErr: []error{
field.ErrorList{
field.Invalid(field.NewPath("root").Index(0).Child("values"), nil, "values set must be empty for exists and does not exist"),
field.Invalid(field.NewPath("root").Index(0).Child("values"), []string{"bar", "baz"}, "values set must be empty for exists and does not exist"),
}.ToAggregate(),
},
},
@@ -360,8 +359,8 @@ func TestNodeSelectorRequirementsAsSelector(t *testing.T) {
for i, tc := range tc {
out, err := nodeSelectorRequirementsAsSelector(tc.in, field.NewPath("root"))
if diff := cmp.Diff(tc.wantErr, err, ignoreBadValue); diff != "" {
t.Errorf("nodeSelectorRequirementsAsSelector returned unexpected error (-want,+got):\n%s", diff)
if !reflect.DeepEqual(tc.wantErr, err) {
t.Errorf("nodeSelectorRequirementsAsSelector returned unexpected error (-want,+got):\n%s", diff.Diff(tc.wantErr, err))
}
if !reflect.DeepEqual(out, tc.out) {
t.Errorf("[%v]expected:\n\t%+v\nbut got:\n\t%+v", i, tc.out, out)