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