From bec19c499d2b6fa430664d16b8b5395dac33c8c6 Mon Sep 17 00:00:00 2001
From: Roman Azarenko <roman.azarenko@iopsys.eu>
Date: Mon, 3 Oct 2022 16:20:28 +0200
Subject: [PATCH] CI: split supervisord config depending on job, make all shell
 scripts strict

Functional testing (both regular and API) relies on starting some services through supervisord. The set of services,
however, is different between regular and API functional testing.

For instance, functional testing does *not* expect `wifimngr` to run through supervisord, but instead executes it
separately. API test, on the other hand, expects supervisord to start `wifimngr`. The supervisord configuration file,
however, was the same for both instances.

At the time when regular functional test reloads supervisord configuration, the `wifimngr` executable hasn't been
compiled yet, and supervisord reports the following:

	ubusd: added process group
	wifimngr: added process group
	ubusd                            RUNNING   pid 285, uptime 0:00:03
	wifimngr                         FATAL     Exited too quickly (process log may have details)

The supervisord log file then contains this:

	valgrind: /builds/iopsys/wifimngr/wifimngr: No such file or directory

which is expected, since `wifimngr` hasn't been compiled yet.

* In supervisord v3.x, the abovementioned fatal error still results in a zero return code of `supervisorctl status all`.
The error thus goes unnoticed, and the pipeline continues.
* In supervisord v4.x, `supervisorctl status` correctly exits with a non-zero exit code, thus interrupting the script
and failing the pipeline.

The solution here is to have dedicated configuration for regular and API functional testing, where regular functional
testing doesn't use supervisord to manage wifimngr. There are multiple ways to approach this (two complete configs,
config per process etc). I picked two complete configs for simplicity, but this can be changed later.
---
 gitlab-ci/functional-api-test.sh                    |  9 ++++++---
 gitlab-ci/functional-test.sh                        |  5 +++--
 gitlab-ci/install-dependencies.sh                   |  2 ++
 gitlab-ci/setup.sh                                  |  4 ++--
 ...rvisord.conf => supervisord-functional-api.conf} |  0
 gitlab-ci/supervisord-functional.conf               | 13 +++++++++++++
 6 files changed, 26 insertions(+), 7 deletions(-)
 rename gitlab-ci/{iopsys-supervisord.conf => supervisord-functional-api.conf} (100%)
 create mode 100644 gitlab-ci/supervisord-functional.conf

diff --git a/gitlab-ci/functional-api-test.sh b/gitlab-ci/functional-api-test.sh
index c09859e..8bd6ed4 100755
--- a/gitlab-ci/functional-api-test.sh
+++ b/gitlab-ci/functional-api-test.sh
@@ -1,17 +1,20 @@
 #!/bin/bash
-set -e
+set -euxo pipefail
 echo "Functional API Tests"
 pwd
 
 make coverage -C ./
 
 supervisorctl status all
+cp -a gitlab-ci/supervisord-functional-api.conf /etc/supervisor/conf.d/
 supervisorctl reload
 
 i=0
 while test $i -le 100
 do
-	supervisorctl status wifimngr | grep RUNNING && break
+	if supervisorctl status wifimngr | grep RUNNING; then
+		break
+	fi
 	echo "."
 	sleep 2
 	i=$((i+1))
@@ -22,7 +25,7 @@ supervisorctl status all
 ubus-api-validator -d ./test/api/json/ > ./api-result.log
 
 supervisorctl stop all
-supervisorctl status
+supervisorctl status || true
 
 #report part
 gcovr -r . --xml -o ./api-test-coverage.xml
diff --git a/gitlab-ci/functional-test.sh b/gitlab-ci/functional-test.sh
index d78d2fe..f141810 100755
--- a/gitlab-ci/functional-test.sh
+++ b/gitlab-ci/functional-test.sh
@@ -1,10 +1,11 @@
 #!/bin/bash
 
-set -e
+set -euxo pipefail
 echo "Functional Tests"
 pwd
 
 supervisorctl status all
+cp -a gitlab-ci/supervisord-functional.conf /etc/supervisor/conf.d/
 supervisorctl update
 sleep 3
 supervisorctl status all
@@ -12,7 +13,7 @@ supervisorctl status all
 make functional-test -C ./
 
 supervisorctl stop all
-supervisorctl status
+supervisorctl status || true
 
 #report part
 #GitLab-CI output
diff --git a/gitlab-ci/install-dependencies.sh b/gitlab-ci/install-dependencies.sh
index 5f540ff..c114e96 100755
--- a/gitlab-ci/install-dependencies.sh
+++ b/gitlab-ci/install-dependencies.sh
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+set -euxo pipefail
+
 echo "install dependencies"
 
 pwd
diff --git a/gitlab-ci/setup.sh b/gitlab-ci/setup.sh
index 41b5c2c..1821a63 100755
--- a/gitlab-ci/setup.sh
+++ b/gitlab-ci/setup.sh
@@ -1,14 +1,14 @@
 #!/bin/bash
 
+set -euxo pipefail
+
 echo "preparation script"
 
 pwd
 
 cp -r ./test/files/etc/* /etc/
 cp -r ./schemas/ubus/* /usr/share/rpcd/schemas
-cp ./gitlab-ci/iopsys-supervisord.conf /etc/supervisor/conf.d/
 
 ls /etc/config/
 ls /usr/share/rpcd/schemas/
-ls /etc/supervisor/conf.d/
 
diff --git a/gitlab-ci/iopsys-supervisord.conf b/gitlab-ci/supervisord-functional-api.conf
similarity index 100%
rename from gitlab-ci/iopsys-supervisord.conf
rename to gitlab-ci/supervisord-functional-api.conf
diff --git a/gitlab-ci/supervisord-functional.conf b/gitlab-ci/supervisord-functional.conf
new file mode 100644
index 0000000..262bba9
--- /dev/null
+++ b/gitlab-ci/supervisord-functional.conf
@@ -0,0 +1,13 @@
+[program:ubusd]
+priority=1
+autorestart=false
+startretries=0
+command=/bin/bash -c "/usr/sbin/ubusd"
+
+[supervisord]
+priority=3
+autorestart=false
+startretries=0
+environment = LIBWIFI_DEBUG_LEVEL=7
+logfile=%(ENV_SUPERVISOR_LOG)s
+loglevel=debug
-- 
GitLab