exec.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env bash
  2. # Copyright 2019 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_kubectl_exec_pod_tests() {
  19. set -o nounset
  20. set -o errexit
  21. create_and_use_new_namespace
  22. kube::log::status "Testing kubectl exec POD COMMAND"
  23. ### Test execute non-existing POD
  24. output_message=$(! kubectl exec abc date 2>&1)
  25. # POD abc should error since it doesn't exist
  26. kube::test::if_has_string "${output_message}" 'pods "abc" not found'
  27. ### Test execute existing POD
  28. # Create test-pod
  29. kubectl create -f hack/testdata/pod.yaml
  30. # Execute existing POD
  31. output_message=$(! kubectl exec test-pod date 2>&1)
  32. # POD test-pod is exists this is shouldn't have output not found
  33. kube::test::if_has_not_string "${output_message}" 'pods "test-pod" not found'
  34. # These must be pass the validate
  35. kube::test::if_has_not_string "${output_message}" 'pod or type/name must be specified'
  36. # Clean up
  37. kubectl delete pods test-pod
  38. set +o nounset
  39. set +o errexit
  40. }
  41. run_kubectl_exec_resource_name_tests() {
  42. set +o nounset
  43. set +o errexit
  44. create_and_use_new_namespace
  45. kube::log::status "Testing kubectl exec TYPE/NAME COMMAND"
  46. ### Test execute invalid resource type
  47. output_message=$(! kubectl exec foo/bar date 2>&1)
  48. # resource type foo should error since it's invalid
  49. kube::test::if_has_string "${output_message}" 'error:'
  50. ### Test execute non-existing resources
  51. output_message=$(! kubectl exec deployments/bar date 2>&1)
  52. # resource type foo should error since it doesn't exist
  53. kube::test::if_has_string "${output_message}" '"bar" not found'
  54. kubectl create -f hack/testdata/pod.yaml
  55. kubectl create -f hack/testdata/frontend-replicaset.yaml
  56. kubectl create -f hack/testdata/configmap.yaml
  57. ### Test execute non-implemented resources
  58. output_message=$(! kubectl exec configmap/test-set-env-config date 2>&1)
  59. # resource type configmap should error since configmap not implemented to be attached
  60. kube::test::if_has_string "${output_message}" 'not implemented'
  61. ### Test execute exists and valid resource type.
  62. # Just check the output, since test-cmd not run kubelet, pod never be assigned.
  63. # and not really can run `kubectl exec` command
  64. output_message=$(! kubectl exec pods/test-pod date 2>&1)
  65. # POD test-pod is exists this is shouldn't have output not found
  66. kube::test::if_has_not_string "${output_message}" 'not found'
  67. # These must be pass the validate
  68. kube::test::if_has_not_string "${output_message}" 'pod or type/name must be specified'
  69. output_message=$(! kubectl exec replicaset/frontend date 2>&1)
  70. # Replicaset frontend is valid and exists will select the first pod.
  71. # and Shouldn't have output not found
  72. kube::test::if_has_not_string "${output_message}" 'not found'
  73. # These must be pass the validate
  74. kube::test::if_has_not_string "${output_message}" 'pod or type/name must be specified'
  75. # Clean up
  76. kubectl delete pods/test-pod
  77. kubectl delete replicaset/frontend
  78. kubectl delete configmap/test-set-env-config
  79. set +o nounset
  80. set +o errexit
  81. }