...

Source file src/k8s.io/kubectl/pkg/polymorphichelpers/multiprotocolsforobject_test.go

Documentation: k8s.io/kubectl/pkg/polymorphichelpers

     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 polymorphichelpers
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  )
    27  
    28  func TestMultiProtocolsForObject(t *testing.T) {
    29  	tests := []struct {
    30  		name      string
    31  		object    runtime.Object
    32  		expectErr bool
    33  		expected  map[string][]string
    34  	}{
    35  		{
    36  			name: "pod with TCP protocol",
    37  			object: &corev1.Pod{
    38  				Spec: corev1.PodSpec{
    39  					Containers: []corev1.Container{
    40  						{
    41  							Ports: []corev1.ContainerPort{
    42  								{
    43  									ContainerPort: 101,
    44  									Protocol:      "TCP",
    45  								},
    46  							},
    47  						},
    48  					},
    49  				},
    50  			},
    51  			expected: map[string][]string{"101": {"TCP"}},
    52  		},
    53  		// No protocol--should default to TCP.
    54  		{
    55  			name: "pod with no protocol",
    56  			object: &corev1.Pod{
    57  				Spec: corev1.PodSpec{
    58  					Containers: []corev1.Container{
    59  						{
    60  							Ports: []corev1.ContainerPort{
    61  								{
    62  									ContainerPort: 101,
    63  								},
    64  							},
    65  						},
    66  					},
    67  				},
    68  			},
    69  			expected: map[string][]string{"101": {"TCP"}},
    70  		},
    71  		{
    72  			name: "pod with same-port,different-protocol",
    73  			object: &corev1.Pod{
    74  				Spec: corev1.PodSpec{
    75  					Containers: []corev1.Container{
    76  						{
    77  							Ports: []corev1.ContainerPort{
    78  								{
    79  									ContainerPort: 101,
    80  									Protocol:      "TCP",
    81  								},
    82  								{
    83  									ContainerPort: 101,
    84  									Protocol:      "UDP",
    85  								},
    86  							},
    87  						},
    88  					},
    89  				},
    90  			},
    91  			expected: map[string][]string{"101": {"TCP", "UDP"}},
    92  		},
    93  		{
    94  			name: "service with TCP protocol",
    95  			object: &corev1.Service{
    96  				Spec: corev1.ServiceSpec{
    97  					Ports: []corev1.ServicePort{
    98  						{
    99  							Port:     101,
   100  							Protocol: "TCP",
   101  						},
   102  					},
   103  				},
   104  			},
   105  			expected: map[string][]string{"101": {"TCP"}},
   106  		},
   107  		// No protocol for service port--default to TCP
   108  		{
   109  			name: "service with no protocol",
   110  			object: &corev1.Service{
   111  				Spec: corev1.ServiceSpec{
   112  					Ports: []corev1.ServicePort{
   113  						{
   114  							Port: 101,
   115  						},
   116  					},
   117  				},
   118  			},
   119  			expected: map[string][]string{"101": {"TCP"}},
   120  		},
   121  		{
   122  			name: "replication with TCP protocol",
   123  			object: &corev1.ReplicationController{
   124  				Spec: corev1.ReplicationControllerSpec{
   125  					Template: &corev1.PodTemplateSpec{
   126  						Spec: corev1.PodSpec{
   127  							Containers: []corev1.Container{
   128  								{
   129  									Ports: []corev1.ContainerPort{
   130  										{
   131  											ContainerPort: 101,
   132  											Protocol:      "TCP",
   133  										},
   134  									},
   135  								},
   136  							},
   137  						},
   138  					},
   139  				},
   140  			},
   141  			expected: map[string][]string{"101": {"TCP"}},
   142  		},
   143  		{
   144  			name: "deployment with TCP protocol",
   145  			object: &extensionsv1beta1.Deployment{
   146  				Spec: extensionsv1beta1.DeploymentSpec{
   147  					Template: corev1.PodTemplateSpec{
   148  						Spec: corev1.PodSpec{
   149  							Containers: []corev1.Container{
   150  								{
   151  									Ports: []corev1.ContainerPort{
   152  										{
   153  											ContainerPort: 101,
   154  											Protocol:      "TCP",
   155  										},
   156  									},
   157  								},
   158  							},
   159  						},
   160  					},
   161  				},
   162  			},
   163  			expected: map[string][]string{"101": {"TCP"}},
   164  		},
   165  		{
   166  			name: "replicaset with TCP protocol",
   167  			object: &extensionsv1beta1.ReplicaSet{
   168  				Spec: extensionsv1beta1.ReplicaSetSpec{
   169  					Template: corev1.PodTemplateSpec{
   170  						Spec: corev1.PodSpec{
   171  							Containers: []corev1.Container{
   172  								{
   173  									Ports: []corev1.ContainerPort{
   174  										{
   175  											ContainerPort: 101,
   176  											Protocol:      "TCP",
   177  										},
   178  									},
   179  								},
   180  							},
   181  						},
   182  					},
   183  				},
   184  			},
   185  			expected: map[string][]string{"101": {"TCP"}},
   186  		},
   187  		{
   188  			name:      "unsupported object",
   189  			object:    &corev1.Node{},
   190  			expectErr: true,
   191  		},
   192  	}
   193  
   194  	for _, test := range tests {
   195  		t.Run(test.name, func(t *testing.T) {
   196  			actual, err := multiProtocolsForObject(test.object)
   197  			if test.expectErr {
   198  				if err == nil {
   199  					t.Error("unexpected non-error")
   200  				}
   201  				return
   202  			}
   203  			if !test.expectErr && err != nil {
   204  				t.Errorf("unexpected error: %v", err)
   205  				return
   206  			}
   207  			if !reflect.DeepEqual(actual, test.expected) {
   208  				t.Errorf("expected ports %v, but got %v", test.expected, actual)
   209  			}
   210  		})
   211  
   212  	}
   213  }
   214  

View as plain text