mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #99083 from gavinfish/requirement-equal
API Machinery: Add equal func for Requirement struct
This commit is contained in:
commit
6c9e83669e
@ -37,6 +37,7 @@ go_library(
|
|||||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_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:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field: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",
|
"//vendor/k8s.io/klog/v2:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
"k8s.io/apimachinery/pkg/selection"
|
"k8s.io/apimachinery/pkg/selection"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/util/validation"
|
"k8s.io/apimachinery/pkg/util/validation"
|
||||||
@ -276,6 +277,17 @@ func (r *Requirement) Values() sets.String {
|
|||||||
return ret
|
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
|
// Empty returns true if the internalSelector doesn't restrict selection space
|
||||||
func (s internalSelector) Empty() bool {
|
func (s internalSelector) Empty() bool {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
|
@ -904,7 +904,7 @@ func TestValidatedSelectorFromSet(t *testing.T) {
|
|||||||
t.Errorf("ValidatedSelectorFromSet %#v returned unexpected error (-want,+got):\n%s", tc.name, diff)
|
t.Errorf("ValidatedSelectorFromSet %#v returned unexpected error (-want,+got):\n%s", tc.name, diff)
|
||||||
}
|
}
|
||||||
if err == nil {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user