...
1#!/usr/bin/env bash
2
3# Copyright 2018 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -o errexit
18set -o nounset
19set -o pipefail
20
21run_job_tests() {
22 set -o nounset
23 set -o errexit
24
25 create_and_use_new_namespace
26 kube::log::status "Testing job"
27
28 ### Create a new namespace
29 # Pre-condition: the test-jobs namespace does not exist
30 kube::test::get_object_assert 'namespaces' "{{range.items}}{{ if eq ${id_field:?} \"test-jobs\" }}found{{end}}{{end}}:" ':'
31 # Command
32 kubectl create namespace test-jobs
33 # Post-condition: namespace 'test-jobs' is created.
34 kube::test::get_object_assert 'namespaces/test-jobs' "{{$id_field}}" 'test-jobs'
35
36 # Pre-condition: cronjob does not exist
37 kube::test::get_object_assert 'cronjob --namespace=test-jobs' "{{range.items}}{{ if eq $id_field \"pi\" }}found{{end}}{{end}}:" ':'
38 # Dry-run create CronJob
39 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[@]:?}"
40 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[@]:?}"
41 kube::test::get_object_assert 'cronjob' "{{range.items}}{{ if eq $id_field \"pi\" }}found{{end}}{{end}}:" ':'
42 ### Create a cronjob in a specific namespace
43 kubectl create cronjob pi --schedule="59 23 31 2 *" --namespace=test-jobs "--image=$IMAGE_PERL" -- perl -Mbignum=bpi -wle 'print bpi(20)' "${kube_flags[@]:?}"
44 # Post-Condition: assertion object exists
45 kube::test::get_object_assert 'cronjob/pi --namespace=test-jobs' "{{$id_field}}" 'pi'
46 kubectl get cronjob/pi --namespace=test-jobs
47 kubectl describe cronjob/pi --namespace=test-jobs
48 # Describe command should respect the chunk size parameter
49 kube::test::describe_resource_chunk_size_assert cronjobs events "--namespace=test-jobs"
50
51 ### Create a job in dry-run mode
52 output_message=$(kubectl create job test-job --from=cronjob/pi --dry-run=true --namespace=test-jobs -o name)
53 # Post-condition: The text 'job.batch/test-job' should be part of the output
54 kube::test::if_has_string "${output_message}" 'job.batch/test-job'
55 # Post-condition: The test-job wasn't created actually
56 kube::test::get_object_assert jobs "{{range.items}}{{$id_field}}{{end}}" ''
57
58 # Pre-condition: job does not exist
59 kube::test::get_object_assert 'job --namespace=test-jobs' "{{range.items}}{{ if eq $id_field \"test-jobs\" }}found{{end}}{{end}}:" ':'
60 ### Dry-run create a job in a specific namespace
61 kubectl create job test-job --from=cronjob/pi --namespace=test-jobs --dry-run=client
62 kubectl create job test-job --from=cronjob/pi --namespace=test-jobs --dry-run=server
63 kube::test::get_object_assert 'job --namespace=test-jobs' "{{range.items}}{{ if eq $id_field \"test-jobs\" }}found{{end}}{{end}}:" ':'
64 ### Create a job in a specific namespace
65 kubectl create job test-job --from=cronjob/pi --namespace=test-jobs
66 # Post-Condition: assertion object exists
67 kube::test::get_object_assert 'job/test-job --namespace=test-jobs' "{{$id_field}}" 'test-job'
68 kubectl get job/test-job --namespace=test-jobs
69 kubectl describe job/test-job --namespace=test-jobs
70 # Describe command should respect the chunk size parameter
71 kube::test::describe_resource_chunk_size_assert jobs events "--namespace=test-jobs"
72 #Clean up
73 kubectl delete job test-job --namespace=test-jobs
74 kubectl delete cronjob pi --namespace=test-jobs
75 kubectl delete namespace test-jobs
76
77 set +o nounset
78 set +o errexit
79}
View as plain text