...

Source file src/k8s.io/kubectl/pkg/polymorphichelpers/protocolsforobject_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  	"testing"
    21  
    22  	"reflect"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  )
    28  
    29  func TestProtocolsForObject(t *testing.T) {
    30  	tests := []struct {
    31  		object    runtime.Object
    32  		expectErr bool
    33  	}{
    34  		{
    35  			object: &corev1.Pod{
    36  				Spec: corev1.PodSpec{
    37  					Containers: []corev1.Container{
    38  						{
    39  							Ports: []corev1.ContainerPort{
    40  								{
    41  									ContainerPort: 101,
    42  									Protocol:      "TCP",
    43  								},
    44  							},
    45  						},
    46  					},
    47  				},
    48  			},
    49  		},
    50  		// No protocol--should default to TCP.
    51  		{
    52  			object: &corev1.Pod{
    53  				Spec: corev1.PodSpec{
    54  					Containers: []corev1.Container{
    55  						{
    56  							Ports: []corev1.ContainerPort{
    57  								{
    58  									ContainerPort: 101,
    59  								},
    60  							},
    61  						},
    62  					},
    63  				},
    64  			},
    65  		},
    66  		{
    67  			object: &corev1.Service{
    68  				Spec: corev1.ServiceSpec{
    69  					Ports: []corev1.ServicePort{
    70  						{
    71  							Port:     101,
    72  							Protocol: "TCP",
    73  						},
    74  					},
    75  				},
    76  			},
    77  		},
    78  		// No protocol for service port--default to TCP
    79  		{
    80  			object: &corev1.Service{
    81  				Spec: corev1.ServiceSpec{
    82  					Ports: []corev1.ServicePort{
    83  						{
    84  							Port: 101,
    85  						},
    86  					},
    87  				},
    88  			},
    89  		},
    90  		{
    91  			object: &corev1.ReplicationController{
    92  				Spec: corev1.ReplicationControllerSpec{
    93  					Template: &corev1.PodTemplateSpec{
    94  						Spec: corev1.PodSpec{
    95  							Containers: []corev1.Container{
    96  								{
    97  									Ports: []corev1.ContainerPort{
    98  										{
    99  											ContainerPort: 101,
   100  											Protocol:      "TCP",
   101  										},
   102  									},
   103  								},
   104  							},
   105  						},
   106  					},
   107  				},
   108  			},
   109  		},
   110  		{
   111  			object: &extensionsv1beta1.Deployment{
   112  				Spec: extensionsv1beta1.DeploymentSpec{
   113  					Template: corev1.PodTemplateSpec{
   114  						Spec: corev1.PodSpec{
   115  							Containers: []corev1.Container{
   116  								{
   117  									Ports: []corev1.ContainerPort{
   118  										{
   119  											ContainerPort: 101,
   120  											Protocol:      "TCP",
   121  										},
   122  									},
   123  								},
   124  							},
   125  						},
   126  					},
   127  				},
   128  			},
   129  		},
   130  		{
   131  			object: &extensionsv1beta1.ReplicaSet{
   132  				Spec: extensionsv1beta1.ReplicaSetSpec{
   133  					Template: corev1.PodTemplateSpec{
   134  						Spec: corev1.PodSpec{
   135  							Containers: []corev1.Container{
   136  								{
   137  									Ports: []corev1.ContainerPort{
   138  										{
   139  											ContainerPort: 101,
   140  											Protocol:      "TCP",
   141  										},
   142  									},
   143  								},
   144  							},
   145  						},
   146  					},
   147  				},
   148  			},
   149  		},
   150  		{
   151  			object:    &corev1.Node{},
   152  			expectErr: true,
   153  		},
   154  	}
   155  	expectedPorts := map[string]string{"101": "TCP"}
   156  
   157  	for _, test := range tests {
   158  		actual, err := protocolsForObject(test.object)
   159  		if test.expectErr {
   160  			if err == nil {
   161  				t.Error("unexpected non-error")
   162  			}
   163  			continue
   164  		}
   165  		if !test.expectErr && err != nil {
   166  			t.Errorf("unexpected error: %v", err)
   167  			continue
   168  		}
   169  		if !reflect.DeepEqual(actual, expectedPorts) {
   170  			t.Errorf("expected ports %v, but got %v", expectedPorts, actual)
   171  		}
   172  	}
   173  }
   174  

View as plain text