github.phpd.cn/cilium/cilium@v1.6.12/pkg/labels/labels_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  	"encoding/json"
    21  	"fmt"
    22  	"reflect"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/cilium/cilium/pkg/checker"
    27  	. "gopkg.in/check.v1"
    28  )
    29  
    30  // Hook up gocheck into the "go test" runner.
    31  func Test(t *testing.T) {
    32  	TestingT(t)
    33  }
    34  
    35  type LabelsSuite struct{}
    36  
    37  var _ = Suite(&LabelsSuite{})
    38  
    39  var (
    40  	// Elements are sorted by the key
    41  	lblsArray = []string{`unspec:%=%ed`, `unspec://=/=`, `unspec:foo=bar`, `unspec:foo2==bar2`, `unspec:foo=====`, `unspec:foo\\==\=`, `unspec:key=`}
    42  	lbls      = Labels{
    43  		"foo":    NewLabel("foo", "bar", LabelSourceUnspec),
    44  		"foo2":   NewLabel("foo2", "=bar2", LabelSourceUnspec),
    45  		"key":    NewLabel("key", "", LabelSourceUnspec),
    46  		"foo==":  NewLabel("foo==", "==", LabelSourceUnspec),
    47  		`foo\\=`: NewLabel(`foo\\=`, `\=`, LabelSourceUnspec),
    48  		`//=/`:   NewLabel(`//=/`, "", LabelSourceUnspec),
    49  		`%`:      NewLabel(`%`, `%ed`, LabelSourceUnspec),
    50  	}
    51  
    52  	DefaultLabelSourceKeyPrefix = LabelSourceAny + "."
    53  )
    54  
    55  func (s *LabelsSuite) TestSHA256Sum(c *C) {
    56  	str := lbls.SHA256Sum()
    57  	c.Assert(str, Equals, "cf51cc7e153a09e82b242f2f0fb2f0f3923d2742a9d84de8bb0de669e5e558e3")
    58  }
    59  
    60  func (s *LabelsSuite) TestSortMap(c *C) {
    61  	lblsString := strings.Join(lblsArray, ";")
    62  	lblsString += ";"
    63  	sortedMap := lbls.SortedList()
    64  	c.Assert(sortedMap, checker.DeepEquals, []byte(lblsString))
    65  }
    66  
    67  func (s *LabelsSuite) TestLabelArraySorted(c *C) {
    68  	lblsString := strings.Join(lblsArray, ";")
    69  	lblsString += ";"
    70  	str := ""
    71  	for _, l := range lbls.LabelArray() {
    72  		str += fmt.Sprintf(`%s:%s=%s;`, l.Source, l.Key, l.Value)
    73  	}
    74  	c.Assert(str, checker.DeepEquals, lblsString)
    75  }
    76  
    77  func (s *LabelsSuite) TestMap2Labels(c *C) {
    78  	m := Map2Labels(map[string]string{
    79  		"k8s:foo":  "bar",
    80  		"k8s:foo2": "=bar2",
    81  		"key":      "",
    82  		"foo==":    "==",
    83  		`foo\\=`:   `\=`,
    84  		`//=/`:     "",
    85  		`%`:        `%ed`,
    86  	}, LabelSourceUnspec)
    87  	c.Assert(m, checker.DeepEquals, lbls)
    88  }
    89  
    90  func (s *LabelsSuite) TestMergeLabels(c *C) {
    91  	to := Labels{
    92  		"key1": NewLabel("key1", "value1", "source1"),
    93  		"key2": NewLabel("key2", "value3", "source4"),
    94  	}
    95  	from := Labels{
    96  		"key1": NewLabel("key1", "value3", "source4"),
    97  	}
    98  	want := Labels{
    99  		"key1": NewLabel("key1", "value3", "source4"),
   100  		"key2": NewLabel("key2", "value3", "source4"),
   101  	}
   102  	to.MergeLabels(from)
   103  	from["key1"] = NewLabel("key1", "changed", "source4")
   104  	c.Assert(to, checker.DeepEquals, want)
   105  }
   106  
   107  func (s *LabelsSuite) TestParseLabel(c *C) {
   108  	tests := []struct {
   109  		str string
   110  		out Label
   111  	}{
   112  		{"source1:key1=value1", NewLabel("key1", "value1", "source1")},
   113  		{"key1=value1", NewLabel("key1", "value1", LabelSourceUnspec)},
   114  		{"value1", NewLabel("value1", "", LabelSourceUnspec)},
   115  		{"source1:key1", NewLabel("key1", "", "source1")},
   116  		{"source1:key1==value1", NewLabel("key1", "=value1", "source1")},
   117  		{"source::key1=value1", NewLabel("::key1", "value1", "source")},
   118  		{"$key1=value1", NewLabel("key1", "value1", LabelSourceReserved)},
   119  		{"1foo", NewLabel("1foo", "", LabelSourceUnspec)},
   120  		{":2foo", NewLabel("2foo", "", LabelSourceUnspec)},
   121  		{":3foo=", NewLabel("3foo", "", LabelSourceUnspec)},
   122  		{"reserved:=key", NewLabel("key", "", LabelSourceReserved)},
   123  		{"4blah=:foo=", NewLabel("foo", "", "4blah=")},
   124  		{"5blah::foo=", NewLabel("::foo", "", "5blah")},
   125  		{"6foo==", NewLabel("6foo", "=", LabelSourceUnspec)},
   126  		{"7foo=bar", NewLabel("7foo", "bar", LabelSourceUnspec)},
   127  		{"k8s:foo=bar:", NewLabel("foo", "bar:", "k8s")},
   128  		{"reservedz=host", NewLabel("reservedz", "host", LabelSourceUnspec)},
   129  		{":", NewLabel("", "", LabelSourceUnspec)},
   130  		{LabelSourceReservedKeyPrefix + "host", NewLabel("host", "", LabelSourceReserved)},
   131  	}
   132  	for _, test := range tests {
   133  		lbl := ParseLabel(test.str)
   134  		c.Assert(lbl, checker.DeepEquals, test.out)
   135  	}
   136  }
   137  
   138  func BenchmarkParseLabel(b *testing.B) {
   139  	tests := []struct {
   140  		str string
   141  		out Label
   142  	}{
   143  		{"source1:key1=value1", NewLabel("key1", "value1", "source1")},
   144  		{"key1=value1", NewLabel("key1", "value1", LabelSourceUnspec)},
   145  		{"value1", NewLabel("value1", "", LabelSourceUnspec)},
   146  		{"source1:key1", NewLabel("key1", "", "source1")},
   147  		{"source1:key1==value1", NewLabel("key1", "=value1", "source1")},
   148  		{"source::key1=value1", NewLabel("::key1", "value1", "source")},
   149  		{"$key1=value1", NewLabel("key1", "value1", LabelSourceReserved)},
   150  		{"1foo", NewLabel("1foo", "", LabelSourceUnspec)},
   151  		{":2foo", NewLabel("2foo", "", LabelSourceUnspec)},
   152  		{":3foo=", NewLabel("3foo", "", LabelSourceUnspec)},
   153  		{"reserved:=key", NewLabel("key", "", LabelSourceReserved)},
   154  		{"4blah=:foo=", NewLabel("foo", "", "4blah=")},
   155  		{"5blah::foo=", NewLabel("::foo", "", "5blah")},
   156  		{"6foo==", NewLabel("6foo", "=", LabelSourceUnspec)},
   157  		{"7foo=bar", NewLabel("7foo", "bar", LabelSourceUnspec)},
   158  		{"k8s:foo=bar:", NewLabel("foo", "bar:", "k8s")},
   159  		{"reservedz=host", NewLabel("reservedz", "host", LabelSourceUnspec)},
   160  		{":", NewLabel("", "", LabelSourceUnspec)},
   161  		{LabelSourceReservedKeyPrefix + "host", NewLabel("host", "", LabelSourceReserved)},
   162  	}
   163  	count := 0
   164  	b.ResetTimer()
   165  	for i := 0; i < b.N; i++ {
   166  		for _, test := range tests {
   167  			if ParseLabel(test.str).Source == LabelSourceUnspec {
   168  				count++
   169  			}
   170  		}
   171  	}
   172  }
   173  
   174  func (s *LabelsSuite) TestParseSelectLabel(c *C) {
   175  	tests := []struct {
   176  		str string
   177  		out Label
   178  	}{
   179  		{"source1:key1=value1", NewLabel("key1", "value1", "source1")},
   180  		{"key1=value1", NewLabel("key1", "value1", LabelSourceAny)},
   181  		{"value1", NewLabel("value1", "", LabelSourceAny)},
   182  		{"source1:key1", NewLabel("key1", "", "source1")},
   183  		{"source1:key1==value1", NewLabel("key1", "=value1", "source1")},
   184  		{"source::key1=value1", NewLabel("::key1", "value1", "source")},
   185  		{"$key1=value1", NewLabel("key1", "value1", LabelSourceReserved)},
   186  		{"1foo", NewLabel("1foo", "", LabelSourceAny)},
   187  		{":2foo", NewLabel("2foo", "", LabelSourceAny)},
   188  		{":3foo=", NewLabel("3foo", "", LabelSourceAny)},
   189  		{"4blah=:foo=", NewLabel("foo", "", "4blah=")},
   190  		{"5blah::foo=", NewLabel("::foo", "", "5blah")},
   191  		{"6foo==", NewLabel("6foo", "=", LabelSourceAny)},
   192  		{"7foo=bar", NewLabel("7foo", "bar", LabelSourceAny)},
   193  		{"k8s:foo=bar:", NewLabel("foo", "bar:", "k8s")},
   194  		{LabelSourceReservedKeyPrefix + "host", NewLabel("host", "", LabelSourceReserved)},
   195  	}
   196  	for _, test := range tests {
   197  		lbl := ParseSelectLabel(test.str)
   198  		c.Assert(lbl, checker.DeepEquals, test.out)
   199  	}
   200  }
   201  
   202  func (s *LabelsSuite) TestLabel(c *C) {
   203  	var label Label
   204  
   205  	longLabel := `{"source": "kubernetes", "key": "io.kubernetes.pod.name", "value": "foo"}`
   206  	invLabel := `{"source": "kubernetes", "value": "foo"}`
   207  	shortLabel := `"web"`
   208  
   209  	err := json.Unmarshal([]byte(longLabel), &label)
   210  	c.Assert(err, Equals, nil)
   211  	c.Assert(label.Source, Equals, "kubernetes")
   212  	c.Assert(label.Value, Equals, "foo")
   213  
   214  	label = Label{}
   215  	err = json.Unmarshal([]byte(invLabel), &label)
   216  	c.Assert(err, Not(Equals), nil)
   217  
   218  	label = Label{}
   219  	err = json.Unmarshal([]byte(shortLabel), &label)
   220  	c.Assert(err, Equals, nil)
   221  	c.Assert(label.Source, Equals, LabelSourceUnspec)
   222  	c.Assert(label.Value, Equals, "")
   223  
   224  	label = Label{}
   225  	err = json.Unmarshal([]byte(""), &label)
   226  	c.Assert(err, Not(Equals), nil)
   227  }
   228  
   229  func (s *LabelsSuite) TestLabelCompare(c *C) {
   230  	a1 := NewLabel(".", "", "")
   231  	a2 := NewLabel(".", "", "")
   232  	b1 := NewLabel("bar", "", LabelSourceUnspec)
   233  	c1 := NewLabel("bar", "", "kubernetes")
   234  	d1 := NewLabel("", "", "")
   235  
   236  	c.Assert(a1.Equals(&a2), Equals, true)
   237  	c.Assert(a2.Equals(&a1), Equals, true)
   238  	c.Assert(a1.Equals(&b1), Equals, false)
   239  	c.Assert(a1.Equals(&c1), Equals, false)
   240  	c.Assert(a1.Equals(&d1), Equals, false)
   241  	c.Assert(b1.Equals(&c1), Equals, false)
   242  }
   243  
   244  func (s *LabelsSuite) TestLabelParseKey(c *C) {
   245  	tests := []struct {
   246  		str string
   247  		out string
   248  	}{
   249  		{"source0:key0=value1", "source0.key0"},
   250  		{"source3:key1", "source3.key1"},
   251  		{"source4:key1==value1", "source4.key1"},
   252  		{"source::key1=value1", "source.:key1"},
   253  		{"4blah=:foo=", "4blah=.foo"},
   254  		{"5blah::foo=", "5blah.:foo"},
   255  		{"source2.key1=value1", DefaultLabelSourceKeyPrefix + "source2.key1"},
   256  		{"1foo", DefaultLabelSourceKeyPrefix + "1foo"},
   257  		{":2foo", DefaultLabelSourceKeyPrefix + "2foo"},
   258  		{":3foo=", DefaultLabelSourceKeyPrefix + "3foo"},
   259  		{"6foo==", DefaultLabelSourceKeyPrefix + "6foo"},
   260  		{"7foo=bar", DefaultLabelSourceKeyPrefix + "7foo"},
   261  		{"cilium.key1=value1", DefaultLabelSourceKeyPrefix + "cilium.key1"},
   262  		{"key1=value1", DefaultLabelSourceKeyPrefix + "key1"},
   263  		{"value1", DefaultLabelSourceKeyPrefix + "value1"},
   264  		{"$world=value1", LabelSourceReservedKeyPrefix + "world"},
   265  		{"k8s:foo=bar:", LabelSourceK8sKeyPrefix + "foo"},
   266  	}
   267  	for _, test := range tests {
   268  		lbl := GetExtendedKeyFrom(test.str)
   269  		c.Assert(lbl, checker.DeepEquals, test.out)
   270  	}
   271  }
   272  
   273  func (s *LabelsSuite) TestLabelsCompare(c *C) {
   274  	la11 := NewLabel("a", "1", "src1")
   275  	la12 := NewLabel("a", "1", "src2")
   276  	la22 := NewLabel("a", "2", "src2")
   277  	lb22 := NewLabel("b", "2", "src2")
   278  
   279  	lblsAll := Labels{la11.Key: la11, la12.Key: la12, la22.Key: la22, lb22.Key: lb22}
   280  	lblsFewer := Labels{la11.Key: la11, la12.Key: la12, la22.Key: la22}
   281  	lblsLa11 := Labels{la11.Key: la11}
   282  	lblsLa12 := Labels{la12.Key: la12}
   283  	lblsLa22 := Labels{la22.Key: la22}
   284  	lblsLb22 := Labels{lb22.Key: lb22}
   285  
   286  	c.Assert(lblsAll.Equals(lblsAll), Equals, true)
   287  	c.Assert(lblsAll.Equals(lblsFewer), Equals, false)
   288  	c.Assert(lblsFewer.Equals(lblsAll), Equals, false)
   289  	c.Assert(lblsLa11.Equals(lblsLa12), Equals, false)
   290  	c.Assert(lblsLa12.Equals(lblsLa11), Equals, false)
   291  	c.Assert(lblsLa12.Equals(lblsLa22), Equals, false)
   292  	c.Assert(lblsLa22.Equals(lblsLa12), Equals, false)
   293  	c.Assert(lblsLa22.Equals(lblsLb22), Equals, false)
   294  	c.Assert(lblsLb22.Equals(lblsLa22), Equals, false)
   295  }
   296  
   297  func TestLabels_GetFromSource(t *testing.T) {
   298  	type args struct {
   299  		source string
   300  	}
   301  	tests := []struct {
   302  		name string
   303  		l    Labels
   304  		args args
   305  		want Labels
   306  	}{
   307  		{
   308  			name: "should contain label with the given source",
   309  			l: Labels{
   310  				"foo":   NewLabel("foo", "bar", "my-source"),
   311  				"other": NewLabel("other", "bar", ""),
   312  			},
   313  			args: args{
   314  				source: "my-source",
   315  			},
   316  			want: Labels{
   317  				"foo": NewLabel("foo", "bar", "my-source"),
   318  			},
   319  		},
   320  		{
   321  			name: "should return an empty slice as there are not labels for the given source",
   322  			l: Labels{
   323  				"foo":   NewLabel("foo", "bar", "any"),
   324  				"other": NewLabel("other", "bar", ""),
   325  			},
   326  			args: args{
   327  				source: "my-source",
   328  			},
   329  			want: Labels{},
   330  		},
   331  	}
   332  	for _, tt := range tests {
   333  		t.Run(tt.name, func(t *testing.T) {
   334  			if got := tt.l.GetFromSource(tt.args.source); !reflect.DeepEqual(got, tt.want) {
   335  				t.Errorf("Labels.GetFromSource() = %v, want %v", got, tt.want)
   336  			}
   337  		})
   338  	}
   339  }