From 29db54c373ec765b43d15b1b8f3d7a73075bea9b Mon Sep 17 00:00:00 2001 From: Jörg Thalheim Date: Mon, 20 Jun 2022 14:48:38 +0200 Subject: nixos/tests: extend shell_interact to accept alternative socat addresses `shell_interact()` is currently not nice to use. If you try to cancel the socat process, it will also break the nixos test. Furthermore ptpython creates it's own terminal that subprocesses are running in, which breaks some of the terminal features of socat. Hence this commit extends `shell_interact` to allow also to connect to arbitrary servers i.e. tcp servers started by socat. --- nixos/lib/test-driver/test_driver/machine.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'nixos/lib') diff --git a/nixos/lib/test-driver/test_driver/machine.py b/nixos/lib/test-driver/test_driver/machine.py index 6af964a0f588e..8f01833bffb42 100644 --- a/nixos/lib/test-driver/test_driver/machine.py +++ b/nixos/lib/test-driver/test_driver/machine.py @@ -549,18 +549,27 @@ class Machine: return (rc, output.decode()) - def shell_interact(self) -> None: - """Allows you to interact with the guest shell + def shell_interact(self, address: Optional[str] = None) -> None: + """Allows you to interact with the guest shell for debugging purposes. - Should only be used during test development, not in the production test.""" + @address string passed to socat that will be connected to the guest shell. + Check the `Running Tests interactivly` chapter of NixOS manual for an example. + """ self.connect() - self.log("Terminal is ready (there is no initial prompt):") + + if address is None: + address = "READLINE,prompt=$ " + self.log("Terminal is ready (there is no initial prompt):") assert self.shell - subprocess.run( - ["socat", "READLINE,prompt=$ ", f"FD:{self.shell.fileno()}"], - pass_fds=[self.shell.fileno()], - ) + try: + subprocess.run( + ["socat", address, f"FD:{self.shell.fileno()}"], + pass_fds=[self.shell.fileno()], + ) + # allow users to cancel this command without breaking the test + except KeyboardInterrupt: + pass def console_interact(self) -> None: """Allows you to interact with QEMU's stdin -- cgit 1.4.1