Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Machinery: Add equal func for Requirement struct #99083

Merged
merged 1 commit into from Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions staging/src/k8s.io/apimachinery/pkg/labels/BUILD
Expand Up @@ -37,6 +37,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/validation:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
"//vendor/github.com/google/go-cmp/cmp:go_default_library",
"//vendor/k8s.io/klog/v2:go_default_library",
],
)
Expand Down
12 changes: 12 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/labels/selector.go
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"
Expand Down Expand Up @@ -276,6 +277,17 @@ func (r *Requirement) Values() sets.String {
return ret
}

// Equal checks the equality of requirement.
func (r Requirement) Equal(x Requirement) bool {
if r.key != x.key {
return false
}
if r.operator != x.operator {
return false
}
return cmp.Equal(r.strValues, x.strValues)
}

// Empty returns true if the internalSelector doesn't restrict selection space
func (s internalSelector) Empty() bool {
if s == nil {
Expand Down
74 changes: 73 additions & 1 deletion staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go
Expand Up @@ -904,7 +904,7 @@ func TestValidatedSelectorFromSet(t *testing.T) {
t.Errorf("ValidatedSelectorFromSet %#v returned unexpected error (-want,+got):\n%s", tc.name, diff)
}
if err == nil {
if diff := cmp.Diff(tc.expectedSelector, selector, cmp.AllowUnexported(Requirement{})); diff != "" {
if diff := cmp.Diff(tc.expectedSelector, selector); diff != "" {
t.Errorf("ValidatedSelectorFromSet %#v returned unexpected selector (-want,+got):\n%s", tc.name, diff)
}
}
Expand All @@ -928,3 +928,75 @@ func BenchmarkRequirementString(b *testing.B) {
}
}
}

func TestRequirementEqual(t *testing.T) {
tests := []struct {
name string
x, y *Requirement
want bool
}{
{
name: "same requirements should be equal",
x: &Requirement{
key: "key",
operator: selection.Equals,
strValues: []string{"foo", "bar"},
},
y: &Requirement{
key: "key",
operator: selection.Equals,
strValues: []string{"foo", "bar"},
},
want: true,
},
{
name: "requirements with different keys should not be equal",
x: &Requirement{
key: "key1",
operator: selection.Equals,
strValues: []string{"foo", "bar"},
},
y: &Requirement{
key: "key2",
operator: selection.Equals,
strValues: []string{"foo", "bar"},
},
want: false,
},
{
name: "requirements with different operators should not be equal",
x: &Requirement{
key: "key",
operator: selection.Equals,
strValues: []string{"foo", "bar"},
},
y: &Requirement{
key: "key",
operator: selection.In,
strValues: []string{"foo", "bar"},
},
want: false,
},
{
name: "requirements with different values should not be equal",
x: &Requirement{
key: "key",
operator: selection.Equals,
strValues: []string{"foo", "bar"},
},
y: &Requirement{
key: "key",
operator: selection.Equals,
strValues: []string{"foobar"},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cmp.Equal(tt.x, tt.y); got != tt.want {
t.Errorf("cmp.Equal() = %v, want %v", got, tt.want)
}
})
}
}