github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/labels/labels_test.go (about)

     1  /*
     2  Copyright 2014 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  	"testing"
    21  )
    22  
    23  func matches(t *testing.T, ls Set, want string) {
    24  	if ls.String() != want {
    25  		t.Errorf("Expected '%s', but got '%s'", want, ls.String())
    26  	}
    27  }
    28  
    29  func TestSetString(t *testing.T) {
    30  	matches(t, Set{"x": "y"}, "x=y")
    31  	matches(t, Set{"foo": "bar"}, "foo=bar")
    32  	matches(t, Set{"foo": "bar", "baz": "qup"}, "baz=qup,foo=bar")
    33  
    34  	// TODO: Make our label representation robust enough to handle labels
    35  	// with ",=!" characters in their names.
    36  }
    37  
    38  func TestLabelHas(t *testing.T) {
    39  	labelHasTests := []struct {
    40  		Ls  Labels
    41  		Key string
    42  		Has bool
    43  	}{
    44  		{Set{"x": "y"}, "x", true},
    45  		{Set{"x": ""}, "x", true},
    46  		{Set{"x": "y"}, "foo", false},
    47  	}
    48  	for _, lh := range labelHasTests {
    49  		if has := lh.Ls.Has(lh.Key); has != lh.Has {
    50  			t.Errorf("%#v.Has(%#v) => %v, expected %v", lh.Ls, lh.Key, has, lh.Has)
    51  		}
    52  	}
    53  }
    54  
    55  func TestLabelGet(t *testing.T) {
    56  	ls := Set{"x": "y"}
    57  	if ls.Get("x") != "y" {
    58  		t.Errorf("Set.Get is broken")
    59  	}
    60  }
    61  
    62  func TestLabelConflict(t *testing.T) {
    63  	tests := []struct {
    64  		labels1  map[string]string
    65  		labels2  map[string]string
    66  		conflict bool
    67  	}{
    68  		{
    69  			labels1:  map[string]string{},
    70  			labels2:  map[string]string{},
    71  			conflict: false,
    72  		},
    73  		{
    74  			labels1:  map[string]string{"env": "test"},
    75  			labels2:  map[string]string{"infra": "true"},
    76  			conflict: false,
    77  		},
    78  		{
    79  			labels1:  map[string]string{"env": "test"},
    80  			labels2:  map[string]string{"infra": "true", "env": "test"},
    81  			conflict: false,
    82  		},
    83  		{
    84  			labels1:  map[string]string{"env": "test"},
    85  			labels2:  map[string]string{"env": "dev"},
    86  			conflict: true,
    87  		},
    88  		{
    89  			labels1:  map[string]string{"env": "test", "infra": "false"},
    90  			labels2:  map[string]string{"infra": "true", "color": "blue"},
    91  			conflict: true,
    92  		},
    93  	}
    94  	for _, test := range tests {
    95  		conflict := Conflicts(Set(test.labels1), Set(test.labels2))
    96  		if conflict != test.conflict {
    97  			t.Errorf("expected: %v but got: %v", test.conflict, conflict)
    98  		}
    99  	}
   100  }
   101  
   102  func TestLabelMerge(t *testing.T) {
   103  	tests := []struct {
   104  		labels1      map[string]string
   105  		labels2      map[string]string
   106  		mergedLabels map[string]string
   107  	}{
   108  		{
   109  			labels1:      map[string]string{},
   110  			labels2:      map[string]string{},
   111  			mergedLabels: map[string]string{},
   112  		},
   113  		{
   114  			labels1:      map[string]string{"infra": "true"},
   115  			labels2:      map[string]string{},
   116  			mergedLabels: map[string]string{"infra": "true"},
   117  		},
   118  		{
   119  			labels1:      map[string]string{"infra": "true"},
   120  			labels2:      map[string]string{"env": "test", "color": "blue"},
   121  			mergedLabels: map[string]string{"infra": "true", "env": "test", "color": "blue"},
   122  		},
   123  	}
   124  	for _, test := range tests {
   125  		mergedLabels := Merge(Set(test.labels1), Set(test.labels2))
   126  		if !Equals(mergedLabels, test.mergedLabels) {
   127  			t.Errorf("expected: %v but got: %v", test.mergedLabels, mergedLabels)
   128  		}
   129  	}
   130  }
   131  
   132  func TestLabelSelectorParse(t *testing.T) {
   133  	tests := []struct {
   134  		selector string
   135  		labels   map[string]string
   136  		valid    bool
   137  	}{
   138  		{
   139  			selector: "",
   140  			labels:   map[string]string{},
   141  			valid:    true,
   142  		},
   143  		{
   144  			selector: "x=a",
   145  			labels:   map[string]string{"x": "a"},
   146  			valid:    true,
   147  		},
   148  		{
   149  			selector: "x=a,y=b,z=c",
   150  			labels:   map[string]string{"x": "a", "y": "b", "z": "c"},
   151  			valid:    true,
   152  		},
   153  		{
   154  			selector: " x = a , y = b , z = c ",
   155  			labels:   map[string]string{"x": "a", "y": "b", "z": "c"},
   156  			valid:    true,
   157  		},
   158  		{
   159  			selector: "color=green,env=test,service=front",
   160  			labels:   map[string]string{"color": "green", "env": "test", "service": "front"},
   161  			valid:    true,
   162  		},
   163  		{
   164  			selector: "color=green, env=test, service=front",
   165  			labels:   map[string]string{"color": "green", "env": "test", "service": "front"},
   166  			valid:    true,
   167  		},
   168  		{
   169  			selector: ",",
   170  			labels:   map[string]string{},
   171  			valid:    false,
   172  		},
   173  		{
   174  			selector: "x",
   175  			labels:   map[string]string{},
   176  			valid:    false,
   177  		},
   178  		{
   179  			selector: "x,y",
   180  			labels:   map[string]string{},
   181  			valid:    false,
   182  		},
   183  		{
   184  			selector: "x=$y",
   185  			labels:   map[string]string{},
   186  			valid:    false,
   187  		},
   188  		{
   189  			selector: "x!=y",
   190  			labels:   map[string]string{},
   191  			valid:    false,
   192  		},
   193  		{
   194  			selector: "x==y",
   195  			labels:   map[string]string{},
   196  			valid:    false,
   197  		},
   198  		{
   199  			selector: "x=a||y=b",
   200  			labels:   map[string]string{},
   201  			valid:    false,
   202  		},
   203  		{
   204  			selector: "x in (y)",
   205  			labels:   map[string]string{},
   206  			valid:    false,
   207  		},
   208  		{
   209  			selector: "x notin (y)",
   210  			labels:   map[string]string{},
   211  			valid:    false,
   212  		},
   213  		{
   214  			selector: "x y",
   215  			labels:   map[string]string{},
   216  			valid:    false,
   217  		},
   218  	}
   219  	for _, test := range tests {
   220  		labels, err := ConvertSelectorToLabelsMap(test.selector)
   221  		if test.valid && err != nil {
   222  			t.Errorf("selector: %s, expected no error but got: %s", test.selector, err)
   223  		} else if !test.valid && err == nil {
   224  			t.Errorf("selector: %s, expected an error", test.selector)
   225  		}
   226  
   227  		if !Equals(Set(labels), test.labels) {
   228  			t.Errorf("expected: %s but got: %s", test.labels, labels)
   229  		}
   230  	}
   231  }