github.phpd.cn/cilium/cilium@v1.6.12/pkg/labels/array_test.go (about)

     1  // Copyright 2016-2017 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package labels
    18  
    19  import (
    20  	"sort"
    21  
    22  	"github.com/cilium/cilium/pkg/checker"
    23  
    24  	. "gopkg.in/check.v1"
    25  )
    26  
    27  var _ = Suite(&LabelsSuite{})
    28  
    29  func (s *LabelsSuite) TestMatches(c *C) {
    30  	a := LabelArray{
    31  		NewLabel("1", "1", "1"),
    32  		NewLabel("2", "2", "1"),
    33  		NewLabel("3", "3", "1"),
    34  	}
    35  	b := LabelArray{
    36  		NewLabel("1", "1", "1"),
    37  		NewLabel("2", "2", "1"),
    38  	}
    39  	empty := LabelArray{}
    40  
    41  	c.Assert(a.Contains(b), Equals, true)      // b is in a
    42  	c.Assert(b.Contains(a), Equals, false)     // a is NOT in b
    43  	c.Assert(a.Contains(empty), Equals, true)  // empty is in a
    44  	c.Assert(b.Contains(empty), Equals, true)  // empty is in b
    45  	c.Assert(empty.Contains(a), Equals, false) // a is NOT in empty
    46  }
    47  
    48  func (s *LabelsSuite) TestParse(c *C) {
    49  	c.Assert(ParseLabelArray(), checker.DeepEquals, LabelArray{})
    50  	c.Assert(ParseLabelArray("magic"), checker.DeepEquals, LabelArray{ParseLabel("magic")})
    51  	// LabelArray is sorted
    52  	c.Assert(ParseLabelArray("a", "c", "b"), checker.DeepEquals,
    53  		LabelArray{ParseLabel("a"), ParseLabel("b"), ParseLabel("c")})
    54  	// NewLabelArrayFromSortedList
    55  	c.Assert(NewLabelArrayFromSortedList("unspec:a=;unspec:b;unspec:c=d"), checker.DeepEquals,
    56  		LabelArray{ParseLabel("a"), ParseLabel("b"), ParseLabel("c=d")})
    57  }
    58  
    59  func (s *LabelsSuite) TestHas(c *C) {
    60  	lbls := LabelArray{
    61  		NewLabel("env", "devel", LabelSourceAny),
    62  		NewLabel("user", "bob", LabelSourceContainer),
    63  	}
    64  	var hasTests = []struct {
    65  		input    string // input
    66  		expected bool   // expected result
    67  	}{
    68  		{"", false},
    69  		{"any", false},
    70  		{"env", true},
    71  		{"container.env", false},
    72  		{"container:env", false},
    73  		{"any:env", false},
    74  		{"any.env", true},
    75  		{"any:user", false},
    76  		{"any.user", true},
    77  		{"user", true},
    78  		{"container.user", true},
    79  		{"container:bob", false},
    80  	}
    81  	for _, tt := range hasTests {
    82  		c.Logf("has %q?", tt.input)
    83  		c.Assert(lbls.Has(tt.input), Equals, tt.expected)
    84  	}
    85  }
    86  
    87  func (s *LabelsSuite) TestSame(c *C) {
    88  	lbls1 := LabelArray{
    89  		NewLabel("env", "devel", LabelSourceAny),
    90  		NewLabel("user", "bob", LabelSourceContainer),
    91  	}
    92  	lbls2 := LabelArray{
    93  		NewLabel("env", "devel", LabelSourceAny),
    94  		NewLabel("user", "bob", LabelSourceContainer),
    95  	}
    96  	lbls3 := LabelArray{
    97  		NewLabel("user", "bob", LabelSourceContainer),
    98  		NewLabel("env", "devel", LabelSourceAny),
    99  	}
   100  	lbls4 := LabelArray{
   101  		NewLabel("env", "devel", LabelSourceAny),
   102  	}
   103  	lbls5 := LabelArray{
   104  		NewLabel("env", "prod", LabelSourceAny),
   105  	}
   106  	lbls6 := LabelArray{}
   107  
   108  	c.Assert(lbls1.Same(lbls1), Equals, true)
   109  	c.Assert(lbls1.Same(lbls2), Equals, true)
   110  	c.Assert(lbls1.Same(lbls3), Equals, false) // inverted order
   111  	c.Assert(lbls1.Same(lbls4), Equals, false) // different count
   112  	c.Assert(lbls1.Same(lbls5), Equals, false)
   113  	c.Assert(lbls1.Same(lbls6), Equals, false)
   114  
   115  	c.Assert(lbls2.Same(lbls1), Equals, true)
   116  	c.Assert(lbls2.Same(lbls2), Equals, true)
   117  	c.Assert(lbls2.Same(lbls3), Equals, false) // inverted order
   118  	c.Assert(lbls2.Same(lbls4), Equals, false) // different count
   119  	c.Assert(lbls2.Same(lbls5), Equals, false)
   120  	c.Assert(lbls2.Same(lbls6), Equals, false)
   121  
   122  	c.Assert(lbls3.Same(lbls1), Equals, false)
   123  	c.Assert(lbls3.Same(lbls2), Equals, false)
   124  	c.Assert(lbls3.Same(lbls3), Equals, true)
   125  	c.Assert(lbls3.Same(lbls4), Equals, false)
   126  	c.Assert(lbls3.Same(lbls5), Equals, false)
   127  	c.Assert(lbls3.Same(lbls6), Equals, false)
   128  
   129  	c.Assert(lbls4.Same(lbls1), Equals, false)
   130  	c.Assert(lbls4.Same(lbls2), Equals, false)
   131  	c.Assert(lbls4.Same(lbls3), Equals, false)
   132  	c.Assert(lbls4.Same(lbls4), Equals, true)
   133  	c.Assert(lbls4.Same(lbls5), Equals, false)
   134  	c.Assert(lbls4.Same(lbls6), Equals, false)
   135  
   136  	c.Assert(lbls5.Same(lbls1), Equals, false)
   137  	c.Assert(lbls5.Same(lbls2), Equals, false)
   138  	c.Assert(lbls5.Same(lbls3), Equals, false)
   139  	c.Assert(lbls5.Same(lbls4), Equals, false)
   140  	c.Assert(lbls5.Same(lbls5), Equals, true)
   141  	c.Assert(lbls5.Same(lbls6), Equals, false)
   142  
   143  	c.Assert(lbls6.Same(lbls1), Equals, false)
   144  	c.Assert(lbls6.Same(lbls2), Equals, false)
   145  	c.Assert(lbls6.Same(lbls3), Equals, false)
   146  	c.Assert(lbls6.Same(lbls4), Equals, false)
   147  	c.Assert(lbls6.Same(lbls5), Equals, false)
   148  	c.Assert(lbls6.Same(lbls6), Equals, true)
   149  }
   150  
   151  // TestOutputConversions tests the various ways a LabelArray can be converted
   152  // into other representations
   153  func (s *LabelsSuite) TestOutputConversions(c *C) {
   154  	lbls := LabelArray{
   155  		NewLabel("env", "devel", LabelSourceAny),
   156  		NewLabel("user", "bob", LabelSourceContainer),
   157  		NewLabel("something", "somethingelse", LabelSourceK8s),
   158  		NewLabel("nosource", "value", ""),
   159  		NewLabel("nosource", "value", "actuallyASource"),
   160  	}
   161  
   162  	expectMdl := []string{"any:env=devel", "container:user=bob", "k8s:something=somethingelse", "unspec:nosource=value", "actuallyASource:nosource=value"}
   163  	sort.StringSlice(expectMdl).Sort()
   164  	mdl := lbls.GetModel()
   165  	sort.StringSlice(mdl).Sort()
   166  	c.Assert(len(mdl), Equals, len(expectMdl))
   167  	for i := range mdl {
   168  		c.Assert(mdl[i], Equals, expectMdl[i])
   169  	}
   170  
   171  	expectString := "[any:env=devel container:user=bob k8s:something=somethingelse unspec:nosource=value actuallyASource:nosource=value]"
   172  	c.Assert(lbls.String(), Equals, expectString)
   173  
   174  	// Note: the two nosource entries do not alias when rendered into the StringMap
   175  	// format, because they have different sources.
   176  	expectMap := map[string]string{
   177  		"any:env":                       "devel",
   178  		"container:user":                "bob",
   179  		"k8s:something":                 "somethingelse",
   180  		LabelSourceUnspec + ":nosource": "value",
   181  		"actuallyASource:nosource":      "value"}
   182  	mp := lbls.StringMap()
   183  	c.Assert(len(mp), Equals, len(expectMap))
   184  	for k, v := range mp {
   185  		c.Assert(v, Equals, expectMap[k])
   186  	}
   187  }