github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/labels_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestCloneSelectorAndAddLabel(t *testing.T) { 25 labels := map[string]string{ 26 "foo1": "bar1", 27 "foo2": "bar2", 28 "foo3": "bar3", 29 } 30 matchExpressions := []LabelSelectorRequirement{ 31 {Key: "foo", Operator: LabelSelectorOpIn, Values: []string{"foo"}}, 32 } 33 34 cases := []struct { 35 labels map[string]string 36 labelKey string 37 labelValue string 38 want map[string]string 39 }{ 40 { 41 labels: labels, 42 want: labels, 43 }, 44 { 45 labels: labels, 46 labelKey: "foo4", 47 labelValue: "89", 48 want: map[string]string{ 49 "foo1": "bar1", 50 "foo2": "bar2", 51 "foo3": "bar3", 52 "foo4": "89", 53 }, 54 }, 55 { 56 labels: nil, 57 labelKey: "foo4", 58 labelValue: "12", 59 want: map[string]string{ 60 "foo4": "12", 61 }, 62 }, 63 } 64 65 for _, tc := range cases { 66 ls_in := LabelSelector{MatchLabels: tc.labels, MatchExpressions: matchExpressions} 67 ls_out := LabelSelector{MatchLabels: tc.want, MatchExpressions: matchExpressions} 68 69 got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue) 70 if !reflect.DeepEqual(got, &ls_out) { 71 t.Errorf("got %v, want %v", got, tc.want) 72 } 73 } 74 } 75 76 func TestAddLabelToSelector(t *testing.T) { 77 labels := map[string]string{ 78 "foo1": "bar1", 79 "foo2": "bar2", 80 "foo3": "bar3", 81 } 82 83 cases := []struct { 84 labels map[string]string 85 labelKey string 86 labelValue string 87 want map[string]string 88 }{ 89 { 90 labels: labels, 91 want: labels, 92 }, 93 { 94 labels: labels, 95 labelKey: "foo4", 96 labelValue: "89", 97 want: map[string]string{ 98 "foo1": "bar1", 99 "foo2": "bar2", 100 "foo3": "bar3", 101 "foo4": "89", 102 }, 103 }, 104 { 105 labels: nil, 106 labelKey: "foo4", 107 labelValue: "12", 108 want: map[string]string{ 109 "foo4": "12", 110 }, 111 }, 112 } 113 114 for _, tc := range cases { 115 ls_in := LabelSelector{MatchLabels: tc.labels} 116 ls_out := LabelSelector{MatchLabels: tc.want} 117 118 got := AddLabelToSelector(&ls_in, tc.labelKey, tc.labelValue) 119 if !reflect.DeepEqual(got, &ls_out) { 120 t.Errorf("got %v, want %v", got, tc.want) 121 } 122 } 123 }