Add equal func for Requirement struct

This commit is contained in:
drfish 2021-02-15 10:09:00 +08:00
parent d08ac1dfcb
commit 872e1661ca
3 changed files with 86 additions and 1 deletions

View File

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

View File

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

View File

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