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