cmd_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Copyright 2014 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 cmd
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "reflect"
  19. "testing"
  20. "k8s.io/cli-runtime/pkg/genericclioptions"
  21. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  22. )
  23. func TestNormalizationFuncGlobalExistence(t *testing.T) {
  24. // This test can be safely deleted when we will not support multiple flag formats
  25. root := NewKubectlCommand(os.Stdin, os.Stdout, os.Stderr)
  26. if root.Parent() != nil {
  27. t.Fatal("We expect the root command to be returned")
  28. }
  29. if root.GlobalNormalizationFunc() == nil {
  30. t.Fatal("We expect that root command has a global normalization function")
  31. }
  32. if reflect.ValueOf(root.GlobalNormalizationFunc()).Pointer() != reflect.ValueOf(root.Flags().GetNormalizeFunc()).Pointer() {
  33. t.Fatal("root command seems to have a wrong normalization function")
  34. }
  35. sub := root
  36. for sub.HasSubCommands() {
  37. sub = sub.Commands()[0]
  38. }
  39. // In case of failure of this test check this PR: spf13/cobra#110
  40. if reflect.ValueOf(sub.Flags().GetNormalizeFunc()).Pointer() != reflect.ValueOf(root.Flags().GetNormalizeFunc()).Pointer() {
  41. t.Fatal("child and root commands should have the same normalization functions")
  42. }
  43. }
  44. func TestKubectlCommandHandlesPlugins(t *testing.T) {
  45. tests := []struct {
  46. name string
  47. args []string
  48. expectPlugin string
  49. expectPluginArgs []string
  50. expectError string
  51. }{
  52. {
  53. name: "test that normal commands are able to be executed, when no plugin overshadows them",
  54. args: []string{"kubectl", "get", "foo"},
  55. expectPlugin: "",
  56. expectPluginArgs: []string{},
  57. },
  58. {
  59. name: "test that a plugin executable is found based on command args",
  60. args: []string{"kubectl", "foo", "--bar"},
  61. expectPlugin: "plugin/testdata/kubectl-foo",
  62. expectPluginArgs: []string{"foo", "--bar"},
  63. },
  64. {
  65. name: "test that a plugin does not execute over an existing command by the same name",
  66. args: []string{"kubectl", "version"},
  67. },
  68. }
  69. for _, test := range tests {
  70. t.Run(test.name, func(t *testing.T) {
  71. pluginsHandler := &testPluginHandler{
  72. pluginsDirectory: "plugin/testdata",
  73. }
  74. _, in, out, errOut := genericclioptions.NewTestIOStreams()
  75. cmdutil.BehaviorOnFatal(func(str string, code int) {
  76. errOut.Write([]byte(str))
  77. })
  78. root := NewDefaultKubectlCommandWithArgs(pluginsHandler, test.args, in, out, errOut)
  79. if err := root.Execute(); err != nil {
  80. t.Fatalf("unexpected error: %v", err)
  81. }
  82. if pluginsHandler.err != nil && pluginsHandler.err.Error() != test.expectError {
  83. t.Fatalf("unexpected error: expected %q to occur, but got %q", test.expectError, pluginsHandler.err)
  84. }
  85. if pluginsHandler.executedPlugin != test.expectPlugin {
  86. t.Fatalf("unexpected plugin execution: expedcted %q, got %q", test.expectPlugin, pluginsHandler.executedPlugin)
  87. }
  88. if len(pluginsHandler.withArgs) != len(test.expectPluginArgs) {
  89. t.Fatalf("unexpected plugin execution args: expedcted %q, got %q", test.expectPluginArgs, pluginsHandler.withArgs)
  90. }
  91. })
  92. }
  93. }
  94. type testPluginHandler struct {
  95. pluginsDirectory string
  96. // execution results
  97. executedPlugin string
  98. withArgs []string
  99. withEnv []string
  100. err error
  101. }
  102. func (h *testPluginHandler) Lookup(filename string) (string, bool) {
  103. // append supported plugin prefix to the filename
  104. filename = fmt.Sprintf("%s-%s", "kubectl", filename)
  105. dir, err := os.Stat(h.pluginsDirectory)
  106. if err != nil {
  107. h.err = err
  108. return "", false
  109. }
  110. if !dir.IsDir() {
  111. h.err = fmt.Errorf("expected %q to be a directory", h.pluginsDirectory)
  112. return "", false
  113. }
  114. plugins, err := ioutil.ReadDir(h.pluginsDirectory)
  115. if err != nil {
  116. h.err = err
  117. return "", false
  118. }
  119. for _, p := range plugins {
  120. if p.Name() == filename {
  121. return fmt.Sprintf("%s/%s", h.pluginsDirectory, p.Name()), true
  122. }
  123. }
  124. h.err = fmt.Errorf("unable to find a plugin executable %q", filename)
  125. return "", false
  126. }
  127. func (h *testPluginHandler) Execute(executablePath string, cmdArgs, env []string) error {
  128. h.executedPlugin = executablePath
  129. h.withArgs = cmdArgs
  130. h.withEnv = env
  131. return nil
  132. }