Skip to content

Commit

Permalink
Handle multiple job IDs in scripts used for openQA-in-openQA test
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Jun 23, 2021
1 parent 0d004d9 commit c048950
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions _common
Expand Up @@ -6,6 +6,6 @@
# From openqa-cli JSON output filter and return the id/ids of jobs,
# for example from a query to the 'jobs get' route or the result string of a
# 'jobs post' or 'isos post'
filter_id() {
jq -r '.ids[0]' "$@"
job_ids() {
jq -r '.ids[]' "$@"
}
54 changes: 26 additions & 28 deletions monitor-openqa_job
@@ -1,11 +1,13 @@
#!/bin/sh -ex
#!/bin/bash -ex

# Monitor an openQA job by polling the status of a job over the API.
#
# Continuously polls the openQA API for the status of the specified job until
# the job finishes. After the job is finished this program exits with an exit
# code corresponding to the job result. For example a passing job would exit
# this program with exit code 0 for success, all other with 1.
# Continuously polls the openQA API for the status of the specified jobs until
# all jobs finish. After the jobs have finished this program exits with an exit
# code corresponding to the jobs' result. For example all jobs pass it would exit
# this program with exit code 0 for success; otherwise with 1.

set -euo pipefail

# configuration variables with defaults.
sleep_time="${sleep_time:-"10"}"
Expand All @@ -17,28 +19,24 @@ obs_component="${obs_component:-"package"}"
# shellcheck source=/dev/null
. "$(dirname "$0")"/_common

# note: JOB_ID can return multiple job ids separated by newline which is not
# properly handled here, instead implicitly only the first job is evaluated,
# hence SC2086 is so far not attended. The proper approach would be to monitor
# all jobs or just accept a single job and not ignore the style warning.

[ -f job_post_response ] || (echo "Need job response status file 'job_post_response'" && exit 2)
JOB_ID=$(filter_id job_post_response)
echo "waiting for job ${JOB_ID} to finish"
while sleep "${sleep_time}" ; do
# shellcheck disable=SC2086
[ "$($openqa_cli api --host "${host}" jobs/"${JOB_ID}" | jq -r '.job.state')" = 'done' ] && break
[[ -f job_post_response ]] || (echo "Need job response status file 'job_post_response'" && exit 2)
failures=0
for job_id in $(job_ids job_post_response); do
echo "Waiting for job $job_id to finish"
while sleep "${sleep_time}" ; do
job_state=$($openqa_cli api --host "$host" jobs/"$job_id" | jq -r '.job.state')
[[ $job_state = 'done' ]] && break
done
response=$($openqa_cli api jobs/"$job_id")
result=$(echo "$response" | jq -r '.job.result')
echo "Result of job $job_id: $result"
obs_package_name=$1
if [[ $result != 'passed' ]] && [[ -n $obs_package_name ]] ; then
VERSION=$(echo "$response" | jq -r '.job.settings.VERSION')
osc api /comments/"${obs_component}"/"$obs_package_name" | grep id= | sed -n 's/.*id="\([^"]*\)">test failed.*/\1/p' | while read -r id; do osc api -X DELETE /comment/"$id"; done
osc api --data="test failed, see https://openqa.opensuse.org/tests/overview?version=$VERSION&groupid=${openqa_groupid}" -X POST "/comments/${obs_component}/$obs_package_name"
failures=$((failures + 1))
fi
done
# shellcheck disable=SC2086
response=$($openqa_cli api jobs/"${JOB_ID}")
result=$(echo "$response" | jq -r '.job.result')
echo "job result: $result"
obs_package_name=$1
if ! echo "$result" | grep -q "passed" && [ -n "$obs_package_name" ] ; then

# shellcheck disable=SC2086
VERSION=$(echo "$response" | jq -r '.job.settings.VERSION')
osc api /comments/"${obs_component}"/"$obs_package_name" | grep id= | sed -n 's/.*id="\([^"]*\)">test failed.*/\1/p' | while read -r id; do osc api -X DELETE /comment/"$id"; done
osc api --data="test failed, see https://openqa.opensuse.org/tests/overview?version=$VERSION&groupid=${openqa_groupid}" -X POST "/comments/${obs_component}/$obs_package_name"
exit 1
fi
[[ $failures -gt 0 ]] && exit 1
8 changes: 5 additions & 3 deletions trigger-openqa_in_openqa
Expand Up @@ -4,7 +4,7 @@
#
# Can be configured by variables.

set -o pipefail
set -euo pipefail

# configuration variables with defaults.
target_host="${target_host:-"openqa.opensuse.org"}"
Expand Down Expand Up @@ -74,8 +74,10 @@ trigger() {
${ARGS} \
| tee job_post_response

JOB_ID=$(filter_id job_post_response)
echo "Triggered as: ${target_host_proto}://${target_host}/tests/${JOB_ID}"
echo 'Triggered as:'
for job_id in $(job_ids job_post_response); do
echo " - ${target_host_proto}://${target_host}/tests/${job_id}"
done
}

main

0 comments on commit c048950

Please sign in to comment.