github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/portforward/forwarder_manager_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold 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 portforward
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"testing"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
    30  	"github.com/GoogleContainerTools/skaffold/testutil"
    31  )
    32  
    33  func TestNewForwarderManager(t *testing.T) {
    34  	tests := []struct {
    35  		description        string
    36  		fmOptions          string
    37  		expectedForwarders int
    38  	}{
    39  		{
    40  			description:        "nil forwarder manager",
    41  			fmOptions:          "",
    42  			expectedForwarders: 0,
    43  		},
    44  		{
    45  			description:        "basic forwarder manager",
    46  			fmOptions:          "user",
    47  			expectedForwarders: 1,
    48  		},
    49  		{
    50  			description:        "two options forwarder manager",
    51  			fmOptions:          "user,debug",
    52  			expectedForwarders: 2,
    53  		},
    54  	}
    55  
    56  	for _, test := range tests {
    57  		testutil.Run(t, test.description, func(t *testutil.T) {
    58  			options := config.PortForwardOptions{}
    59  			options.Set(test.fmOptions)
    60  			fm := NewForwarderManager(&kubectl.CLI{},
    61  				&kubernetes.ImageList{},
    62  				"",
    63  				"",
    64  				nil,
    65  				options,
    66  				nil)
    67  
    68  			if fm != nil {
    69  				t.CheckDeepEqual(test.expectedForwarders, len(fm.forwarders))
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestForwarderManagerZeroValue(t *testing.T) {
    76  	var m *ForwarderManager
    77  
    78  	// Should not raise a nil dereference
    79  	m.Start(context.Background(), ioutil.Discard)
    80  	m.Stop()
    81  }
    82  
    83  func TestAllPorts(t *testing.T) {
    84  	ports := []v1.ContainerPort{
    85  		{Name: "dlv", ContainerPort: 56286},
    86  		{Name: "http", ContainerPort: 8080},
    87  	}
    88  	container := v1.Container{Name: "test", Ports: ports}
    89  	pod := v1.Pod{Spec: v1.PodSpec{Containers: []v1.Container{container}}}
    90  	testutil.CheckDeepEqual(t, ports, allPorts(&pod, container))
    91  }
    92  
    93  func TestDebugPorts(t *testing.T) {
    94  	ports := []v1.ContainerPort{
    95  		{Name: "dlv", ContainerPort: 56268},
    96  		{Name: "http", ContainerPort: 8080},
    97  	}
    98  	container := v1.Container{Name: "test", Ports: ports}
    99  	pod := v1.Pod{
   100  		ObjectMeta: metav1.ObjectMeta{Name: "name", Annotations: map[string]string{"debug.cloud.google.com/config": `{"test":{"runtime":"foo","ports":{"dlv":56268}}}`}},
   101  		Spec:       v1.PodSpec{Containers: []v1.Container{container}}}
   102  	testutil.CheckDeepEqual(t, []v1.ContainerPort{{Name: "dlv", ContainerPort: 56268}}, debugPorts(&pod, container))
   103  }
   104  
   105  func TestAddForwarder(t *testing.T) {
   106  	tests := []struct {
   107  		description        string
   108  		fmOptions          string
   109  		expectedForwarders []int
   110  	}{
   111  		{
   112  			description:        "nil forwarder manager",
   113  			fmOptions:          "",
   114  			expectedForwarders: []int{0, 0},
   115  		},
   116  		{
   117  			description:        "basic forwarder manager",
   118  			fmOptions:          "user",
   119  			expectedForwarders: []int{1, 1},
   120  		},
   121  		{
   122  			description:        "two options forwarder manager",
   123  			fmOptions:          "user,debug",
   124  			expectedForwarders: []int{2, 3},
   125  		},
   126  	}
   127  
   128  	for _, test := range tests {
   129  		testutil.Run(t, test.description, func(t *testutil.T) {
   130  			options := config.PortForwardOptions{}
   131  			options.Set(test.fmOptions)
   132  			fm := NewForwarderManager(&kubectl.CLI{}, &kubernetes.ImageList{}, "", "", nil, options, nil)
   133  
   134  			if fm != nil {
   135  				t.CheckDeepEqual(test.expectedForwarders[0], len(fm.forwarders))
   136  				fm.AddPodForwarder(&kubectl.CLI{}, &kubernetes.ImageList{}, "", options)
   137  				t.CheckDeepEqual(test.expectedForwarders[1], len(fm.forwarders))
   138  			}
   139  		})
   140  	}
   141  }