github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/api/selector_test.go (about)

     1  // Copyright 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 api
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  	"github.com/cilium/cilium/pkg/labels"
    25  
    26  	. "gopkg.in/check.v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	k8sLbls "k8s.io/apimachinery/pkg/labels"
    29  	"k8s.io/apimachinery/pkg/selection"
    30  )
    31  
    32  var _ = Suite(&PolicyAPITestSuite{})
    33  
    34  func (s *PolicyAPITestSuite) TestSelectsAllEndpoints(c *C) {
    35  
    36  	// Empty endpoint selector slice does NOT equate to a wildcard.
    37  	selectorSlice := EndpointSelectorSlice{}
    38  	c.Assert(selectorSlice.SelectsAllEndpoints(), Equals, false)
    39  
    40  	selectorSlice = EndpointSelectorSlice{WildcardEndpointSelector}
    41  	c.Assert(selectorSlice.SelectsAllEndpoints(), Equals, true)
    42  
    43  	// Entity "reserved:all" maps to WildcardEndpointSelector
    44  	selectorSlice = EntitySlice{EntityAll}.GetAsEndpointSelectors()
    45  	c.Assert(selectorSlice.SelectsAllEndpoints(), Equals, true)
    46  
    47  	// Slice that contains wildcard and other selectors still selects all endpoints.
    48  	selectorSlice = EndpointSelectorSlice{WildcardEndpointSelector, NewESFromLabels(labels.ParseSelectLabel("bar"))}
    49  	c.Assert(selectorSlice.SelectsAllEndpoints(), Equals, true)
    50  
    51  	selectorSlice = EndpointSelectorSlice{NewESFromLabels(labels.ParseSelectLabel("bar")), NewESFromLabels(labels.ParseSelectLabel("foo"))}
    52  	c.Assert(selectorSlice.SelectsAllEndpoints(), Equals, false)
    53  }
    54  
    55  func (s *PolicyAPITestSuite) TestLabelSelectorToRequirements(c *C) {
    56  	labelSelector := &metav1.LabelSelector{
    57  		MatchLabels: map[string]string{
    58  			"any.foo": "bar",
    59  			"k8s.baz": "alice",
    60  		},
    61  		MatchExpressions: []metav1.LabelSelectorRequirement{
    62  			{
    63  				Key:      "any.foo",
    64  				Operator: "NotIn",
    65  				Values:   []string{"default"},
    66  			},
    67  		},
    68  	}
    69  
    70  	expRequirements := k8sLbls.Requirements{}
    71  	req, err := k8sLbls.NewRequirement("any.foo", selection.Equals, []string{"bar"})
    72  	c.Assert(err, IsNil)
    73  	expRequirements = append(expRequirements, *req)
    74  	req, err = k8sLbls.NewRequirement("any.foo", selection.NotIn, []string{"default"})
    75  	c.Assert(err, IsNil)
    76  	expRequirements = append(expRequirements, *req)
    77  	req, err = k8sLbls.NewRequirement("k8s.baz", selection.Equals, []string{"alice"})
    78  	c.Assert(err, IsNil)
    79  	expRequirements = append(expRequirements, *req)
    80  
    81  	c.Assert(labelSelectorToRequirements(labelSelector), checker.DeepEquals, &expRequirements)
    82  }
    83  
    84  func benchmarkMatchesSetup(match string, count int) (EndpointSelector, labels.LabelArray) {
    85  	stringLabels := []string{}
    86  	for i := 0; i < count; i++ {
    87  		stringLabels = append(stringLabels, fmt.Sprintf("%d", i))
    88  	}
    89  	lbls := labels.NewLabelsFromModel(stringLabels)
    90  	return NewESFromLabels(lbls.ToSlice()...), labels.ParseLabelArray(match)
    91  }
    92  
    93  func BenchmarkMatchesValid1000(b *testing.B) {
    94  	es, match := benchmarkMatchesSetup("42", 1000)
    95  	b.ResetTimer()
    96  	for i := 0; i < b.N; i++ {
    97  		es.Matches(match)
    98  	}
    99  }
   100  
   101  func BenchmarkMatchesInvalid1000(b *testing.B) {
   102  	es, match := benchmarkMatchesSetup("foo", 1000)
   103  	b.ResetTimer()
   104  	for i := 0; i < b.N; i++ {
   105  		es.Matches(match)
   106  	}
   107  }
   108  
   109  func BenchmarkMatchesValid1000Parallel(b *testing.B) {
   110  	es, match := benchmarkMatchesSetup("42", 1000)
   111  	b.RunParallel(func(pb *testing.PB) {
   112  		for pb.Next() {
   113  			es.Matches(match)
   114  		}
   115  	})
   116  }
   117  
   118  func BenchmarkMatchesInvalid1000Parallel(b *testing.B) {
   119  	es, match := benchmarkMatchesSetup("foo", 1000)
   120  	b.RunParallel(func(pb *testing.PB) {
   121  		for pb.Next() {
   122  			es.Matches(match)
   123  		}
   124  	})
   125  }