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 -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}'";
72 $startCommand .= " -drive file=$diskImage,if=virtio,werror=report";
73 $startCommand .= " \$QEMU_OPTS";
74
75 my $machine = createMachine({ startCommand => $startCommand });
76
77 ${script}
78 '';
79 };
80
81 snakeOilPrivateKey = ''
82 -----BEGIN OPENSSH PRIVATE KEY-----
83 b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
84 QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye
85 EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q
86 AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4
87 FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE=
88 -----END OPENSSH PRIVATE KEY-----
89 '';
90
91 snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key";
92
93in {
94 boot-ec2-nixops = makeEc2Test {
95 name = "nixops-userdata";
96 sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key!
97
98 userData = ''
99 SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey}
100 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
101 '';
102 script = ''
103 $machine->start;
104 $machine->waitForFile("/etc/ec2-metadata/user-data");
105 $machine->waitForUnit("sshd.service");
106
107 $machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path");
108
109 # We have no keys configured on the client side yet, so this should fail
110 $machine->fail("ssh -o BatchMode=yes localhost exit");
111
112 # Let's install our client private key
113 $machine->succeed("mkdir -p ~/.ssh");
114
115 $machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519");
116 $machine->succeed("chmod 600 ~/.ssh/id_ed25519");
117
118 # We haven't configured the host key yet, so this should still fail
119 $machine->fail("ssh -o BatchMode=yes localhost exit");
120
121 # Add the host key; ssh should finally succeed
122 $machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts");
123 $machine->succeed("ssh -o BatchMode=yes localhost exit");
124
125 # Test whether the root disk was resized.
126 my $blocks = $machine->succeed("stat -c %b -f /");
127 my $bsize = $machine->succeed("stat -c %S -f /");
128 my $size = $blocks * $bsize;
129 die "wrong free space $size" if $size < 9.7 * 1024 * 1024 * 1024 || $size > 10 * 1024 * 1024 * 1024;
130
131 # Just to make sure resizing is idempotent.
132 $machine->shutdown;
133 $machine->start;
134 $machine->waitForFile("/etc/ec2-metadata/user-data");
135 '';
136 };
137
138 boot-ec2-config = makeEc2Test {
139 name = "config-userdata";
140 sshPublicKey = snakeOilPublicKey;
141
142 # ### http://nixos.org/channels/nixos-unstable nixos
143 userData = ''
144 { pkgs, ... }:
145
146 {
147 imports = [
148 <nixpkgs/nixos/modules/virtualisation/amazon-image.nix>
149 <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
150 <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
151 ];
152 environment.etc.testFile = {
153 text = "whoa";
154 };
155
156 services.httpd = {
157 enable = true;
158 adminAddr = "test@example.org";
159 documentRoot = "${pkgs.valgrind.doc}/share/doc/valgrind/html";
160 };
161 networking.firewall.allowedTCPPorts = [ 80 ];
162 }
163 '';
164 script = ''
165 $machine->start;
166 $machine->waitForFile("/etc/testFile");
167 $machine->succeed("cat /etc/testFile | grep -q 'whoa'");
168
169 $machine->waitForUnit("httpd.service");
170 $machine->succeed("curl http://localhost | grep Valgrind");
171 '';
172 };
173}