github.com/imran-kn/cilium-fork@v1.6.9/pkg/envoy/sort_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 envoy
    18  
    19  import (
    20  	"github.com/cilium/cilium/pkg/checker"
    21  
    22  	"github.com/cilium/proxy/go/cilium/api"
    23  	envoy_api_v2_core "github.com/cilium/proxy/go/envoy/api/v2/core"
    24  	envoy_api_v2_route "github.com/cilium/proxy/go/envoy/api/v2/route"
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  type SortSuite struct{}
    29  
    30  var _ = Suite(&SortSuite{})
    31  
    32  var HeaderMatcher1 = &envoy_api_v2_route.HeaderMatcher{
    33  	Name:                 "aaa",
    34  	HeaderMatchSpecifier: &envoy_api_v2_route.HeaderMatcher_RegexMatch{RegexMatch: "aaa"},
    35  }
    36  
    37  var HeaderMatcher2 = &envoy_api_v2_route.HeaderMatcher{
    38  	Name:                 "bbb",
    39  	HeaderMatchSpecifier: &envoy_api_v2_route.HeaderMatcher_RegexMatch{RegexMatch: "aaa"},
    40  }
    41  
    42  var HeaderMatcher3 = &envoy_api_v2_route.HeaderMatcher{
    43  	Name:                 "bbb",
    44  	HeaderMatchSpecifier: &envoy_api_v2_route.HeaderMatcher_RegexMatch{RegexMatch: "bbb"},
    45  }
    46  
    47  var HeaderMatcher4 = &envoy_api_v2_route.HeaderMatcher{
    48  	Name:                 "bbb",
    49  	HeaderMatchSpecifier: &envoy_api_v2_route.HeaderMatcher_RegexMatch{RegexMatch: "bbb"},
    50  }
    51  
    52  func (s *SortSuite) TestSortHeaderMatchers(c *C) {
    53  	var slice, expected []*envoy_api_v2_route.HeaderMatcher
    54  
    55  	slice = []*envoy_api_v2_route.HeaderMatcher{
    56  		HeaderMatcher4,
    57  		HeaderMatcher3,
    58  		HeaderMatcher2,
    59  		HeaderMatcher1,
    60  	}
    61  	expected = []*envoy_api_v2_route.HeaderMatcher{
    62  		HeaderMatcher1,
    63  		HeaderMatcher2,
    64  		HeaderMatcher3,
    65  		HeaderMatcher4,
    66  	}
    67  	SortHeaderMatchers(slice)
    68  	c.Assert(slice, checker.DeepEquals, expected)
    69  }
    70  
    71  var HTTPNetworkPolicyRule1 = &cilium.HttpNetworkPolicyRule{}
    72  
    73  var HTTPNetworkPolicyRule2 = &cilium.HttpNetworkPolicyRule{
    74  	Headers: []*envoy_api_v2_route.HeaderMatcher{HeaderMatcher1},
    75  }
    76  
    77  var HTTPNetworkPolicyRule3 = &cilium.HttpNetworkPolicyRule{
    78  	Headers: []*envoy_api_v2_route.HeaderMatcher{HeaderMatcher1, HeaderMatcher2},
    79  }
    80  
    81  var HTTPNetworkPolicyRule4 = &cilium.HttpNetworkPolicyRule{
    82  	Headers: []*envoy_api_v2_route.HeaderMatcher{HeaderMatcher1, HeaderMatcher3},
    83  }
    84  
    85  func (s *SortSuite) TestSortHttpNetworkPolicyRules(c *C) {
    86  	var slice, expected []*cilium.HttpNetworkPolicyRule
    87  
    88  	slice = []*cilium.HttpNetworkPolicyRule{
    89  		HTTPNetworkPolicyRule4,
    90  		HTTPNetworkPolicyRule3,
    91  		HTTPNetworkPolicyRule2,
    92  		HTTPNetworkPolicyRule1,
    93  	}
    94  	expected = []*cilium.HttpNetworkPolicyRule{
    95  		HTTPNetworkPolicyRule1,
    96  		HTTPNetworkPolicyRule2,
    97  		HTTPNetworkPolicyRule3,
    98  		HTTPNetworkPolicyRule4,
    99  	}
   100  	SortHTTPNetworkPolicyRules(slice)
   101  	c.Assert(slice, checker.DeepEquals, expected)
   102  }
   103  
   104  var PortNetworkPolicyRule1 = &cilium.PortNetworkPolicyRule{
   105  	RemotePolicies: nil,
   106  	L7:             nil,
   107  }
   108  
   109  var PortNetworkPolicyRule2 = &cilium.PortNetworkPolicyRule{
   110  	RemotePolicies: []uint64{1},
   111  	L7:             nil,
   112  }
   113  
   114  var PortNetworkPolicyRule3 = &cilium.PortNetworkPolicyRule{
   115  	RemotePolicies: []uint64{1, 2},
   116  	L7:             nil,
   117  }
   118  
   119  var PortNetworkPolicyRule4 = &cilium.PortNetworkPolicyRule{
   120  	RemotePolicies: nil,
   121  	L7: &cilium.PortNetworkPolicyRule_HttpRules{
   122  		HttpRules: &cilium.HttpNetworkPolicyRules{
   123  			HttpRules: []*cilium.HttpNetworkPolicyRule{
   124  				HTTPNetworkPolicyRule1,
   125  			},
   126  		},
   127  	},
   128  }
   129  
   130  var PortNetworkPolicyRule5 = &cilium.PortNetworkPolicyRule{
   131  	RemotePolicies: []uint64{1, 2},
   132  	L7: &cilium.PortNetworkPolicyRule_HttpRules{
   133  		HttpRules: &cilium.HttpNetworkPolicyRules{
   134  			HttpRules: []*cilium.HttpNetworkPolicyRule{
   135  				HTTPNetworkPolicyRule1,
   136  			},
   137  		},
   138  	},
   139  }
   140  
   141  var PortNetworkPolicyRule6 = &cilium.PortNetworkPolicyRule{
   142  	RemotePolicies: []uint64{1, 2},
   143  	L7: &cilium.PortNetworkPolicyRule_HttpRules{
   144  		HttpRules: &cilium.HttpNetworkPolicyRules{
   145  			HttpRules: []*cilium.HttpNetworkPolicyRule{
   146  				HTTPNetworkPolicyRule1,
   147  				HTTPNetworkPolicyRule2,
   148  			},
   149  		},
   150  	},
   151  }
   152  
   153  var PortNetworkPolicyRule7 = &cilium.PortNetworkPolicyRule{
   154  	RemotePolicies: []uint64{1, 2},
   155  	L7: &cilium.PortNetworkPolicyRule_HttpRules{
   156  		HttpRules: &cilium.HttpNetworkPolicyRules{
   157  			HttpRules: []*cilium.HttpNetworkPolicyRule{
   158  				HTTPNetworkPolicyRule1,
   159  				HTTPNetworkPolicyRule3,
   160  			},
   161  		},
   162  	},
   163  }
   164  
   165  // TODO: Test sorting Kafka rules.
   166  
   167  func (s *SortSuite) TestSortPortNetworkPolicyRules(c *C) {
   168  	var slice, expected []*cilium.PortNetworkPolicyRule
   169  
   170  	slice = []*cilium.PortNetworkPolicyRule{
   171  		PortNetworkPolicyRule7,
   172  		PortNetworkPolicyRule6,
   173  		PortNetworkPolicyRule5,
   174  		PortNetworkPolicyRule4,
   175  		PortNetworkPolicyRule3,
   176  		PortNetworkPolicyRule2,
   177  		PortNetworkPolicyRule1,
   178  	}
   179  	expected = []*cilium.PortNetworkPolicyRule{
   180  		PortNetworkPolicyRule1,
   181  		PortNetworkPolicyRule2,
   182  		PortNetworkPolicyRule3,
   183  		PortNetworkPolicyRule4,
   184  		PortNetworkPolicyRule5,
   185  		PortNetworkPolicyRule6,
   186  		PortNetworkPolicyRule7,
   187  	}
   188  	SortPortNetworkPolicyRules(slice)
   189  	c.Assert(slice, checker.DeepEquals, expected)
   190  }
   191  
   192  var PortNetworkPolicy1 = &cilium.PortNetworkPolicy{
   193  	Protocol: envoy_api_v2_core.SocketAddress_TCP,
   194  	Port:     10001,
   195  	Rules:    nil,
   196  }
   197  
   198  var PortNetworkPolicy2 = &cilium.PortNetworkPolicy{
   199  	Protocol: envoy_api_v2_core.SocketAddress_UDP,
   200  	Port:     10001,
   201  	Rules:    nil,
   202  }
   203  
   204  var PortNetworkPolicy3 = &cilium.PortNetworkPolicy{
   205  	Protocol: envoy_api_v2_core.SocketAddress_UDP,
   206  	Port:     10002,
   207  	Rules:    nil,
   208  }
   209  
   210  var PortNetworkPolicy4 = &cilium.PortNetworkPolicy{
   211  	Protocol: envoy_api_v2_core.SocketAddress_UDP,
   212  	Port:     10002,
   213  	Rules: []*cilium.PortNetworkPolicyRule{
   214  		PortNetworkPolicyRule1,
   215  	},
   216  }
   217  
   218  var PortNetworkPolicy5 = &cilium.PortNetworkPolicy{
   219  	Protocol: envoy_api_v2_core.SocketAddress_UDP,
   220  	Port:     10002,
   221  	Rules: []*cilium.PortNetworkPolicyRule{
   222  		PortNetworkPolicyRule1,
   223  		PortNetworkPolicyRule2,
   224  	},
   225  }
   226  
   227  var PortNetworkPolicy6 = &cilium.PortNetworkPolicy{
   228  	Protocol: envoy_api_v2_core.SocketAddress_UDP,
   229  	Port:     10002,
   230  	Rules: []*cilium.PortNetworkPolicyRule{
   231  		PortNetworkPolicyRule1,
   232  		PortNetworkPolicyRule3,
   233  	},
   234  }
   235  
   236  func (s *SortSuite) TestSortPortNetworkPolicies(c *C) {
   237  	var slice, expected []*cilium.PortNetworkPolicy
   238  
   239  	slice = []*cilium.PortNetworkPolicy{
   240  		PortNetworkPolicy6,
   241  		PortNetworkPolicy5,
   242  		PortNetworkPolicy4,
   243  		PortNetworkPolicy3,
   244  		PortNetworkPolicy2,
   245  		PortNetworkPolicy1,
   246  	}
   247  	expected = []*cilium.PortNetworkPolicy{
   248  		PortNetworkPolicy1,
   249  		PortNetworkPolicy2,
   250  		PortNetworkPolicy3,
   251  		PortNetworkPolicy4,
   252  		PortNetworkPolicy5,
   253  		PortNetworkPolicy6,
   254  	}
   255  	SortPortNetworkPolicies(slice)
   256  	c.Assert(slice, checker.DeepEquals, expected)
   257  }