git.iko.soy/typeable/nixpkgs
on master help sign in
nixosTests.ec2: Port tests that depend on common/ec2.nix
commitc32369676ba85d572758abaaa438513bbed92175 authorJacek Galowicz <jacek.galowicz@cyberus-technology.de> dateSun 2 Feb 2020, 09:48 (7 years ago) parent4555623
3 files changed+144−123
nixos/tests/common/ec2.nix+36−22 nixos/tests/ec2.nix+71−68 nixos/tests/openstack-image.nix+37−33
nixos/tests/common/ec2.nix+36−22
@@ -20,30 +20,44 @@ with pkgs.lib;
2020 in makeTest {
2121 name = "ec2-" + name;
2222 nodes = {};
23− testScript =
24− ''
25− my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine";
26− mkdir $imageDir, 0700;
27− my $diskImage = "$imageDir/machine.qcow2";
28− system("qemu-img create -f qcow2 -o backing_file=${image} $diskImage") == 0 or die;
29− system("qemu-img resize $diskImage 10G") == 0 or die;
23+ testScript = ''
24+ import os
25+ import subprocess
3026
31− # Note: we use net=169.0.0.0/8 rather than
32− # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
33− # confused. (It would get a DHCP lease in the 169.254.*
34− # range, which it would then configure and prompty delete
35− # again when it deletes link-local addresses.) Ideally we'd
36− # turn off the DHCP server, but qemu does not have an option
37− # to do that.
38− my $startCommand = "qemu-kvm -m 1024";
39− $startCommand .= " -device virtio-net-pci,netdev=vlan0";
40− $startCommand .= " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'";
41− $startCommand .= " -drive file=$diskImage,if=virtio,werror=report";
42− $startCommand .= " \$QEMU_OPTS";
27+ image_dir = os.path.join(
28+ os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine"
29+ )
30+ os.makedirs(image_dir, mode=0o700, exist_ok=True)
31+ disk_image = os.path.join(image_dir, "machine.qcow2")
32+ subprocess.check_call(
33+ [
34+ "qemu-img",
35+ "create",
36+ "-f",
37+ "qcow2",
38+ "-o",
39+ "backing_file=${image}",
40+ disk_image,
41+ ]
42+ )
43+ subprocess.check_call(["qemu-img", "resize", disk_image, "10G"])
4344
44− my $machine = createMachine({ startCommand => $startCommand });
45+ # Note: we use net=169.0.0.0/8 rather than
46+ # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
47+ # confused. (It would get a DHCP lease in the 169.254.*
48+ # range, which it would then configure and prompty delete
49+ # again when it deletes link-local addresses.) Ideally we'd
50+ # turn off the DHCP server, but qemu does not have an option
51+ # to do that.
52+ start_command = (
53+ "qemu-kvm -m 1024"
54+ + " -device virtio-net-pci,netdev=vlan0"
55+ + " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"
56+ + f" -drive file={disk_image},if=virtio,werror=report"
57+ + " $QEMU_OPTS"
58+ )
4559
46− ${script}
47− '';
60+ machine = create_machine({"startCommand": start_command})
61+ '' + script;
4862 };
4963}
nixos/tests/ec2.nix+71−68
@@ -3,58 +3,58 @@
33 pkgs ? import ../.. { inherit system config; }
44}:
55
6−with import ../lib/testing.nix { inherit system pkgs; };
6+with import ../lib/testing-python.nix { inherit system pkgs; };
77with pkgs.lib;
88
99with import common/ec2.nix { inherit makeTest pkgs; };
1010
1111let
12− imageCfg =
13− (import ../lib/eval-config.nix {
14− inherit system;
15− modules = [
16− ../maintainers/scripts/ec2/amazon-image.nix
17− ../modules/testing/test-instrumentation.nix
18− ../modules/profiles/qemu-guest.nix
19− { ec2.hvm = true;
20−
21− # Hack to make the partition resizing work in QEMU.
22− boot.initrd.postDeviceCommands = mkBefore
23− ''
24− ln -s vda /dev/xvda
25− ln -s vda1 /dev/xvda1
26− '';
27−
28− # Needed by nixos-rebuild due to the lack of network
29− # access. Determined by trial and error.
30− system.extraDependencies =
31− with pkgs; (
32− [
33− # Needed for a nixos-rebuild.
34− busybox
35− stdenv
36− stdenvNoCC
37− mkinitcpio-nfs-utils
38− unionfs-fuse
39− cloud-utils
40− desktop-file-utils
41− texinfo
42− libxslt.bin
43− xorg.lndir
44−
45− # These are used in the configure-from-userdata tests
46− # for EC2. Httpd and valgrind are requested by the
47− # configuration.
48− apacheHttpd apacheHttpd.doc apacheHttpd.man valgrind.doc
49− ]
50− );
51− }
52− ];
53− }).config;
12+ imageCfg = (import ../lib/eval-config.nix {
13+ inherit system;
14+ modules = [
15+ ../maintainers/scripts/ec2/amazon-image.nix
16+ ../modules/testing/test-instrumentation.nix
17+ ../modules/profiles/qemu-guest.nix
18+ {
19+ ec2.hvm = true;
20+
21+ # Hack to make the partition resizing work in QEMU.
22+ boot.initrd.postDeviceCommands = mkBefore ''
23+ ln -s vda /dev/xvda
24+ ln -s vda1 /dev/xvda1
25+ '';
26+
27+ # Needed by nixos-rebuild due to the lack of network
28+ # access. Determined by trial and error.
29+ system.extraDependencies = with pkgs; ( [
30+ # Needed for a nixos-rebuild.
31+ busybox
32+ cloud-utils
33+ desktop-file-utils
34+ libxslt.bin
35+ mkinitcpio-nfs-utils
36+ stdenv
37+ stdenvNoCC
38+ texinfo
39+ unionfs-fuse
40+ xorg.lndir
41+
42+ # These are used in the configure-from-userdata tests
43+ # for EC2. Httpd and valgrind are requested by the
44+ # configuration.
45+ apacheHttpd
46+ apacheHttpd.doc
47+ apacheHttpd.man
48+ valgrind.doc
49+ ]);
50+ }
51+ ];
52+ }).config;
5453 image = "${imageCfg.system.build.amazonImage}/${imageCfg.amazonImage.name}.vhd";
5554
5655 sshKeys = import ./ssh-keys.nix pkgs;
5756 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
57+ snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
5858 snakeOilPublicKey = sshKeys.snakeOilPublicKey;
5959
6060in {
@@ -68,38 +68,41 @@ in {
6868 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
6969 '';
7070 script = ''
71− $machine->start;
72− $machine->waitForFile("/etc/ec2-metadata/user-data");
73− $machine->waitForUnit("sshd.service");
71+ machine.start()
72+ machine.wait_for_file("/etc/ec2-metadata/user-data")
73+ machine.wait_for_unit("sshd.service")
7474
75− $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path");
75+ machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
7676
7777 # We have no keys configured on the client side yet, so this should fail
78− $machine->fail("ssh -o BatchMode=yes localhost exit");
78+ machine.fail("ssh -o BatchMode=yes localhost exit")
7979
8080 # Let's install our client private key
81− $machine->succeed("mkdir -p ~/.ssh");
81+ machine.succeed("mkdir -p ~/.ssh")
8282
83− $machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519");
84− $machine->succeed("chmod 600 ~/.ssh/id_ed25519");
83+ machine.copy_from_host_via_shell(
84+ "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
85+ )
86+ machine.succeed("chmod 600 ~/.ssh/id_ed25519")
8587
8688 # We haven't configured the host key yet, so this should still fail
87− $machine->fail("ssh -o BatchMode=yes localhost exit");
89+ machine.fail("ssh -o BatchMode=yes localhost exit")
8890
8991 # Add the host key; ssh should finally succeed
90− $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts");
91− $machine->succeed("ssh -o BatchMode=yes localhost exit");
92+ machine.succeed(
93+ "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"
94+ )
95+ machine.succeed("ssh -o BatchMode=yes localhost exit")
9296
9397 # Test whether the root disk was resized.
94− my $blocks = $machine->succeed("stat -c %b -f /");
95− my $bsize = $machine->succeed("stat -c %S -f /");
96− my $size = $blocks * $bsize;
97− die "wrong free space $size" if $size < 9.7 * 1024 * 1024 * 1024 || $size > 10 * 1024 * 1024 * 1024;
98+ blocks, block_size = map(int, machine.succeed("stat -c %b:%S -f /").split(":"))
99+ GB = 1024 ** 3
100+ assert 9.7 * GB <= blocks * block_size <= 10 * GB
98101
99102 # Just to make sure resizing is idempotent.
100− $machine->shutdown;
101− $machine->start;
102− $machine->waitForFile("/etc/ec2-metadata/user-data");
103+ machine.shutdown()
104+ machine.start()
105+ machine.wait_for_file("/etc/ec2-metadata/user-data")
103106 '';
104107 };
105108
@@ -133,17 +136,17 @@ in {
133136 }
134137 '';
135138 script = ''
136− $machine->start;
139+ machine.start()
137140
138141 # amazon-init must succeed. if it fails, make the test fail
139− # immediately instead of timing out in waitForFile.
140− $machine->waitForUnit('amazon-init.service');
142+ # immediately instead of timing out in wait_for_file.
143+ machine.wait_for_unit("amazon-init.service")
141144
142− $machine->waitForFile("/etc/testFile");
143− $machine->succeed("cat /etc/testFile | grep -q 'whoa'");
145+ machine.wait_for_file("/etc/testFile")
146+ assert "whoa" in machine.succeed("cat /etc/testFile")
144147
145− $machine->waitForUnit("httpd.service");
146− $machine->succeed("curl http://localhost | grep Valgrind");
148+ machine.wait_for_unit("httpd.service")
149+ assert "Valgrind" in machine.succeed("curl http://localhost")
147150 '';
148151 };
149152}
nixos/tests/openstack-image.nix+37−33
@@ -3,30 +3,30 @@
33 pkgs ? import ../.. { inherit system config; }
44}:
55
6−with import ../lib/testing.nix { inherit system pkgs; };
6+with import ../lib/testing-python.nix { inherit system pkgs; };
77with pkgs.lib;
88
99with import common/ec2.nix { inherit makeTest pkgs; };
1010
1111let
12− image =
13− (import ../lib/eval-config.nix {
14− inherit system;
15− modules = [
16− ../maintainers/scripts/openstack/openstack-image.nix
17− ../modules/testing/test-instrumentation.nix
18− ../modules/profiles/qemu-guest.nix
19− {
20− # Needed by nixos-rebuild due to lack of network access.
21− system.extraDependencies = with pkgs; [
22− stdenv
23− ];
24− }
25− ];
26− }).config.system.build.openstackImage + "/nixos.qcow2";
12+ image = (import ../lib/eval-config.nix {
13+ inherit system;
14+ modules = [
15+ ../maintainers/scripts/openstack/openstack-image.nix
16+ ../modules/testing/test-instrumentation.nix
17+ ../modules/profiles/qemu-guest.nix
18+ {
19+ # Needed by nixos-rebuild due to lack of network access.
20+ system.extraDependencies = with pkgs; [
21+ stdenv
22+ ];
23+ }
24+ ];
25+ }).config.system.build.openstackImage + "/nixos.qcow2";
2726
2827 sshKeys = import ./ssh-keys.nix pkgs;
2928 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
29+ snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
3030 snakeOilPublicKey = sshKeys.snakeOilPublicKey;
3131
3232in {
@@ -39,32 +39,36 @@ in {
3939 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
4040 '';
4141 script = ''
42− $machine->start;
43− $machine->waitForFile("/etc/ec2-metadata/user-data");
44− $machine->waitForUnit("sshd.service");
42+ machine.start()
43+ machine.wait_for_file("/etc/ec2-metadata/user-data")
44+ machine.wait_for_unit("sshd.service")
4545
46− $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path");
46+ machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
4747
4848 # We have no keys configured on the client side yet, so this should fail
49− $machine->fail("ssh -o BatchMode=yes localhost exit");
49+ machine.fail("ssh -o BatchMode=yes localhost exit")
5050
5151 # Let's install our client private key
52− $machine->succeed("mkdir -p ~/.ssh");
52+ machine.succeed("mkdir -p ~/.ssh")
5353
54− $machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519");
55− $machine->succeed("chmod 600 ~/.ssh/id_ed25519");
54+ machine.copy_from_host_via_shell(
55+ "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
56+ )
57+ machine.succeed("chmod 600 ~/.ssh/id_ed25519")
5658
5759 # We haven't configured the host key yet, so this should still fail
58− $machine->fail("ssh -o BatchMode=yes localhost exit");
60+ machine.fail("ssh -o BatchMode=yes localhost exit")
5961
6062 # Add the host key; ssh should finally succeed
61− $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts");
62− $machine->succeed("ssh -o BatchMode=yes localhost exit");
63+ machine.succeed(
64+ "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"
65+ )
66+ machine.succeed("ssh -o BatchMode=yes localhost exit")
6367
6468 # Just to make sure resizing is idempotent.
65− $machine->shutdown;
66− $machine->start;
67− $machine->waitForFile("/etc/ec2-metadata/user-data");
69+ machine.shutdown()
70+ machine.start()
71+ machine.wait_for_file("/etc/ec2-metadata/user-data")
6872 '';
6973 };
7074
@@ -86,9 +90,9 @@ in {
8690 }
8791 '';
8892 script = ''
89− $machine->start;
90− $machine->waitForFile("/etc/testFile");
91− $machine->succeed("cat /etc/testFile | grep -q 'whoa'");
93+ machine.start()
94+ machine.wait_for_file("/etc/testFile")
95+ assert "whoa" in machine.succeed("cat /etc/testFile")
9296 '';
9397 };
9498}