legacy-script.sh 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. #!/usr/bin/env bash
  2. # Copyright 2016 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 contains util code for testing kubectl.
  16. set -o errexit
  17. set -o nounset
  18. set -o pipefail
  19. # Set locale to ensure english responses from kubectl commands
  20. export LANG=C
  21. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
  22. # Expects the following has already been done by whatever sources this script
  23. # source "${KUBE_ROOT}/hack/lib/init.sh"
  24. # source "${KUBE_ROOT}/hack/lib/test.sh"
  25. source "${KUBE_ROOT}/test/cmd/apply.sh"
  26. source "${KUBE_ROOT}/test/cmd/apps.sh"
  27. source "${KUBE_ROOT}/test/cmd/authorization.sh"
  28. source "${KUBE_ROOT}/test/cmd/batch.sh"
  29. source "${KUBE_ROOT}/test/cmd/certificate.sh"
  30. source "${KUBE_ROOT}/test/cmd/core.sh"
  31. source "${KUBE_ROOT}/test/cmd/crd.sh"
  32. source "${KUBE_ROOT}/test/cmd/create.sh"
  33. source "${KUBE_ROOT}/test/cmd/delete.sh"
  34. source "${KUBE_ROOT}/test/cmd/diff.sh"
  35. source "${KUBE_ROOT}/test/cmd/discovery.sh"
  36. source "${KUBE_ROOT}/test/cmd/exec.sh"
  37. source "${KUBE_ROOT}/test/cmd/generic-resources.sh"
  38. source "${KUBE_ROOT}/test/cmd/get.sh"
  39. source "${KUBE_ROOT}/test/cmd/kubeadm.sh"
  40. source "${KUBE_ROOT}/test/cmd/kubeconfig.sh"
  41. source "${KUBE_ROOT}/test/cmd/node-management.sh"
  42. source "${KUBE_ROOT}/test/cmd/plugins.sh"
  43. source "${KUBE_ROOT}/test/cmd/proxy.sh"
  44. source "${KUBE_ROOT}/test/cmd/rbac.sh"
  45. source "${KUBE_ROOT}/test/cmd/request-timeout.sh"
  46. source "${KUBE_ROOT}/test/cmd/run.sh"
  47. source "${KUBE_ROOT}/test/cmd/save-config.sh"
  48. source "${KUBE_ROOT}/test/cmd/storage.sh"
  49. source "${KUBE_ROOT}/test/cmd/template-output.sh"
  50. source "${KUBE_ROOT}/test/cmd/version.sh"
  51. source "${KUBE_ROOT}/test/cmd/wait.sh"
  52. ETCD_HOST=${ETCD_HOST:-127.0.0.1}
  53. ETCD_PORT=${ETCD_PORT:-2379}
  54. API_PORT=${API_PORT:-8080}
  55. SECURE_API_PORT=${SECURE_API_PORT:-6443}
  56. API_HOST=${API_HOST:-127.0.0.1}
  57. KUBELET_HEALTHZ_PORT=${KUBELET_HEALTHZ_PORT:-10248}
  58. CTLRMGR_PORT=${CTLRMGR_PORT:-10252}
  59. PROXY_HOST=127.0.0.1 # kubectl only serves on localhost.
  60. IMAGE_NGINX="k8s.gcr.io/nginx:1.7.9"
  61. export IMAGE_DEPLOYMENT_R1="k8s.gcr.io/nginx:test-cmd" # deployment-revision1.yaml
  62. export IMAGE_DEPLOYMENT_R2="$IMAGE_NGINX" # deployment-revision2.yaml
  63. export IMAGE_PERL="k8s.gcr.io/perl"
  64. export IMAGE_PAUSE_V2="k8s.gcr.io/pause:2.0"
  65. export IMAGE_DAEMONSET_R2="k8s.gcr.io/pause:latest"
  66. export IMAGE_DAEMONSET_R2_2="k8s.gcr.io/nginx:test-cmd" # rollingupdate-daemonset-rv2.yaml
  67. export IMAGE_STATEFULSET_R1="k8s.gcr.io/nginx-slim:0.7"
  68. export IMAGE_STATEFULSET_R2="k8s.gcr.io/nginx-slim:0.8"
  69. # Expose kubectl directly for readability
  70. PATH="${KUBE_OUTPUT_HOSTBIN}":$PATH
  71. # Define variables for resource types to prevent typos.
  72. clusterroles="clusterroles"
  73. configmaps="configmaps"
  74. csr="csr"
  75. deployments="deployments"
  76. namespaces="namespaces"
  77. nodes="nodes"
  78. persistentvolumeclaims="persistentvolumeclaims"
  79. persistentvolumes="persistentvolumes"
  80. pods="pods"
  81. podtemplates="podtemplates"
  82. replicasets="replicasets"
  83. replicationcontrollers="replicationcontrollers"
  84. roles="roles"
  85. secrets="secrets"
  86. serviceaccounts="serviceaccounts"
  87. services="services"
  88. statefulsets="statefulsets"
  89. storageclass="storageclass"
  90. subjectaccessreviews="subjectaccessreviews"
  91. customresourcedefinitions="customresourcedefinitions"
  92. daemonsets="daemonsets"
  93. controllerrevisions="controllerrevisions"
  94. job="jobs"
  95. # include shell2junit library
  96. sh2ju="${KUBE_ROOT}/third_party/forked/shell2junit/sh2ju.sh"
  97. if [[ -f "${sh2ju}" ]]; then
  98. source "${sh2ju}"
  99. else
  100. echo "failed to find third_party/forked/shell2junit/sh2ju.sh"
  101. exit 1
  102. fi
  103. # record_command runs the command and records its output/error messages in junit format
  104. # it expects the first to be the name of the command
  105. # Example:
  106. # record_command run_kubectl_tests
  107. #
  108. # WARNING: Variable changes in the command will NOT be effective after record_command returns.
  109. # This is because the command runs in subshell.
  110. function record_command() {
  111. set +o nounset
  112. set +o errexit
  113. local name="$1"
  114. local output="${KUBE_JUNIT_REPORT_DIR:-/tmp/junit-results}"
  115. echo "Recording: ${name}"
  116. echo "Running command: $*"
  117. juLog -output="${output}" -class="test-cmd" -name="${name}" "$@"
  118. local exitCode=$?
  119. if [[ ${exitCode} -ne 0 ]]; then
  120. # Record failures for any non-canary commands
  121. if [ "${name}" != "record_command_canary" ]; then
  122. echo "Error when running ${name}"
  123. foundError="${foundError}""${name}"", "
  124. fi
  125. elif [ "${name}" == "record_command_canary" ]; then
  126. # If the canary command passed, fail
  127. echo "record_command_canary succeeded unexpectedly"
  128. foundError="${foundError}""${name}"", "
  129. fi
  130. set -o nounset
  131. set -o errexit
  132. }
  133. # Ensure our record_command stack correctly propagates and detects errexit failures in invoked commands - see https://issue.k8s.io/84871
  134. foundError=""
  135. function record_command_canary()
  136. {
  137. set -o nounset
  138. set -o errexit
  139. bogus-expected-to-fail
  140. set +o nounset
  141. set +o errexit
  142. }
  143. KUBE_JUNIT_REPORT_DIR=$(mktemp -d /tmp/record_command_canary.XXXXX) record_command record_command_canary
  144. if [[ -n "${foundError}" ]]; then
  145. echo "FAILED TESTS: record_command_canary"
  146. exit 1
  147. fi
  148. # Stops the running kubectl proxy, if there is one.
  149. function stop-proxy()
  150. {
  151. [[ -n "${PROXY_PORT-}" ]] && kube::log::status "Stopping proxy on port ${PROXY_PORT}"
  152. [[ -n "${PROXY_PID-}" ]] && kill "${PROXY_PID}" 1>&2 2>/dev/null
  153. [[ -n "${PROXY_PORT_FILE-}" ]] && rm -f "${PROXY_PORT_FILE}"
  154. PROXY_PID=
  155. PROXY_PORT=
  156. PROXY_PORT_FILE=
  157. }
  158. # Starts "kubect proxy" to test the client proxy. $1: api_prefix
  159. function start-proxy()
  160. {
  161. stop-proxy
  162. PROXY_PORT_FILE=$(mktemp proxy-port.out.XXXXX)
  163. kube::log::status "Starting kubectl proxy on random port; output file in ${PROXY_PORT_FILE}; args: ${1-}"
  164. if [ $# -eq 0 ]; then
  165. kubectl proxy --port=0 --www=. 1>"${PROXY_PORT_FILE}" 2>&1 &
  166. else
  167. kubectl proxy --port=0 --www=. --api-prefix="$1" 1>"${PROXY_PORT_FILE}" 2>&1 &
  168. fi
  169. PROXY_PID=$!
  170. PROXY_PORT=
  171. local attempts=0
  172. while [[ -z ${PROXY_PORT} ]]; do
  173. if (( attempts > 9 )); then
  174. kill "${PROXY_PID}"
  175. kube::log::error_exit "Couldn't start proxy. Failed to read port after ${attempts} tries. Got: $(cat "${PROXY_PORT_FILE}")"
  176. fi
  177. sleep .5
  178. kube::log::status "Attempt ${attempts} to read ${PROXY_PORT_FILE}..."
  179. PROXY_PORT=$(sed 's/.*Starting to serve on 127.0.0.1:\([0-9]*\)$/\1/'< "${PROXY_PORT_FILE}")
  180. attempts=$((attempts+1))
  181. done
  182. kube::log::status "kubectl proxy running on port ${PROXY_PORT}"
  183. # We try checking kubectl proxy 30 times with 1s delays to avoid occasional
  184. # failures.
  185. if [ $# -eq 0 ]; then
  186. kube::util::wait_for_url "http://127.0.0.1:${PROXY_PORT}/healthz" "kubectl proxy"
  187. else
  188. kube::util::wait_for_url "http://127.0.0.1:${PROXY_PORT}/$1/healthz" "kubectl proxy --api-prefix=$1"
  189. fi
  190. }
  191. function cleanup()
  192. {
  193. [[ -n "${APISERVER_PID-}" ]] && kill "${APISERVER_PID}" 1>&2 2>/dev/null
  194. [[ -n "${CTLRMGR_PID-}" ]] && kill "${CTLRMGR_PID}" 1>&2 2>/dev/null
  195. [[ -n "${KUBELET_PID-}" ]] && kill "${KUBELET_PID}" 1>&2 2>/dev/null
  196. stop-proxy
  197. kube::etcd::cleanup
  198. rm -rf "${KUBE_TEMP}"
  199. local junit_dir="${KUBE_JUNIT_REPORT_DIR:-/tmp/junit-results}"
  200. echo "junit report dir:" "${junit_dir}"
  201. kube::log::status "Clean up complete"
  202. }
  203. # Executes curl against the proxy. $1 is the path to use, $2 is the desired
  204. # return code. Prints a helpful message on failure.
  205. function check-curl-proxy-code()
  206. {
  207. local status
  208. local -r address=$1
  209. local -r desired=$2
  210. local -r full_address="${PROXY_HOST}:${PROXY_PORT}${address}"
  211. status=$(curl -w "%{http_code}" --silent --output /dev/null "${full_address}")
  212. if [ "${status}" == "${desired}" ]; then
  213. return 0
  214. fi
  215. echo "For address ${full_address}, got ${status} but wanted ${desired}"
  216. return 1
  217. }
  218. # TODO: Remove this function when we do the retry inside the kubectl commands. See #15333.
  219. function kubectl-with-retry()
  220. {
  221. ERROR_FILE="${KUBE_TEMP}/kubectl-error"
  222. preserve_err_file=${PRESERVE_ERR_FILE:-false}
  223. for count in {0..3}; do
  224. kubectl "$@" 2> "${ERROR_FILE}" || true
  225. if grep -q "the object has been modified" "${ERROR_FILE}"; then
  226. kube::log::status "retry $1, error: $(cat "${ERROR_FILE}")"
  227. rm "${ERROR_FILE}"
  228. sleep $((2**count))
  229. else
  230. if [ "$preserve_err_file" != true ] ; then
  231. rm "${ERROR_FILE}"
  232. fi
  233. break
  234. fi
  235. done
  236. }
  237. # Waits for the pods with the given label to match the list of names. Don't call
  238. # this function unless you know the exact pod names, or expect no pods.
  239. # $1: label to match
  240. # $2: list of pod names sorted by name
  241. # Example invocation:
  242. # wait-for-pods-with-label "app=foo" "nginx-0nginx-1"
  243. function wait-for-pods-with-label()
  244. {
  245. local i
  246. for i in $(seq 1 10); do
  247. kubeout=$(kubectl get po -l "$1" --output=go-template --template='{{range.items}}{{.metadata.name}}{{end}}' --sort-by metadata.name "${kube_flags[@]}")
  248. if [[ $kubeout = "$2" ]]; then
  249. return
  250. fi
  251. echo Waiting for pods: "$2", found "$kubeout"
  252. sleep "$i"
  253. done
  254. kube::log::error_exit "Timeout waiting for pods with label $1"
  255. }
  256. # Code to be run before running the tests.
  257. setup() {
  258. kube::util::trap_add cleanup EXIT SIGINT
  259. kube::util::ensure-temp-dir
  260. # ensure ~/.kube/config isn't loaded by tests
  261. HOME="${KUBE_TEMP}"
  262. kube::etcd::start
  263. # Find a standard sed instance for use with edit scripts
  264. kube::util::ensure-gnu-sed
  265. kube::log::status "Building kubectl"
  266. make -C "${KUBE_ROOT}" WHAT="cmd/kubectl"
  267. # Check kubectl
  268. kube::log::status "Running kubectl with no options"
  269. "${KUBE_OUTPUT_HOSTBIN}/kubectl"
  270. # TODO: we need to note down the current default namespace and set back to this
  271. # namespace after the tests are done.
  272. kubectl config view
  273. CONTEXT="test"
  274. kubectl config set-context "${CONTEXT}"
  275. kubectl config use-context "${CONTEXT}"
  276. kube::log::status "Setup complete"
  277. }
  278. # Runs all kubectl tests.
  279. # Requires an env var SUPPORTED_RESOURCES which is a comma separated list of
  280. # resources for which tests should be run.
  281. runTests() {
  282. foundError=""
  283. if [ -z "${SUPPORTED_RESOURCES:-}" ]; then
  284. echo "Need to set SUPPORTED_RESOURCES env var. It is a list of resources that are supported and hence should be tested. Set it to (*) to test all resources"
  285. exit 1
  286. fi
  287. kube::log::status "Checking kubectl version"
  288. kubectl version
  289. # Generate a random namespace name, based on the current time (to make
  290. # debugging slightly easier) and a random number. Don't use `date +%N`
  291. # because that doesn't work on OSX.
  292. create_and_use_new_namespace() {
  293. local ns_name
  294. ns_name="namespace-$(date +%s)-${RANDOM}"
  295. kube::log::status "Creating namespace ${ns_name}"
  296. kubectl create namespace "${ns_name}"
  297. kubectl config set-context "${CONTEXT}" --namespace="${ns_name}"
  298. }
  299. kube_flags=(
  300. '-s' "http://127.0.0.1:${API_PORT}"
  301. )
  302. # token defined in hack/testdata/auth-tokens.csv
  303. kube_flags_with_token=(
  304. '-s' "https://127.0.0.1:${SECURE_API_PORT}" '--token=admin-token' '--insecure-skip-tls-verify=true'
  305. )
  306. if [[ -z "${ALLOW_SKEW:-}" ]]; then
  307. kube_flags+=('--match-server-version')
  308. kube_flags_with_token+=('--match-server-version')
  309. fi
  310. if kube::test::if_supports_resource "${nodes}" ; then
  311. [ "$(kubectl get nodes -o go-template='{{ .apiVersion }}' "${kube_flags[@]}")" == "v1" ]
  312. fi
  313. # Define helper variables for fields to prevent typos.
  314. # They will be used in some other files under test/cmd,
  315. # Let's export them as https://github.com/koalaman/shellcheck/wiki/SC2034 suggested.
  316. export id_field=".metadata.name"
  317. export labels_field=".metadata.labels"
  318. export annotations_field=".metadata.annotations"
  319. export service_selector_field=".spec.selector"
  320. export rc_replicas_field=".spec.replicas"
  321. export rc_status_replicas_field=".status.replicas"
  322. export rc_container_image_field=".spec.template.spec.containers"
  323. export rs_replicas_field=".spec.replicas"
  324. export port_field="(index .spec.ports 0).port"
  325. export port_name="(index .spec.ports 0).name"
  326. export second_port_field="(index .spec.ports 1).port"
  327. export second_port_name="(index .spec.ports 1).name"
  328. export image_field="(index .spec.containers 0).image"
  329. export pod_container_name_field="(index .spec.containers 0).name"
  330. export container_name_field="(index .spec.template.spec.containers 0).name"
  331. export hpa_min_field=".spec.minReplicas"
  332. export hpa_max_field=".spec.maxReplicas"
  333. export hpa_cpu_field=".spec.targetCPUUtilizationPercentage"
  334. export template_labels=".spec.template.metadata.labels.name"
  335. export statefulset_replicas_field=".spec.replicas"
  336. export statefulset_observed_generation=".status.observedGeneration"
  337. export job_parallelism_field=".spec.parallelism"
  338. export deployment_replicas=".spec.replicas"
  339. export secret_data=".data"
  340. export secret_type=".type"
  341. export change_cause_annotation='.*kubernetes.io/change-cause.*'
  342. export pdb_min_available=".spec.minAvailable"
  343. export pdb_max_unavailable=".spec.maxUnavailable"
  344. export generation_field=".metadata.generation"
  345. export container_len="(len .spec.template.spec.containers)"
  346. export image_field0="(index .spec.template.spec.containers 0).image"
  347. export image_field1="(index .spec.template.spec.containers 1).image"
  348. # Make sure "default" namespace exists.
  349. if kube::test::if_supports_resource "${namespaces}" ; then
  350. output_message=$(kubectl get "${kube_flags[@]}" namespaces)
  351. if ! grep -q "default" <<< "${output_message}"; then
  352. # Create default namespace
  353. kubectl create "${kube_flags[@]}" ns default
  354. fi
  355. fi
  356. # Make sure "kubernetes" service exists.
  357. if kube::test::if_supports_resource "${services}" ; then
  358. # Attempt to create the kubernetes service, tolerating failure (since it might already exist)
  359. kubectl create "${kube_flags[@]}" -f hack/testdata/kubernetes-service.yaml || true
  360. # Require the service to exist (either we created it or the API server did)
  361. kubectl get "${kube_flags[@]}" -f hack/testdata/kubernetes-service.yaml
  362. fi
  363. cleanup_tests(){
  364. kube::test::clear_all
  365. if [[ -n "${foundError}" ]]; then
  366. echo "FAILED TESTS: ""${foundError}"
  367. exit 1
  368. fi
  369. }
  370. if [[ -n "${WHAT-}" ]]; then
  371. for pkg in ${WHAT}
  372. do
  373. # running of kubeadm is captured in hack/make-targets/test-cmd.sh
  374. if [[ "${pkg}" != "kubeadm" ]]; then
  375. record_command "run_${pkg}_tests"
  376. fi
  377. done
  378. cleanup_tests
  379. return
  380. fi
  381. #########################
  382. # Kubectl version #
  383. #########################
  384. record_command run_kubectl_version_tests
  385. #######################
  386. # kubectl config set #
  387. #######################
  388. record_command run_kubectl_config_set_tests
  389. #######################
  390. # kubectl local proxy #
  391. #######################
  392. record_command run_kubectl_local_proxy_tests
  393. #########################
  394. # RESTMapper evaluation #
  395. #########################
  396. record_command run_RESTMapper_evaluation_tests
  397. # find all resources
  398. kubectl "${kube_flags[@]}" api-resources
  399. # find all namespaced resources that support list by name and get them
  400. kubectl "${kube_flags[@]}" api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl "${kube_flags[@]}" get -o name
  401. ################
  402. # Cluster Role #
  403. ################
  404. if kube::test::if_supports_resource "${clusterroles}" ; then
  405. record_command run_clusterroles_tests
  406. fi
  407. ########
  408. # Role #
  409. ########
  410. if kube::test::if_supports_resource "${roles}" ; then
  411. record_command run_role_tests
  412. fi
  413. #########################
  414. # Assert short name #
  415. #########################
  416. record_command run_assert_short_name_tests
  417. #########################
  418. # Assert categories #
  419. #########################
  420. ## test if a category is exported during discovery
  421. if kube::test::if_supports_resource "${pods}" ; then
  422. record_command run_assert_categories_tests
  423. fi
  424. ###########################
  425. # POD creation / deletion #
  426. ###########################
  427. if kube::test::if_supports_resource "${pods}" ; then
  428. record_command run_pod_tests
  429. fi
  430. if kube::test::if_supports_resource "${pods}" ; then
  431. record_command run_save_config_tests
  432. fi
  433. if kube::test::if_supports_resource "${pods}" ; then
  434. record_command run_kubectl_create_error_tests
  435. fi
  436. if kube::test::if_supports_resource "${pods}" ; then
  437. record_command run_kubectl_apply_tests
  438. record_command run_kubectl_run_tests
  439. record_command run_kubectl_create_filter_tests
  440. fi
  441. if kube::test::if_supports_resource "${deployments}" ; then
  442. record_command run_kubectl_apply_deployments_tests
  443. fi
  444. ################
  445. # Kubectl diff #
  446. ################
  447. record_command run_kubectl_diff_tests
  448. record_command run_kubectl_diff_same_names
  449. ###############
  450. # Kubectl get #
  451. ###############
  452. if kube::test::if_supports_resource "${pods}" ; then
  453. record_command run_kubectl_get_tests
  454. fi
  455. ################
  456. # Kubectl exec #
  457. ################
  458. if kube::test::if_supports_resource "${pods}"; then
  459. record_command run_kubectl_exec_pod_tests
  460. if kube::test::if_supports_resource "${replicasets}" && kube::test::if_supports_resource "${configmaps}"; then
  461. record_command run_kubectl_exec_resource_name_tests
  462. fi
  463. fi
  464. ######################
  465. # Create #
  466. ######################
  467. if kube::test::if_supports_resource "${secrets}" ; then
  468. record_command run_create_secret_tests
  469. fi
  470. if kube::test::if_supports_resource "${deployments}"; then
  471. record_command run_kubectl_create_kustomization_directory_tests
  472. fi
  473. ######################
  474. # Delete #
  475. ######################
  476. if kube::test::if_supports_resource "${configmaps}" ; then
  477. record_command run_kubectl_delete_allnamespaces_tests
  478. fi
  479. ##################
  480. # Global timeout #
  481. ##################
  482. if kube::test::if_supports_resource "${pods}" ; then
  483. record_command run_kubectl_request_timeout_tests
  484. fi
  485. #####################################
  486. # CustomResourceDefinitions #
  487. #####################################
  488. # customresourcedefinitions cleanup after themselves.
  489. if kube::test::if_supports_resource "${customresourcedefinitions}" ; then
  490. record_command run_crd_tests
  491. fi
  492. #####################################
  493. # Recursive Resources via directory #
  494. #####################################
  495. if kube::test::if_supports_resource "${pods}" ; then
  496. run_recursive_resources_tests
  497. fi
  498. ##############
  499. # Namespaces #
  500. ##############
  501. if kube::test::if_supports_resource "${namespaces}" ; then
  502. record_command run_namespace_tests
  503. fi
  504. ###########
  505. # Secrets #
  506. ###########
  507. if kube::test::if_supports_resource "${namespaces}" ; then
  508. if kube::test::if_supports_resource "${secrets}" ; then
  509. record_command run_secrets_test
  510. fi
  511. fi
  512. ######################
  513. # ConfigMap #
  514. ######################
  515. if kube::test::if_supports_resource "${namespaces}"; then
  516. if kube::test::if_supports_resource "${configmaps}" ; then
  517. record_command run_configmap_tests
  518. fi
  519. fi
  520. ####################
  521. # Client Config #
  522. ####################
  523. record_command run_client_config_tests
  524. ####################
  525. # Service Accounts #
  526. ####################
  527. if kube::test::if_supports_resource "${namespaces}" && kube::test::if_supports_resource "${serviceaccounts}" ; then
  528. record_command run_service_accounts_tests
  529. fi
  530. ####################
  531. # Job #
  532. ####################
  533. if kube::test::if_supports_resource "${job}" ; then
  534. record_command run_job_tests
  535. record_command run_create_job_tests
  536. fi
  537. #################
  538. # Pod templates #
  539. #################
  540. if kube::test::if_supports_resource "${podtemplates}" ; then
  541. record_command run_pod_templates_tests
  542. fi
  543. ############
  544. # Services #
  545. ############
  546. if kube::test::if_supports_resource "${services}" ; then
  547. record_command run_service_tests
  548. fi
  549. ##################
  550. # DaemonSets #
  551. ##################
  552. if kube::test::if_supports_resource "${daemonsets}" ; then
  553. record_command run_daemonset_tests
  554. if kube::test::if_supports_resource "${controllerrevisions}"; then
  555. record_command run_daemonset_history_tests
  556. fi
  557. fi
  558. ###########################
  559. # Replication controllers #
  560. ###########################
  561. if kube::test::if_supports_resource "${namespaces}" ; then
  562. if kube::test::if_supports_resource "${replicationcontrollers}" ; then
  563. record_command run_rc_tests
  564. fi
  565. fi
  566. ######################
  567. # Deployments #
  568. ######################
  569. if kube::test::if_supports_resource "${deployments}" ; then
  570. record_command run_deployment_tests
  571. fi
  572. ######################
  573. # Replica Sets #
  574. ######################
  575. if kube::test::if_supports_resource "${replicasets}" ; then
  576. record_command run_rs_tests
  577. fi
  578. #################
  579. # Stateful Sets #
  580. #################
  581. if kube::test::if_supports_resource "${statefulsets}" ; then
  582. record_command run_stateful_set_tests
  583. if kube::test::if_supports_resource "${controllerrevisions}"; then
  584. record_command run_statefulset_history_tests
  585. fi
  586. fi
  587. ######################
  588. # Lists #
  589. ######################
  590. if kube::test::if_supports_resource "${services}" ; then
  591. if kube::test::if_supports_resource "${deployments}" ; then
  592. record_command run_lists_tests
  593. fi
  594. fi
  595. ######################
  596. # Multiple Resources #
  597. ######################
  598. if kube::test::if_supports_resource "${services}" ; then
  599. if kube::test::if_supports_resource "${replicationcontrollers}" ; then
  600. record_command run_multi_resources_tests
  601. fi
  602. fi
  603. ######################
  604. # Persistent Volumes #
  605. ######################
  606. if kube::test::if_supports_resource "${persistentvolumes}" ; then
  607. record_command run_persistent_volumes_tests
  608. fi
  609. ############################
  610. # Persistent Volume Claims #
  611. ############################
  612. if kube::test::if_supports_resource "${persistentvolumeclaims}" ; then
  613. record_command run_persistent_volume_claims_tests
  614. fi
  615. ############################
  616. # Storage Classes #
  617. ############################
  618. if kube::test::if_supports_resource "${storageclass}" ; then
  619. record_command run_storage_class_tests
  620. fi
  621. #########
  622. # Nodes #
  623. #########
  624. if kube::test::if_supports_resource "${nodes}" ; then
  625. record_command run_nodes_tests
  626. fi
  627. ########################
  628. # authorization.k8s.io #
  629. ########################
  630. if kube::test::if_supports_resource "${subjectaccessreviews}" ; then
  631. record_command run_authorization_tests
  632. fi
  633. # kubectl auth can-i
  634. # kube-apiserver is started with authorization mode AlwaysAllow, so kubectl can-i always returns yes
  635. if kube::test::if_supports_resource "${subjectaccessreviews}" ; then
  636. output_message=$(kubectl auth can-i '*' '*' 2>&1 "${kube_flags[@]}")
  637. kube::test::if_has_string "${output_message}" "yes"
  638. output_message=$(kubectl auth can-i get pods --subresource=log 2>&1 "${kube_flags[@]}")
  639. kube::test::if_has_string "${output_message}" "yes"
  640. output_message=$(kubectl auth can-i get invalid_resource 2>&1 "${kube_flags[@]}")
  641. kube::test::if_has_string "${output_message}" "the server doesn't have a resource type"
  642. output_message=$(kubectl auth can-i get /logs/ 2>&1 "${kube_flags[@]}")
  643. kube::test::if_has_string "${output_message}" "yes"
  644. output_message=$(! kubectl auth can-i get /logs/ --subresource=log 2>&1 "${kube_flags[@]}")
  645. kube::test::if_has_string "${output_message}" "subresource can not be used with NonResourceURL"
  646. output_message=$(kubectl auth can-i list jobs.batch/bar -n foo --quiet 2>&1 "${kube_flags[@]}")
  647. kube::test::if_empty_string "${output_message}"
  648. output_message=$(kubectl auth can-i get pods --subresource=log 2>&1 "${kube_flags[@]}"; echo $?)
  649. kube::test::if_has_string "${output_message}" '0'
  650. output_message=$(kubectl auth can-i get pods --subresource=log --quiet 2>&1 "${kube_flags[@]}"; echo $?)
  651. kube::test::if_has_string "${output_message}" '0'
  652. # kubectl auth can-i get '*' does not warn about namespaced scope or print an error
  653. output_message=$(kubectl auth can-i get '*' 2>&1 "${kube_flags[@]}")
  654. kube::test::if_has_not_string "${output_message}" "Warning"
  655. # kubectl auth can-i get foo does not print a namespaced warning message, and only prints a single lookup error
  656. output_message=$(kubectl auth can-i get foo 2>&1 "${kube_flags[@]}")
  657. kube::test::if_has_string "${output_message}" "Warning: the server doesn't have a resource type 'foo'"
  658. kube::test::if_has_not_string "${output_message}" "Warning: resource 'foo' is not namespace scoped"
  659. # kubectl auth can-i get pods does not print a namespaced warning message or a lookup error
  660. output_message=$(kubectl auth can-i get pods 2>&1 "${kube_flags[@]}")
  661. kube::test::if_has_not_string "${output_message}" "Warning"
  662. # kubectl auth can-i get nodes prints a namespaced warning message
  663. output_message=$(kubectl auth can-i get nodes 2>&1 "${kube_flags[@]}")
  664. kube::test::if_has_string "${output_message}" "Warning: resource 'nodes' is not namespace scoped"
  665. # kubectl auth can-i get nodes --all-namespaces does not print a namespaced warning message
  666. output_message=$(kubectl auth can-i get nodes --all-namespaces 2>&1 "${kube_flags[@]}")
  667. kube::test::if_has_not_string "${output_message}" "Warning: resource 'nodes' is not namespace scoped"
  668. fi
  669. # kubectl auth reconcile
  670. if kube::test::if_supports_resource "${clusterroles}" ; then
  671. kubectl auth reconcile "${kube_flags[@]}" -f test/fixtures/pkg/kubectl/cmd/auth/rbac-resource-plus.yaml
  672. kube::test::get_object_assert 'rolebindings -n some-other-random -l test-cmd=auth' "{{range.items}}{{$id_field}}:{{end}}" 'testing-RB:'
  673. kube::test::get_object_assert 'roles -n some-other-random -l test-cmd=auth' "{{range.items}}{{$id_field}}:{{end}}" 'testing-R:'
  674. kube::test::get_object_assert 'clusterrolebindings -l test-cmd=auth' "{{range.items}}{{$id_field}}:{{end}}" 'testing-CRB:'
  675. kube::test::get_object_assert 'clusterroles -l test-cmd=auth' "{{range.items}}{{$id_field}}:{{end}}" 'testing-CR:'
  676. failure_message=$(! kubectl auth reconcile "${kube_flags[@]}" -f test/fixtures/pkg/kubectl/cmd/auth/rbac-v1beta1.yaml 2>&1 )
  677. kube::test::if_has_string "${failure_message}" 'only rbac.authorization.k8s.io/v1 is supported'
  678. kubectl delete "${kube_flags[@]}" rolebindings,role,clusterroles,clusterrolebindings -n some-other-random -l test-cmd=auth
  679. fi
  680. #####################
  681. # Retrieve multiple #
  682. #####################
  683. if kube::test::if_supports_resource "${nodes}" ; then
  684. if kube::test::if_supports_resource "${services}" ; then
  685. record_command run_retrieve_multiple_tests
  686. fi
  687. fi
  688. #####################
  689. # Resource aliasing #
  690. #####################
  691. if kube::test::if_supports_resource "${services}" ; then
  692. if kube::test::if_supports_resource "${replicationcontrollers}" ; then
  693. record_command run_resource_aliasing_tests
  694. fi
  695. fi
  696. ###########
  697. # Explain #
  698. ###########
  699. if kube::test::if_supports_resource "${pods}" ; then
  700. record_command run_kubectl_explain_tests
  701. fi
  702. ###########
  703. # Swagger #
  704. ###########
  705. record_command run_swagger_tests
  706. #####################
  707. # Kubectl --sort-by #
  708. #####################
  709. if kube::test::if_supports_resource "${pods}" ; then
  710. record_command run_kubectl_sort_by_tests
  711. fi
  712. ############################
  713. # Kubectl --all-namespaces #
  714. ############################
  715. if kube::test::if_supports_resource "${pods}" ; then
  716. if kube::test::if_supports_resource "${nodes}" ; then
  717. record_command run_kubectl_all_namespace_tests
  718. fi
  719. fi
  720. ######################
  721. # kubectl --template #
  722. ######################
  723. if kube::test::if_supports_resource "${pods}" ; then
  724. record_command run_template_output_tests
  725. fi
  726. ################
  727. # Certificates #
  728. ################
  729. if kube::test::if_supports_resource "${csr}" ; then
  730. record_command run_certificates_tests
  731. fi
  732. ######################
  733. # Cluster Management #
  734. ######################
  735. if kube::test::if_supports_resource "${nodes}" ; then
  736. record_command run_cluster_management_tests
  737. fi
  738. ###########
  739. # Plugins #
  740. ###########
  741. record_command run_plugins_tests
  742. #################
  743. # Impersonation #
  744. #################
  745. record_command run_impersonation_tests
  746. ####################
  747. # kubectl wait #
  748. ####################
  749. record_command run_wait_tests
  750. cleanup_tests
  751. }