github.com/elfadel/cilium@v1.6.12/pkg/service/store_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 service
    18  
    19  import (
    20  	"github.com/cilium/cilium/pkg/checker"
    21  	"github.com/cilium/cilium/pkg/loadbalancer"
    22  
    23  	"gopkg.in/check.v1"
    24  )
    25  
    26  type ServiceGenericSuite struct{}
    27  
    28  var _ = check.Suite(&ServiceGenericSuite{})
    29  
    30  func (s *ServiceGenericSuite) TestClusterService(c *check.C) {
    31  	svc := NewClusterService("foo", "bar")
    32  	svc.Cluster = "default"
    33  
    34  	c.Assert(svc.Name, check.Equals, "foo")
    35  	c.Assert(svc.Namespace, check.Equals, "bar")
    36  
    37  	c.Assert(svc.String(), check.Equals, "default/bar/foo")
    38  
    39  	b, err := svc.Marshal()
    40  	c.Assert(err, check.IsNil)
    41  
    42  	unmarshal := ClusterService{}
    43  	err = unmarshal.Unmarshal(b)
    44  	c.Assert(err, check.IsNil)
    45  	c.Assert(svc, checker.DeepEquals, unmarshal)
    46  
    47  	c.Assert(svc.GetKeyName(), check.Equals, "default/bar/foo")
    48  }
    49  
    50  func (s *ServiceGenericSuite) TestPortConfigurationDeepEqual(c *check.C) {
    51  	tests := []struct {
    52  		a    PortConfiguration
    53  		b    PortConfiguration
    54  		want bool
    55  	}{
    56  
    57  		{
    58  			a: PortConfiguration{
    59  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    60  			},
    61  			b: PortConfiguration{
    62  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    63  			},
    64  			want: true,
    65  		},
    66  		{
    67  			a: PortConfiguration{
    68  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    69  			},
    70  			b: PortConfiguration{
    71  				"foz": {Protocol: loadbalancer.NONE, Port: 1},
    72  			},
    73  			want: false,
    74  		},
    75  		{
    76  			a: PortConfiguration{
    77  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    78  			},
    79  			b: PortConfiguration{
    80  				"foo": {Protocol: loadbalancer.NONE, Port: 2},
    81  			},
    82  			want: false,
    83  		},
    84  		{
    85  			a: PortConfiguration{
    86  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    87  			},
    88  			b: PortConfiguration{
    89  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    90  				"baz": {Protocol: loadbalancer.NONE, Port: 2},
    91  			},
    92  			want: false,
    93  		},
    94  		{
    95  			a: PortConfiguration{},
    96  			b: PortConfiguration{
    97  				"foo": {Protocol: loadbalancer.NONE, Port: 1},
    98  			},
    99  			want: false,
   100  		},
   101  		{
   102  			want: true,
   103  		},
   104  	}
   105  	for _, tt := range tests {
   106  		if got := tt.a.DeepEquals(tt.b); got != tt.want {
   107  			c.Errorf("PortConfiguration.DeepEqual() = %v, want %v", got, tt.want)
   108  		}
   109  	}
   110  }