github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/apis/cilium.io/v2/register_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 v2
    18  
    19  import (
    20  	"regexp"
    21  
    22  	. "gopkg.in/check.v1"
    23  	apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  type CiliumV2RegisterSuite struct{}
    28  
    29  var _ = Suite(&CiliumV2RegisterSuite{})
    30  
    31  func (s *CiliumV2RegisterSuite) getTestUpToDateDefinition() *apiextensionsv1beta1.CustomResourceDefinition {
    32  	return &apiextensionsv1beta1.CustomResourceDefinition{
    33  		ObjectMeta: metav1.ObjectMeta{
    34  			Labels: map[string]string{
    35  				CustomResourceDefinitionSchemaVersionKey: CustomResourceDefinitionSchemaVersion,
    36  			},
    37  		},
    38  		Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
    39  			Validation: &CNPCRV,
    40  		},
    41  	}
    42  }
    43  
    44  func (s *CiliumV2RegisterSuite) TestNeedsUpdateNoValidation(c *C) {
    45  	crd := s.getTestUpToDateDefinition()
    46  
    47  	crd.Spec.Validation = nil
    48  
    49  	c.Assert(needsUpdate(crd), Equals, true)
    50  }
    51  
    52  func (s *CiliumV2RegisterSuite) TestNeedsUpdateNoLabels(c *C) {
    53  	crd := s.getTestUpToDateDefinition()
    54  
    55  	crd.Labels = nil
    56  
    57  	c.Assert(needsUpdate(crd), Equals, true)
    58  }
    59  
    60  func (s *CiliumV2RegisterSuite) TestNeedsUpdateNoVersionLabel(c *C) {
    61  	crd := s.getTestUpToDateDefinition()
    62  
    63  	crd.Labels = map[string]string{"test": "test"}
    64  
    65  	c.Assert(needsUpdate(crd), Equals, true)
    66  }
    67  
    68  func (s *CiliumV2RegisterSuite) TestNeedsUpdateOlderVersion(c *C) {
    69  	crd := s.getTestUpToDateDefinition()
    70  
    71  	crd.Labels[CustomResourceDefinitionSchemaVersionKey] = "0.9"
    72  
    73  	c.Assert(needsUpdate(crd), Equals, true)
    74  }
    75  
    76  func (s *CiliumV2RegisterSuite) TestNeedsUpdateCorruptedVersion(c *C) {
    77  	crd := s.getTestUpToDateDefinition()
    78  
    79  	crd.Labels[CustomResourceDefinitionSchemaVersionKey] = "totally-not-semver"
    80  
    81  	c.Assert(needsUpdate(crd), Equals, true)
    82  }
    83  
    84  func (s *CiliumV2RegisterSuite) TestCIDRRegex(c *C) {
    85  	goodCIDRs := []string{
    86  		"192.0.2.3/32",
    87  		"192.0.2.0/24",
    88  		"0.0.0.0/0",
    89  		"::/0",
    90  		"::cafe/128",
    91  		"::f00d:cafe/128",
    92  		"0:0:0:0:0:0:0:cafe/128",
    93  		"cafe:cafe:cafe:cafe:cafe:cafe:cafe:cafe/128",
    94  		"bad:f00d:cafe:0:0:0:0:add/64",
    95  		"bad:f00d:cafe::bad/64",
    96  		"f00d::/64",
    97  		"f00d::0:0/120",
    98  		"f00d:cafe::1:2/120",
    99  	}
   100  
   101  continueTest:
   102  	for _, input := range goodCIDRs {
   103  		for _, prop := range CIDR.OneOf {
   104  			matched, err := regexp.MatchString(prop.Pattern, input)
   105  			c.Assert(err, IsNil)
   106  			if matched {
   107  				continue continueTest
   108  			}
   109  		}
   110  		// The below is always false, valid CIDR prefixes should
   111  		// always skip this by continuing in the above loop.
   112  		c.Assert(input, Equals, "failed to match CIDR.OneOf[*].Pattern")
   113  	}
   114  
   115  	badCIDRs := []string{
   116  		"192.0.2.3",
   117  		"192.0.2.3/",
   118  		"abcdef",
   119  		"0.0.0.0/0/0",
   120  		"::",
   121  		":",
   122  		":/",
   123  		"0:0",
   124  		"::cafe/128/12",
   125  		"abc:def",
   126  		"abc:def/64",
   127  		"f00d::/",
   128  		"f00d::0:0",
   129  		"bad.f00d.cafe.0.0.0.0.add/20",
   130  		"::192.0.2.3/128",
   131  		"::ffff:192.0.2.3/128",
   132  		"abcd:192.0.2.3/128",
   133  		":abcd:192.0.2.3/128",
   134  		"abcd::192.0.2.3/128",
   135  		":abcd::192.0.2.3/128",
   136  		"bad::f00d::cafe/1",
   137  		":bad::f00d::cafe/1",
   138  		"::bad::f00d::cafe/1",
   139  		"::bad::food::cafe/1",
   140  	}
   141  
   142  	for _, input := range badCIDRs {
   143  		for _, prop := range CIDR.OneOf {
   144  			matched, err := regexp.MatchString(prop.Pattern, input)
   145  			c.Assert(err, IsNil)
   146  			if matched {
   147  				// The below is always false, invalid CIDR
   148  				// prefixes are not supposed to match the regex.
   149  				c.Assert(input, Equals, "unexpectedly matched CIDR.OneOf[*].Pattern")
   150  			}
   151  		}
   152  	}
   153  }
   154  
   155  func (s *CiliumV2RegisterSuite) TestFQDNNameRegex(c *C) {
   156  	nameRegex := regexp.MustCompile(fqdnNameRegex)
   157  	patternRegex := regexp.MustCompile(fqdnPatternRegex)
   158  
   159  	badFqdns := []string{
   160  		"%%",
   161  		"",
   162  		"😀.com",
   163  	}
   164  
   165  	goodFqdns := []string{
   166  		"cilium.io",
   167  		"cilium.io.",
   168  		"www.xn--e28h.com",
   169  	}
   170  
   171  	badFqdnPatterns := []string{
   172  		"%$*.*",
   173  		"",
   174  	}
   175  
   176  	goodFqdnPatterns := []string{
   177  		"*.cilium.io",
   178  		"*.cilium.io.*",
   179  		"*.cilium.io.*.",
   180  		"*.xn--e28h.com",
   181  	}
   182  
   183  	for _, f := range badFqdns {
   184  		c.Assert(nameRegex.MatchString(f), Equals, false, Commentf(f))
   185  		c.Assert(patternRegex.MatchString(f), Equals, false, Commentf(f))
   186  	}
   187  
   188  	for _, f := range goodFqdns {
   189  		c.Assert(nameRegex.MatchString(f), Equals, true, Commentf(f))
   190  		c.Assert(patternRegex.MatchString(f), Equals, true, Commentf(f))
   191  	}
   192  
   193  	for _, f := range badFqdnPatterns {
   194  		c.Assert(nameRegex.MatchString(f), Equals, false, Commentf(f))
   195  		c.Assert(patternRegex.MatchString(f), Equals, false, Commentf(f))
   196  	}
   197  
   198  	for _, f := range goodFqdnPatterns {
   199  		c.Assert(nameRegex.MatchString(f), Equals, false, Commentf(f))
   200  		c.Assert(patternRegex.MatchString(f), Equals, true, Commentf(f))
   201  	}
   202  }