From 77831e8467ae24ca4ddf1a48d8dd8a17faae10d6 Mon Sep 17 00:00:00 2001 From: aszlig Date: Sat, 13 Dec 2014 12:13:30 +0100 Subject: [PATCH] nixos/tests/virtualbox: Generalize expression. We're going to create more than one VirtualBox VM, so let's dynamically generate subs specific to a particular VirtualBox VM, merging everything into the testScript and machine expressions. Signed-off-by: aszlig --- nixos/tests/virtualbox.nix | 225 ++++++++++++++++++++----------------- 1 file changed, 125 insertions(+), 100 deletions(-) diff --git a/nixos/tests/virtualbox.nix b/nixos/tests/virtualbox.nix index c8912760f3d6..170b0f553cd4 100644 --- a/nixos/tests/virtualbox.nix +++ b/nixos/tests/virtualbox.nix @@ -1,4 +1,4 @@ -import ./make-test.nix ({ pkgs, ... }: let +import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let testVMConfig = { config, pkgs, ... }: { boot.kernelParams = [ @@ -79,39 +79,17 @@ import ./make-test.nix ({ pkgs, ... }: let cat > /mnt/grub/grub.cfg <&2"); + } + + sub waitForVMBoot_${name} { + $machine->execute(ru( + 'set -e; i=0; '. + 'while ! test -e ${sharePath}/boot-done; do '. + 'sleep 10; i=$(($i + 10)); [ $i -le 3600 ]; '. + 'VBoxManage list runningvms | grep -q "^\"${name}\""; '. + 'done' + )); + } + + sub checkRunning_${name} { + my $cmd = 'VBoxManage list runningvms | grep -q "^\"${name}\""'; + my ($status, $out) = $machine->execute(ru $cmd); + return $status == 0; + } + + sub waitForStartup_${name} { + for (my $i = 0; $i <= 120; $i += 10) { + $machine->sleep(10); + return if checkRunning_${name}; + } + die "VirtualBox VM didn't start up within 2 minutes"; + } + + sub waitForShutdown_${name} { + for (my $i = 0; $i <= 120; $i += 10) { + $machine->sleep(10); + return unless checkRunning_${name}; + } + die "VirtualBox VM didn't shut down within 2 minutes"; + } + + sub shutdownVM_${name} { + $machine->succeed(ru "touch ${sharePath}/shutdown"); + $machine->waitUntilSucceeds( + "test ! -e ${sharePath}/shutdown ". + " -a ! -e ${sharePath}/boot-done" + ); + waitForShutdown_${name}; + } + + sub cleanup_${name} { + $machine->execute(ru "VBoxManage controlvm ${name} poweroff") + if checkRunning_${name}; + $machine->succeed("rm -rf ${sharePath}"); + $machine->succeed("mkdir -p ${sharePath}"); + $machine->succeed("chown alice.users ${sharePath}"); + } + ''; + }; + + vboxVMs.test1 = createVM "test1"; + +in { + name = "virtualbox"; + + machine = { pkgs, ... }: { + imports = let + mkVMConf = name: val: val.machine // { key = "${name}-config"; }; + vmConfigs = mapAttrsToList mkVMConf vboxVMs; + in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs; + services.virtualboxHost.enable = true; + }; + + testScript = '' sub ru { return "su - alice -c '$_[0]'"; } - sub waitForVMBoot { - $machine->execute(ru( - 'set -e; i=0; '. - 'while ! test -e /home/alice/vboxshare/boot-done; do '. - 'sleep 10; i=$(($i + 10)); [ $i -le 3600 ]; '. - 'VBoxManage list runningvms | grep -qF test; '. - 'done' - )); - } + sub vbm { + $machine->succeed(ru("VBoxManage ".$_[0])); + }; - sub checkRunning { - my $checkrunning = ru "VBoxManage list runningvms | grep -qF test"; - my ($status, $out) = $machine->execute($checkrunning); - return $status == 0; - } - - sub waitForStartup { - for (my $i = 0; $i <= 120; $i += 10) { - $machine->sleep(10); - return if checkRunning; - } - die "VirtualBox VM didn't start up within 2 minutes"; - } - - sub waitForShutdown { - for (my $i = 0; $i <= 120; $i += 10) { - $machine->sleep(10); - return unless checkRunning; - } - die "VirtualBox VM didn't shut down within 2 minutes"; - } - - sub shutdownVM { - $machine->succeed(ru "touch /home/alice/vboxshare/shutdown"); - $machine->waitUntilSucceeds( - "test ! -e /home/alice/vboxshare/shutdown ". - " -a ! -e /home/alice/vboxshare/boot-done" - ); - waitForShutdown; - } - - sub cleanup { - $machine->execute(ru "VBoxManage controlvm test poweroff") - if checkRunning; - $machine->succeed("rm -rf /home/alice/vboxshare"); - $machine->succeed("mkdir -p /home/alice/vboxshare"); - $machine->succeed("chown alice.users /home/alice/vboxshare"); - } + ${concatStrings (mapAttrsToList (_: getAttr "testSubs") vboxVMs)} $machine->waitForX; - $machine->succeed(ru "VBoxManage createvm --name test ${createFlags}"); - $machine->succeed(ru "VBoxManage modifyvm test ${vmFlags}"); + createVM_test1; - $machine->fail("test -e '/root/VirtualBox VMs'"); - $machine->succeed("test -e '/home/alice/VirtualBox VMs'"); + cleanup_test1; - $machine->succeed(ru "VBoxManage storagectl test ${controllerFlags}"); - $machine->succeed(ru "VBoxManage storageattach test ${diskFlags}"); - - $machine->succeed(ru "VBoxManage sharedfolder add test ${sharedFlags}"); - - $machine->succeed(ru "VBoxManage showvminfo test >&2"); - - cleanup; - - subtest "virtualbox-gui", sub { + subtest "simple-gui", sub { $machine->succeed(ru "VirtualBox &"); $machine->waitForWindow(qr/Oracle VM VirtualBox Manager/); $machine->sleep(5); $machine->screenshot("gui_manager_started"); $machine->sendKeys("ret"); $machine->screenshot("gui_manager_sent_startup"); - waitForStartup; + waitForStartup_test1; $machine->screenshot("gui_started"); - waitForVMBoot; + waitForVMBoot_test1; $machine->screenshot("gui_booted"); - shutdownVM; + shutdownVM_test1; $machine->sleep(5); $machine->screenshot("gui_stopped"); $machine->sendKeys("ctrl-q"); @@ -233,17 +256,19 @@ in { $machine->screenshot("gui_manager_stopped"); }; - cleanup; + cleanup_test1; - subtest "virtualbox-cli", sub { - $machine->succeed(ru "VBoxManage startvm test"); - waitForStartup; + subtest "simple-cli", sub { + vbm("startvm test1"); + waitForStartup_test1; $machine->screenshot("cli_started"); - waitForVMBoot; + waitForVMBoot_test1; $machine->screenshot("cli_booted"); - shutdownVM; + shutdownVM_test1; }; + cleanup_test1; + $machine->fail("test -e '/root/VirtualBox VMs'"); $machine->succeed("test -e '/home/alice/VirtualBox VMs'"); '';