mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 20:53:33 +00:00
Increase coverage in pkg/util/labels
This commit is contained in:
parent
2076071bbf
commit
448081b8bd
@ -19,6 +19,8 @@ package labels
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCloneAndAddLabel(t *testing.T) {
|
func TestCloneAndAddLabel(t *testing.T) {
|
||||||
@ -53,8 +55,157 @@ func TestCloneAndAddLabel(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
got := CloneAndAddLabel(tc.labels, tc.labelKey, tc.labelValue)
|
got := CloneAndAddLabel(tc.labels, tc.labelKey, tc.labelValue)
|
||||||
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
|
t.Errorf("[Add] got %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
// now test the inverse.
|
||||||
|
got_rm := CloneAndRemoveLabel(got, tc.labelKey)
|
||||||
|
if !reflect.DeepEqual(got_rm, tc.labels) {
|
||||||
|
t.Errorf("[RM] got %v, want %v", got_rm, tc.labels)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddLabel(t *testing.T) {
|
||||||
|
labels := map[string]string{
|
||||||
|
"foo1": "bar1",
|
||||||
|
"foo2": "bar2",
|
||||||
|
"foo3": "bar3",
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
labels map[string]string
|
||||||
|
labelKey string
|
||||||
|
labelValue string
|
||||||
|
want map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
labels: labels,
|
||||||
|
want: labels,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: labels,
|
||||||
|
labelKey: "foo4",
|
||||||
|
labelValue: "food",
|
||||||
|
want: map[string]string{
|
||||||
|
"foo1": "bar1",
|
||||||
|
"foo2": "bar2",
|
||||||
|
"foo3": "bar3",
|
||||||
|
"foo4": "food",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: nil,
|
||||||
|
labelKey: "foo4",
|
||||||
|
labelValue: "food",
|
||||||
|
want: map[string]string{
|
||||||
|
"foo4": "food",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
got := AddLabel(tc.labels, tc.labelKey, tc.labelValue)
|
||||||
if !reflect.DeepEqual(got, tc.want) {
|
if !reflect.DeepEqual(got, tc.want) {
|
||||||
t.Errorf("got %v, want %v", got, tc.want)
|
t.Errorf("got %v, want %v", got, tc.want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCloneSelectorAndAddLabel(t *testing.T) {
|
||||||
|
labels := map[string]string{
|
||||||
|
"foo1": "bar1",
|
||||||
|
"foo2": "bar2",
|
||||||
|
"foo3": "bar3",
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
labels map[string]string
|
||||||
|
labelKey string
|
||||||
|
labelValue uint32
|
||||||
|
want map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
labels: labels,
|
||||||
|
want: labels,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: labels,
|
||||||
|
labelKey: "foo4",
|
||||||
|
labelValue: 89,
|
||||||
|
want: map[string]string{
|
||||||
|
"foo1": "bar1",
|
||||||
|
"foo2": "bar2",
|
||||||
|
"foo3": "bar3",
|
||||||
|
"foo4": "89",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: nil,
|
||||||
|
labelKey: "foo4",
|
||||||
|
labelValue: 12,
|
||||||
|
want: map[string]string{
|
||||||
|
"foo4": "12",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
ls_in := unversioned.LabelSelector{MatchLabels: tc.labels}
|
||||||
|
ls_out := unversioned.LabelSelector{MatchLabels: tc.want}
|
||||||
|
|
||||||
|
got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue)
|
||||||
|
if !reflect.DeepEqual(got, &ls_out) {
|
||||||
|
t.Errorf("got %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddLabelToSelector(t *testing.T) {
|
||||||
|
labels := map[string]string{
|
||||||
|
"foo1": "bar1",
|
||||||
|
"foo2": "bar2",
|
||||||
|
"foo3": "bar3",
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
labels map[string]string
|
||||||
|
labelKey string
|
||||||
|
labelValue string
|
||||||
|
want map[string]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
labels: labels,
|
||||||
|
want: labels,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: labels,
|
||||||
|
labelKey: "foo4",
|
||||||
|
labelValue: "89",
|
||||||
|
want: map[string]string{
|
||||||
|
"foo1": "bar1",
|
||||||
|
"foo2": "bar2",
|
||||||
|
"foo3": "bar3",
|
||||||
|
"foo4": "89",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: nil,
|
||||||
|
labelKey: "foo4",
|
||||||
|
labelValue: "12",
|
||||||
|
want: map[string]string{
|
||||||
|
"foo4": "12",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
ls_in := unversioned.LabelSelector{MatchLabels: tc.labels}
|
||||||
|
ls_out := unversioned.LabelSelector{MatchLabels: tc.want}
|
||||||
|
|
||||||
|
got := AddLabelToSelector(&ls_in, tc.labelKey, tc.labelValue)
|
||||||
|
if !reflect.DeepEqual(got, &ls_out) {
|
||||||
|
t.Errorf("got %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user