github.phpd.cn/cilium/cilium@v1.6.12/pkg/labels/filter_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  	"github.com/cilium/cilium/pkg/checker"
    21  	k8sConst "github.com/cilium/cilium/pkg/k8s/apis/cilium.io"
    22  
    23  	. "gopkg.in/check.v1"
    24  )
    25  
    26  // Hook up gocheck into the "go test" runner.
    27  type LabelsPrefCfgSuite struct{}
    28  
    29  var _ = Suite(&LabelsPrefCfgSuite{})
    30  
    31  func (s *LabelsPrefCfgSuite) TestFilterLabels(c *C) {
    32  	wanted := Labels{
    33  		"id.lizards":                  NewLabel("id.lizards", "web", LabelSourceContainer),
    34  		"id.lizards.k8s":              NewLabel("id.lizards.k8s", "web", LabelSourceK8s),
    35  		"io.kubernetes.pod.namespace": NewLabel("io.kubernetes.pod.namespace", "default", LabelSourceContainer),
    36  		"app.kubernetes.io":           NewLabel("app.kubernetes.io", "my-nginx", LabelSourceContainer),
    37  	}
    38  
    39  	dlpcfg := defaultLabelPrefixCfg()
    40  	d, err := parseLabelPrefix(":!ignor[eE]")
    41  	c.Assert(err, IsNil)
    42  	c.Assert(d, Not(IsNil))
    43  	dlpcfg.LabelPrefixes = append(dlpcfg.LabelPrefixes, d)
    44  	d, err = parseLabelPrefix("id.*")
    45  	c.Assert(err, IsNil)
    46  	c.Assert(d, Not(IsNil))
    47  	dlpcfg.LabelPrefixes = append(dlpcfg.LabelPrefixes, d)
    48  	allNormalLabels := map[string]string{
    49  		"io.kubernetes.container.hash":                              "cf58006d",
    50  		"io.kubernetes.container.name":                              "POD",
    51  		"io.kubernetes.container.restartCount":                      "0",
    52  		"io.kubernetes.container.terminationMessagePath":            "",
    53  		"io.kubernetes.pod.name":                                    "my-nginx-3800858182-07i3n",
    54  		"io.kubernetes.pod.namespace":                               "default",
    55  		"app.kubernetes.io":                                         "my-nginx",
    56  		"kubernetes.io.foo":                                         "foo",
    57  		"beta.kubernetes.io.foo":                                    "foo",
    58  		"annotation.kubectl.kubernetes.io":                          "foo",
    59  		"annotation.hello":                                          "world",
    60  		"annotation." + k8sConst.CiliumIdentityAnnotationDeprecated: "12356",
    61  		"io.kubernetes.pod.terminationGracePeriod":                  "30",
    62  		"io.kubernetes.pod.uid":                                     "c2e22414-dfc3-11e5-9792-080027755f5a",
    63  		"ignore":                                                    "foo",
    64  		"ignorE":                                                    "foo",
    65  		"annotation.kubernetes.io/config.seen":                      "2017-05-30T14:22:17.691491034Z",
    66  		"controller-revision-hash":                                  "123456",
    67  	}
    68  	allLabels := Map2Labels(allNormalLabels, LabelSourceContainer)
    69  	filtered, _ := dlpcfg.filterLabels(allLabels)
    70  	c.Assert(len(filtered), Equals, 2)
    71  	allLabels["id.lizards"] = NewLabel("id.lizards", "web", LabelSourceContainer)
    72  	allLabels["id.lizards.k8s"] = NewLabel("id.lizards.k8s", "web", LabelSourceK8s)
    73  	filtered, _ = dlpcfg.filterLabels(allLabels)
    74  	c.Assert(len(filtered), Equals, 4)
    75  	c.Assert(filtered, checker.DeepEquals, wanted)
    76  	// Making sure we are deep copying the labels
    77  	allLabels["id.lizards"] = NewLabel("id.lizards", "web", "I can change this and doesn't affect any one")
    78  	c.Assert(filtered, checker.DeepEquals, wanted)
    79  }