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