From 8a3f7184282981c994197e0301d227fbe6f9339c Mon Sep 17 00:00:00 2001
From: Chase Qi <chase.qi@linaro.org>
Date: Mon, 20 Dec 2021 10:26:51 +0800
Subject: [PATCH] ci: search zephyr build images by machine name

Introduce machine variable for lava-test and search build images for the
test target device only to make sure only images for the target device
submitted for test.

This closes #215.

Signed-off-by: Chase Qi <chase.qi@linaro.org>
---
 .gitlab-ci.yml              | 18 ++++++++++++------
 .oniro-ci/test-generic.yaml |  9 ++++++---
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 2f831a30..0e77bbba 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -125,8 +125,9 @@ lava-linux-avenger96:
   stage: test
   extends: .lava-test
   variables:
-    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/avenger96-acts.yaml"
+    MACHINE: stm32mp1-av96
     CI_BUILD_JOB_NAME: linux-stm32mp1-av96
+    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/avenger96-acts.yaml"
     CI_REPORT_JOB_NAME: lava-report
   rules:
     - when: never
@@ -136,8 +137,9 @@ lava-qemu-x86:
   stage: test
   extends: .lava-test
   variables:
-    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-x86.yaml"
+    MACHINE: qemux86
     CI_BUILD_JOB_NAME: linux-qemu-x86
+    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-x86.yaml"
     CI_REPORT_JOB_NAME: lava-report
 
 lava-qemu-x86_64:
@@ -145,8 +147,9 @@ lava-qemu-x86_64:
   stage: test
   extends: .lava-test
   variables:
-    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-x86_64.yaml"
+    MACHINE: qemux86-64
     CI_BUILD_JOB_NAME: linux-qemu-x86_64
+    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-x86_64.yaml"
     CI_REPORT_JOB_NAME: lava-report
 
 lava-zephyr-96b-nitrogen-tests:
@@ -154,8 +157,9 @@ lava-zephyr-96b-nitrogen-tests:
   stage: test
   extends: .lava-test
   variables:
-    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/nitrogen-test.yaml"
+    MACHINE: 96b-nitrogen
     CI_BUILD_JOB_NAME: zephyr-96b-nitrogen-tests
+    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/nitrogen-test.yaml"
     CI_REPORT_JOB_NAME: lava-report
 
 lava-zephyr-qemu-cortex-m3:
@@ -163,8 +167,9 @@ lava-zephyr-qemu-cortex-m3:
   stage: test
   extends: .lava-test
   variables:
-    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-zephyr-cortex-m3.yaml"
+    MACHINE: qemu-cortex-m3
     CI_BUILD_JOB_NAME: zephyr-qemu-cortex-m3
+    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-zephyr-cortex-m3.yaml"
     CI_REPORT_JOB_NAME: lava-report
 
 lava-zephyr-qemu-x86:
@@ -172,8 +177,9 @@ lava-zephyr-qemu-x86:
   stage: test
   extends: .lava-test
   variables:
-    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-zephyr-x86.yaml"
+    MACHINE: qemu-x86
     CI_BUILD_JOB_NAME: zephyr-qemu-x86
+    CI_LAVA_JOB_DEFINITION: "https://git.ostc-eu.org/OSTC/infrastructure/lava/lava-config/-/raw/master/lava.ostc-eu.org/job-definitions/ci/qemu-zephyr-x86.yaml"
     CI_REPORT_JOB_NAME: lava-report
 
 lava-report:
diff --git a/.oniro-ci/test-generic.yaml b/.oniro-ci/test-generic.yaml
index 1fa08b71..59bf0c2e 100644
--- a/.oniro-ci/test-generic.yaml
+++ b/.oniro-ci/test-generic.yaml
@@ -16,12 +16,15 @@
         echo "precondition failed - please disable the child job if CI_LAVA_TOKEN not set in gitlab CI/CD variables"
         && exit 1 )
     # Check if the job is configured properly.
-    - test -n "$CI_LAVA_JOB_DEFINITION" || (
-        echo "precondition failed - set CI_LAVA_JOB_DEFINITION to the URL of the LAVA test job definition"
+    - test -n "$MACHINE" || (
+        echo "precondition failed - set MACHINE to the name of the target device for which the image is built"
         && exit 1 )
     - test -n "$CI_BUILD_JOB_NAME" || (
         echo "precondition failed - set CI_BUILD_JOB_NAME to the appropriate job name from which LAVA will pick up build artifact"
         && exit 1 )
+    - test -n "$CI_LAVA_JOB_DEFINITION" || (
+        echo "precondition failed - set CI_LAVA_JOB_DEFINITION to the URL of the LAVA test job definition"
+        && exit 1 )
     - test -n "$CI_REPORT_JOB_NAME" || (
         echo "precondition failed - set CI_REPORT_JOB_NAME to the CI job name which will gather results back from LAVA"
         && exit 1 )
@@ -44,7 +47,7 @@
     - |
       rm -rf lava_jobs && mkdir lava_jobs
       if echo "${CI_BUILD_JOB_NAME}" | grep -i "zephyr"; then
-        for image in $(find artifacts/ -name "*.elf" -exec basename {} \;); do
+        for image in $(find artifacts/images/${MACHINE} -name "*.elf" -exec basename {} \;); do
           job_name=$(basename "${image}" ".elf")
           echo "--- Generating lava job definition ${job_name}.yaml ---"
           sed "s/\$elf_file/$image/" job_def.yaml | tee lava_jobs/"${job_name}".yaml
-- 
GitLab