github.com/imran-kn/cilium-fork@v1.6.9/pkg/k8s/ingress_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 k8s
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  	"github.com/cilium/cilium/pkg/k8s/types"
    25  	"github.com/cilium/cilium/pkg/loadbalancer"
    26  
    27  	"gopkg.in/check.v1"
    28  	"k8s.io/api/extensions/v1beta1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/apimachinery/pkg/util/intstr"
    31  )
    32  
    33  func (s *K8sSuite) TestParseIngressID(c *check.C) {
    34  	k8sIngress := &types.Ingress{
    35  		Ingress: &v1beta1.Ingress{
    36  			ObjectMeta: metav1.ObjectMeta{
    37  				Namespace: "bar",
    38  			},
    39  			Spec: v1beta1.IngressSpec{
    40  				Backend: &v1beta1.IngressBackend{
    41  					ServiceName: "foo",
    42  				},
    43  			},
    44  		},
    45  	}
    46  
    47  	c.Assert(ParseIngressID(k8sIngress), checker.DeepEquals, ServiceID{Namespace: "bar", Name: "foo"})
    48  }
    49  
    50  func (s *K8sSuite) TestParseIngress(c *check.C) {
    51  	k8sIngress := &types.Ingress{
    52  		Ingress: &v1beta1.Ingress{
    53  			ObjectMeta: metav1.ObjectMeta{
    54  				Namespace: "bar",
    55  			},
    56  			Spec: v1beta1.IngressSpec{
    57  				Backend: &v1beta1.IngressBackend{
    58  					ServiceName: "svc1",
    59  					ServicePort: intstr.IntOrString{
    60  						IntVal: 8080,
    61  						StrVal: "foo",
    62  						Type:   intstr.Int,
    63  					},
    64  				},
    65  			},
    66  		},
    67  	}
    68  	host := net.ParseIP("172.0.0.1")
    69  
    70  	id, ingress, err := ParseIngress(k8sIngress, host)
    71  	c.Assert(err, check.IsNil)
    72  	c.Assert(id, checker.DeepEquals, ServiceID{Namespace: "bar", Name: "svc1"})
    73  	c.Assert(ingress, checker.DeepEquals, &Service{
    74  		FrontendIP: net.ParseIP("172.0.0.1"),
    75  		Ports: map[loadbalancer.FEPortName]*loadbalancer.FEPort{
    76  			loadbalancer.FEPortName("svc1/8080"): {
    77  				L4Addr: &loadbalancer.L4Addr{
    78  					Protocol: loadbalancer.TCP,
    79  					Port:     8080,
    80  				},
    81  			},
    82  		},
    83  		NodePorts: map[loadbalancer.FEPortName]map[string]*loadbalancer.L3n4AddrID{},
    84  	})
    85  }
    86  
    87  func (s *K8sSuite) Test_parsingV1beta1(c *check.C) {
    88  	type args struct {
    89  		i    *types.Ingress
    90  		host net.IP
    91  	}
    92  	tests := []struct {
    93  		name        string
    94  		setupArgs   func() args
    95  		setupWanted func() (*Service, error)
    96  	}{
    97  		{
    98  			name: "Parse a normal Single Service Ingress with no ports",
    99  			setupArgs: func() args {
   100  				return args{
   101  					i: &types.Ingress{
   102  						Ingress: &v1beta1.Ingress{
   103  							Spec: v1beta1.IngressSpec{
   104  								Backend: &v1beta1.IngressBackend{
   105  									ServiceName: "svc1",
   106  								},
   107  							},
   108  						},
   109  					},
   110  					host: net.ParseIP("172.0.0.1"),
   111  				}
   112  			},
   113  			setupWanted: func() (*Service, error) {
   114  				return nil, fmt.Errorf("invalid port number")
   115  			},
   116  		},
   117  		{
   118  			name: "Parse a normal Single Service Ingress with ports",
   119  			setupArgs: func() args {
   120  				return args{
   121  					i: &types.Ingress{
   122  						Ingress: &v1beta1.Ingress{
   123  							Spec: v1beta1.IngressSpec{
   124  								Backend: &v1beta1.IngressBackend{
   125  									ServiceName: "svc1",
   126  									ServicePort: intstr.IntOrString{
   127  										IntVal: 8080,
   128  										StrVal: "foo",
   129  										Type:   intstr.Int,
   130  									},
   131  								},
   132  							},
   133  						},
   134  					},
   135  					host: net.ParseIP("172.0.0.1"),
   136  				}
   137  			},
   138  			setupWanted: func() (*Service, error) {
   139  				return &Service{
   140  					FrontendIP: net.ParseIP("172.0.0.1"),
   141  					Ports: map[loadbalancer.FEPortName]*loadbalancer.FEPort{
   142  						loadbalancer.FEPortName("svc1/8080"): {
   143  							L4Addr: &loadbalancer.L4Addr{
   144  								Protocol: loadbalancer.TCP,
   145  								Port:     8080,
   146  							},
   147  						},
   148  					},
   149  					NodePorts: map[loadbalancer.FEPortName]map[string]*loadbalancer.L3n4AddrID{},
   150  				}, nil
   151  			},
   152  		},
   153  	}
   154  	for _, tt := range tests {
   155  		args := tt.setupArgs()
   156  		wantK8sSvcInfo, wantError := tt.setupWanted()
   157  		_, gotK8sSvcInfo, gotError := ParseIngress(args.i, args.host)
   158  		c.Assert(gotError, checker.DeepEquals, wantError, check.Commentf("Test name: %q", tt.name))
   159  		c.Assert(gotK8sSvcInfo, checker.DeepEquals, wantK8sSvcInfo, check.Commentf("Test name: %q", tt.name))
   160  	}
   161  }
   162  
   163  func (s *K8sSuite) Test_supportV1beta1(c *check.C) {
   164  	type args struct {
   165  		i *types.Ingress
   166  	}
   167  	tests := []struct {
   168  		name      string
   169  		setupArgs func() args
   170  		want      bool
   171  	}{
   172  		{
   173  			name: "We only support Single Service Ingress, which means Spec.Backend is not nil",
   174  			setupArgs: func() args {
   175  				return args{
   176  					i: &types.Ingress{
   177  						Ingress: &v1beta1.Ingress{
   178  							Spec: v1beta1.IngressSpec{
   179  								Backend: &v1beta1.IngressBackend{
   180  									ServiceName: "svc1",
   181  								},
   182  							},
   183  						},
   184  					},
   185  				}
   186  			},
   187  			want: true,
   188  		},
   189  		{
   190  			name: "We don't support any other ingress type",
   191  			setupArgs: func() args {
   192  				return args{
   193  					i: &types.Ingress{
   194  						Ingress: &v1beta1.Ingress{
   195  							Spec: v1beta1.IngressSpec{
   196  								Rules: []v1beta1.IngressRule{
   197  									{
   198  										Host: "hostless",
   199  									},
   200  								},
   201  							},
   202  						},
   203  					},
   204  				}
   205  			},
   206  			want: false,
   207  		},
   208  	}
   209  	for _, tt := range tests {
   210  		args := tt.setupArgs()
   211  		want := tt.want
   212  		got := supportV1beta1(args.i)
   213  		c.Assert(got, checker.DeepEquals, want, check.Commentf("Test name: %q", tt.name))
   214  	}
   215  }