plugins.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bash
  2. # Copyright 2018 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. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. run_plugins_tests() {
  19. set -o nounset
  20. set -o errexit
  21. kube::log::status "Testing kubectl plugins"
  22. # Create a folder which only contains our kubectl executable
  23. TEMP_PATH=$(mktemp -d /tmp/tmp-kubectl-path-XXXXX)
  24. ln -s "$(which kubectl)" "${TEMP_PATH}/kubectl"
  25. # test plugins that overwrite existing kubectl commands
  26. output_message=$(! PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/version" kubectl plugin list 2>&1)
  27. kube::test::if_has_string "${output_message}" 'kubectl-version overwrites existing command: "kubectl version"'
  28. # test plugins that overwrite similarly-named plugins
  29. output_message=$(! PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins:test/fixtures/pkg/kubectl/plugins/foo" kubectl plugin list 2>&1)
  30. kube::test::if_has_string "${output_message}" 'test/fixtures/pkg/kubectl/plugins/foo/kubectl-foo is overshadowed by a similarly named plugin'
  31. # test plugins with no warnings
  32. output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins" kubectl plugin list 2>&1)
  33. kube::test::if_has_string "${output_message}" 'plugins are available'
  34. # no plugins
  35. output_message=$(! PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/empty" kubectl plugin list 2>&1)
  36. kube::test::if_has_string "${output_message}" 'unable to find any kubectl plugins in your PATH'
  37. # attempt to run a plugin in the user's PATH
  38. output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins" kubectl foo)
  39. kube::test::if_has_string "${output_message}" 'plugin foo'
  40. # check arguments passed to the plugin
  41. output_message=$(PATH=${PATH}:"test/fixtures/pkg/kubectl/plugins/bar" kubectl bar arg1)
  42. kube::test::if_has_string "${output_message}" 'test/fixtures/pkg/kubectl/plugins/bar/kubectl-bar arg1'
  43. # ensure that a kubectl command supersedes a plugin that overshadows it
  44. output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/version" kubectl version --client)
  45. kube::test::if_has_string "${output_message}" 'Client Version'
  46. kube::test::if_has_not_string "${output_message}" 'overshadows an existing plugin'
  47. rm -fr "${TEMP_PATH}"
  48. set +o nounset
  49. set +o errexit
  50. }