From 448081b8bd8c68c0a3189bb7e9f2025633391e13 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Wed, 1 Jun 2016 14:18:33 +1000 Subject: [PATCH] Increase coverage in pkg/util/labels --- pkg/util/labels/labels_test.go | 151 +++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/pkg/util/labels/labels_test.go b/pkg/util/labels/labels_test.go index 34f385ae95f..de727dedca2 100644 --- a/pkg/util/labels/labels_test.go +++ b/pkg/util/labels/labels_test.go @@ -19,6 +19,8 @@ package labels import ( "reflect" "testing" + + "k8s.io/kubernetes/pkg/api/unversioned" ) func TestCloneAndAddLabel(t *testing.T) { @@ -53,8 +55,157 @@ func TestCloneAndAddLabel(t *testing.T) { for _, tc := range cases { 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) { 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) + } + } +}