batch.sh 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_job_tests() {
  19. set -o nounset
  20. set -o errexit
  21. create_and_use_new_namespace
  22. kube::log::status "Testing job"
  23. ### Create a new namespace
  24. # Pre-condition: the test-jobs namespace does not exist
  25. kube::test::get_object_assert 'namespaces' "{{range.items}}{{ if eq ${id_field:?} \\\"test-jobs\\\" }}found{{end}}{{end}}:" ':'
  26. # Command
  27. kubectl create namespace test-jobs
  28. # Post-condition: namespace 'test-jobs' is created.
  29. kube::test::get_object_assert 'namespaces/test-jobs' "{{$id_field}}" 'test-jobs'
  30. # Pre-condition: cronjob does not exist
  31. kube::test::get_object_assert 'cronjob --namespace=test-jobs' "{{range.items}}{{ if eq $id_field \\\"pi\\\" }}found{{end}}{{end}}:" ':'
  32. # Dry-run create CronJob
  33. kubectl create cronjob pi --dry-run=client --schedule="59 23 31 2 *" --namespace=test-jobs "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]:?}"
  34. kubectl create cronjob pi --dry-run=server --schedule="59 23 31 2 *" --namespace=test-jobs "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]:?}"
  35. kube::test::get_object_assert 'cronjob' "{{range.items}}{{ if eq $id_field \\\"pi\\\" }}found{{end}}{{end}}:" ':'
  36. ### Create a cronjob in a specific namespace
  37. kubectl create cronjob pi --schedule="59 23 31 2 *" --namespace=test-jobs "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]:?}"
  38. # Post-Condition: assertion object exists
  39. kube::test::get_object_assert 'cronjob/pi --namespace=test-jobs' "{{$id_field}}" 'pi'
  40. kubectl get cronjob/pi --namespace=test-jobs
  41. kubectl describe cronjob/pi --namespace=test-jobs
  42. ### Create a job in dry-run mode
  43. output_message=$(kubectl create job test-job --from=cronjob/pi --dry-run=true --namespace=test-jobs -o name)
  44. # Post-condition: The text 'job.batch/test-job' should be part of the output
  45. kube::test::if_has_string "${output_message}" 'job.batch/test-job'
  46. # Post-condition: The test-job wasn't created actually
  47. kube::test::get_object_assert jobs "{{range.items}}{{$id_field}}{{end}}" ''
  48. # Pre-condition: job does not exist
  49. kube::test::get_object_assert 'job --namespace=test-jobs' "{{range.items}}{{ if eq $id_field \\\"test-jobs\\\" }}found{{end}}{{end}}:" ':'
  50. ### Dry-run create a job in a specific namespace
  51. kubectl create job test-job --from=cronjob/pi --namespace=test-jobs --dry-run=client
  52. kubectl create job test-job --from=cronjob/pi --namespace=test-jobs --dry-run=server
  53. kube::test::get_object_assert 'job --namespace=test-jobs' "{{range.items}}{{ if eq $id_field \\\"test-jobs\\\" }}found{{end}}{{end}}:" ':'
  54. ### Create a job in a specific namespace
  55. kubectl create job test-job --from=cronjob/pi --namespace=test-jobs
  56. # Post-Condition: assertion object exists
  57. kube::test::get_object_assert 'job/test-job --namespace=test-jobs' "{{$id_field}}" 'test-job'
  58. kubectl get job/test-job --namespace=test-jobs
  59. kubectl describe job/test-job --namespace=test-jobs
  60. #Clean up
  61. kubectl delete job test-job --namespace=test-jobs
  62. kubectl delete cronjob pi --namespace=test-jobs
  63. kubectl delete namespace test-jobs
  64. set +o nounset
  65. set +o errexit
  66. }