123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /*
- Copyright 2016 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package labels
- import (
- "reflect"
- "testing"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- )
- func TestCloneAndAddLabel(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: "42",
- want: map[string]string{
- "foo1": "bar1",
- "foo2": "bar2",
- "foo3": "bar3",
- "foo4": "42",
- },
- },
- }
- 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 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 := metav1.LabelSelector{MatchLabels: tc.labels}
- ls_out := metav1.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 := metav1.LabelSelector{MatchLabels: tc.labels}
- ls_out := metav1.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)
- }
- }
- }
|