grml-debootstrap man page: point users towards github instead of private mail
[grml-debootstrap.git] / travis / execute.sh
1 #!/bin/bash
2
3 set -eu -o pipefail
4 set -x
5
6 if [ -z "${TRAVIS:-}" ] ; then
7   echo "Running outside of Travis."
8
9   if [ "$#" -ne 1 ] ; then
10     echo "Usage: $(basename "$0") ./grml-debootstrap*.deb" >&2
11     exit 1
12   else
13     GRML_DEBOOTSTRAP_DEB="$1"
14     if [ "$(dirname "$(realpath "$GRML_DEBOOTSTRAP_DEB")")" != "$(pwd)" ] ; then
15       echo "Error: the grml-debootstrap*.deb needs to be inside $(pwd) to be shared with docker container." >&2
16       exit 1
17     fi
18   fi
19 fi
20
21 RELEASE="${RELEASE:-stretch}"
22 export RELEASE
23
24 TARGET="${TARGET:-qemu.img}"
25
26 bailout() {
27   if [ -n "${QEMU_PID:-}" ] ; then
28     # shellcheck disable=SC2009
29     ps --pid="${QEMU_PID}" -o pid= | grep -q '.' && kill "${QEMU_PID:-}"
30   fi
31
32   if [ -f "${TARGET:-}" ] ; then
33     sudo kpartx -dv "$(realpath "${TARGET}")"
34   fi
35
36   if [ -n "${LOOP_DISK:-}" ] ; then
37     if sudo dmsetup ls | grep -q "${LOOP_DISK}"; then
38       sudo kpartx -d "/dev/${LOOP_DISK}"
39     fi
40   fi
41
42   local loopmount
43   loopmount="$(sudo losetup -a | grep "$(realpath "${TARGET}")" | cut -f1 -d: || true)"
44
45   if [ -n "${loopmount:-}" ] ; then
46     sudo losetup -d "${loopmount}"
47   fi
48
49   [ -n "${1:-}" ] && EXIT_CODE="$1" || EXIT_CODE=1
50   exit "$EXIT_CODE"
51 }
52 trap bailout 1 2 3 6 14 15
53
54 # run shellcheck tests
55 docker run koalaman/shellcheck:stable --version
56 docker run --rm -v "$(pwd)":/code koalaman/shellcheck:stable -e SC2181 /code/chroot-script /code/grml-debootstrap
57
58 # build Debian package
59 if [ -z "${TRAVIS:-}" ] ; then
60   echo "Not running under Travis, installing local grml-debootstrap package ${GRML_DEBOOTSTRAP_DEB}."
61 else
62   if ! [ "${TRAVIS_DEBIAN_DISTRIBUTION:-}" = "unstable" ] ; then
63     echo "TRAVIS_DEBIAN_DISTRIBUTION is $TRAVIS_DEBIAN_DISTRIBUTION and not unstable, skipping VM build tests."
64     exit 0
65   fi
66   wget -O- https://travis.debian.net/script.sh | sh -
67   # copy only the binary from the TRAVIS_DEBIAN_INCREMENT_VERSION_NUMBER=true build
68   cp ../grml-debootstrap_*travis*deb .
69 fi
70
71 # we need to run in privileged mode to be able to use loop devices
72 docker run --privileged -v "$(pwd)":/code --rm -i -t debian:stretch /code/travis/build-vm.sh
73
74 [ -x ./goss ] || curl -fsSL https://goss.rocks/install | GOSS_DST="$(pwd)" sh
75
76 # Ubuntu trusty (14.04LTS) doesn't have realpath in coreutils yet
77 if ! command -v realpath &>/dev/null ; then
78   REALPATH_PACKAGE=realpath
79 fi
80
81 sudo apt-get update
82 sudo apt-get -y install qemu-system-x86 kpartx python-pexpect python-serial ${REALPATH_PACKAGE:-}
83
84 # run tests from inside Debian system
85 DEVINFO=$(sudo kpartx -asv "${TARGET}")
86 LOOP_PART="${DEVINFO##add map }"
87 LOOP_PART="${LOOP_PART// */}"
88 LOOP_DISK="${LOOP_PART%p*}"
89 IMG_FILE="/dev/mapper/$LOOP_PART"
90
91 MNTPOINT="$(mktemp -d)"
92 sudo mount "$IMG_FILE" "${MNTPOINT}"
93
94 sudo cp ./goss "${MNTPOINT}"/usr/local/bin/goss
95 sudo cp ./travis/goss.yaml "${MNTPOINT}"/root/goss.yaml
96
97 sudo umount "${MNTPOINT}"
98 sudo kpartx -dv "$(realpath "${TARGET}")"
99 if sudo dmsetup ls | grep -q "${LOOP_DISK}"; then
100   sudo kpartx -d "/dev/${LOOP_DISK}"
101 fi
102
103 rmdir "$MNTPOINT"
104
105 sudo chown "$(id -un)" qemu.img
106 rm -f ./serial0
107 mkfifo ./serial0
108 qemu-system-x86_64 -hda qemu.img -display none -vnc :0 \
109                    -device virtio-serial-pci \
110                    -chardev pipe,id=ch0,path=./serial0 \
111                    -device virtserialport,chardev=ch0,name=serial0 \
112                    -serial pty &>qemu.log &
113 QEMU_PID="$!"
114
115 timeout=30
116 success=0
117 while [ "$timeout" -gt 0 ] ; do
118   ((timeout--))
119   if grep -q 'char device redirected to ' qemu.log ; then
120     success=1
121     sleep 1
122     break
123   else
124     echo "No serial console from Qemu found yet [$timeout retries left]"
125     sleep 1
126   fi
127 done
128
129 if [ "$success" = "1" ] ; then
130   serial_port=$(awk '/char device redirected/ {print $5}' qemu.log)
131 else
132   echo "Error: Failed to identify serial console port." >&2
133   exit 1
134 fi
135
136 timeout=30
137 success=0
138 while [ "$timeout" -gt 0 ] ; do
139   ((timeout--))
140   if [ -c "$serial_port" ] ; then
141     success=1
142     sleep 1
143     break
144   else
145     echo "No block device for serial console found yet [$timeout retries left]"
146     sleep 1
147   fi
148 done
149
150 if [ "$success" = "0" ] ; then
151   echo "Error: can't access serial console block device." >&2
152   exit 1
153 fi
154
155 sudo chown "$(id -un)" "$serial_port"
156 ./travis/serial-console-connection --port "$serial_port" --hostname "$RELEASE" --pipefile "serial0" --vmoutput "vm-output.txt"
157
158 cat vm-output.txt
159
160 RC=0
161 if grep -q '^failure_exit' vm-output.txt ; then
162   echo "We noticed failing tests."
163   RC=1
164 else
165   echo "All tests passed."
166 fi
167
168 echo "Finished serial console connection [timeout=${timeout}]."
169
170 bailout $RC
171
172 # EOF