github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubectl/cli_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 kubectl
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  func TestCLI(t *testing.T) {
    28  	const (
    29  		kubeContext = "some-kubecontext"
    30  		output      = "this is the expected output"
    31  	)
    32  
    33  	tests := []struct {
    34  		name            string
    35  		kubeconfig      string
    36  		namespace       string
    37  		expectedCommand string
    38  	}{
    39  		{
    40  			name:            "without namespace or kubeconfig",
    41  			expectedCommand: "kubectl --context some-kubecontext exec arg1 arg2",
    42  		},
    43  		{
    44  			name:            "only namespace, no kubeconfig",
    45  			namespace:       "some-namespace",
    46  			expectedCommand: "kubectl --context some-kubecontext --namespace some-namespace exec arg1 arg2",
    47  		},
    48  		{
    49  			name:            "only kubeconfig, no namespace",
    50  			kubeconfig:      "some-kubeconfig",
    51  			expectedCommand: "kubectl --context some-kubecontext --kubeconfig some-kubeconfig exec arg1 arg2",
    52  		},
    53  		{
    54  			name:            "with namespace and kubeconfig",
    55  			kubeconfig:      "some-kubeconfig",
    56  			namespace:       "some-namespace",
    57  			expectedCommand: "kubectl --context some-kubecontext --namespace some-namespace --kubeconfig some-kubeconfig exec arg1 arg2",
    58  		},
    59  	}
    60  
    61  	// test cli.Run()
    62  	for _, test := range tests {
    63  		testutil.Run(t, test.name, func(t *testutil.T) {
    64  			t.Override(&util.DefaultExecCommand, testutil.CmdRun(
    65  				test.expectedCommand,
    66  			))
    67  
    68  			cli := NewCLI(&mockConfig{
    69  				kubeContext: kubeContext,
    70  				kubeConfig:  test.kubeconfig,
    71  				namespace:   test.namespace,
    72  			}, "")
    73  			err := cli.Run(context.Background(), nil, nil, "exec", "arg1", "arg2")
    74  
    75  			t.CheckNoError(err)
    76  		})
    77  	}
    78  
    79  	// test cli.RunOut()
    80  	for _, test := range tests {
    81  		testutil.Run(t, test.name, func(t *testutil.T) {
    82  			t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
    83  				test.expectedCommand,
    84  				output,
    85  			))
    86  
    87  			cli := NewCLI(&mockConfig{
    88  				kubeContext: kubeContext,
    89  				kubeConfig:  test.kubeconfig,
    90  				namespace:   test.namespace,
    91  			}, "")
    92  			out, err := cli.RunOut(context.Background(), "exec", "arg1", "arg2")
    93  
    94  			t.CheckNoError(err)
    95  			t.CheckDeepEqual(string(out), output)
    96  		})
    97  	}
    98  
    99  	// test cli.CommandWithStrictCancellation()
   100  	for _, test := range tests {
   101  		testutil.Run(t, test.name, func(t *testutil.T) {
   102  			t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
   103  				test.expectedCommand,
   104  				output,
   105  			))
   106  
   107  			cli := NewCLI(&mockConfig{
   108  				kubeContext: kubeContext,
   109  				kubeConfig:  test.kubeconfig,
   110  				namespace:   test.namespace,
   111  			}, "")
   112  			cmd := cli.CommandWithStrictCancellation(context.Background(), "exec", "arg1", "arg2")
   113  			out, err := util.RunCmdOut(context.Background(), cmd.Cmd)
   114  
   115  			t.CheckNoError(err)
   116  			t.CheckDeepEqual(string(out), output)
   117  		})
   118  	}
   119  }
   120  
   121  type mockConfig struct {
   122  	kubeContext string
   123  	kubeConfig  string
   124  	namespace   string
   125  }
   126  
   127  func (c *mockConfig) GetKubeContext() string   { return c.kubeContext }
   128  func (c *mockConfig) GetKubeConfig() string    { return c.kubeConfig }
   129  func (c *mockConfig) GetKubeNamespace() string { return c.namespace }