[gnome-build-meta/sthursfield/james/openqa-master: 18/22] openqa: Move functionality into helper scripts




commit fa2f4c35b52ab6eebd40725c34e2ebd6ae6652fb
Author: Sam Thursfield <sam afuera me uk>
Date:   Thu Aug 5 16:59:56 2021 +0200

    openqa: Move functionality into helper scripts
    
    The openqa-cli tool is pretty verbose, so wrap it in helper scripts
    and keep .gitlab-ci.yml a bit smaller.

 .gitlab-ci.yml                  | 68 ++++++-----------------------------------
 openqa/utils/setup_worker.sh    | 34 +++++++++++++++++++++
 openqa/utils/start_job.sh       | 22 +++++++++++++
 openqa/utils/teardown_worker.sh | 10 ++++++
 openqa/utils/wait_for_job.sh    | 27 ++++++++++++++++
 5 files changed, 103 insertions(+), 58 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3760e745..d48a884e 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -424,73 +424,25 @@ test-iso-installer-x86_64:
   - job: 'iso-installer-x86_64'
     artifacts: true
   script:
-  - mkdir -p /data/factory/iso/
-  - rm /etc/openqa/*
   - |
+    mkdir -p /data/factory/iso/
+    ln -s $(pwd)/image/installer.iso /data/factory/iso/installer.iso
+  - |
+  - rm /etc/openqa/*
     cat >/etc/openqa/client.conf <<EOF
     [openqa.gnome.org]
     key = $OPENQA_API_KEY
     secret = $OPENQA_API_SECRET
     EOF
   - |
-    # To ensure we create a unique worker for each ad-hoc test run, create a new
-    # uniqe machine class, which can only be used for this test
     worker_class=qemu_x86_64-${CI_JOB_ID}
-    cat >/etc/openqa/workers.ini <<EOF
-    [global]
-    WORKER_CLASS=$worker_class
-    BACKEND = qemu
-    HOST = $OPENQA_HOST
-    EOF
-    openqa-cli api --apikey $OPENQA_API_KEY --apisecret $OPENQA_API_SECRET \
-      --host $OPENQA_HOST \
-      -X POST machines/ \
-      name=gitlab-runner-$worker_class \
-      backend=qemu \
-      settings[ARCH]=x86_64 \
-      settings[PART_TABLE_TYPE]=gpt \
-      settings[QEMUCPUS]=2 \
-      settings[QEMURAM]=2560 \
-      settings[QEMUVGA]=virtio \
-      settings[UEFI]=1 \
-      settings[UEFI_PFLASH_CODE]=/usr/share/qemu/ovmf-x86_64-code.bin \
-      settings[WORKER_CLASS]=$worker_class | jq .id > /tmp/machine_id
-  - ln -s $(pwd)/image/installer.iso /data/factory/iso/installer.iso
-  - /run_openqa_worker.sh &> worker.log &
-  - |
-    # Start the job and monitor it
-    openqa-cli api --apikey $OPENQA_API_KEY --apisecret $OPENQA_API_SECRET \
-      --host $OPENQA_HOST \
-      -X POST isos \
-      ISO=installer.iso \
-      DISTRI=gnomeos \
-      VERSION=${CI_COMMIT_REF_NAME} \
-      FLAVOR=iso \
-      ARCH=x86_64 \
-      WORKER_CLASS=$worker_class \
-      CASEDIR=$(pwd)/openqa \
-      NEEDLES_DIR=$OPENQA_NEEDLES_GIT#$OPENQA_NEEDLES_SHA | jq .ids[0] > /tmp/job_id
-  - echo "Test job has been started, visit $OPENQA_HOST/tests/$(cat /tmp/job_id) to see the progress"
-  - |
-    alias job_check="openqa-cli api --apikey $OPENQA_API_KEY \
-      --apisecret $OPENQA_API_KEY \
-      --host $OPENQA_HOST \
-      jobs/$(cat /tmp/job_id)"
-    state=$(job_check | jq .job.state)
-    while [ "$state" != "\"done\"" ]; do sleep 10 && state=$(job_check | jq .job.state); done
-    echo "Tests finished"
-
-    result=$(job_check | jq .job.result)
-    if [ "$result" != "\"passed\"" ]; then
-      echo "Test job $(cat /tmp/job_id) *FAILED*"
-      echo 1 > /tmp/exit_code
-    else
-      echo "Test job $(cat /tmp/job_id) *PASSED*"
-      echo 0 > /tmp/exit_code
-    fi
+    openqa/utils/setup_worker.sh ${worker_class} > /tmp/machine_id
+    /run_openqa_worker.sh &> worker.log &
   - |
-    openqa-cli api --apikey $OPENQA_API_KEY --apisecret $OPENQA_API_SECRET \
-      --host $OPENQA_HOST -X DELETE machines/$(cat /tmp/machine_id)
+    openqa/utils/start_job.sh ${worker_class} ${CI_COMMIT_REF_NAME} > /tmp/job_id
+    echo "Test job has been started, visit $OPENQA_HOST/tests/$(cat /tmp/job_id) to see the progress"
+  - openqa/utils/wait_for_job.sh $(cat /tmp/job_id) > /tmp/exit_code
+  - openqa/utils/teardown_worker.sh $(cat /tmp/machine_id)
   - exit $(cat /tmp/exit_code)
   artifacts:
     when: always
diff --git a/openqa/utils/setup_worker.sh b/openqa/utils/setup_worker.sh
new file mode 100755
index 00000000..65c5d8b1
--- /dev/null
+++ b/openqa/utils/setup_worker.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+# Prepare a local OpenQA worker and register it with $OPENQA_HOST.
+#
+# On success, write the machine ID returned by server to stdout.
+
+set -eu
+
+# Unique identifier for this machine, so we can tell the server to schedule
+# tests here.
+worker_class=$1
+
+# Config for local OpenQA worker instance.
+cat >/etc/openqa/workers.ini <<EOF
+[global]
+WORKER_CLASS=$worker_class
+BACKEND = qemu
+HOST = $OPENQA_HOST
+EOF
+
+# Register local worker as a new machine on OpenQA server.
+openqa-cli api --apikey $OPENQA_API_KEY --apisecret $OPENQA_API_SECRET \
+  --host $OPENQA_HOST \
+  -X POST machines/ \
+  name=gitlab-runner-$worker_class \
+  backend=qemu \
+  settings[ARCH]=x86_64 \
+  settings[PART_TABLE_TYPE]=gpt \
+  settings[QEMUCPUS]=2 \
+  settings[QEMURAM]=2560 \
+  settings[QEMUVGA]=virtio \
+  settings[UEFI]=1 \
+  settings[UEFI_PFLASH_CODE]=/usr/share/qemu/ovmf-x86_64-code.bin \
+  settings[WORKER_CLASS]=$worker_class | jq .id > /tmp/machine_id
diff --git a/openqa/utils/start_job.sh b/openqa/utils/start_job.sh
new file mode 100755
index 00000000..0bb97eb7
--- /dev/null
+++ b/openqa/utils/start_job.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# Start job on $OPENQA_HOST.
+#
+# On success, write the job ID returned by server to stdout.
+
+set -eu
+
+worker_class=$1
+version=$2
+
+openqa-cli api --apikey $OPENQA_API_KEY --apisecret $OPENQA_API_SECRET \
+  --host $OPENQA_HOST \
+  -X POST isos \
+  ISO=installer.iso \
+  DISTRI=gnomeos \
+  VERSION=$version \
+  FLAVOR=iso \
+  ARCH=x86_64 \
+  WORKER_CLASS=$worker_class \
+  CASEDIR=$(pwd)/openqa \
+  NEEDLES_DIR=$OPENQA_NEEDLES_GIT#$OPENQA_NEEDLES_SHA | jq .ids[0]
diff --git a/openqa/utils/teardown_worker.sh b/openqa/utils/teardown_worker.sh
new file mode 100755
index 00000000..95870e5a
--- /dev/null
+++ b/openqa/utils/teardown_worker.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# Delete machine on $OPENQA_HOST.
+
+set -eu
+
+machine_id=$1
+
+openqa-cli api --apikey $OPENQA_API_KEY --apisecret $OPENQA_API_SECRET \
+    --host $OPENQA_HOST -X DELETE machines/${machine_id}
diff --git a/openqa/utils/wait_for_job.sh b/openqa/utils/wait_for_job.sh
new file mode 100755
index 00000000..e96e8022
--- /dev/null
+++ b/openqa/utils/wait_for_job.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# Block until a job on $OPENQA_HOST completes.
+#
+# Write a status to stdout on completion: 1 if job fails, 0 if job passes.
+
+set -eu
+
+job_id=$1
+
+alias job_check="openqa-cli api --apikey $OPENQA_API_KEY \
+  --apisecret $OPENQA_API_KEY \
+  --host $OPENQA_HOST \
+  jobs/${job_id}"
+
+state=$(job_check | jq .job.state)
+while [ "$state" != "\"done\"" ]; do sleep 10 && state=$(job_check | jq .job.state); done
+echo >&2 "Tests finished"
+
+result=$(job_check | jq .job.result)
+if [ "$result" != "\"passed\"" ]; then
+  echo >&2 "Test job ${job_id} *FAILED*"
+  echo 1
+else
+  echo >&2 "Test job ${job_id} *PASSED*"
+  echo 0
+fi


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]