From 6032561e9b12b99fa51b4b38bdeee7f831adcaee Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 3 Jun 2024 07:43:00 +0200 Subject: Adding debian version 0.143. Signed-off-by: Daniel Baumann --- debian/tests/check-log | 266 ++++++++++++++++++++++++++++++++++++++ debian/tests/control | 55 ++++++++ debian/tests/hooks/drop-hostname | 17 +++ debian/tests/hooks/persistent-net | 32 +++++ debian/tests/qemu-ata-only | 24 ++++ debian/tests/qemu-busybox | 21 +++ debian/tests/qemu-klibc | 21 +++ debian/tests/qemu-net | 128 ++++++++++++++++++ debian/tests/qemu-panic-shell | 35 +++++ debian/tests/qemu-separate-usr | 37 ++++++ debian/tests/qemu-virtio-only | 23 ++++ debian/tests/run-qemu | 50 +++++++ debian/tests/test-common | 204 +++++++++++++++++++++++++++++ 13 files changed, 913 insertions(+) create mode 100755 debian/tests/check-log create mode 100644 debian/tests/control create mode 100755 debian/tests/hooks/drop-hostname create mode 100755 debian/tests/hooks/persistent-net create mode 100755 debian/tests/qemu-ata-only create mode 100755 debian/tests/qemu-busybox create mode 100755 debian/tests/qemu-klibc create mode 100755 debian/tests/qemu-net create mode 100755 debian/tests/qemu-panic-shell create mode 100755 debian/tests/qemu-separate-usr create mode 100755 debian/tests/qemu-virtio-only create mode 100755 debian/tests/run-qemu create mode 100644 debian/tests/test-common (limited to 'debian/tests') diff --git a/debian/tests/check-log b/debian/tests/check-log new file mode 100755 index 0000000..01b24b9 --- /dev/null +++ b/debian/tests/check-log @@ -0,0 +1,266 @@ +#!/usr/bin/python3 + +# pylint: disable=invalid-name +# pylint: enable=invalid-name + +"""Run given checks on the log output.""" + +import argparse +import json +import pathlib +import re +import shlex +import sys +import typing + + +class Check: + """The Check class contains all the checks that the caller can run.""" + + def __init__(self, log: str) -> None: + self.errors = 0 + self.command_outputs = self._extract_command_outputs_from_log(log) + + def _error(self, msg: str) -> None: + print("ERROR: " + msg) + self.errors += 1 + + @staticmethod + def _extract_command_outputs_from_log(log: str) -> dict[str, str]: + """Extract command outputs from the given log output. + + The output must be framed by a header and a footer line. The header + line contains the key surrounded by 10 # characters. The footer + line consist of 40 # characters. Returns a mapping from the output + key to the command output. + """ + marker = "#" * 10 + footer = "#" * 40 + matches = re.findall( + f"^{marker} ([^#]+) {marker}\n(.*?\n){footer}$", + log, + flags=re.DOTALL | re.MULTILINE, + ) + return {m[0]: m[1] for m in matches} + + def get_commands_from_ps_output(self) -> list[str]: + """Get list of command from `ps -ww aux` output.""" + ps_output = self.command_outputs["ps -ww aux"] + lines = ps_output.strip().split("\n")[1:] + commands = [] + for line in lines: + columns = re.split(r"\s+", line, maxsplit=10) + commands.append(columns[10]) + return commands + + def get_ip_addr(self) -> list[typing.Any]: + """Get IP address information from `ip addr` JSON output.""" + ip_addr = json.loads(self.command_outputs["ip -json addr"]) + assert isinstance(ip_addr, list) + return ip_addr + + def get_ip_route(self) -> list[typing.Any]: + """Get IP route information from `ip route` JSON output.""" + ip_route = json.loads(self.command_outputs["ip -json route"]) + assert isinstance(ip_route, list) + return ip_route + + def run_checks(self, args: list[str]) -> int: + """Run the checks and return the number of errors found. + + The methods of this class can be u + """ + if not args: + return self.errors + + try: + check = getattr(self, args[0]) + except AttributeError: + self._error(f"Check '{args[0]}' not found.") + return self.errors + check_args = [] + + for arg in args[1:]: + if not hasattr(self, arg): + check_args.append(arg) + continue + + check(*check_args) + check = getattr(self, arg) + check_args = [] + + check(*check_args) + return self.errors + + def _check_is_subset(self, actual: set[str], expected: set[str]) -> None: + """Check that the first dictionary is a subset of the second one. + + Log errors if the sets are different. + """ + unexpected = actual - expected + if unexpected: + self._error(f"Not expected entries: {unexpected}") + + def _check_is_subdict( + self, + expected_dict: dict[str, str], + actual_dict: dict[str, str], + log_prefix: str, + ) -> None: + """Check that the first dictionary is a subset of the second one. + + Log errors if differences are found. + """ + missing_keys = set(expected_dict.keys()) - set(actual_dict.keys()) + if missing_keys: + self._error(f"{log_prefix}Missing keys: {missing_keys}") + for key, expected_value in sorted(expected_dict.items()): + actual_value = actual_dict.get(key, "") + if expected_value != actual_value: + self._error( + f"{log_prefix}Value for key '{key}' differs:" + f" '{expected_value}' expected, but got '{actual_value}'" + ) + + # Below are all checks that the user might call. + + def has_hostname(self, hostname_pattern: str) -> None: + """Check that the hostname matches the given regular expression.""" + hostname = self.command_outputs["hostname"].strip() + if re.fullmatch(hostname_pattern, hostname): + print(f"hostname '{hostname}' matches pattern '{hostname_pattern}'") + return + self._error( + f"hostname '{hostname}' does not match" + f" expected pattern '{hostname_pattern}'" + ) + + def has_interface_mtu(self, device_pattern: str, expected_mtu: str) -> None: + """Check that a matching network device has the expected MTU set.""" + for device in self.get_ip_addr(): + if not re.fullmatch(device_pattern, device["ifname"]): + continue + if str(device["mtu"]) == expected_mtu: + print(f"device {device['ifname']} has MTU {device['mtu']}") + return + self._error( + f"device {device['ifname']} has MTU {device['mtu']}" + f" but expected {expected_mtu}" + ) + return + self._error(f"no link found that matches '{device_pattern}'") + + def has_ip_addr(self, family: str, addr_pattern: str, device_pattern: str) -> None: + """Check that a matching network device has a matching IP address.""" + for device in self.get_ip_addr(): + if not re.fullmatch(device_pattern, device["ifname"]): + continue + for addr in device["addr_info"]: + if addr["family"] != family or addr["scope"] != "global": + continue + address = f"{addr['local']}/{addr['prefixlen']}" + if re.fullmatch(addr_pattern, address): + print(f"found addr {address} for {device['ifname']}: {addr}") + return + self._error( + f"addr {address} for {device['ifname']}" + f" does not match {addr_pattern}: {addr}" + ) + return + name = {"inet": "IPv4", "inet6": "IPv6"}[family] + self._error( + f"no link found that matches '{device_pattern}' and has an {name} address" + ) + + def has_ipv4_addr(self, addr_pattern: str, device_pattern: str) -> None: + """Check that a matching network device has a matching IPv4 address.""" + self.has_ip_addr("inet", addr_pattern, device_pattern) + + def has_ipv6_addr(self, addr_pattern: str, device_pattern: str) -> None: + """Check that a matching network device has a matching IPv6 address.""" + self.has_ip_addr("inet6", addr_pattern, device_pattern) + + def has_ipv4_default_route(self, gateway_pattern: str, device_pattern: str) -> None: + """Check that the IPv4 default route is via a matching gateway and device.""" + for route in self.get_ip_route(): + if route["dst"] != "default": + continue + if not re.fullmatch(gateway_pattern, route["gateway"]) or not re.fullmatch( + device_pattern, route["dev"] + ): + self._error( + f"Default IPv4 route does not match expected gateway pattern" + f" '{gateway_pattern}' or dev pattern '{device_pattern}': {route}" + ) + continue + print( + f"found IPv4 default route via {route['gateway']}" + f" for {route['dev']}: {route}" + ) + return + self._error("no IPv4 default route found") + + def has_net_conf(self, min_expected_files: str, *expected_net_confs: str) -> None: + """Compare the /run/net*.conf files. + + There must be at least `min_expected_files` /run/net*.conf files + in the log output and no unexpected one. The format for + `expected_net_confs` is `=`. + """ + + expected = dict(nc.split("=", maxsplit=1) for nc in expected_net_confs) + prog = re.compile(r"/run/net[^#]+\.conf") + got = { + key: value for key, value in self.command_outputs.items() if prog.match(key) + } + + if len(got) < int(min_expected_files): + self._error( + f"Expected at least {min_expected_files} /run/net*.conf files," + f" but got only {len(got)}: {set(got.keys())}" + ) + self._check_is_subset(set(got.keys()), set(expected.keys())) + + for net_dev in sorted(got.keys()): + log_prefix = f"{net_dev}: " + expected_net_conf = parse_net_conf(expected.get(net_dev, "")) + actual_net_conf = parse_net_conf(got.get(net_dev, "")) + self._check_is_subdict(expected_net_conf, actual_net_conf, log_prefix) + print(f"compared {len(expected_net_conf)} items from {net_dev}") + + def has_no_running_processes(self) -> None: + """Check that there are no remaining running processes from the initrd.""" + processes = drop_kernel_processes(self.get_commands_from_ps_output()) + if len(processes) == 2: + print(f"found only expected init and ps process: {processes}") + return + self._error( + f"Expected only init and ps process, but got {len(processes)}: {processes}" + ) + + +def drop_kernel_processes(processes: list[str]) -> list[str]: + """Return a list of processes with the kernel processes dropped.""" + return [p for p in processes if not p.startswith("[") or not p.endswith("]")] + + +def parse_net_conf(net_conf: str) -> dict[str, str]: + """Parse /run/net*.conf file and return a key to value mapping.""" + items = shlex.split(net_conf) + return dict(item.split("=", maxsplit=1) for item in items) + + +def main(arguments: list[str]) -> int: + """Run given checks on the log output. Return number of errors.""" + parser = argparse.ArgumentParser() + parser.add_argument("log_file", metavar="log-file") + parser.add_argument("checks", metavar="check", nargs="+") + args = parser.parse_args(arguments) + + log = pathlib.Path(args.log_file).read_text(encoding="ascii") + check = Check(log) + return check.run_checks(args.checks) + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/debian/tests/control b/debian/tests/control new file mode 100644 index 0000000..f0fd817 --- /dev/null +++ b/debian/tests/control @@ -0,0 +1,55 @@ +Tests: qemu-klibc +Architecture: amd64 armhf s390x +Depends: genext2fs, + ipxe-qemu, + klibc-utils, + linux-image-generic, + qemu-efi-arm [armhf], + qemu-kvm, + zstd, + @ + +Tests: qemu-busybox +Architecture: amd64 armhf s390x +Depends: busybox | busybox-initramfs, + genext2fs, + ipxe-qemu, + klibc-utils, + linux-image-generic, + qemu-efi-arm [armhf], + qemu-kvm, + zstd, + @ + +Tests: qemu-ata-only +Architecture: amd64 +Depends: genext2fs, klibc-utils, linux-image-generic, qemu-kvm, zstd, @ + +Tests: qemu-virtio-only qemu-separate-usr qemu-panic-shell +Architecture: amd64 arm64 armhf ppc64el s390x +Depends: genext2fs, + ipxe-qemu, + klibc-utils, + linux-image-generic, + qemu-efi-aarch64 [arm64], + qemu-efi-arm [armhf], + qemu-kvm, + seabios [ppc64el], + zstd, + @ + +Tests: qemu-net +Architecture: amd64 arm64 armhf ppc64el s390x +Depends: genext2fs, + iproute2, + ipxe-qemu, + klibc-utils, + linux-image-generic, + procps, + python3, + qemu-efi-aarch64 [arm64], + qemu-efi-arm [armhf], + qemu-kvm, + seabios [ppc64el], + zstd, + @ diff --git a/debian/tests/hooks/drop-hostname b/debian/tests/hooks/drop-hostname new file mode 100755 index 0000000..e7b8500 --- /dev/null +++ b/debian/tests/hooks/drop-hostname @@ -0,0 +1,17 @@ +#!/bin/sh + +PREREQ="" + +prereqs() +{ + echo "$PREREQ" +} + +case "$1" in +prereqs) + prereqs + exit 0 + ;; +esac + +rm -f "${DESTDIR}/etc/hostname" diff --git a/debian/tests/hooks/persistent-net b/debian/tests/hooks/persistent-net new file mode 100755 index 0000000..819109e --- /dev/null +++ b/debian/tests/hooks/persistent-net @@ -0,0 +1,32 @@ +#!/bin/sh + +PREREQ="" + +prereqs() +{ + echo "$PREREQ" +} + +case "$1" in +prereqs) + prereqs + exit 0 + ;; +esac + +persist_net() { + name="$1" + mac="$2" + + mkdir -p "${DESTDIR}/etc/systemd/network" + cat >"${DESTDIR}/etc/systemd/network/10-persistent-${name}.link" <>"${CONFDIR}/initramfs.conf" <"${CONFDIR}/modules" <>"${CONFDIR}/initramfs.conf" <>"${CONFDIR}/initramfs.conf" <>"${CONFDIR}/initramfs.conf" <"${CONFDIR}/modules" <>"${CONFDIR}/initramfs.conf" <"${CONFDIR}/modules" <>"${CONFDIR}/initramfs.conf" <"${CONFDIR}/modules" < "${ROOTDIR}/etc/fstab" "/dev/${USRDISK_LINUX_NAME} /usr ext2 defaults 0 2" +USRDIR="$(mktemp -d)" +mv "${ROOTDIR}/usr/"* "${USRDIR}" + +build_rootfs_ext2 +build_fs_ext2 "${USRDIR}" "${USRDISK}" + +run_qemu +check_no_network_configuration + +# Check that fsck ran on both devices +grep -q "^/dev/${ROOTDISK_LINUX_NAME}: clean," "${OUTPUT}" +grep -q "^/dev/${USRDISK_LINUX_NAME}: clean," "${OUTPUT}" diff --git a/debian/tests/qemu-virtio-only b/debian/tests/qemu-virtio-only new file mode 100755 index 0000000..79670cd --- /dev/null +++ b/debian/tests/qemu-virtio-only @@ -0,0 +1,23 @@ +#!/bin/sh -e + +SUPPORTED_FLAVOURS='amd64 arm64 armmp powerpc64 s390x generic' +ROOTDISK_QEMU_IF=virtio +ROOTDISK_LINUX_NAME=vda +. debian/tests/test-common + +cat >>"${CONFDIR}/initramfs.conf" <"${CONFDIR}/modules" <&2 + echo "Usage: ${0##*/} kernel initrd append [extra_args]" >&2 + exit 1 +fi + +kernel="$1" +initrd="$2" +append="$3" +shift 3 + +ARCHITECTURE=$(dpkg --print-architecture) + +case "$ARCHITECTURE" in +arm64) + machine="virt,gic-version=max" + cpu="max,pauth-impdef=on" + efi_code=/usr/share/AAVMF/AAVMF_CODE.fd + efi_vars=/usr/share/AAVMF/AAVMF_VARS.fd + ;; +armhf) + machine="virt" + efi_code=/usr/share/AAVMF/AAVMF32_CODE.fd + efi_vars=/usr/share/AAVMF/AAVMF32_VARS.fd + console=ttyAMA0 + ;; +ppc64el) + machine="cap-ccf-assist=off,cap-cfpc=broken,cap-ibs=broken,cap-sbbc=broken" + console=hvc0 + ;; +esac + +if test -f "${efi_vars-}"; then + efi_vars_copy="$(mktemp -t "${efi_vars##*/}.XXXXXXXXXX")" + cp "$efi_vars" "$efi_vars_copy" +fi + +set -- ${machine:+-machine "${machine}"} -cpu "${cpu-max}" -m 1G \ + ${efi_code:+-drive "file=${efi_code},if=pflash,format=raw,read-only=on"} \ + ${efi_vars:+-drive "file=${efi_vars_copy},if=pflash,format=raw"} \ + -device virtio-rng-pci,rng=rng0 -object rng-random,filename=/dev/urandom,id=rng0 \ + -nodefaults -no-reboot -kernel "${kernel}" -initrd "${initrd}" "$@" \ + -append "console=${console:-ttyS0},115200 ro ${append}" +echo "${0##*/}: qemu-system-${ARCHITECTURE} $*" +exec "qemu-system-${ARCHITECTURE}" "$@" diff --git a/debian/tests/test-common b/debian/tests/test-common new file mode 100644 index 0000000..981aafe --- /dev/null +++ b/debian/tests/test-common @@ -0,0 +1,204 @@ +# -*- mode: sh -*- + +# Find kernel flavour and release +KVER= +for flavour in $SUPPORTED_FLAVOURS; do + KVER="$(dpkg-query -Wf '${Depends}' "linux-image-${flavour}" 2>/dev/null | tr ',' '\n' | sed -n 's/^ *linux-image-\([-a-z0-9+.]*\).*/\1/p')" + if [ "$KVER" ]; then + break + fi +done +if [ -z "$KVER" ]; then + echo >&2 "E: Test must set SUPPORTED_FLAVOURS and depend on those flavours" + exit 2 +fi + +case "$(dpkg --print-architecture)" in +arm64) + # The Ubuntu arm64 autopkgtest runs rarely into the 1200 seconds timeout. + QEMU_TIMEOUT=1800 + ;; +armhf) + # qemu-busybox on Ubuntu armhf runs into the 300 seconds timeout. + QEMU_TIMEOUT=600 + ;; +ppc64el) + # Slowest execution seen in Ubuntu ppc64el autopkgtest: 230 seconds + QEMU_TIMEOUT=600 + ;; +*) + QEMU_TIMEOUT=120 +esac + +if [ -n "${AUTOPKGTEST_TMP-}" ]; then + export TMPDIR="${AUTOPKGTEST_TMP}" +fi + +BASEDIR="$(mktemp -d -t initramfs-test.XXXXXXXXXX)" + +# Skeleton configuration directory +CONFDIR="${BASEDIR}/config" +mkdir -p "${CONFDIR}" +cp conf/initramfs.conf "${CONFDIR}/initramfs.conf" +echo "RESUME=none" >>"${CONFDIR}/initramfs.conf" +mkdir "${CONFDIR}/hooks" +touch "${CONFDIR}/modules" +mkdir "${CONFDIR}/scripts" + +# initramfs image file +INITRAMFS="${BASEDIR}/initrd.img" + +# root disk image file +ROOTDISK="${BASEDIR}/rootdisk.raw" + +# root disk interface type (for qemu) and device name (for Linux) +test -n "${ROOTDISK_QEMU_IF}" || ROOTDISK_QEMU_IF=virtio +test -n "${ROOTDISK_LINUX_NAME}" || ROOTDISK_LINUX_NAME=vda + +# Create a root fs with a trivial userspace +ROOTDIR="${BASEDIR}/rootdir" +INIT_MESSAGE='root fs init system started successfully' +for subdir in "" dev proc run sys usr usr/bin usr/lib usr/lib64 usr/sbin; do + mkdir "${ROOTDIR}/${subdir}" +done +for subdir in bin lib lib64 sbin; do + ln -s "usr/$subdir" "${ROOTDIR}/${subdir}" +done +cat >"${ROOTDIR}/sbin/init" <"${root_dir}/usr/sbin/init" <&1 -b "${blocks}" -N "${inodes}" -U -d "${dir}" "${disk}" +} + +build_rootfs_ext2() { + build_fs_ext2 "${ROOTDIR}" "${ROOTDISK}" +} + +_run_qemu() { + local extra_params="$*" + + echo "I: Running qemu (with a timeout of $QEMU_TIMEOUT seconds)..." + timeout --foreground "$QEMU_TIMEOUT" \ + debian/tests/run-qemu /boot/vmlinu*-"${KVER}" "${INITRAMFS}" \ + "root=/dev/${ROOTDISK_LINUX_NAME} ${extra_params}" -nographic \ + -drive "file=${ROOTDISK},if=${ROOTDISK_QEMU_IF},media=disk,format=raw" \ + ${USRDISK:+-drive "file=${USRDISK},if=${USRDISK_QEMU_IF},media=disk,format=raw"} \ + -device "virtio-net-pci,netdev=lan0,mac=52:54:00:65:43:21" \ + -netdev "user,id=lan0,net=10.0.3.0/24,ipv6-net=fec7::/48,hostname=pizza,dnssearch=test,domainname=example.com,bootfile=/path/to/bootfile2" \ + -device "virtio-net-pci,netdev=lan1,mac=52:54:00:12:34:56" \ + -netdev "user,id=lan1,hostname=goulash,dnssearch=example,dnssearch=example.net,domainname=test,bootfile=/path/to/bootfile" \ + -chardev stdio,id=char0 -serial chardev:char0 | tee "${OUTPUT}" +} + +run_qemu_nocheck() { + # hide error messages from autopkgtest + _run_qemu 2>&1 "$@" +} + +run_qemu() { + _run_qemu "panic=-1 $*" + grep -qF "${INIT_MESSAGE}" "${OUTPUT}" +} + +check_no_output() { + local msg="$1" + if grep -qF "${msg}" "${OUTPUT}"; then + echo >&2 "E: Message '${msg}' found in log output '${OUTPUT}." + exit 1 + fi +} + +check_output() { + local msg="$1" + if ! grep -qF "${msg}" "${OUTPUT}"; then + echo >&2 "E: Message '${msg}' not found in log output '${OUTPUT}." + exit 1 + fi +} + +check_no_network_configuration() { + check_no_output "Waiting up to 180 secs for" +} -- cgit v1.2.3