legacy-script.sh 28 KB

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