Skip to content

Commit

Permalink
tests: Try to capture screenshot via VNC if serial-console-connection…
Browse files Browse the repository at this point in the history
… fails

While debugging the failing GitHub Actions (see #278), we noticed that
it might serve useful to have a screenshot of the booted system
available. We can capture such a screenshot via vncsnapshot.

NOTE: we move the files inside `results` only if the directory as such
exists in $PWD, otherwise we'd fail the build when being executed as
`tests/build-vm-and-test.sh test` from inside the grml-debootstrap.git.
This happens when manually reproducing the GH tests, though then
`build-vm-and-test.sh` is supposed to be executed from the parent
directory of the `tests` directory (being the grml-debootstrap.git
checkout).
  • Loading branch information
mika authored and zeha committed Aug 17, 2024
1 parent bca8834 commit f3a5401
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tests/build-vm-and-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ fi
if [ "$1" == "setup" ]; then
sudo apt-get update
sudo apt-get -qq -y install curl qemu-system-x86 kpartx python3-pexpect python3-serial
# vncsnapshot might not be available, though we don't want to abort execution then
sudo apt-get -qq -y install vncsnapshot || true
[ -x ./tests/goss ] || curl -fsSL https://goss.rocks/install | GOSS_DST="$(pwd)/tests" sh
# TODO: docker.io
exit 0
Expand Down
25 changes: 25 additions & 0 deletions tests/serial-console-connection
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python
import argparse
import serial
import os
import shutil
import subprocess
import sys
import time
from pexpect import fdpexpect
Expand All @@ -25,6 +28,11 @@ parser.add_argument(
type=int,
help="Maximum time for finding the login prompt, in seconds",
)
parser.add_argument(
"--screenshot",
default="screenshot.jpg",
help="file name for screenshot captured via VNC on error",
)
parser.add_argument(
"--tries",
default="12",
Expand Down Expand Up @@ -74,13 +82,29 @@ def login(ser, hostname, user, password, timeout=5):
child.expect("%s@%s" % (user, hostname), timeout=timeout)


def capture_vnc_screenshot(screenshot_file):
if not shutil.which("vncsnapshot"):
print("WARN: vncsnapshot not available, skipping vnc snapshot capturing.")
return

print("Trying to capture screenshot via vncsnapshot to", screenshot_file)

proc = subprocess.Popen(["vncsnapshot", "localhost", screenshot_file])
proc.wait()
if proc.returncode != 0:
print("WARN: failed to capture vnc snapshot :(")
else:
print("Screenshot file '%s' available" % os.path.abspath(screenshot_file))


def main():
args = parser.parse_args()
hostname = args.hostname
password = args.password
port = args.port
user = args.user
commands = args.command
screenshot_file = args.screenshot

ser = serial.Serial(port, 115200)
ser.flushInput()
Expand Down Expand Up @@ -116,6 +140,7 @@ def main():
# after poweroff, the serial device will probably vanish. do not attempt reading from it anymore.

if not success:
capture_vnc_screenshot(screenshot_file)
sys.exit(1)


Expand Down
13 changes: 11 additions & 2 deletions tests/test-vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ if [ "$success" = "0" ] ; then
exit 1
fi

RC=0
"$TEST_PWD"/tests/serial-console-connection \
--tries 180 \
--screenshot "$TEST_PWD/tests/screenshot.jpg" \
--port "$serial_port" \
--hostname "$VM_HOSTNAME" \
--poweroff \
"mount -t 9p -o trans=virtio,version=9p2000.L,rw $MOUNT_TAG /mnt && cd /mnt && ./testrunner"
"mount -t 9p -o trans=virtio,version=9p2000.L,rw $MOUNT_TAG /mnt && cd /mnt && ./testrunner" || RC=$?

if [ ! -d results ] || [ ! -f ./results/goss.tap ] || [ ! -f ./results/goss.exitcode ]; then
echo "Running tests inside VM failed for unknown reason" >&2
Expand All @@ -133,7 +135,14 @@ fi

echo "Finished serial console connection [timeout=${timeout}]."

mv results/* "$TESTS_RESULTSDIR/"
# in case of errors we might have captured a screenshot via VNC
if [ -r "${TEST_PWD}"/tests/screenshot.jpg ] ; then
cp "${TEST_PWD}"/tests/screenshot.jpg "${TESTS_RESULTSDIR}"
fi

if [ -d results ] ; then
mv results/* "$TESTS_RESULTSDIR/"
fi

bailout $RC

Expand Down

0 comments on commit f3a5401

Please sign in to comment.