nixos/test-driver: provide a global timeout

Since the debut of the test-driver, we didn't obtain
a race timer with the test execution to ensure that tests doesn't run beyond
a certain amount of time.

This is particularly important when you are running into hanging tests
which cannot be detected by current facilities (requires more pvpanic wiring up, QMP
API stuff, etc.).

Two easy examples:

- Some QEMU tests may get stuck in some situation and run for more than 24 hours → we default to 1 hour max.
- Some QEMU tests may panic in the wrong place, e.g. UEFI firmware or worse → end users can set a "reasonable" amount of time

And then, we should let the retry logic retest them until they succeed and adjust
their global timeouts.

Of course, this does not help with the fact that the timeout may need to be
a function of the actual busyness of the machine running the tests.
This is only one step towards increased reliability.

Changed files
+48
nixos
lib
test-driver
test_driver
testing
+9
nixos/lib/test-driver/test_driver/__init__.py
···
help="vlans to span by the driver",
)
arg_parser.add_argument(
"-o",
"--output_directory",
help="""The path to the directory where outputs copied from the VM will be placed.
···
args.testscript.read_text(),
args.output_directory.resolve(),
args.keep_vm_state,
) as driver:
if args.interactive:
history_dir = os.getcwd()
···
help="vlans to span by the driver",
)
arg_parser.add_argument(
+
"--global-timeout",
+
type=int,
+
metavar="GLOBAL_TIMEOUT",
+
action=EnvDefault,
+
envvar="globalTimeout",
+
help="Timeout in seconds for the whole test",
+
)
+
arg_parser.add_argument(
"-o",
"--output_directory",
help="""The path to the directory where outputs copied from the VM will be placed.
···
args.testscript.read_text(),
args.output_directory.resolve(),
args.keep_vm_state,
+
args.global_timeout,
) as driver:
if args.interactive:
history_dir = os.getcwd()
+25
nixos/lib/test-driver/test_driver/driver.py
···
import os
import re
import tempfile
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Callable, ContextManager, Dict, Iterator, List, Optional, Union
···
vlans: List[VLan]
machines: List[Machine]
polling_conditions: List[PollingCondition]
def __init__(
self,
···
tests: str,
out_dir: Path,
keep_vm_state: bool = False,
):
self.tests = tests
self.out_dir = out_dir
tmp_dir = get_tmp_dir()
···
def __exit__(self, *_: Any) -> None:
with rootlog.nested("cleanup"):
for machine in self.machines:
machine.release()
···
def run_tests(self) -> None:
"""Run the test script (for non-interactive test runs)"""
self.test_script()
# TODO: Collect coverage data
for machine in self.machines:
···
with rootlog.nested("wait for all VMs to finish"):
for machine in self.machines:
machine.wait_for_shutdown()
def create_machine(self, args: Dict[str, Any]) -> Machine:
tmp_dir = get_tmp_dir()
···
import os
import re
+
import signal
import tempfile
+
import threading
from contextlib import contextmanager
from pathlib import Path
from typing import Any, Callable, ContextManager, Dict, Iterator, List, Optional, Union
···
vlans: List[VLan]
machines: List[Machine]
polling_conditions: List[PollingCondition]
+
global_timeout: int
+
race_timer: threading.Timer
def __init__(
self,
···
tests: str,
out_dir: Path,
keep_vm_state: bool = False,
+
global_timeout: int = 24 * 60 * 60 * 7,
):
self.tests = tests
self.out_dir = out_dir
+
self.global_timeout = global_timeout
+
self.race_timer = threading.Timer(global_timeout, self.terminate_test)
tmp_dir = get_tmp_dir()
···
def __exit__(self, *_: Any) -> None:
with rootlog.nested("cleanup"):
+
self.race_timer.cancel()
for machine in self.machines:
machine.release()
···
def run_tests(self) -> None:
"""Run the test script (for non-interactive test runs)"""
+
rootlog.info(
+
f"Test will time out and terminate in {self.global_timeout} seconds"
+
)
+
self.race_timer.start()
self.test_script()
# TODO: Collect coverage data
for machine in self.machines:
···
with rootlog.nested("wait for all VMs to finish"):
for machine in self.machines:
machine.wait_for_shutdown()
+
self.race_timer.cancel()
+
+
def terminate_test(self) -> None:
+
# This will be usually running in another thread than
+
# the thread actually executing the test script.
+
with rootlog.nested("timeout reached; test terminating..."):
+
for machine in self.machines:
+
machine.release()
+
# As we cannot `sys.exit` from another thread
+
# We can at least force the main thread to get SIGTERM'ed.
+
# This will prevent any user who caught all the exceptions
+
# to swallow them and prevent itself from terminating.
+
os.kill(os.getpid(), signal.SIGTERM)
def create_machine(self, args: Dict[str, Any]) -> Machine:
tmp_dir = get_tmp_dir()
+1
nixos/lib/testing-python.nix
···
, nodes ? {}
, testScript
, enableOCR ? false
, name ? "unnamed"
, skipTypeCheck ? false
# Skip linting (mainly intended for faster dev cycles)
···
, nodes ? {}
, testScript
, enableOCR ? false
+
, globalTimeout ? (60 * 60)
, name ? "unnamed"
, skipTypeCheck ? false
# Skip linting (mainly intended for faster dev cycles)
+13
nixos/lib/testing/driver.nix
···
wrapProgram $out/bin/nixos-test-driver \
--set startScripts "''${vmStartScripts[*]}" \
--set testScript "$out/test-script" \
--set vlans '${toString vlans}' \
${lib.escapeShellArgs (lib.concatMap (arg: ["--add-flags" arg]) config.extraDriverArgs)}
'';
···
type = types.package;
default = hostPkgs.qemu_test;
defaultText = "hostPkgs.qemu_test";
};
enableOCR = mkOption {
···
wrapProgram $out/bin/nixos-test-driver \
--set startScripts "''${vmStartScripts[*]}" \
--set testScript "$out/test-script" \
+
--set globalTimeout "${toString config.globalTimeout}" \
--set vlans '${toString vlans}' \
${lib.escapeShellArgs (lib.concatMap (arg: ["--add-flags" arg]) config.extraDriverArgs)}
'';
···
type = types.package;
default = hostPkgs.qemu_test;
defaultText = "hostPkgs.qemu_test";
+
};
+
+
globalTimeout = mkOption {
+
description = mdDoc ''
+
A global timeout for the complete test, expressed in seconds.
+
Beyond that timeout, every resource will be killed and released and the test will fail.
+
+
By default, we use a 1 hour timeout.
+
'';
+
type = types.int;
+
default = 60 * 60;
+
example = 10 * 60;
};
enableOCR = mkOption {