...

Source file src/k8s.io/kubectl/pkg/util/pod_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  )
    24  
    25  func TestLookupContainerPortNumberByName(t *testing.T) {
    26  	tests := []struct {
    27  		name     string
    28  		pod      v1.Pod
    29  		portname string
    30  		portnum  int32
    31  		err      bool
    32  	}{
    33  		{
    34  			name: "test success 1",
    35  			pod: v1.Pod{
    36  				Spec: v1.PodSpec{
    37  					Containers: []v1.Container{
    38  						{
    39  							Ports: []v1.ContainerPort{
    40  								{
    41  									Name:          "https",
    42  									ContainerPort: int32(443)},
    43  								{
    44  									Name:          "http",
    45  									ContainerPort: int32(80)},
    46  							},
    47  						},
    48  					},
    49  				},
    50  			},
    51  			portname: "http",
    52  			portnum:  int32(80),
    53  			err:      false,
    54  		},
    55  		{
    56  			name: "test faulure 1",
    57  			pod: v1.Pod{
    58  				Spec: v1.PodSpec{
    59  					Containers: []v1.Container{
    60  						{
    61  							Ports: []v1.ContainerPort{
    62  								{
    63  									Name:          "https",
    64  									ContainerPort: int32(443)},
    65  							},
    66  						},
    67  					},
    68  				},
    69  			},
    70  			portname: "www",
    71  			portnum:  int32(0),
    72  			err:      true,
    73  		},
    74  	}
    75  
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			portnum, err := LookupContainerPortNumberByName(tt.pod, tt.portname)
    79  			if err != nil {
    80  				if tt.err {
    81  					return
    82  				}
    83  
    84  				t.Errorf("%v: unexpected error: %v", tt.name, err)
    85  				return
    86  			}
    87  
    88  			if tt.err {
    89  				t.Errorf("%v: unexpected success", tt.name)
    90  				return
    91  			}
    92  
    93  			if portnum != tt.portnum {
    94  				t.Errorf("%v: expected port number %v; got %v", tt.name, tt.portnum, portnum)
    95  			}
    96  		})
    97  	}
    98  }
    99  

View as plain text