Release NixOS 23.11
[NixPkgs.git] / nixos / tests / ec2.nix
blobe649761d029df2fc2b970e112f4154b2c3c36687
1 { system ? builtins.currentSystem,
2   config ? {},
3   pkgs ? import ../.. { inherit system config; }
4 }:
6 with import ../lib/testing-python.nix { inherit system pkgs; };
7 with pkgs.lib;
9 with import common/ec2.nix { inherit makeTest pkgs; };
11 let
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         '';
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;
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
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";
58   sshKeys = import ./ssh-keys.nix pkgs;
59   snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
60   snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
61   snakeOilPublicKey = sshKeys.snakeOilPublicKey;
63 in {
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!
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")
78       machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
80       # We have no keys configured on the client side yet, so this should fail
81       machine.fail("ssh -o BatchMode=yes localhost exit")
83       # Let's install our client private key
84       machine.succeed("mkdir -p ~/.ssh")
86       machine.copy_from_host_via_shell(
87           "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
88       )
89       machine.succeed("chmod 600 ~/.ssh/id_ed25519")
91       # We haven't configured the host key yet, so this should still fail
92       machine.fail("ssh -o BatchMode=yes localhost exit")
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")
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
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   };
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;
118     # ### https://nixos.org/channels/nixos-unstable nixos
119     userData = ''
120       { pkgs, ... }:
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         };
132         networking.hostName = "ec2-test-vm"; # required by services.httpd
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()
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")
149       machine.wait_for_file("/etc/testFile")
150       assert "whoa" in machine.succeed("cat /etc/testFile")
152       machine.wait_for_unit("httpd.service")
153       assert "Valgrind" in machine.succeed("curl http://localhost")
154     '';
155   };