...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/preflight/utils_test.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/preflight

     1  /*
     2  Copyright 2017 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 preflight
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	utilsexec "k8s.io/utils/exec"
    25  	fakeexec "k8s.io/utils/exec/testing"
    26  )
    27  
    28  func TestGetKubeletVersion(t *testing.T) {
    29  	cases := []struct {
    30  		output   string
    31  		expected string
    32  		err      error
    33  		valid    bool
    34  	}{
    35  		{"Kubernetes v1.7.0", "1.7.0", nil, true},
    36  		{"Kubernetes v1.8.0-alpha.2.1231+afabd012389d53a", "1.8.0-alpha.2.1231+afabd012389d53a", nil, true},
    37  		{"Kubernetes v1.8.0-alpha.2.1231+afabd012389d53a ", "1.8.0-alpha.2.1231+afabd012389d53a", nil, true},
    38  		{" Kubernetes v1.8.0-alpha.2.1231+afabd012389d53a", "1.8.0-alpha.2.1231+afabd012389d53a", nil, true},
    39  		{"Kubernetes  v1.8.0-alpha.2.1231+afabd012389d53a", "", nil, false},
    40  		{"something-invalid", "", nil, false},
    41  		{"command not found", "", errors.New("kubelet not found"), false},
    42  		{"", "", nil, false},
    43  	}
    44  
    45  	for _, tc := range cases {
    46  		t.Run(tc.output, func(t *testing.T) {
    47  			fcmd := fakeexec.FakeCmd{
    48  				OutputScript: []fakeexec.FakeAction{
    49  					func() ([]byte, []byte, error) { return []byte(tc.output), nil, tc.err },
    50  				},
    51  			}
    52  			fexec := &fakeexec.FakeExec{
    53  				CommandScript: []fakeexec.FakeCommandAction{
    54  					func(cmd string, args ...string) utilsexec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
    55  				},
    56  			}
    57  			ver, err := GetKubeletVersion(fexec)
    58  			switch {
    59  			case err != nil && tc.valid:
    60  				t.Errorf("GetKubeletVersion: unexpected error for %q. Error: %v", tc.output, err)
    61  			case err == nil && !tc.valid:
    62  				t.Errorf("GetKubeletVersion: error expected for key %q, but result is %q", tc.output, ver)
    63  			case ver != nil && ver.String() != tc.expected:
    64  				t.Errorf("GetKubeletVersion: unexpected version result for key %q. Expected: %q Actual: %q", tc.output, tc.expected, ver)
    65  			}
    66  		})
    67  	}
    68  }
    69  

View as plain text