lol

nixosTests.ec2: Port tests that depend on common/ec2.nix

authored by

Jacek Galowicz and committed by
Florian Klink
c3236967 4555623f

+141 -120
+36 -22
nixos/tests/common/ec2.nix
··· 20 20 in makeTest { 21 21 name = "ec2-" + name; 22 22 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 30 26 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"]) 43 44 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 + ) 45 59 46 - ${script} 47 - ''; 60 + machine = create_machine({"startCommand": start_command}) 61 + '' + script; 48 62 }; 49 63 }
+68 -65
nixos/tests/ec2.nix
··· 3 3 pkgs ? import ../.. { inherit system config; } 4 4 }: 5 5 6 - with import ../lib/testing.nix { inherit system pkgs; }; 6 + with import ../lib/testing-python.nix { inherit system pkgs; }; 7 7 with pkgs.lib; 8 8 9 9 with import common/ec2.nix { inherit makeTest pkgs; }; 10 10 11 11 let 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; 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 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 - ''; 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 + ''; 27 26 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 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 44 41 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; 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; 54 53 image = "${imageCfg.system.build.amazonImage}/${imageCfg.amazonImage.name}.vhd"; 55 54 56 55 sshKeys = import ./ssh-keys.nix pkgs; 57 56 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; 57 + snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey; 58 58 snakeOilPublicKey = sshKeys.snakeOilPublicKey; 59 59 60 60 in { ··· 68 68 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} 69 69 ''; 70 70 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") 74 74 75 - $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); 75 + machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path") 76 76 77 77 # 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") 79 79 80 80 # Let's install our client private key 81 - $machine->succeed("mkdir -p ~/.ssh"); 81 + machine.succeed("mkdir -p ~/.ssh") 82 82 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") 85 87 86 88 # 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") 88 90 89 91 # 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") 92 96 93 97 # 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 98 101 99 102 # 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") 103 106 ''; 104 107 }; 105 108 ··· 133 136 } 134 137 ''; 135 138 script = '' 136 - $machine->start; 139 + machine.start() 137 140 138 141 # 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") 141 144 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") 144 147 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") 147 150 ''; 148 151 }; 149 152 }
+37 -33
nixos/tests/openstack-image.nix
··· 3 3 pkgs ? import ../.. { inherit system config; } 4 4 }: 5 5 6 - with import ../lib/testing.nix { inherit system pkgs; }; 6 + with import ../lib/testing-python.nix { inherit system pkgs; }; 7 7 with pkgs.lib; 8 8 9 9 with import common/ec2.nix { inherit makeTest pkgs; }; 10 10 11 11 let 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"; 27 26 28 27 sshKeys = import ./ssh-keys.nix pkgs; 29 28 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; 29 + snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey; 30 30 snakeOilPublicKey = sshKeys.snakeOilPublicKey; 31 31 32 32 in { ··· 39 39 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} 40 40 ''; 41 41 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") 45 45 46 - $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); 46 + machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path") 47 47 48 48 # 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") 50 50 51 51 # Let's install our client private key 52 - $machine->succeed("mkdir -p ~/.ssh"); 52 + machine.succeed("mkdir -p ~/.ssh") 53 53 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") 56 58 57 59 # 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") 59 61 60 62 # 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") 63 67 64 68 # 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") 68 72 ''; 69 73 }; 70 74 ··· 86 90 } 87 91 ''; 88 92 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") 92 96 ''; 93 97 }; 94 98 }