From af4f1eb0cd386e7d6aa06a097998f4bbcbf9bb43 Mon Sep 17 00:00:00 2001
From: Jochen Sprickerhof <git@jochen.sprickerhof.de>
Date: Fri, 23 Dec 2022 20:55:37 +0100
Subject: Move tests to tests/create-and-run.sh (Closes: #4)

---
 .gitlab-ci.yml          | 31 ++++---------------------------
 tests/create-and-run.sh | 30 ++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 27 deletions(-)
 create mode 100755 tests/create-and-run.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 8af5b41..5d5f375 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -14,27 +14,7 @@ codespell:
     - apt-get --no-install-recommends --yes install codespell
     - codespell debvm-*
 
-.test_script:
-  script:
-    - timeout 240s ./debvm-run -s 2222 &
-    - |
-      timeout=5
-      sshopt="-o StrictHostKeyChecking=no $(if test "$RELEASE" = jessie; then echo -o PubkeyAcceptedKeyTypes=+ssh-rsa; fi)"
-      ts=$(sleepenh 0 || [ $? -eq 1 ])
-      for i in $(seq 30); do
-        rv=0
-        ssh $sshopt -o ConnectTimeout="$timeout" -i ssh_id -p 2222 root@localhost echo success || rv=$?
-        test $rv -eq 0 && break
-        ts=$(sleepenh "$ts" "$timeout" || [ $? -eq 1 ]);
-        if test $i -eq 30; then
-          echo "timeout reached" >&2
-          exit 1
-        fi
-      done
-    - ssh $sshopt -i ssh_id -p 2222 root@localhost poweroff
-
 release_test:
-  extends: .test_script
   parallel:
     matrix:
       - RELEASE:
@@ -44,15 +24,13 @@ release_test:
           - buster
           - stretch
           - jessie
-  before_script:
+  script:
     - apt-get update
     - apt-get dist-upgrade --yes
     - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client sleepenh qemu-kvm
-    - ssh-keygen -f ssh_id -N ''
-    - ./debvm-create -k ssh_id.pub -r "$RELEASE"
+    - PATH=.:$PATH ./tests/create-and-run.sh -r "$RELEASE"
 
 arch_test:
-  extends: .test_script
   parallel:
     matrix:
       - ARCHITECTURE:
@@ -63,7 +41,7 @@ arch_test:
           - mipsel
           - ppc64el
           - s390x
-  before_script:
+  script:
     - |
       if [ ! -e /proc/sys/fs/binfmt_misc/status ]; then
         mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
@@ -71,5 +49,4 @@ arch_test:
     - apt-get update
     - apt-get dist-upgrade --yes
     - apt-get --no-install-recommends --yes install e2fsprogs genext2fs mmdebstrap openssh-client sleepenh qemu-system binfmt-support arch-test qemu-user-static
-    - ssh-keygen -f ssh_id -N ''
-    - ./debvm-create -k ssh_id.pub -a "$ARCHITECTURE"
+    - PATH=.:$PATH ./tests/create-and-run.sh -a "$ARCHITECTURE"
diff --git a/tests/create-and-run.sh b/tests/create-and-run.sh
new file mode 100755
index 0000000..dfd4022
--- /dev/null
+++ b/tests/create-and-run.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+# shellcheck disable=SC2086
+
+set -ex
+
+cleanup() {
+	rm -f ssh_id ssh_id.pub test.ext2
+}
+
+trap cleanup EXIT INT TERM QUIT
+
+ssh-keygen -f ssh_id -N ''
+debvm-create -k ssh_id.pub -o test.ext2 "$@"
+
+timeout 240s debvm-run -s 2222 -i test.ext2 &
+timeout=5
+sshopt="-o StrictHostKeyChecking=no $(if test "$2" = jessie; then echo -o PubkeyAcceptedKeyTypes=+ssh-rsa; fi)"
+ts=$(sleepenh 0 || [ $? -eq 1 ])
+for i in $(seq 30); do
+	rv=0
+	ssh $sshopt -o ConnectTimeout="$timeout" -i ssh_id -p 2222 root@localhost echo success || rv=$?
+	test $rv -eq 0 && break
+	ts=$(sleepenh "$ts" "$timeout" || [ $? -eq 1 ]);
+	if test "$i" -eq 30; then
+		echo "timeout reached" >&2
+		exit 1
+	fi
+done
+ssh $sshopt -i ssh_id -p 2222 root@localhost poweroff
-- 
cgit v1.2.3