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