github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubectl/version_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  	"errors"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/warnings"
    26  	"github.com/GoogleContainerTools/skaffold/testutil"
    27  )
    28  
    29  func TestCheckVersion(t *testing.T) {
    30  	tests := []struct {
    31  		description     string
    32  		commands        util.Command
    33  		shouldErr       bool
    34  		warnings        []string
    35  		expectedVersion string
    36  	}{
    37  		{
    38  			description:     "1.12 is valid",
    39  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"12"}}`),
    40  			expectedVersion: "1.12",
    41  		},
    42  		{
    43  			description:     "1.12+ is valid",
    44  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"12+"}}`),
    45  			expectedVersion: "1.12+",
    46  		},
    47  		{
    48  			description:     "1.13 is valid",
    49  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"13"}}`),
    50  			expectedVersion: "1.13",
    51  		},
    52  		{
    53  			description:     "2.11 is valid",
    54  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"2","minor":"11"}}`),
    55  			expectedVersion: "2.11",
    56  		},
    57  		{
    58  			description:     "1.11 is too old",
    59  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"11"}}`),
    60  			shouldErr:       true,
    61  			expectedVersion: "1.11",
    62  		},
    63  		{
    64  			description:     "invalid version",
    65  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `not json`),
    66  			shouldErr:       true,
    67  			warnings:        []string{"unable to parse client version: invalid character 'o' in literal null (expecting 'u')"},
    68  			expectedVersion: "unknown",
    69  		},
    70  		{
    71  			description:     "invalid minor",
    72  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"1","minor":"X"}}`),
    73  			shouldErr:       true,
    74  			expectedVersion: "1.X",
    75  		},
    76  		{
    77  			description:     "invalid major",
    78  			commands:        testutil.CmdRunOut("kubectl version --client -ojson", `{"clientVersion":{"major":"X","minor":"1"}}`),
    79  			shouldErr:       true,
    80  			expectedVersion: "X.1",
    81  		},
    82  		{
    83  			description:     "cli not found",
    84  			commands:        testutil.CmdRunOutErr("kubectl version --client -ojson", ``, errors.New("not found")),
    85  			shouldErr:       true,
    86  			warnings:        []string{"unable to get kubectl client version: not found"},
    87  			expectedVersion: "unknown",
    88  		},
    89  	}
    90  	for _, test := range tests {
    91  		testutil.Run(t, test.description, func(t *testutil.T) {
    92  			fakeWarner := &warnings.Collect{}
    93  			t.Override(&warnings.Printf, fakeWarner.Warnf)
    94  			t.Override(&util.DefaultExecCommand, test.commands)
    95  
    96  			cli := CLI{}
    97  
    98  			version := cli.Version(context.Background()).String()
    99  			t.CheckDeepEqual(test.expectedVersion, version)
   100  
   101  			err := cli.CheckVersion(context.Background())
   102  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.warnings, fakeWarner.Warnings)
   103  		})
   104  	}
   105  }