github.phpd.cn/cilium/cilium@v1.6.12/pkg/identity/identity_test.go (about) 1 // Copyright 2016-2018 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 identity 18 19 import ( 20 "net" 21 "testing" 22 23 "github.com/cilium/cilium/pkg/checker" 24 "github.com/cilium/cilium/pkg/labels" 25 "github.com/cilium/cilium/pkg/labels/cidr" 26 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 IdentityTestSuite struct{} 36 37 var _ = Suite(&IdentityTestSuite{}) 38 39 func (s *IdentityTestSuite) TestReservedID(c *C) { 40 i := GetReservedID("host") 41 c.Assert(i, Equals, NumericIdentity(1)) 42 c.Assert(i.String(), Equals, "host") 43 44 i = GetReservedID("world") 45 c.Assert(i, Equals, NumericIdentity(2)) 46 c.Assert(i.String(), Equals, "world") 47 48 // This is an obsoleted identity, we verify that it returns 0 49 i = GetReservedID("cluster") 50 c.Assert(i, Equals, NumericIdentity(0)) 51 c.Assert(i.String(), Equals, "0") 52 53 i = GetReservedID("health") 54 c.Assert(i, Equals, NumericIdentity(4)) 55 c.Assert(i.String(), Equals, "health") 56 57 i = GetReservedID("init") 58 c.Assert(i, Equals, NumericIdentity(5)) 59 c.Assert(i.String(), Equals, "init") 60 61 i = GetReservedID("unmanaged") 62 c.Assert(i, Equals, NumericIdentity(3)) 63 c.Assert(i.String(), Equals, "unmanaged") 64 65 c.Assert(GetReservedID("unknown"), Equals, IdentityUnknown) 66 unknown := NumericIdentity(700) 67 c.Assert(unknown.String(), Equals, "700") 68 } 69 70 func (s *IdentityTestSuite) TestIsReservedIdentity(c *C) { 71 c.Assert(ReservedIdentityHealth.IsReservedIdentity(), Equals, true) 72 c.Assert(ReservedIdentityHost.IsReservedIdentity(), Equals, true) 73 c.Assert(ReservedIdentityWorld.IsReservedIdentity(), Equals, true) 74 c.Assert(ReservedIdentityInit.IsReservedIdentity(), Equals, true) 75 c.Assert(ReservedIdentityUnmanaged.IsReservedIdentity(), Equals, true) 76 77 c.Assert(NumericIdentity(123456).IsReservedIdentity(), Equals, false) 78 } 79 80 func (s *IdentityTestSuite) TestRequiresGlobalIdentity(c *C) { 81 _, ipnet, err := net.ParseCIDR("0.0.0.0/0") 82 c.Assert(err, IsNil) 83 c.Assert(RequiresGlobalIdentity(cidr.GetCIDRLabels(ipnet)), Equals, false) 84 85 _, ipnet, err = net.ParseCIDR("192.168.23.0/24") 86 c.Assert(err, IsNil) 87 c.Assert(RequiresGlobalIdentity(cidr.GetCIDRLabels(ipnet)), Equals, false) 88 89 c.Assert(RequiresGlobalIdentity(labels.NewLabelsFromModel([]string{"k8s:foo=bar"})), Equals, true) 90 } 91 92 func (s *IdentityTestSuite) TestNewIdentityFromLabelArray(c *C) { 93 id := NewIdentityFromLabelArray(NumericIdentity(1001), 94 labels.NewLabelArrayFromSortedList("unspec:a=;unspec:b;unspec:c=d")) 95 96 lbls := labels.Labels{ 97 "a": labels.ParseLabel("a"), 98 "c": labels.ParseLabel("c=d"), 99 "b": labels.ParseLabel("b"), 100 } 101 c.Assert(id.ID, Equals, NumericIdentity(1001)) 102 c.Assert(id.Labels, checker.DeepEquals, lbls) 103 c.Assert(id.LabelArray, checker.DeepEquals, lbls.LabelArray()) 104 }