utils_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package preflight
  14. import (
  15. "testing"
  16. "github.com/pkg/errors"
  17. utilsexec "k8s.io/utils/exec"
  18. fakeexec "k8s.io/utils/exec/testing"
  19. )
  20. func TestGetKubeletVersion(t *testing.T) {
  21. cases := []struct {
  22. output string
  23. expected string
  24. err error
  25. valid bool
  26. }{
  27. {"Kubernetes v1.7.0", "1.7.0", nil, true},
  28. {"Kubernetes v1.8.0-alpha.2.1231+afabd012389d53a", "1.8.0-alpha.2.1231+afabd012389d53a", nil, true},
  29. {"something-invalid", "", nil, false},
  30. {"command not found", "", errors.New("kubelet not found"), false},
  31. {"", "", nil, false},
  32. }
  33. for _, tc := range cases {
  34. t.Run(tc.output, func(t *testing.T) {
  35. fcmd := fakeexec.FakeCmd{
  36. OutputScript: []fakeexec.FakeAction{
  37. func() ([]byte, []byte, error) { return []byte(tc.output), nil, tc.err },
  38. },
  39. }
  40. fexec := &fakeexec.FakeExec{
  41. CommandScript: []fakeexec.FakeCommandAction{
  42. func(cmd string, args ...string) utilsexec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
  43. },
  44. }
  45. ver, err := GetKubeletVersion(fexec)
  46. switch {
  47. case err != nil && tc.valid:
  48. t.Errorf("GetKubeletVersion: unexpected error for %q. Error: %v", tc.output, err)
  49. case err == nil && !tc.valid:
  50. t.Errorf("GetKubeletVersion: error expected for key %q, but result is %q", tc.output, ver)
  51. case ver != nil && ver.String() != tc.expected:
  52. t.Errorf("GetKubeletVersion: unexpected version result for key %q. Expected: %q Actual: %q", tc.output, tc.expected, ver)
  53. }
  54. })
  55. }
  56. }