test-cmd.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env bash
  2. # Copyright 2014 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. # This command checks that the built commands can function together for
  16. # simple scenarios. It does not require Docker.
  17. set -o errexit
  18. set -o nounset
  19. set -o pipefail
  20. # start the cache mutation detector by default so that cache mutators will be found
  21. KUBE_CACHE_MUTATION_DETECTOR="${KUBE_CACHE_MUTATION_DETECTOR:-true}"
  22. export KUBE_CACHE_MUTATION_DETECTOR
  23. # panic the server on watch decode errors since they are considered coder mistakes
  24. KUBE_PANIC_WATCH_DECODE_ERROR="${KUBE_PANIC_WATCH_DECODE_ERROR:-true}"
  25. export KUBE_PANIC_WATCH_DECODE_ERROR
  26. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
  27. source "${KUBE_ROOT}/hack/lib/init.sh"
  28. source "${KUBE_ROOT}/hack/lib/test.sh"
  29. source "${KUBE_ROOT}/test/cmd/legacy-script.sh"
  30. # Runs kube-apiserver
  31. #
  32. # Exports:
  33. # APISERVER_PID
  34. function run_kube_apiserver() {
  35. kube::log::status "Building kube-apiserver"
  36. make -C "${KUBE_ROOT}" WHAT="cmd/kube-apiserver"
  37. # Start kube-apiserver
  38. kube::log::status "Starting kube-apiserver"
  39. # Admission Controllers to invoke prior to persisting objects in cluster
  40. ENABLE_ADMISSION_PLUGINS="LimitRanger,ResourceQuota"
  41. DISABLE_ADMISSION_PLUGINS="ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,StorageObjectInUseProtection"
  42. # Include RBAC (to exercise bootstrapping), and AlwaysAllow to allow all actions
  43. AUTHORIZATION_MODE="RBAC,AlwaysAllow"
  44. # Enable features
  45. ENABLE_FEATURE_GATES="ServerSideApply=true"
  46. "${KUBE_OUTPUT_HOSTBIN}/kube-apiserver" \
  47. --insecure-bind-address="127.0.0.1" \
  48. --bind-address="127.0.0.1" \
  49. --insecure-port="${API_PORT}" \
  50. --authorization-mode="${AUTHORIZATION_MODE}" \
  51. --secure-port="${SECURE_API_PORT}" \
  52. --feature-gates="${ENABLE_FEATURE_GATES}" \
  53. --enable-admission-plugins="${ENABLE_ADMISSION_PLUGINS}" \
  54. --disable-admission-plugins="${DISABLE_ADMISSION_PLUGINS}" \
  55. --etcd-servers="http://${ETCD_HOST}:${ETCD_PORT}" \
  56. --runtime-config=api/v1 \
  57. --storage-media-type="${KUBE_TEST_API_STORAGE_TYPE-}" \
  58. --cert-dir="${TMPDIR:-/tmp/}" \
  59. --service-cluster-ip-range="10.0.0.0/24" \
  60. --token-auth-file=hack/testdata/auth-tokens.csv 1>&2 &
  61. export APISERVER_PID=$!
  62. kube::util::wait_for_url "http://127.0.0.1:${API_PORT}/healthz" "apiserver"
  63. }
  64. # Runs run_kube_controller_manager
  65. #
  66. # Exports:
  67. # CTLRMGR_PID
  68. function run_kube_controller_manager() {
  69. kube::log::status "Building kube-controller-manager"
  70. make -C "${KUBE_ROOT}" WHAT="cmd/kube-controller-manager"
  71. # Start controller manager
  72. kube::log::status "Starting controller-manager"
  73. "${KUBE_OUTPUT_HOSTBIN}/kube-controller-manager" \
  74. --port="${CTLRMGR_PORT}" \
  75. --kube-api-content-type="${KUBE_TEST_API_TYPE-}" \
  76. --master="127.0.0.1:${API_PORT}" 1>&2 &
  77. export CTLRMGR_PID=$!
  78. kube::util::wait_for_url "http://127.0.0.1:${CTLRMGR_PORT}/healthz" "controller-manager"
  79. }
  80. # Creates a node object with name 127.0.0.1. This is required because we do not
  81. # run kubelet.
  82. #
  83. # Exports:
  84. # SUPPORTED_RESOURCES(Array of all resources supported by the apiserver).
  85. function create_node() {
  86. kubectl create -f - -s "http://127.0.0.1:${API_PORT}" << __EOF__
  87. {
  88. "kind": "Node",
  89. "apiVersion": "v1",
  90. "metadata": {
  91. "name": "127.0.0.1"
  92. },
  93. "status": {
  94. "capacity": {
  95. "memory": "1Gi"
  96. }
  97. }
  98. }
  99. __EOF__
  100. }
  101. # Run it if:
  102. # 1) $WHAT is empty
  103. # 2) $WHAT is not empty and kubeadm is part of $WHAT
  104. WHAT=${WHAT:-}
  105. if [[ ${WHAT} == "" || ${WHAT} =~ .*kubeadm.* ]] ; then
  106. kube::log::status "Running kubeadm tests"
  107. run_kubeadm_tests
  108. # if we ONLY want to run kubeadm, then exit here.
  109. if [[ ${WHAT} == "kubeadm" ]]; then
  110. kube::log::status "TESTS PASSED"
  111. exit 0
  112. fi
  113. fi
  114. kube::log::status "Running kubectl tests for kube-apiserver"
  115. setup
  116. run_kube_apiserver
  117. run_kube_controller_manager
  118. create_node
  119. export SUPPORTED_RESOURCES=("*")
  120. # WARNING: Do not wrap this call in a subshell to capture output, e.g. output=$(runTests)
  121. # Doing so will suppress errexit behavior inside runTests
  122. runTests
  123. kube::log::status "TESTS PASSED"