github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/api/entity_test.go (about) 1 // Copyright 2018-2019 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 api 18 19 import ( 20 "fmt" 21 22 k8sapi "github.com/cilium/cilium/pkg/k8s/apis/cilium.io" 23 "github.com/cilium/cilium/pkg/labels" 24 25 . "gopkg.in/check.v1" 26 ) 27 28 // matches returns true if the entity matches the labels 29 func (e Entity) matches(ctx labels.LabelArray) bool { 30 return EntitySlice{e}.matches(ctx) 31 } 32 33 // matches returns true if any of the entities in the slice match the labels 34 func (s EntitySlice) matches(ctx labels.LabelArray) bool { 35 return s.GetAsEndpointSelectors().Matches(ctx) 36 } 37 38 func (s *PolicyAPITestSuite) TestEntityMatches(c *C) { 39 InitEntities("cluster1") 40 41 c.Assert(EntityHost.matches(labels.ParseLabelArray("reserved:host")), Equals, true) 42 c.Assert(EntityHost.matches(labels.ParseLabelArray("reserved:host", "id:foo")), Equals, true) 43 c.Assert(EntityHost.matches(labels.ParseLabelArray("reserved:world")), Equals, false) 44 c.Assert(EntityHost.matches(labels.ParseLabelArray("reserved:unmanaged")), Equals, false) 45 c.Assert(EntityHost.matches(labels.ParseLabelArray("reserved:none")), Equals, false) 46 c.Assert(EntityHost.matches(labels.ParseLabelArray("id=foo")), Equals, false) 47 48 c.Assert(EntityAll.matches(labels.ParseLabelArray("reserved:host")), Equals, true) 49 c.Assert(EntityAll.matches(labels.ParseLabelArray("reserved:world")), Equals, true) 50 c.Assert(EntityAll.matches(labels.ParseLabelArray("reserved:unmanaged")), Equals, true) 51 c.Assert(EntityAll.matches(labels.ParseLabelArray("reserved:none")), Equals, true) // in a white-list model, All trumps None 52 c.Assert(EntityAll.matches(labels.ParseLabelArray("id=foo")), Equals, true) 53 54 c.Assert(EntityCluster.matches(labels.ParseLabelArray("reserved:host")), Equals, true) 55 c.Assert(EntityCluster.matches(labels.ParseLabelArray("reserved:init")), Equals, true) 56 c.Assert(EntityCluster.matches(labels.ParseLabelArray("reserved:unmanaged")), Equals, true) 57 c.Assert(EntityCluster.matches(labels.ParseLabelArray("reserved:world")), Equals, false) 58 c.Assert(EntityCluster.matches(labels.ParseLabelArray("reserved:none")), Equals, false) 59 60 clusterLabel := fmt.Sprintf("k8s:%s=%s", k8sapi.PolicyLabelCluster, "cluster1") 61 c.Assert(EntityCluster.matches(labels.ParseLabelArray(clusterLabel, "id=foo")), Equals, true) 62 c.Assert(EntityCluster.matches(labels.ParseLabelArray(clusterLabel, "id=foo", "id=bar")), Equals, true) 63 c.Assert(EntityCluster.matches(labels.ParseLabelArray("id=foo")), Equals, false) 64 65 c.Assert(EntityWorld.matches(labels.ParseLabelArray("reserved:host")), Equals, false) 66 c.Assert(EntityWorld.matches(labels.ParseLabelArray("reserved:world")), Equals, true) 67 c.Assert(EntityWorld.matches(labels.ParseLabelArray("reserved:unmanaged")), Equals, false) 68 c.Assert(EntityWorld.matches(labels.ParseLabelArray("reserved:none")), Equals, false) 69 c.Assert(EntityWorld.matches(labels.ParseLabelArray("id=foo")), Equals, false) 70 c.Assert(EntityWorld.matches(labels.ParseLabelArray("id=foo", "id=bar")), Equals, false) 71 72 c.Assert(EntityNone.matches(labels.ParseLabelArray("reserved:host")), Equals, false) 73 c.Assert(EntityNone.matches(labels.ParseLabelArray("reserved:world")), Equals, false) 74 c.Assert(EntityNone.matches(labels.ParseLabelArray("reserved:unmanaged")), Equals, false) 75 c.Assert(EntityNone.matches(labels.ParseLabelArray("reserved:init")), Equals, false) 76 c.Assert(EntityNone.matches(labels.ParseLabelArray("id=foo")), Equals, false) 77 c.Assert(EntityNone.matches(labels.ParseLabelArray(clusterLabel, "id=foo", "id=bar")), Equals, false) 78 79 } 80 81 func (s *PolicyAPITestSuite) TestEntitySliceMatches(c *C) { 82 InitEntities("cluster1") 83 84 slice := EntitySlice{EntityHost, EntityWorld} 85 c.Assert(slice.matches(labels.ParseLabelArray("reserved:host")), Equals, true) 86 c.Assert(slice.matches(labels.ParseLabelArray("reserved:world")), Equals, true) 87 c.Assert(slice.matches(labels.ParseLabelArray("reserved:unmanaged")), Equals, false) 88 c.Assert(slice.matches(labels.ParseLabelArray("reserved:none")), Equals, false) 89 c.Assert(slice.matches(labels.ParseLabelArray("id=foo")), Equals, false) 90 }