Skip to content

Commit e4a8323

Browse files
committed
Fix test-driver output parsing crash on Ubuntu
When running tests on Ubuntu VMs, the `test-driver` can crash if the executed command calls `sudo` and writes to stderr. For example: ```python vm.execute("sudo bash -c \"echo 'Created foo → bar.\n' >&2 && echo 'foo' \"") ``` `sudo` spawns a new TTY on Ubuntu, merging both `stdout` and `stderr` into a single stream. However, the `test-driver` only expects base64-encoded `stdout` and fails when stderr data appears. - Normal command (note `1` and `2` are different consoles) ``` > bash -c 'echo $$; ls -l /proc/$$/fd' lr-x------ 1 root root 64 Feb 18 06:10 0 -> /dev/hvc0 l-wx------ 1 root root 64 Feb 18 06:10 1 -> pipe:[20942] l-wx------ 1 root root 64 Feb 18 06:10 2 -> /dev/ttyS0 lr-x------ 1 root root 64 Feb 18 06:10 3 -> /proc/664/fd ``` - Using `sudo` (note both `1` and `2` use the same console `/dev/pts/0`) ``` > sudo bash -c 'echo $$; ls -l /proc/$$/fd lrwx------ 1 root root 64 Feb 18 06:10 0 -> /dev/pts/0 l-wx------ 1 root root 64 Feb 18 06:10 1 -> pipe:[20943] lrwx------ 1 root root 64 Feb 18 06:10 2 -> /dev/pts/0 lr-x------ 1 root root 64 Feb 18 06:10 3 -> /proc/670/fd ``` Because `stderr` is now mixed with `stdout` on `/dev/pts/0`, the `test-driver`'s base64 decoding code crashes on unexpected data: ([encoding step](https://github.com/NixOS/nixpkgs/blob/8a24fbd0f3b4f47f01c897a95b1b65d6a5576c01/nixos/lib/test-driver/src/test_driver/machine.py#L578), [decoding step](https://github.com/NixOS/nixpkgs/blob/8a24fbd0f3b4f47f01c897a95b1b65d6a5576c01/nixos/lib/test-driver/src/test_driver/machine.py#L588)). A minimal reproducible example is provided in [am-on/nix-test-driver-ubuntu-bug](https://github.com/am-on/nix-test-driver-ubuntu-bug). Related issues: - numtide#84 - numtide#5
1 parent c176787 commit e4a8323

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/ubuntu.nix

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ in {
4949
'';
5050
}).sandboxed;
5151

52+
sudoSameConsole = (lib.ubuntu."23_04" {
53+
sharedDirs = {};
54+
testScript = ''
55+
# Ensure using sudo doesn't crash the test-driver
56+
vm.execute("sudo bash -c \"echo 'Created foo → bar.\n' >&2 && echo 'foo' \"")
57+
'';
58+
}).sandboxed;
59+
5260
}
5361
// package.ubuntu.images
5462
// runTestOnEveryImage multiUserTest

ubuntu/default.nix

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ let
5757
systemctl mask snapd.socket
5858
systemctl mask snapd.seeded.service
5959
60+
# Disable TTY usage in sudo.
61+
# Otherwise, using sudo spawns a new pty, causing the test-driver to
62+
# receive mixed stdout and stderr when processing command output.
63+
# The driver only expects base64-encoded stdout, so extra stderr data
64+
# can break the output parsing.
65+
mkdir -p /etc/sudoers.d
66+
cat << EOF > /etc/sudoers.d/disable-pty
67+
Defaults !requiretty
68+
Defaults !use_pty
69+
EOF
70+
visudo -cf /etc/sudoers.d/disable-pty
71+
chmod 440 /etc/sudoers.d/disable-pty
72+
6073
# We have no network in the test VMs, avoid an error on bootup
6174
systemctl mask ssh.service
6275
systemctl mask ssh.socket

0 commit comments

Comments
 (0)