From 0e72073cb6b5f2d6c3b1ec0eb87412dce809f48c Mon Sep 17 00:00:00 2001 From: aszlig Date: Sat, 29 Aug 2020 00:57:47 +0200 Subject: tests: Switch all tests to Python test driver Since the removal[1] of the Perl test driver, our tests will no longer run or even evaluate. Fortunately, the test API is more or less the same, so the transition to Python was not very involved. However, I did add a "# fmt: off" on top of every testScript, since formatting with black not only has issues with parameterised antiquotations but is also plain ugly to mix 2 spaces of indentation with 4 spaces of indentation. Additionally, I'd like to have a maximum line length of 79 characters in my Nix expressions while black on the other side even *insists* of using longer lines. [1]: https://github.com/NixOS/nixpkgs/commit/0620184f3f94f1bf8de014ab168 Signed-off-by: aszlig --- tests/aszlig/dnyarri/luks2-bcache.nix | 143 ++++++++++++++++------------------ tests/aszlig/programs/psi.nix | 13 ++-- tests/games/starbound.nix | 33 ++++---- tests/make-test.nix | 2 +- tests/programs/gnupg/default.nix | 122 ++++++++++++++--------------- tests/sandbox.nix | 25 +++--- tests/system/kernel/bfq.nix | 5 +- 7 files changed, 165 insertions(+), 178 deletions(-) (limited to 'tests') diff --git a/tests/aszlig/dnyarri/luks2-bcache.nix b/tests/aszlig/dnyarri/luks2-bcache.nix index 0347a0da..1c7fd5fc 100644 --- a/tests/aszlig/dnyarri/luks2-bcache.nix +++ b/tests/aszlig/dnyarri/luks2-bcache.nix @@ -37,33 +37,32 @@ uuid1 = "07b821b9-0912-4f03-9ebc-89f41704caff"; uuid2 = "d140fd40-bb3c-48b5-98e0-b75878dbce66"; in '' - $machine->waitForUnit('multi-user.target'); + # fmt: off + machine.wait_for_unit('multi-user.target') - $machine->nest('setting up LUKS2 and bcache backing devices', sub { - $machine->succeed('dd if=/dev/urandom of=/dev/vdb bs=1 count=200'); + with machine.nested('setting up LUKS2 and bcache backing devices'): + machine.succeed('dd if=/dev/urandom of=/dev/vdb bs=1 count=200') - $machine->succeed('make-bcache -B /dev/vdc'); - $machine->succeed('make-bcache -B /dev/vdd'); + machine.succeed('make-bcache -B /dev/vdc') + machine.succeed('make-bcache -B /dev/vdd') - $machine->waitUntilSucceeds( + machine.wait_until_succeeds( '[ $(echo /dev/bcache[0-9]* | wc -w) -eq 2 ]' - ); - my $bcache1 = $machine->succeed('ls -1 /dev/bcache[0-9]* | head -n1'); - chomp $bcache1; - my $bcache2 = $machine->succeed('ls -1 /dev/bcache[0-9]* | tail -n1'); - chomp $bcache2; - - $machine->succeed( - "${luksFormat} $bcache1 --uuid ${uuid1} /dev/vdb", - "cryptsetup open $bcache1 l1 --key-file=/dev/vdb", - ); - - $machine->succeed( - "${luksFormat} $bcache2 --uuid ${uuid2} /dev/vdb", - "cryptsetup open $bcache2 l2 --key-file=/dev/vdb", - ); - - $machine->succeed( + ) + bcache1 = machine.succeed('ls -1 /dev/bcache[0-9]* | head -n1').strip() + bcache2 = machine.succeed('ls -1 /dev/bcache[0-9]* | tail -n1').strip() + + machine.succeed( + f"${luksFormat} {bcache1} --uuid ${uuid1} /dev/vdb", + f"cryptsetup open {bcache1} l1 --key-file=/dev/vdb", + ) + + machine.succeed( + f"${luksFormat} {bcache2} --uuid ${uuid2} /dev/vdb", + f"cryptsetup open {bcache2} l2 --key-file=/dev/vdb", + ) + + machine.succeed( 'mkfs.btrfs -L testfs -m raid1 -d raid1 /dev/mapper/l1 /dev/mapper/l2', 'btrfs dev scan', 'mkdir /mnt-test', @@ -72,61 +71,53 @@ 'umount /mnt-test', 'cryptsetup close l1', 'cryptsetup close l2', - ); - }); - - $machine->nest('rebooting into new configuration', sub { - $machine->shutdown; - $newmachine->{stateDir} = $machine->{stateDir}; - $newmachine->waitForUnit('multi-user.target'); - }); - - my $bcache1 = - $newmachine->succeed('cd /dev; ls -1 bcache[0-9]* | head -n1'); - chomp $bcache1; - my $bcache2 = - $newmachine->succeed('cd /dev; ls -1 bcache[0-9]* | tail -n1'); - chomp $bcache2; - - $machine->nest('attaching bcache cache device', sub { - my $csetuuid = $newmachine->succeed( + ) + + with machine.nested('rebooting into new configuration'): + machine.shutdown() + newmachine.state_dir = machine.state_dir + newmachine.wait_for_unit('multi-user.target') + + bcache1 = newmachine.succeed( + 'cd /dev; ls -1 bcache[0-9]* | head -n1' + ).strip() + bcache2 = newmachine.succeed( + 'cd /dev; ls -1 bcache[0-9]* | tail -n1' + ).strip() + + with machine.nested('attaching bcache cache device'): + csetuuid = newmachine.succeed( 'make-bcache -C /dev/vde | sed -n -e "s/^Set UUID:[^a-f0-9]*//p"' - ); - chomp $csetuuid; - - $newmachine->nest('wait for cache device to appear', sub { - $newmachine->waitUntilSucceeds("test -e /sys/fs/bcache/$csetuuid"); - }); - - $newmachine->succeed( - "echo $csetuuid > /sys/block/$bcache1/bcache/attach", - "echo writeback > /sys/block/$bcache1/bcache/cache_mode", - "echo $csetuuid > /sys/block/$bcache2/bcache/attach", - "echo writeback > /sys/block/$bcache2/bcache/cache_mode" - ); - }); - - $machine->nest('write random files to test file system', sub { - $newmachine->succeed( - 'for i in $(seq 100); do'. - ' dd if=/dev/urandom of="/test/randfile.$i" bs=1 count=100;'. - ' sha256sum "/test/randfile.$i" > "/test/randfile.$i.sha256"; '. + ).strip() + + with newmachine.nested('wait for cache device to appear'): + newmachine.wait_until_succeeds(f"test -e /sys/fs/bcache/{csetuuid}") + + newmachine.succeed( + f"echo {csetuuid} > /sys/block/{bcache1}/bcache/attach", + f"echo writeback > /sys/block/{bcache1}/bcache/cache_mode", + f"echo {csetuuid} > /sys/block/{bcache2}/bcache/attach", + f"echo writeback > /sys/block/{bcache2}/bcache/cache_mode", + ) + + with machine.nested('write random files to test file system'): + newmachine.succeed( + 'for i in $(seq 100); do' + ' dd if=/dev/urandom of="/test/randfile.$i" bs=1 count=100;' + ' sha256sum "/test/randfile.$i" > "/test/randfile.$i.sha256"; ' 'done' - ); - }); - - $machine->nest('reboot to clear disk buffers', sub { - $newmachine->shutdown; - $newmachine->waitForUnit('multi-user.target'); - }); - - $machine->nest('verifying contents of random files created earlier', sub { - $newmachine->succeed( - 'for i in $(seq 100); do'. - ' sha256sum "/test/randfile.$i" | cmp - "/test/randfile.$i.sha256"'. - ' || exit 1; '. + ) + + with machine.nested('reboot to clear disk buffers'): + newmachine.shutdown() + newmachine.wait_for_unit('multi-user.target') + + with machine.nested('verifying contents of random files created earlier'): + newmachine.succeed( + 'for i in $(seq 100); do' + ' sha256sum "/test/randfile.$i" | cmp - "/test/randfile.$i.sha256"' + ' || exit 1; ' 'done' - ); - }); + ) ''; } diff --git a/tests/aszlig/programs/psi.nix b/tests/aszlig/programs/psi.nix index 0b6643f8..05b87697 100644 --- a/tests/aszlig/programs/psi.nix +++ b/tests/aszlig/programs/psi.nix @@ -15,11 +15,12 @@ enableOCR = true; testScript = '' - $machine->waitForX; - $machine->waitForFile("/home/alice/.Xauthority"); - $machine->succeed("xauth merge ~alice/.Xauthority"); - $machine->succeed('su -c "DISPLAY=:0.0 psi" - alice &'); - $machine->waitForText(qr/Register new account/i); - $machine->screenshot('psi'); + # fmt: off + machine.wait_for_x() + machine.wait_for_file("/home/alice/.Xauthority") + machine.succeed("xauth merge ~alice/.Xauthority") + machine.succeed('su -c "DISPLAY=:0.0 psi" - alice &') + machine.wait_for_text('(?i)Register new account') + machine.screenshot('psi') ''; } diff --git a/tests/games/starbound.nix b/tests/games/starbound.nix index d1ef15e1..5fbb85aa 100644 --- a/tests/games/starbound.nix +++ b/tests/games/starbound.nix @@ -10,10 +10,9 @@ let ''; escapeScreenshot = pkgs.lib.replaceStrings ["-"] ["_"]; in '' - $client->nest("${description}", sub { - $client->screenshot("before_${escapeScreenshot name}"); - $client->succeed("${pkgs.xdotool}/bin/xdotool '${xdoFile}'"); - }); + with client.nested("${description}"): + client.screenshot("before_${escapeScreenshot name}") + client.succeed("${pkgs.xdotool}/bin/xdotool '${xdoFile}'") ''; clickAt = name: x: y: xdo { @@ -68,22 +67,22 @@ in { }; testScript = '' - $server->waitForUnit("starbound.service"); + # fmt: off + server.wait_for_unit("starbound.service") - $client->nest("waiting for client to start up", sub { - $client->waitForX; - $client->succeed("starbound >&2 &"); - $client->waitForText(qr/options/i); - }); + with client.nested("waiting for client to start up"): + client.wait_for_x() + client.succeed("starbound >&2 &") + client.wait_for_text('(?i)options') ${clickAt "join-game" 100 560} - $client->waitForText(qr/select/i); + client.wait_for_text('(?i)select') ${clickAt "new-character" 460 220} - $client->waitForText(qr/randomise/i); + client.wait_for_text('(?i)randomise') ${clickAt "create-character" 600 625} - $client->waitForText(qr/select/i); + client.wait_for_text('(?i)select') ${clickAt "use-character" 460 220} - $client->waitForText(qr/ser[vu]er/i); + client.wait_for_text('(?i)ser[vu]er') ${clickAt "server-address" 460 322} ${typeText "server-address" "192.168.0.1"} @@ -96,8 +95,8 @@ in { ${clickAt "join-server" 495 420} - $client->waitForText(qr/graduation/i); - $client->sleep(30); - $client->screenshot("client"); + client.wait_for_text('(?i)graduation') + client.sleep(30) + client.screenshot("client") ''; } diff --git a/tests/make-test.nix b/tests/make-test.nix index cdc763c7..39586932 100644 --- a/tests/make-test.nix +++ b/tests/make-test.nix @@ -9,7 +9,7 @@ testFun: pkgs = import nixpkgsPath { inherit system; }; - testLib = import "${nixpkgsPath}/nixos/lib/testing.nix" { + testLib = import "${nixpkgsPath}/nixos/lib/testing-python.nix" { inherit pkgs system; }; diff --git a/tests/programs/gnupg/default.nix b/tests/programs/gnupg/default.nix index 504f6e46..098178a4 100644 --- a/tests/programs/gnupg/default.nix +++ b/tests/programs/gnupg/default.nix @@ -42,30 +42,28 @@ in { }; testScript = '' - $machine->waitForUnit("sshd.service"); - $machine->succeed("ssh-keygen -t ed25519 -f /root/id_ed25519 -N '''"); - my $cmd = 'mkdir -p ~/.ssh && cat > ~/.ssh/authorized_keys'; - $machine->succeed("su -c 'umask 0077; $cmd' alice < /root/id_ed25519.pub"); - - $machine->waitForX; - - sub ssh ($) { - my $esc = $_[0] =~ s/'/'\\${"'"}'/gr; - return "ssh -q -i /root/id_ed25519". - " -o StrictHostKeyChecking=no". - " alice\@127.0.0.1 -- '$esc'"; - } - - sub xsu ($) { - my $esc = $_[0] =~ s/'/'\\${"'"}'/gr; - return "DISPLAY=:0 su alice -c '$esc'"; - } - - $machine->nest("import snakeoil key", sub { - $machine->succeed(ssh "${cliTestWithPassphrase '' + # fmt: off + from shlex import quote + + machine.wait_for_unit("sshd.service") + machine.succeed("ssh-keygen -t ed25519 -f /root/id_ed25519 -N '''") + cmd = 'mkdir -p ~/.ssh && cat > ~/.ssh/authorized_keys' + machine.succeed(f"su -c 'umask 0077; {cmd}' alice < /root/id_ed25519.pub") + + machine.wait_for_x() + + def ssh(cmd: str) -> str: + return "ssh -q -i /root/id_ed25519 -o StrictHostKeyChecking=no" \ + f" alice@127.0.0.1 -- {quote(cmd)}" + + def xsu(cmd: str) -> str: + return f"DISPLAY=:0 su alice -c {quote(cmd)}" + + with machine.nested("import snakeoil key"): + machine.succeed(ssh("${cliTestWithPassphrase '' gpg --import ${./snakeoil.asc} - ''}"); - $machine->succeed(ssh "${mkExpect '' + ''}")) + machine.succeed(ssh("${mkExpect '' expect gpg> send trust\r expect decision? @@ -74,63 +72,59 @@ in { send y\r expect gpg> send save\r - '' "gpg --edit-key ECC15FE1"}"); - }); + '' "gpg --edit-key ECC15FE1"}")) - subtest "test SSH agent support", sub { - $machine->succeed(ssh 'ssh-keygen -t ed25519 -f ~/testkey -N ""'); - $machine->succeed(ssh '${mkExpect '' + with subtest("test SSH agent support"): + machine.succeed(ssh('ssh-keygen -t ed25519 -f ~/testkey -N ""')) + machine.succeed(ssh('${mkExpect '' expect -regexp ---+.*Please.enter send supersecret\r expect -regexp ---+.*Please.re-en send supersecret\r - '' "ssh-add ~/testkey"}'); + '' "ssh-add ~/testkey"}')) - $machine->succeed("umask 0077; $cmd < ~alice/testkey.pub"); - $machine->succeed(ssh 'rm ~/testkey*'); + machine.succeed(f"umask 0077; {cmd} < ~alice/testkey.pub") + machine.succeed(ssh('rm ~/testkey*')) - $machine->succeed(ssh 'ssh -o StrictHostKeyChecking=no root@127.0.0.1'. - ' touch /i_have_thu_powarr'); - $machine->succeed("test -e /i_have_thu_powarr"); + machine.succeed(ssh( + 'ssh -o StrictHostKeyChecking=no root@127.0.0.1' + ' touch /i_have_thu_powarr' + )) + machine.succeed("test -e /i_have_thu_powarr") - $machine->succeed(ssh "systemctl --user reload gpg-agent"); + machine.succeed(ssh("systemctl --user reload gpg-agent")) - $machine->succeed(ssh "${cliTestWithPassphrase '' + machine.succeed(ssh("${cliTestWithPassphrase '' ssh -o StrictHostKeyChecking=no root@127.0.0.1 \ touch /i_still_have_thu_powarr - ''}"); - $machine->succeed("test -e /i_still_have_thu_powarr"); - }; - - subtest "socket persists after restart", sub { - $machine->succeed(ssh 'test -e "$SSH_AUTH_SOCK"'); - $machine->succeed(ssh 'systemctl --user stop gpg-agent.service'); - $machine->succeed(ssh 'test -e "$SSH_AUTH_SOCK"'); - }; - - subtest "test from SSH", sub { - $machine->execute(ssh "systemctl --user reload gpg-agent"); - $machine->succeed(ssh "${cliTestWithPassphrase '' + ''}")) + machine.succeed("test -e /i_still_have_thu_powarr") + + with subtest("socket persists after restart"): + machine.succeed(ssh('test -e "$SSH_AUTH_SOCK"')) + machine.succeed(ssh('systemctl --user stop gpg-agent.service')) + machine.succeed(ssh('test -e "$SSH_AUTH_SOCK"')) + + with subtest("test from SSH"): + machine.execute(ssh("systemctl --user reload gpg-agent")) + machine.succeed(ssh("${cliTestWithPassphrase '' echo encrypt me > to_encrypt gpg -sea -r ECC15FE1 to_encrypt rm to_encrypt - ''}"); - $machine->succeed(ssh "${cliTest '' + ''}")) + machine.succeed(ssh("${cliTest '' [ "$(gpg -d to_encrypt.asc)" = "encrypt me" ] - ''}"); - }; + ''}")) - subtest "test from X", sub { - $machine->execute(ssh "systemctl --user reload gpg-agent"); - my $pid = $machine->succeed(xsu + with subtest("test from X"): + machine.execute(ssh("systemctl --user reload gpg-agent")) + pid = machine.succeed(xsu( 'echo encrypt me | gpg -sea -r ECC15FE1 > encrypted_x.asc & echo $!' - ); - chomp $pid; - $machine->waitForText(qr/[Pp]assphrase/); - $machine->screenshot("passphrase_dialog"); - $machine->sendChars("supersecret\n"); - $machine->waitUntilFails("kill -0 $pid"); - $machine->succeed(xsu '[ "$(gpg -d encrypted_x.asc)" = "encrypt me" ]'); - }; + )).strip() + machine.wait_for_text('(?i)[Pp]assphrase') + machine.screenshot("passphrase_dialog") + machine.send_chars("supersecret\n") + machine.wait_until_fails(f"kill -0 {pid}") + machine.succeed(xsu('[ "$(gpg -d encrypted_x.asc)" = "encrypt me" ]')) ''; } diff --git a/tests/sandbox.nix b/tests/sandbox.nix index 66187b23..63d8af6e 100644 --- a/tests/sandbox.nix +++ b/tests/sandbox.nix @@ -120,21 +120,22 @@ }; testScript = '' - $machine->waitForUnit('multi-user.target'); - $machine->succeed('su - -c "xvfb-run gtk-launch test" foo >&2'); - $machine->waitForFile('/home/foo/.cache/xdg/done'); + # fmt: off + machine.wait_for_unit('multi-user.target') + machine.succeed('su - -c "xvfb-run gtk-launch test" foo >&2') + machine.wait_for_file('/home/foo/.cache/xdg/done') - $machine->succeed('test -d /home/foo/existing'); - $machine->succeed('grep -qF foo /home/foo/existing/bar'); - $machine->fail('test -d /home/foo/nonexisting'); + machine.succeed('test -d /home/foo/existing') + machine.succeed('grep -qF foo /home/foo/existing/bar') + machine.fail('test -d /home/foo/nonexisting') - $machine->succeed('grep -qF XDG1 /home/foo/.local/share/xdg/1'); - $machine->succeed('grep -qF XDG2 /home/foo/.config/xdg/2'); - $machine->succeed('grep -qF XDG3 /home/foo/.cache/xdg/3'); + machine.succeed('grep -qF XDG1 /home/foo/.local/share/xdg/1') + machine.succeed('grep -qF XDG2 /home/foo/.config/xdg/2') + machine.succeed('grep -qF XDG3 /home/foo/.cache/xdg/3') - $machine->succeed('test "$(< /home/foo/.cache/xdg/procpids)" = /proc/1'); - $machine->succeed('test "$(< /home/foo/.cache/xdg/ownpid)" = 1'); + machine.succeed('test "$(< /home/foo/.cache/xdg/procpids)" = /proc/1') + machine.succeed('test "$(< /home/foo/.cache/xdg/ownpid)" = 1') - $machine->succeed('test "$(su -c test-sandbox2 foo)" = "/bin/sh works"'); + machine.succeed('test "$(su -c test-sandbox2 foo)" = "/bin/sh works"') ''; } diff --git a/tests/system/kernel/bfq.nix b/tests/system/kernel/bfq.nix index 8a76d2a0..8aab8925 100644 --- a/tests/system/kernel/bfq.nix +++ b/tests/system/kernel/bfq.nix @@ -8,7 +8,8 @@ }; testScript = '' - $machine->execute('tail /sys/block/*/queue/scheduler >&2'); - $machine->succeed('grep -HF "[bfq]" /sys/block/sda/queue/scheduler'); + # fmt: off + machine.execute('tail /sys/block/*/queue/scheduler >&2') + machine.succeed('grep -HF "[bfq]" /sys/block/sda/queue/scheduler') ''; } -- cgit 1.4.1