...

Source file src/k8s.io/kubectl/pkg/polymorphichelpers/portsforobject_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 TestPortsForObject(t *testing.T) {
    30  	tests := []struct {
    31  		object    runtime.Object
    32  		expectErr bool
    33  		expected  []string
    34  	}{
    35  		{
    36  			object: &corev1.Pod{
    37  				Spec: corev1.PodSpec{
    38  					Containers: []corev1.Container{
    39  						{
    40  							Ports: []corev1.ContainerPort{
    41  								{
    42  									ContainerPort: 101,
    43  								},
    44  							},
    45  						},
    46  					},
    47  				},
    48  			},
    49  			expected: []string{"101"},
    50  		},
    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  									ContainerPort: 102,
    62  								},
    63  							},
    64  						},
    65  					},
    66  				},
    67  			},
    68  			expected: []string{"101", "102"},
    69  		},
    70  		{
    71  			object: &corev1.Pod{
    72  				Spec: corev1.PodSpec{
    73  					Containers: []corev1.Container{
    74  						{
    75  							Ports: []corev1.ContainerPort{
    76  								{
    77  									ContainerPort: 101,
    78  									Protocol:      corev1.ProtocolTCP,
    79  								},
    80  								{
    81  									ContainerPort: 101,
    82  									Protocol:      corev1.ProtocolUDP,
    83  								},
    84  								{
    85  									ContainerPort: 102,
    86  								},
    87  							},
    88  						},
    89  					},
    90  				},
    91  			},
    92  			expected: []string{"101", "102"},
    93  		},
    94  		{
    95  			object: &corev1.Pod{
    96  				Spec: corev1.PodSpec{
    97  					Containers: []corev1.Container{
    98  						{
    99  							Ports: []corev1.ContainerPort{
   100  								{
   101  									ContainerPort: 101,
   102  									Protocol:      corev1.ProtocolTCP,
   103  								},
   104  								{
   105  									ContainerPort: 102,
   106  								},
   107  								{
   108  									ContainerPort: 101,
   109  									Protocol:      corev1.ProtocolUDP,
   110  								},
   111  							},
   112  						},
   113  					},
   114  				},
   115  			},
   116  			expected: []string{"101", "102"},
   117  		},
   118  		{
   119  			object: &corev1.Service{
   120  				Spec: corev1.ServiceSpec{
   121  					Ports: []corev1.ServicePort{
   122  						{
   123  							Port: 101,
   124  						},
   125  					},
   126  				},
   127  			},
   128  			expected: []string{"101"},
   129  		},
   130  		{
   131  			object: &corev1.ReplicationController{
   132  				Spec: corev1.ReplicationControllerSpec{
   133  					Template: &corev1.PodTemplateSpec{
   134  						Spec: corev1.PodSpec{
   135  							Containers: []corev1.Container{
   136  								{
   137  									Ports: []corev1.ContainerPort{
   138  										{
   139  											ContainerPort: 101,
   140  										},
   141  									},
   142  								},
   143  							},
   144  						},
   145  					},
   146  				},
   147  			},
   148  			expected: []string{"101"},
   149  		},
   150  		{
   151  			object: &extensionsv1beta1.Deployment{
   152  				Spec: extensionsv1beta1.DeploymentSpec{
   153  					Template: corev1.PodTemplateSpec{
   154  						Spec: corev1.PodSpec{
   155  							Containers: []corev1.Container{
   156  								{
   157  									Ports: []corev1.ContainerPort{
   158  										{
   159  											ContainerPort: 101,
   160  										},
   161  									},
   162  								},
   163  							},
   164  						},
   165  					},
   166  				},
   167  			},
   168  			expected: []string{"101"},
   169  		},
   170  		{
   171  			object: &extensionsv1beta1.ReplicaSet{
   172  				Spec: extensionsv1beta1.ReplicaSetSpec{
   173  					Template: corev1.PodTemplateSpec{
   174  						Spec: corev1.PodSpec{
   175  							Containers: []corev1.Container{
   176  								{
   177  									Ports: []corev1.ContainerPort{
   178  										{
   179  											ContainerPort: 101,
   180  										},
   181  									},
   182  								},
   183  							},
   184  						},
   185  					},
   186  				},
   187  			},
   188  			expected: []string{"101"},
   189  		},
   190  		{
   191  			object:    &corev1.Node{},
   192  			expectErr: true,
   193  		},
   194  	}
   195  
   196  	for _, test := range tests {
   197  		actual, err := portsForObject(test.object)
   198  		if test.expectErr {
   199  			if err == nil {
   200  				t.Error("unexpected non-error")
   201  			}
   202  			continue
   203  		}
   204  		if !test.expectErr && err != nil {
   205  			t.Errorf("unexpected error: %v", err)
   206  			continue
   207  		}
   208  		if !reflect.DeepEqual(actual, test.expected) {
   209  			t.Errorf("expected ports %v, but got %v", test.expected, actual)
   210  		}
   211  	}
   212  }
   213  

View as plain text