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/minimal.nix 15 ../modules/profiles/qemu-guest.nix 16 { ec2.hvm = true; 17 18 # Hack to make the partition resizing work in QEMU. 19 boot.initrd.postDeviceCommands = mkBefore 20 '' 21 ln -s vda /dev/xvda 22 ln -s vda1 /dev/xvda1 23 ''; 24 } 25 ]; 26 }).config.system.build.amazonImage; 27 28 makeEc2Test = { name, userData, script, hostname ? "ec2-instance", sshPublicKey ? null }: 29 let 30 metaData = pkgs.stdenv.mkDerivation { 31 name = "metadata"; 32 buildCommand = '' 33 mkdir -p $out/2011-01-01 34 ln -s ${pkgs.writeText "userData" userData} $out/2011-01-01/user-data 35 mkdir -p $out/1.0/meta-data 36 echo "${hostname}" > $out/1.0/meta-data/hostname 37 echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path 38 '' + optionalString (sshPublicKey != null) '' 39 mkdir -p $out/1.0/meta-data/public-keys/0 40 ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key 41 ''; 42 }; 43 in makeTest { 44 name = "ec2-" + name; 45 nodes = {}; 46 testScript = 47 '' 48 my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine"; 49 mkdir $imageDir, 0700; 50 my $diskImage = "$imageDir/machine.qcow2"; 51 system("qemu-img create -f qcow2 -o backing_file=${image}/nixos.img $diskImage") == 0 or die; 52 system("qemu-img resize $diskImage 10G") == 0 or die; 53 54 # Note: we use net=169.0.0.0/8 rather than 55 # net=169.254.0.0/16 to prevent dhcpcd from getting horribly 56 # confused. (It would get a DHCP lease in the 169.254.* 57 # range, which it would then configure and prompty delete 58 # again when it deletes link-local addresses.) Ideally we'd 59 # turn off the DHCP server, but qemu does not have an option 60 # to do that. 61 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}'"; 62 $startCommand .= " -drive file=$diskImage,if=virtio,werror=report"; 63 $startCommand .= " \$QEMU_OPTS"; 64 65 my $machine = createMachine({ startCommand => $startCommand }); 66 67 ${script} 68 ''; 69 }; 70 71 snakeOilPrivateKey = '' 72 -----BEGIN OPENSSH PRIVATE KEY----- 73 b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 74 QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye 75 EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q 76 AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4 77 FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE= 78 -----END OPENSSH PRIVATE KEY----- 79 ''; 80 81 snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key"; 82 83in { 84 boot-ec2-nixops = makeEc2Test { 85 name = "nixops-userdata"; 86 sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key! 87 88 userData = '' 89 SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey} 90 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} 91 ''; 92 script = '' 93 $machine->start; 94 $machine->waitForFile("/root/user-data"); 95 $machine->waitForUnit("sshd.service"); 96 97 $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); 98 99 # We have no keys configured on the client side yet, so this should fail 100 $machine->fail("ssh -o BatchMode=yes localhost exit"); 101 102 # Let's install our client private key 103 $machine->succeed("mkdir -p ~/.ssh"); 104 105 $machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519"); 106 $machine->succeed("chmod 600 ~/.ssh/id_ed25519"); 107 108 # We haven't configured the host key yet, so this should still fail 109 $machine->fail("ssh -o BatchMode=yes localhost exit"); 110 111 # Add the host key; ssh should finally succeed 112 $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"); 113 $machine->succeed("ssh -o BatchMode=yes localhost exit"); 114 115 # Test whether the root disk was resized. 116 my $blocks = $machine->succeed("stat -c %b -f /"); 117 my $bsize = $machine->succeed("stat -c %S -f /"); 118 my $size = $blocks * $bsize; 119 die "wrong free space $size" if $size < 9.7 * 1024 * 1024 * 1024 || $size > 10 * 1024 * 1024 * 1024; 120 121 # Just to make sure resizing is idempotent. 122 $machine->shutdown; 123 $machine->start; 124 $machine->waitForFile("/root/user-data"); 125 ''; 126 }; 127 128 boot-ec2-config = makeEc2Test { 129 name = "config-userdata"; 130 sshPublicKey = snakeOilPublicKey; 131 132 # ### http://nixos.org/channels/nixos-unstable nixos 133 userData = '' 134 { 135 imports = [ 136 <nixpkgs/nixos/modules/virtualisation/amazon-image.nix> 137 <nixpkgs/nixos/modules/testing/test-instrumentation.nix> 138 ]; 139 environment.etc.testFile = { 140 text = "whoa"; 141 }; 142 } 143 ''; 144 script = '' 145 $machine->start; 146 $machine->waitForFile("/etc/testFile"); 147 $machine->succeed("cat /etc/testFile | grep -q 'whoa'"); 148 ''; 149 }; 150}