1{ system ? builtins.currentSystem,
2 config ? {},
3 pkgs ? import ../.. { inherit system config; }
4}:
5
6with import ../lib/testing-python.nix { inherit system pkgs; };
7with pkgs.lib;
8
9with import common/ec2.nix { inherit makeTest pkgs; };
10
11let
12 imageCfg = (import ../lib/eval-config.nix {
13 inherit system;
14 modules = [
15 ../maintainers/scripts/ec2/amazon-image.nix
16 ../modules/testing/test-instrumentation.nix
17 ../modules/profiles/qemu-guest.nix
18 {
19 # Hack to make the partition resizing work in QEMU.
20 boot.initrd.postDeviceCommands = mkBefore ''
21 ln -s vda /dev/xvda
22 ln -s vda1 /dev/xvda1
23 '';
24
25 # In a NixOS test the serial console is occupied by the "backdoor"
26 # (see testing/test-instrumentation.nix) and is incompatible with
27 # the configuration in virtualisation/amazon-image.nix.
28 systemd.services."serial-getty@ttyS0".enable = mkForce false;
29
30 # Needed by nixos-rebuild due to the lack of network
31 # access. Determined by trial and error.
32 system.extraDependencies = with pkgs; ( [
33 # Needed for a nixos-rebuild.
34 busybox
35 cloud-utils
36 desktop-file-utils
37 libxslt.bin
38 mkinitcpio-nfs-utils
39 stdenv
40 stdenvNoCC
41 texinfo
42 unionfs-fuse
43 xorg.lndir
44
45 # These are used in the configure-from-userdata tests
46 # for EC2. Httpd and valgrind are requested by the
47 # configuration.
48 apacheHttpd
49 apacheHttpd.doc
50 apacheHttpd.man
51 valgrind.doc
52 ]);
53 }
54 ];
55 }).config;
56 image = "${imageCfg.system.build.amazonImage}/${imageCfg.amazonImage.name}.vhd";
57
58 sshKeys = import ./ssh-keys.nix pkgs;
59 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
60 snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
61 snakeOilPublicKey = sshKeys.snakeOilPublicKey;
62
63in {
64 boot-ec2-nixops = makeEc2Test {
65 name = "nixops-userdata";
66 inherit image;
67 sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key!
68
69 userData = ''
70 SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey}
71 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
72 '';
73 script = ''
74 machine.start()
75 machine.wait_for_file("/etc/ec2-metadata/user-data")
76 machine.wait_for_unit("sshd.service")
77
78 machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
79
80 # We have no keys configured on the client side yet, so this should fail
81 machine.fail("ssh -o BatchMode=yes localhost exit")
82
83 # Let's install our client private key
84 machine.succeed("mkdir -p ~/.ssh")
85
86 machine.copy_from_host_via_shell(
87 "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
88 )
89 machine.succeed("chmod 600 ~/.ssh/id_ed25519")
90
91 # We haven't configured the host key yet, so this should still fail
92 machine.fail("ssh -o BatchMode=yes localhost exit")
93
94 # Add the host key; ssh should finally succeed
95 machine.succeed(
96 "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"
97 )
98 machine.succeed("ssh -o BatchMode=yes localhost exit")
99
100 # Test whether the root disk was resized.
101 blocks, block_size = map(int, machine.succeed("stat -c %b:%S -f /").split(":"))
102 GB = 1024 ** 3
103 assert 9.7 * GB <= blocks * block_size <= 10 * GB
104
105 # Just to make sure resizing is idempotent.
106 machine.shutdown()
107 machine.start()
108 machine.wait_for_file("/etc/ec2-metadata/user-data")
109 '';
110 };
111
112 boot-ec2-config = makeEc2Test {
113 name = "config-userdata";
114 meta.broken = true; # amazon-init wants to download from the internet while building the system
115 inherit image;
116 sshPublicKey = snakeOilPublicKey;
117
118 # ### https://nixos.org/channels/nixos-unstable nixos
119 userData = ''
120 { pkgs, ... }:
121
122 {
123 imports = [
124 <nixpkgs/nixos/modules/virtualisation/amazon-image.nix>
125 <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
126 <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
127 ];
128 environment.etc.testFile = {
129 text = "whoa";
130 };
131
132 networking.hostName = "ec2-test-vm"; # required by services.httpd
133
134 services.httpd = {
135 enable = true;
136 adminAddr = "test@example.org";
137 virtualHosts.localhost.documentRoot = "''${pkgs.valgrind.doc}/share/doc/valgrind/html";
138 };
139 networking.firewall.allowedTCPPorts = [ 80 ];
140 }
141 '';
142 script = ''
143 machine.start()
144
145 # amazon-init must succeed. if it fails, make the test fail
146 # immediately instead of timing out in wait_for_file.
147 machine.wait_for_unit("amazon-init.service")
148
149 machine.wait_for_file("/etc/testFile")
150 assert "whoa" in machine.succeed("cat /etc/testFile")
151
152 machine.wait_for_unit("httpd.service")
153 assert "Valgrind" in machine.succeed("curl http://localhost")
154 '';
155 };
156}