kubeconfig.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_kubectl_config_set_tests() {
  19. set -o nounset
  20. set -o errexit
  21. create_and_use_new_namespace
  22. kube::log::status "Testing kubectl(v1:config set)"
  23. kubectl config set-cluster test-cluster --server="https://does-not-work"
  24. # Get the api cert and add a comment to avoid flag parsing problems
  25. cert_data=$(echo "#Comment" && cat "${TMPDIR:-/tmp}/apiserver.crt")
  26. kubectl config set clusters.test-cluster.certificate-authority-data "$cert_data" --set-raw-bytes
  27. r_written=$(kubectl config view --raw -o jsonpath='{.clusters[?(@.name == "test-cluster")].cluster.certificate-authority-data}')
  28. encoded=$(echo -n "$cert_data" | base64)
  29. kubectl config set clusters.test-cluster.certificate-authority-data "$encoded"
  30. e_written=$(kubectl config view --raw -o jsonpath='{.clusters[?(@.name == "test-cluster")].cluster.certificate-authority-data}')
  31. test "$e_written" == "$r_written"
  32. set +o nounset
  33. set +o errexit
  34. }
  35. run_client_config_tests() {
  36. set -o nounset
  37. set -o errexit
  38. create_and_use_new_namespace
  39. kube::log::status "Testing client config"
  40. # Command
  41. # Pre-condition: kubeconfig "missing" is not a file or directory
  42. output_message=$(! kubectl get pod --context="" --kubeconfig=missing 2>&1)
  43. kube::test::if_has_string "${output_message}" "missing: no such file or directory"
  44. # Pre-condition: kubeconfig "missing" is not a file or directory
  45. # Command
  46. output_message=$(! kubectl get pod --user="" --kubeconfig=missing 2>&1)
  47. # Post-condition: --user contains a valid / empty value, missing config file returns error
  48. kube::test::if_has_string "${output_message}" "missing: no such file or directory"
  49. # Command
  50. output_message=$(! kubectl get pod --cluster="" --kubeconfig=missing 2>&1)
  51. # Post-condition: --cluster contains a "valid" value, missing config file returns error
  52. kube::test::if_has_string "${output_message}" "missing: no such file or directory"
  53. # Pre-condition: context "missing-context" does not exist
  54. # Command
  55. output_message=$(! kubectl get pod --context="missing-context" 2>&1)
  56. kube::test::if_has_string "${output_message}" 'context was not found for specified context: missing-context'
  57. # Post-condition: invalid or missing context returns error
  58. # Pre-condition: cluster "missing-cluster" does not exist
  59. # Command
  60. output_message=$(! kubectl get pod --cluster="missing-cluster" 2>&1)
  61. kube::test::if_has_string "${output_message}" 'no server found for cluster "missing-cluster"'
  62. # Post-condition: invalid or missing cluster returns error
  63. # Pre-condition: user "missing-user" does not exist
  64. # Command
  65. output_message=$(! kubectl get pod --user="missing-user" 2>&1)
  66. kube::test::if_has_string "${output_message}" 'auth info "missing-user" does not exist'
  67. # Post-condition: invalid or missing user returns error
  68. # test invalid config
  69. kubectl config view | sed -E "s/apiVersion: .*/apiVersion: v-1/g" > "${TMPDIR:-/tmp}"/newconfig.yaml
  70. output_message=$(! "${KUBE_OUTPUT_HOSTBIN}/kubectl" get pods --context="" --user="" --kubeconfig="${TMPDIR:-/tmp}"/newconfig.yaml 2>&1)
  71. kube::test::if_has_string "${output_message}" "error loading config file"
  72. output_message=$(! kubectl get pod --kubeconfig=missing-config 2>&1)
  73. kube::test::if_has_string "${output_message}" 'no such file or directory'
  74. set +o nounset
  75. set +o errexit
  76. }