...

Source file src/k8s.io/kubectl/pkg/util/service_port_test.go

Documentation: k8s.io/kubectl/pkg/util

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/util/intstr"
    24  )
    25  
    26  func TestLookupContainerPortNumberByServicePort(t *testing.T) {
    27  	tests := []struct {
    28  		name          string
    29  		svc           v1.Service
    30  		pod           v1.Pod
    31  		port          int32
    32  		containerPort int32
    33  		err           bool
    34  	}{
    35  		{
    36  			name: "test success 1 (int port)",
    37  			svc: v1.Service{
    38  				Spec: v1.ServiceSpec{
    39  					Ports: []v1.ServicePort{
    40  						{
    41  							Port:       80,
    42  							TargetPort: intstr.FromInt32(8080),
    43  						},
    44  					},
    45  				},
    46  			},
    47  			pod: v1.Pod{
    48  				Spec: v1.PodSpec{
    49  					Containers: []v1.Container{
    50  						{
    51  							Ports: []v1.ContainerPort{
    52  								{
    53  									Name:          "http",
    54  									ContainerPort: int32(8080)},
    55  							},
    56  						},
    57  					},
    58  				},
    59  			},
    60  			port:          80,
    61  			containerPort: 8080,
    62  			err:           false,
    63  		},
    64  		{
    65  			name: "test success 2 (clusterIP: None)",
    66  			svc: v1.Service{
    67  				Spec: v1.ServiceSpec{
    68  					ClusterIP: v1.ClusterIPNone,
    69  					Ports: []v1.ServicePort{
    70  						{
    71  							Port:       80,
    72  							TargetPort: intstr.FromInt32(8080),
    73  						},
    74  					},
    75  				},
    76  			},
    77  			pod: v1.Pod{
    78  				Spec: v1.PodSpec{
    79  					Containers: []v1.Container{
    80  						{
    81  							Ports: []v1.ContainerPort{
    82  								{
    83  									Name:          "http",
    84  									ContainerPort: int32(8080)},
    85  							},
    86  						},
    87  					},
    88  				},
    89  			},
    90  			port:          80,
    91  			containerPort: 80,
    92  			err:           false,
    93  		},
    94  		{
    95  			name: "test success 3 (named port)",
    96  			svc: v1.Service{
    97  				Spec: v1.ServiceSpec{
    98  					Ports: []v1.ServicePort{
    99  						{
   100  							Port:       80,
   101  							TargetPort: intstr.FromString("http"),
   102  						},
   103  					},
   104  				},
   105  			},
   106  			pod: v1.Pod{
   107  				Spec: v1.PodSpec{
   108  					Containers: []v1.Container{
   109  						{
   110  							Ports: []v1.ContainerPort{
   111  								{
   112  									Name:          "http",
   113  									ContainerPort: int32(8080)},
   114  							},
   115  						},
   116  					},
   117  				},
   118  			},
   119  			port:          80,
   120  			containerPort: 8080,
   121  			err:           false,
   122  		},
   123  		{
   124  			name: "test success (targetPort omitted)",
   125  			svc: v1.Service{
   126  				Spec: v1.ServiceSpec{
   127  					Ports: []v1.ServicePort{
   128  						{
   129  							Port: 80,
   130  						},
   131  					},
   132  				},
   133  			},
   134  			pod: v1.Pod{
   135  				Spec: v1.PodSpec{
   136  					Containers: []v1.Container{
   137  						{
   138  							Ports: []v1.ContainerPort{
   139  								{
   140  									Name:          "http",
   141  									ContainerPort: int32(80)},
   142  							},
   143  						},
   144  					},
   145  				},
   146  			},
   147  			port:          80,
   148  			containerPort: 80,
   149  			err:           false,
   150  		},
   151  		{
   152  			name: "test failure 1 (cannot find a matching named port)",
   153  			svc: v1.Service{
   154  				Spec: v1.ServiceSpec{
   155  					Ports: []v1.ServicePort{
   156  						{
   157  							Port:       80,
   158  							TargetPort: intstr.FromString("http"),
   159  						},
   160  					},
   161  				},
   162  			},
   163  			pod: v1.Pod{
   164  				Spec: v1.PodSpec{
   165  					Containers: []v1.Container{
   166  						{
   167  							Ports: []v1.ContainerPort{
   168  								{
   169  									Name:          "https",
   170  									ContainerPort: int32(443)},
   171  							},
   172  						},
   173  					},
   174  				},
   175  			},
   176  			port:          80,
   177  			containerPort: -1,
   178  			err:           true,
   179  		},
   180  		{
   181  			name: "test failure 2 (cannot find a matching service port)",
   182  			svc: v1.Service{
   183  				Spec: v1.ServiceSpec{
   184  					Ports: []v1.ServicePort{
   185  						{
   186  							Port:       80,
   187  							TargetPort: intstr.FromString("http"),
   188  						},
   189  					},
   190  				},
   191  			},
   192  			pod: v1.Pod{
   193  				Spec: v1.PodSpec{
   194  					Containers: []v1.Container{
   195  						{
   196  							Ports: []v1.ContainerPort{
   197  								{
   198  									Name:          "https",
   199  									ContainerPort: int32(443)},
   200  							},
   201  						},
   202  					},
   203  				},
   204  			},
   205  			port:          443,
   206  			containerPort: 443,
   207  			err:           true,
   208  		},
   209  		{
   210  			name: "test failure 2 (cannot find a matching service port, but ClusterIP: None)",
   211  			svc: v1.Service{
   212  				Spec: v1.ServiceSpec{
   213  					ClusterIP: v1.ClusterIPNone,
   214  					Ports: []v1.ServicePort{
   215  						{
   216  							Port:       80,
   217  							TargetPort: intstr.FromString("http"),
   218  						},
   219  					},
   220  				},
   221  			},
   222  			pod: v1.Pod{
   223  				Spec: v1.PodSpec{
   224  					Containers: []v1.Container{
   225  						{
   226  							Ports: []v1.ContainerPort{
   227  								{
   228  									Name:          "http",
   229  									ContainerPort: int32(80)},
   230  							},
   231  						},
   232  					},
   233  				},
   234  			},
   235  			port:          443,
   236  			containerPort: 443,
   237  			err:           true,
   238  		},
   239  	}
   240  
   241  	for _, tt := range tests {
   242  		t.Run(tt.name, func(t *testing.T) {
   243  			containerPort, err := LookupContainerPortNumberByServicePort(tt.svc, tt.pod, tt.port)
   244  			if err != nil {
   245  				if tt.err {
   246  					if containerPort != tt.containerPort {
   247  						t.Errorf("%v: expected port %v; got %v", tt.name, tt.containerPort, containerPort)
   248  					}
   249  					return
   250  				}
   251  
   252  				t.Errorf("%v: unexpected error: %v", tt.name, err)
   253  				return
   254  			}
   255  
   256  			if tt.err {
   257  				t.Errorf("%v: unexpected success", tt.name)
   258  				return
   259  			}
   260  
   261  			if containerPort != tt.containerPort {
   262  				t.Errorf("%v: expected port %v; got %v", tt.name, tt.containerPort, containerPort)
   263  			}
   264  		})
   265  	}
   266  }
   267  

View as plain text