git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/tests/virtualbox: Add test for hostonlyif.

Essentially adds two more VirtualBox VMs to the test and also increases the memory size of the qemu VM to 768 MB to make sure we don't run out of memory too soon. We're testing whether those two VMs can talk to either each other (currently via ICMP only) or to/from the host via TCP/IP. Also, this restructures the VM test a bit, so that we now pass in a custom stage2Init script that has access to the store via a private mount over the /nix/store that's already in the initrd. The reason why this is a private mount is that we don't want to shadow the Nix store of the initrd, essentially breaking cleanup functionality after the custom stage 2 script (currently this is only "poweroff -f"). Note that setting the hostname inside the VirtualBox VM is *not* for additional fanciness but to produce a different store path for the VM image, so that VirtualBox doesn't bail out when trying to use an image which is already attached to another VM. Signed-off-by: aszlig <aszlig@redmoonstudios.org>

commit9bf16a9c3336cd932f294929167ddf267044b1f6 authoraszlig <aszlig@redmoonstudios.org> dateMon 15 Dec 2014, 08:58 (12 years ago) parent245baeb
1 file changed+160−48
nixos/tests/virtualbox.nix+160−48
nixos/tests/virtualbox.nix+160−48
@@ -1,9 +1,27 @@
11import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
22
3− testVMConfig = { config, pkgs, ... }: {
4− boot.kernelParams = [
3+ testVMConfig = vmName: attrs: { config, pkgs, ... }: {
4+ boot.kernelParams = let
5+ miniInit = ''
6+ #!${pkgs.stdenv.shell} -xe
7+ export PATH="${pkgs.coreutils}/bin:${pkgs.utillinux}/bin"
8+
9+ ${pkgs.linuxPackages.virtualboxGuestAdditions}/sbin/VBoxService
10+ ${(attrs.vmScript or (const "")) pkgs}
11+
12+ i=0
13+ while [ ! -e /mnt-root/shutdown ]; do
14+ sleep 10
15+ i=$(($i + 10))
16+ [ $i -le 120 ] || fail
17+ done
18+
19+ rm -f /mnt-root/boot-done /mnt-root/shutdown
20+ '';
21+ in [
522 "console=tty0" "console=ttyS0" "ignore_loglevel"
623 "boot.trace" "panic=1" "boot.panic_on_fail"
24+ "init=${pkgs.writeScript "mini-init.sh" miniInit}"
725 ];
826
927 fileSystems."/" = {
@@ -13,24 +31,26 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
1331
1432 services.virtualboxGuest.enable = true;
1533
16− boot.initrd.kernelModules = [ "vboxsf" ];
34+ boot.initrd.kernelModules = [
35+ "af_packet" "vboxsf"
36+ "virtio" "virtio_pci" "virtio_ring" "virtio_net" "vboxguest"
37+ ];
1738
1839 boot.initrd.extraUtilsCommands = ''
1940 cp -av -t "$out/bin/" \
20− "${pkgs.linuxPackages.virtualboxGuestAdditions}/sbin/mount.vboxsf"
41+ "${pkgs.linuxPackages.virtualboxGuestAdditions}/sbin/mount.vboxsf" \
42+ "${pkgs.utillinux}/bin/unshare"
43+ ${(attrs.extraUtilsCommands or (const "")) pkgs}
2144 '';
2245
2346 boot.initrd.postMountCommands = ''
2447 touch /mnt-root/boot-done
25−
26− i=0
27− while [ ! -e /mnt-root/shutdown ]; do
28− sleep 10
29− i=$(($i + 10))
30− [ $i -le 120 ] || fail
31− done
32−
33− rm -f /mnt-root/boot-done /mnt-root/shutdown
48+ hostname "${vmName}"
49+ mkdir -p /nix/store
50+ unshare -m "@shell@" -c '
51+ mount -t vboxsf nixstore /nix/store
52+ exec "$stage2Init"
53+ '
3454 poweroff -f
3555 '';
3656
@@ -40,12 +60,12 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
4060 ];
4161 };
4262
43− testVM = let
63+ testVM = vmName: vmScript: let
4464 cfg = (import ../lib/eval-config.nix {
4565 system = "i686-linux";
4666 modules = [
4767 ../modules/profiles/minimal.nix
48− testVMConfig
68+ (testVMConfig vmName vmScript)
4969 ];
5070 }).config;
5171 in pkgs.vmTools.runInLinuxVM (pkgs.runCommand "virtualbox-image" {
@@ -86,7 +106,7 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
86106 umount /mnt
87107 '');
88108
89− createVM = name: let
109+ createVM = name: attrs: let
90110 mkFlags = concatStringsSep " ";
91111
92112 sharePath = "/home/alice/vboxshare-${name}";
@@ -96,10 +116,10 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
96116 "--register"
97117 ];
98118
99− vmFlags = mkFlags [
119+ vmFlags = mkFlags ([
100120 "--uart1 0x3F8 4"
101121 "--uartmode1 client /run/virtualbox-log-${name}.sock"
102− ];
122+ ] ++ (attrs.vmFlags or []));
103123
104124 controllerFlags = mkFlags [
105125 "--name SATA"
@@ -114,13 +134,19 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
114134 "--device 0"
115135 "--type hdd"
116136 "--mtype immutable"
117− "--medium ${testVM}/disk.vdi"
137+ "--medium ${testVM name attrs}/disk.vdi"
118138 ];
119139
120140 sharedFlags = mkFlags [
121141 "--name vboxshare"
122142 "--hostpath ${sharePath}"
123143 ];
144+
145+ nixstoreFlags = mkFlags [
146+ "--name nixstore"
147+ "--hostpath /nix/store"
148+ "--readonly"
149+ ];
124150 in {
125151 machine = {
126152 systemd.sockets = listToAttrs (singleton {
@@ -147,13 +173,35 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
147173 };
148174
149175 testSubs = ''
176+ sub checkRunning_${name} {
177+ my $cmd = 'VBoxManage list runningvms | grep -q "^\"${name}\""';
178+ my ($status, $out) = $machine->execute(ru $cmd);
179+ return $status == 0;
180+ }
181+
182+ sub cleanup_${name} {
183+ $machine->execute(ru "VBoxManage controlvm ${name} poweroff")
184+ if checkRunning_${name};
185+ $machine->succeed("rm -rf ${sharePath}");
186+ $machine->succeed("mkdir -p ${sharePath}");
187+ $machine->succeed("chown alice.users ${sharePath}");
188+ }
189+
150190 sub createVM_${name} {
151191 vbm("createvm --name ${name} ${createFlags}");
152192 vbm("modifyvm ${name} ${vmFlags}");
193+ vbm("setextradata ${name} VBoxInternal/PDM/HaltOnReset 1");
153194 vbm("storagectl ${name} ${controllerFlags}");
154195 vbm("storageattach ${name} ${diskFlags}");
155196 vbm("sharedfolder add ${name} ${sharedFlags}");
197+ vbm("sharedfolder add ${name} ${nixstoreFlags}");
156198 vbm("showvminfo ${name} >&2");
199+ cleanup_${name};
200+ }
201+
202+ sub destroyVM_${name} {
203+ cleanup_${name};
204+ vbm("unregistervm ${name} --delete");
157205 }
158206
159207 sub waitForVMBoot_${name} {
@@ -166,10 +214,22 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
166214 ));
167215 }
168216
169− sub checkRunning_${name} {
170− my $cmd = 'VBoxManage list runningvms | grep -q "^\"${name}\""';
171− my ($status, $out) = $machine->execute(ru $cmd);
172− return $status == 0;
217+ sub waitForIP_${name} ($) {
218+ my $property = "/VirtualBox/GuestInfo/Net/$_[0]/V4/IP";
219+ my $getip = "VBoxManage guestproperty get ${name} $property | ".
220+ "sed -n -e 's/^Value: //p'";
221+ my $ip = $machine->succeed(ru(
222+ 'for i in $(seq 1000); do '.
223+ 'if ipaddr="$('.$getip.')" && [ -n "$ipaddr" ]; then '.
224+ 'echo "$ipaddr"; exit 0; '.
225+ 'fi; '.
226+ 'sleep 1; '.
227+ 'done; '.
228+ 'echo "Could not get IPv4 address for ${name}!" >&2; '.
229+ 'exit 1'
230+ ));
231+ chomp $ip;
232+ return $ip;
173233 }
174234
175235 sub waitForStartup_${name} {
@@ -196,18 +256,36 @@ import ./make-test.nix ({ pkgs, ... }: with pkgs.lib; let
196256 );
197257 waitForShutdown_${name};
198258 }
199−
200− sub cleanup_${name} {
201− $machine->execute(ru "VBoxManage controlvm ${name} poweroff")
202− if checkRunning_${name};
203− $machine->succeed("rm -rf ${sharePath}");
204− $machine->succeed("mkdir -p ${sharePath}");
205− $machine->succeed("chown alice.users ${sharePath}");
206− }
207259 '';
208260 };
209261
210− vboxVMs.test1 = createVM "test1";
262+ hostonlyVMFlags = [
263+ "--nictype1 virtio"
264+ "--nictype2 virtio"
265+ "--nic2 hostonly"
266+ "--hostonlyadapter2 vboxnet0"
267+ ];
268+
269+ dhcpScript = pkgs: ''
270+ ${pkgs.dhcp}/bin/dhclient \
271+ -lf /run/dhcp.leases \
272+ -pf /run/dhclient.pid \
273+ -v eth0 eth1
274+
275+ otherIP="$(${pkgs.netcat}/bin/netcat -clp 1234 || :)"
276+ ${pkgs.iputils}/bin/ping -I eth1 -c1 "$otherIP"
277+ echo "$otherIP reachable" | ${pkgs.netcat}/bin/netcat -clp 5678 || :
278+ '';
279+
280+ vboxVMs = mapAttrs createVM {
281+ simple = {};
282+
283+ test1.vmFlags = hostonlyVMFlags;
284+ test1.vmScript = dhcpScript;
285+
286+ test2.vmFlags = hostonlyVMFlags;
287+ test2.vmScript = dhcpScript;
288+ };
211289
212290in {
213291 name = "virtualbox";
@@ -217,12 +295,15 @@ in {
217295 mkVMConf = name: val: val.machine // { key = "${name}-config"; };
218296 vmConfigs = mapAttrsToList mkVMConf vboxVMs;
219297 in [ ./common/user-account.nix ./common/x11.nix ] ++ vmConfigs;
298+ virtualisation.memorySize = 768;
220299 services.virtualboxHost.enable = true;
300+ users.extraUsers.alice.extraGroups = [ "vboxusers" ];
221301 };
222302
223303 testScript = ''
224− sub ru {
225− return "su - alice -c '$_[0]'";
304+ sub ru ($) {
305+ my $esc = $_[0] =~ s/'/'\\${"'"}'/gr;
306+ return "su - alice -c '$esc'";
226307 }
227308
228309 sub vbm {
@@ -233,9 +314,7 @@ in {
233314
234315 $machine->waitForX;
235316
236− createVM_test1;
237−
238− cleanup_test1;
317+ createVM_simple;
239318
240319 subtest "simple-gui", sub {
241320 $machine->succeed(ru "VirtualBox &");
@@ -244,11 +323,11 @@ in {
244323 $machine->screenshot("gui_manager_started");
245324 $machine->sendKeys("ret");
246325 $machine->screenshot("gui_manager_sent_startup");
247− waitForStartup_test1;
326+ waitForStartup_simple;
248327 $machine->screenshot("gui_started");
249− waitForVMBoot_test1;
328+ waitForVMBoot_simple;
250329 $machine->screenshot("gui_booted");
251− shutdownVM_test1;
330+ shutdownVM_simple;
252331 $machine->sleep(5);
253332 $machine->screenshot("gui_stopped");
254333 $machine->sendKeys("ctrl-q");
@@ -256,20 +335,53 @@ in {
256335 $machine->screenshot("gui_manager_stopped");
257336 };
258337
259− cleanup_test1;
338+ cleanup_simple;
260339
261340 subtest "simple-cli", sub {
262− vbm("startvm test1");
263− waitForStartup_test1;
341+ vbm("startvm simple");
342+ waitForStartup_simple;
264343 $machine->screenshot("cli_started");
265− waitForVMBoot_test1;
344+ waitForVMBoot_simple;
266345 $machine->screenshot("cli_booted");
267− shutdownVM_test1;
346+ shutdownVM_simple;
268347 };
269348
270− cleanup_test1;
349+ subtest "privilege-escalation", sub {
350+ $machine->fail("test -e '/root/VirtualBox VMs'");
351+ $machine->succeed("test -e '/home/alice/VirtualBox VMs'");
352+ };
353+
354+ destroyVM_simple;
355+
356+ subtest "net-hostonlyif", sub {
357+ createVM_test1;
358+ createVM_test2;
359+
360+ vbm("startvm test1");
361+ waitForStartup_test1;
362+
363+ vbm("startvm test2");
364+ waitForStartup_test2;
365+
366+ waitForVMBoot_test1;
367+ waitForVMBoot_test2;
368+
369+ $machine->screenshot("net_booted");
370+
371+ my $test1IP = waitForIP_test1 1;
372+ my $test2IP = waitForIP_test2 1;
373+
374+ $machine->succeed("echo '$test2IP' | netcat -c '$test1IP' 1234");
375+ $machine->succeed("echo '$test1IP' | netcat -c '$test2IP' 1234");
271376
272− $machine->fail("test -e '/root/VirtualBox VMs'");
273− $machine->succeed("test -e '/home/alice/VirtualBox VMs'");
377+ $machine->waitUntilSucceeds("netcat -c '$test1IP' 5678 >&2");
378+ $machine->waitUntilSucceeds("netcat -c '$test2IP' 5678 >&2");
379+
380+ shutdownVM_test1;
381+ shutdownVM_test2;
382+
383+ destroyVM_test1;
384+ destroyVM_test2;
385+ };
274386 '';
275387})