From 872e1661ca67ab98a61360730d4bca7487a0fc5c Mon Sep 17 00:00:00 2001 From: drfish Date: Mon, 15 Feb 2021 10:09:00 +0800 Subject: [PATCH] Add equal func for Requirement struct --- .../src/k8s.io/apimachinery/pkg/labels/BUILD | 1 + .../apimachinery/pkg/labels/selector.go | 12 +++ .../apimachinery/pkg/labels/selector_test.go | 74 ++++++++++++++++++- 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/labels/BUILD b/staging/src/k8s.io/apimachinery/pkg/labels/BUILD index 8608238255db..894d1e538284 100644 --- a/staging/src/k8s.io/apimachinery/pkg/labels/BUILD +++ b/staging/src/k8s.io/apimachinery/pkg/labels/BUILD @@ -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", ], ) diff --git a/staging/src/k8s.io/apimachinery/pkg/labels/selector.go b/staging/src/k8s.io/apimachinery/pkg/labels/selector.go index 6eb503c0fef1..b0865777a29a 100644 --- a/staging/src/k8s.io/apimachinery/pkg/labels/selector.go +++ b/staging/src/k8s.io/apimachinery/pkg/labels/selector.go @@ -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" @@ -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 { diff --git a/staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go b/staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go index d1297333a849..07774bc9a0f7 100644 --- a/staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/labels/selector_test.go @@ -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) } } @@ -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) + } + }) + } +}