create.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. # Runs tests related to kubectl create --filename(-f) --selector(-l).
  19. run_kubectl_create_filter_tests() {
  20. set -o nounset
  21. set -o errexit
  22. create_and_use_new_namespace
  23. kube::log::status "Testing kubectl create filter"
  24. ## kubectl create -f with label selector should only create matching objects
  25. # Pre-Condition: no POD exists
  26. kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" ''
  27. # create
  28. kubectl create -l unique-label=bingbang -f hack/testdata/filter "${kube_flags[@]}"
  29. # check right pod exists
  30. kube::test::get_object_assert 'pods selector-test-pod' "{{${labels_field}.name}}" 'selector-test-pod'
  31. # check wrong pod doesn't exist
  32. output_message=$(! kubectl get pods selector-test-pod-dont-apply 2>&1 "${kube_flags[@]}")
  33. kube::test::if_has_string "${output_message}" 'pods "selector-test-pod-dont-apply" not found'
  34. # cleanup
  35. kubectl delete pods selector-test-pod
  36. set +o nounset
  37. set +o errexit
  38. }
  39. run_kubectl_create_error_tests() {
  40. set -o nounset
  41. set -o errexit
  42. create_and_use_new_namespace
  43. kube::log::status "Testing kubectl create with error"
  44. # Passing no arguments to create is an error
  45. ! kubectl create
  46. ## kubectl create should not panic on empty string lists in a template
  47. ERROR_FILE="${KUBE_TEMP}/validation-error"
  48. kubectl create -f hack/testdata/invalid-rc-with-empty-args.yaml "${kube_flags[@]}" 2> "${ERROR_FILE}" || true
  49. # Post-condition: should get an error reporting the empty string
  50. if grep -q "unknown object type \"nil\" in ReplicationController" "${ERROR_FILE}"; then
  51. kube::log::status "\"kubectl create with empty string list returns error as expected: $(cat ${ERROR_FILE})"
  52. else
  53. kube::log::status "\"kubectl create with empty string list returns unexpected error or non-error: $(cat ${ERROR_FILE})"
  54. exit 1
  55. fi
  56. rm "${ERROR_FILE}"
  57. # Posting a pod to namespaces should fail. Also tests --raw forcing the post location
  58. [ "$( kubectl convert -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml -o json | kubectl create "${kube_flags[@]}" --raw /api/v1/namespaces -f - --v=8 2>&1 | grep 'cannot be handled as a Namespace: converting (v1.Pod)')" ]
  59. [ "$( kubectl create "${kube_flags[@]}" --raw /api/v1/namespaces -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --edit 2>&1 | grep 'raw and --edit are mutually exclusive')" ]
  60. set +o nounset
  61. set +o errexit
  62. }
  63. # Runs kubectl create job tests
  64. run_create_job_tests() {
  65. set -o nounset
  66. set -o errexit
  67. create_and_use_new_namespace
  68. # Test kubectl create job
  69. kubectl create job test-job --image=k8s.gcr.io/nginx:test-cmd
  70. # Post-Condition: job nginx is created
  71. kube::test::get_object_assert 'job test-job' "{{$image_field0}}" 'k8s.gcr.io/nginx:test-cmd'
  72. # Clean up
  73. kubectl delete job test-job "${kube_flags[@]}"
  74. # Test kubectl create job with command
  75. kubectl create job test-job-pi "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(20)'
  76. kube::test::get_object_assert 'job test-job-pi' "{{$image_field0}}" $IMAGE_PERL
  77. # Clean up
  78. kubectl delete job test-job-pi
  79. # Test kubectl create job from cronjob
  80. # Pre-Condition: create a cronjob
  81. kubectl run test-pi --schedule="* */5 * * *" --generator=cronjob/v1beta1 "--image=$IMAGE_PERL" --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(10)'
  82. kubectl create job my-pi --from=cronjob/test-pi
  83. # Post-condition: container args contain expected command
  84. output_message=$(kubectl get job my-pi -o go-template='{{(index .spec.template.spec.containers 0).args}}' "${kube_flags[@]}")
  85. kube::test::if_has_string "${output_message}" "perl -Mbignum=bpi -wle print bpi(10)"
  86. # Clean up
  87. kubectl delete job my-pi
  88. kubectl delete cronjob test-pi
  89. set +o nounset
  90. set +o errexit
  91. }
  92. run_kubectl_create_kustomization_directory_tests() {
  93. set -o nounset
  94. set -o errexit
  95. ## kubectl create -k <dir> for kustomization directory
  96. # Pre-condition: no ConfigMap, Deployment, Service exist
  97. kube::test::get_object_assert configmaps "{{range.items}}{{$id_field}}:{{end}}" ''
  98. kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
  99. kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
  100. # Command
  101. kubectl create -k hack/testdata/kustomize
  102. # Post-condition: test-the-map, test-the-deployment, test-the-service exist
  103. # Check that all items in the list are printed
  104. kube::test::get_object_assert 'configmap test-the-map' "{{${id_field}}}" 'test-the-map'
  105. kube::test::get_object_assert 'deployment test-the-deployment' "{{${id_field}}}" 'test-the-deployment'
  106. kube::test::get_object_assert 'service test-the-service' "{{${id_field}}}" 'test-the-service'
  107. # cleanup
  108. kubectl delete -k hack/testdata/kustomize
  109. set +o nounset
  110. set +o errexit
  111. }