Release new version 0.108
[grml-debootstrap.git] / tests / build-vm-and-test.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 #
4 # Install an already built grml-debootstrap.deb in docker and use it to
5 # build a test VM image. Then run this VM image in qemu and check if it
6 # boots.
7
8 set -eu -o pipefail
9
10 usage() {
11   echo "Usage: $0 setup"
12   echo " then: $0 run"
13   echo " then: $0 test"
14   echo "WARNING: $0 is potentially dangerous and may destroy the host system and/or any data."
15   exit 0
16 }
17
18 if [ "${1:-}" == "--help" ] || [ "${1:-}" == "help" ]; then
19   usage
20 fi
21
22 if [ -z "${1:-}" ]; then
23   echo "$0: unknown parameters, see --help" >&2
24   exit 1
25 fi
26
27 set -x
28
29 if [ ! -d ./tests ]; then
30   echo "$0: Started from incorrect working directory" >&2
31   exit 1
32 fi
33
34 if [ "$1" == "setup" ]; then
35   [ -x ./tests/goss ] || curl -fsSL https://goss.rocks/install | GOSS_DST="$(pwd)/tests" sh
36   sudo apt-get update
37   sudo apt-get -qq -y install qemu-system-x86 kpartx python3-pexpect python3-serial
38   # TODO: docker.io
39   exit 0
40 fi
41
42 # Debian version to install using grml-debootstrap
43 RELEASE="${RELEASE:-bookworm}"
44
45 TARGET="${TARGET:-qemu.img}"
46
47 # debootstrap to use, default empty (let grml-debootstrap decide)
48 DEBOOTSTRAP="${DEBOOTSTRAP:-}"
49
50 if [ "$1" == "run" ]; then
51   # Debian version on which grml-debootstrap will *run*
52   HOST_RELEASE="${HOST_RELEASE:-bookworm}"
53
54   DEB_NAME=$(ls ./grml-debootstrap*.deb || true)
55   if [ -z "$DEB_NAME" ]; then
56     echo "$0: No grml-debootstrap*.deb found, aborting" >&2
57     exit 1
58   fi
59
60   # we need to run in privileged mode to be able to use loop devices
61   exec docker run --privileged --rm -i \
62     -v "$(pwd)":/code \
63     -e TERM="$TERM" \
64     -e DEBOOTSTRAP="$DEBOOTSTRAP" \
65     -w /code \
66     debian:"$HOST_RELEASE" \
67     bash -c './tests/docker-install-deb.sh '"$DEB_NAME"' && ./tests/docker-build-vm.sh '"$(id -u)"' '"/code/$TARGET"' '"$RELEASE"
68
69 elif [ "$1" == "test" ]; then
70   # run tests from inside Debian system
71   exec ./tests/test-vm.sh "$PWD/$TARGET" "$RELEASE"
72
73 else
74   echo "$0: unknown parameters, see --help" >&2
75   exit 1
76 fi
77
78 # EOF