batch.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. ### Create a cronjob in a specific namespace
  31. kubectl run pi --schedule="59 23 31 2 *" --namespace=test-jobs --generator=cronjob/v1beta1 "--image=$IMAGE_PERL" --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]}"
  32. # Post-Condition: assertion object exists
  33. kube::test::get_object_assert 'cronjob/pi --namespace=test-jobs' "{{$id_field}}" 'pi'
  34. kubectl get cronjob/pi --namespace=test-jobs
  35. kubectl describe cronjob/pi --namespace=test-jobs
  36. ### Create a job in dry-run mode
  37. output_message=$(kubectl create job test-job --from=cronjob/pi --dry-run=true --namespace=test-jobs -o name)
  38. # Post-condition: The text 'job.batch/test-job' should be part of the output
  39. kube::test::if_has_string "${output_message}" 'job.batch/test-job'
  40. # Post-condition: The test-job wasn't created actually
  41. kube::test::get_object_assert jobs "{{range.items}}{{$id_field}}{{end}}" ''
  42. ### Create a job in a specific namespace
  43. kubectl create job test-job --from=cronjob/pi --namespace=test-jobs
  44. # Post-Condition: assertion object exists
  45. kube::test::get_object_assert 'job/test-job --namespace=test-jobs' "{{$id_field}}" 'test-job'
  46. kubectl get job/test-job --namespace=test-jobs
  47. kubectl describe job/test-job --namespace=test-jobs
  48. #Clean up
  49. kubectl delete job test-job --namespace=test-jobs
  50. kubectl delete cronjob pi --namespace=test-jobs
  51. kubectl delete namespace test-jobs
  52. set +o nounset
  53. set +o errexit
  54. }