at 17.09-beta 6.6 kB view raw
1{ system ? builtins.currentSystem }: 2 3with import ../lib/testing.nix { inherit system; }; 4with import ../lib/qemu-flags.nix; 5with pkgs.lib; 6 7let 8 image = 9 (import ../lib/eval-config.nix { 10 inherit system; 11 modules = [ 12 ../maintainers/scripts/ec2/amazon-image.nix 13 ../modules/testing/test-instrumentation.nix 14 ../modules/profiles/qemu-guest.nix 15 { ec2.hvm = true; 16 17 # Hack to make the partition resizing work in QEMU. 18 boot.initrd.postDeviceCommands = mkBefore 19 '' 20 ln -s vda /dev/xvda 21 ln -s vda1 /dev/xvda1 22 ''; 23 24 # Needed by nixos-rebuild due to the lack of network 25 # access. Mostly copied from 26 # modules/profiles/installation-device.nix. 27 system.extraDependencies = 28 with pkgs; [ 29 stdenv busybox perlPackages.ArchiveCpio unionfs-fuse mkinitcpio-nfs-utils 30 31 # These are used in the configure-from-userdata tests for EC2. Httpd and valgrind are requested 32 # directly by the configuration we set, and libxslt.bin is used indirectly as a build dependency 33 # of the derivation for dbus configuration files. 34 apacheHttpd valgrind.doc libxslt.bin 35 ]; 36 } 37 ]; 38 }).config.system.build.amazonImage; 39 40 makeEc2Test = { name, userData, script, hostname ? "ec2-instance", sshPublicKey ? null }: 41 let 42 metaData = pkgs.stdenv.mkDerivation { 43 name = "metadata"; 44 buildCommand = '' 45 mkdir -p $out/1.0/meta-data 46 ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data 47 echo "${hostname}" > $out/1.0/meta-data/hostname 48 echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path 49 '' + optionalString (sshPublicKey != null) '' 50 mkdir -p $out/1.0/meta-data/public-keys/0 51 ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key 52 ''; 53 }; 54 in makeTest { 55 name = "ec2-" + name; 56 nodes = {}; 57 testScript = 58 '' 59 my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine"; 60 mkdir $imageDir, 0700; 61 my $diskImage = "$imageDir/machine.qcow2"; 62 system("qemu-img create -f qcow2 -o backing_file=${image}/nixos.qcow2 $diskImage") == 0 or die; 63 system("qemu-img resize $diskImage 10G") == 0 or die; 64 65 # Note: we use net=169.0.0.0/8 rather than 66 # net=169.254.0.0/16 to prevent dhcpcd from getting horribly 67 # confused. (It would get a DHCP lease in the 169.254.* 68 # range, which it would then configure and prompty delete 69 # again when it deletes link-local addresses.) Ideally we'd 70 # turn off the DHCP server, but qemu does not have an option 71 # to do that. 72 my $startCommand = "qemu-kvm -m 768 -net nic,vlan=0,model=virtio -net 'user,vlan=0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"; 73 $startCommand .= " -drive file=$diskImage,if=virtio,werror=report"; 74 $startCommand .= " \$QEMU_OPTS"; 75 76 my $machine = createMachine({ startCommand => $startCommand }); 77 78 ${script} 79 ''; 80 }; 81 82 snakeOilPrivateKey = '' 83 -----BEGIN OPENSSH PRIVATE KEY----- 84 b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 85 QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye 86 EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q 87 AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4 88 FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE= 89 -----END OPENSSH PRIVATE KEY----- 90 ''; 91 92 snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key"; 93 94in { 95 boot-ec2-nixops = makeEc2Test { 96 name = "nixops-userdata"; 97 sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key! 98 99 userData = '' 100 SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey} 101 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} 102 ''; 103 script = '' 104 $machine->start; 105 $machine->waitForFile("/etc/ec2-metadata/user-data"); 106 $machine->waitForUnit("sshd.service"); 107 108 $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); 109 110 # We have no keys configured on the client side yet, so this should fail 111 $machine->fail("ssh -o BatchMode=yes localhost exit"); 112 113 # Let's install our client private key 114 $machine->succeed("mkdir -p ~/.ssh"); 115 116 $machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519"); 117 $machine->succeed("chmod 600 ~/.ssh/id_ed25519"); 118 119 # We haven't configured the host key yet, so this should still fail 120 $machine->fail("ssh -o BatchMode=yes localhost exit"); 121 122 # Add the host key; ssh should finally succeed 123 $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"); 124 $machine->succeed("ssh -o BatchMode=yes localhost exit"); 125 126 # Test whether the root disk was resized. 127 my $blocks = $machine->succeed("stat -c %b -f /"); 128 my $bsize = $machine->succeed("stat -c %S -f /"); 129 my $size = $blocks * $bsize; 130 die "wrong free space $size" if $size < 9.7 * 1024 * 1024 * 1024 || $size > 10 * 1024 * 1024 * 1024; 131 132 # Just to make sure resizing is idempotent. 133 $machine->shutdown; 134 $machine->start; 135 $machine->waitForFile("/etc/ec2-metadata/user-data"); 136 ''; 137 }; 138 139 boot-ec2-config = makeEc2Test { 140 name = "config-userdata"; 141 sshPublicKey = snakeOilPublicKey; 142 143 # ### http://nixos.org/channels/nixos-unstable nixos 144 userData = '' 145 { pkgs, ... }: 146 147 { 148 imports = [ 149 <nixpkgs/nixos/modules/virtualisation/amazon-image.nix> 150 <nixpkgs/nixos/modules/testing/test-instrumentation.nix> 151 <nixpkgs/nixos/modules/profiles/qemu-guest.nix> 152 ]; 153 environment.etc.testFile = { 154 text = "whoa"; 155 }; 156 157 services.httpd = { 158 enable = true; 159 adminAddr = "test@example.org"; 160 documentRoot = "${pkgs.valgrind.doc}/share/doc/valgrind/html"; 161 }; 162 networking.firewall.allowedTCPPorts = [ 80 ]; 163 } 164 ''; 165 script = '' 166 $machine->start; 167 $machine->waitForFile("/etc/testFile"); 168 $machine->succeed("cat /etc/testFile | grep -q 'whoa'"); 169 170 $machine->waitForUnit("httpd.service"); 171 $machine->succeed("curl http://localhost | grep Valgrind"); 172 ''; 173 }; 174}