tests: add test for cron job support

Add simple test for cron job support

Signed-off-by: Saul Paredes <saulparedes@microsoft.com>
This commit is contained in:
Saul Paredes 2024-08-12 14:51:46 -07:00
parent 88451d26d0
commit af598a232b
4 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,59 @@
#!/usr/bin/env bats
#
# Copyright (c) 2019 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
load "${BATS_TEST_DIRNAME}/../../common.bash"
load "${BATS_TEST_DIRNAME}/tests_common.sh"
setup() {
get_pod_config_dir
job_name="cron-job-pi-test"
yaml_file="${pod_config_dir}/cron-job.yaml"
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"
}
@test "Run a cron job to completion" {
# Create cron job
kubectl apply -f "${yaml_file}"
# Verify job
waitForProcess "$wait_time" "$sleep_time" "kubectl describe cronjobs.batch $job_name | grep SuccessfulCreate"
# List pods that belong to the cron-job
pod_name=$(kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep '^cron-job-pi-test' | head -n 1)
# Verify that the job is completed
cmd="kubectl get pods -o jsonpath='{.items[*].status.phase}' | grep Succeeded"
waitForProcess "$wait_time" "$sleep_time" "$cmd"
# Verify the output of the pod
pi_number="3.14"
kubectl logs "$pod_name" | grep "$pi_number"
}
teardown() {
# Debugging information
kubectl describe pod "$pod_name"
kubectl describe cronjobs.batch/"$job_name"
# Clean-up
kubectl delete cronjobs.batch/"$job_name"
# Verify that the job is not running
run kubectl get cronjobs.batch
echo "$output"
[[ "$output" =~ "No resources found" ]]
# Verify that pod is not running
run kubectl get pods
echo "$output"
[[ "$output" =~ "No resources found" ]]
delete_tmp_policy_settings_dir "${policy_settings_dir}"
}

View File

@ -37,6 +37,7 @@ else
"k8s-copy-file.bats" \
"k8s-cpu-ns.bats" \
"k8s-credentials-secrets.bats" \
"k8s-cron-job.bats" \
"k8s-custom-dns.bats" \
"k8s-empty-dirs.bats" \
"k8s-env.bats" \

View File

@ -0,0 +1,15 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: cron-job-pi-test
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: pi
image: quay.io/prometheus/busybox:latest
command: ["/bin/sh", "-c", "echo 'scale=5; 4*a(1)' | bc -l"]
restartPolicy: OnFailure

View File

@ -77,6 +77,13 @@ add_annotations_to_yaml() {
"${K8S_TEST_YAML}"
;;
CronJob)
info "Adding \"${annotation_name}=${annotation_value}\" to ${resource_kind} from ${yaml_file}"
yq -i \
".spec.jobTemplate.spec.template.metadata.annotations.\"${annotation_name}\" = \"${annotation_value}\"" \
"${K8S_TEST_YAML}"
;;
List)
info "Issue #7765: adding annotations to ${resource_kind} from ${yaml_file} is not implemented yet"
;;