...

Source file src/k8s.io/kubernetes/pkg/util/labels/labels_test.go

Documentation: k8s.io/kubernetes/pkg/util/labels

     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 labels
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  func TestCloneAndAddLabel(t *testing.T) {
    27  	labels := map[string]string{
    28  		"foo1": "bar1",
    29  		"foo2": "bar2",
    30  		"foo3": "bar3",
    31  	}
    32  
    33  	cases := []struct {
    34  		labels     map[string]string
    35  		labelKey   string
    36  		labelValue string
    37  		want       map[string]string
    38  	}{
    39  		{
    40  			labels: labels,
    41  			want:   labels,
    42  		},
    43  		{
    44  			labels:     labels,
    45  			labelKey:   "foo4",
    46  			labelValue: "42",
    47  			want: map[string]string{
    48  				"foo1": "bar1",
    49  				"foo2": "bar2",
    50  				"foo3": "bar3",
    51  				"foo4": "42",
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, tc := range cases {
    57  		got := CloneAndAddLabel(tc.labels, tc.labelKey, tc.labelValue)
    58  		if !reflect.DeepEqual(got, tc.want) {
    59  			t.Errorf("[Add] got %v, want %v", got, tc.want)
    60  		}
    61  		// now test the inverse.
    62  		got_rm := CloneAndRemoveLabel(got, tc.labelKey)
    63  		if !reflect.DeepEqual(got_rm, tc.labels) {
    64  			t.Errorf("[RM] got %v, want %v", got_rm, tc.labels)
    65  		}
    66  	}
    67  }
    68  
    69  func TestAddLabel(t *testing.T) {
    70  	labels := map[string]string{
    71  		"foo1": "bar1",
    72  		"foo2": "bar2",
    73  		"foo3": "bar3",
    74  	}
    75  
    76  	cases := []struct {
    77  		labels     map[string]string
    78  		labelKey   string
    79  		labelValue string
    80  		want       map[string]string
    81  	}{
    82  		{
    83  			labels: labels,
    84  			want:   labels,
    85  		},
    86  		{
    87  			labels:     labels,
    88  			labelKey:   "foo4",
    89  			labelValue: "food",
    90  			want: map[string]string{
    91  				"foo1": "bar1",
    92  				"foo2": "bar2",
    93  				"foo3": "bar3",
    94  				"foo4": "food",
    95  			},
    96  		},
    97  		{
    98  			labels:     nil,
    99  			labelKey:   "foo4",
   100  			labelValue: "food",
   101  			want: map[string]string{
   102  				"foo4": "food",
   103  			},
   104  		},
   105  	}
   106  
   107  	for _, tc := range cases {
   108  		got := AddLabel(tc.labels, tc.labelKey, tc.labelValue)
   109  		if !reflect.DeepEqual(got, tc.want) {
   110  			t.Errorf("got %v, want %v", got, tc.want)
   111  		}
   112  	}
   113  }
   114  
   115  func TestCloneSelectorAndAddLabel(t *testing.T) {
   116  	labels := map[string]string{
   117  		"foo1": "bar1",
   118  		"foo2": "bar2",
   119  		"foo3": "bar3",
   120  	}
   121  
   122  	cases := []struct {
   123  		labels     map[string]string
   124  		labelKey   string
   125  		labelValue string
   126  		want       map[string]string
   127  	}{
   128  		{
   129  			labels: labels,
   130  			want:   labels,
   131  		},
   132  		{
   133  			labels:     labels,
   134  			labelKey:   "foo4",
   135  			labelValue: "89",
   136  			want: map[string]string{
   137  				"foo1": "bar1",
   138  				"foo2": "bar2",
   139  				"foo3": "bar3",
   140  				"foo4": "89",
   141  			},
   142  		},
   143  		{
   144  			labels:     nil,
   145  			labelKey:   "foo4",
   146  			labelValue: "12",
   147  			want: map[string]string{
   148  				"foo4": "12",
   149  			},
   150  		},
   151  	}
   152  
   153  	for _, tc := range cases {
   154  		ls_in := metav1.LabelSelector{MatchLabels: tc.labels}
   155  		ls_out := metav1.LabelSelector{MatchLabels: tc.want}
   156  
   157  		got := CloneSelectorAndAddLabel(&ls_in, tc.labelKey, tc.labelValue)
   158  		if !reflect.DeepEqual(got, &ls_out) {
   159  			t.Errorf("got %v, want %v", got, tc.want)
   160  		}
   161  	}
   162  }
   163  
   164  func TestAddLabelToSelector(t *testing.T) {
   165  	labels := map[string]string{
   166  		"foo1": "bar1",
   167  		"foo2": "bar2",
   168  		"foo3": "bar3",
   169  	}
   170  
   171  	cases := []struct {
   172  		labels     map[string]string
   173  		labelKey   string
   174  		labelValue string
   175  		want       map[string]string
   176  	}{
   177  		{
   178  			labels: labels,
   179  			want:   labels,
   180  		},
   181  		{
   182  			labels:     labels,
   183  			labelKey:   "foo4",
   184  			labelValue: "89",
   185  			want: map[string]string{
   186  				"foo1": "bar1",
   187  				"foo2": "bar2",
   188  				"foo3": "bar3",
   189  				"foo4": "89",
   190  			},
   191  		},
   192  		{
   193  			labels:     nil,
   194  			labelKey:   "foo4",
   195  			labelValue: "12",
   196  			want: map[string]string{
   197  				"foo4": "12",
   198  			},
   199  		},
   200  	}
   201  
   202  	for _, tc := range cases {
   203  		ls_in := metav1.LabelSelector{MatchLabels: tc.labels}
   204  		ls_out := metav1.LabelSelector{MatchLabels: tc.want}
   205  
   206  		got := AddLabelToSelector(&ls_in, tc.labelKey, tc.labelValue)
   207  		if !reflect.DeepEqual(got, &ls_out) {
   208  			t.Errorf("got %v, want %v", got, tc.want)
   209  		}
   210  	}
   211  }
   212  

View as plain text