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 image =
14 (import ../lib/eval-config.nix {
15 system = null;
16 modules = [
17 ../maintainers/scripts/openstack/openstack-image.nix
18 ../modules/testing/test-instrumentation.nix
19 ../modules/profiles/qemu-guest.nix
20 {
21 # Needed by nixos-rebuild due to lack of network access.
22 system.extraDependencies = with pkgs; [
23 stdenv
24 ];
25
26 nixpkgs.pkgs = pkgs;
27 }
28 ];
29 }).config.system.build.openstackImage
30 + "/nixos.qcow2";
31
32 sshKeys = import ./ssh-keys.nix pkgs;
33 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
34 snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
35 snakeOilPublicKey = sshKeys.snakeOilPublicKey;
36
37in
38{
39 metadata = makeEc2Test {
40 name = "openstack-ec2-metadata";
41 inherit image;
42 sshPublicKey = snakeOilPublicKey;
43 userData = ''
44 SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey}
45 SSH_HOST_ED25519_KEY:${replaceStrings [ "\n" ] [ "|" ] snakeOilPrivateKey}
46 '';
47 script = ''
48 machine.start()
49 machine.wait_for_file("/etc/ec2-metadata/user-data")
50 machine.wait_for_unit("sshd.service")
51
52 machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
53
54 # We have no keys configured on the client side yet, so this should fail
55 machine.fail("ssh -o BatchMode=yes localhost exit")
56
57 # Let's install our client private key
58 machine.succeed("mkdir -p ~/.ssh")
59
60 machine.copy_from_host_via_shell(
61 "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
62 )
63 machine.succeed("chmod 600 ~/.ssh/id_ed25519")
64
65 # We haven't configured the host key yet, so this should still fail
66 machine.fail("ssh -o BatchMode=yes localhost exit")
67
68 # Add the host key; ssh should finally succeed
69 machine.succeed(
70 "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"
71 )
72 machine.succeed("ssh -o BatchMode=yes localhost exit")
73
74 # Just to make sure resizing is idempotent.
75 machine.shutdown()
76 machine.start()
77 machine.wait_for_file("/etc/ec2-metadata/user-data")
78 '';
79 };
80
81 userdata = makeEc2Test {
82 name = "openstack-ec2-metadata";
83 inherit image;
84 sshPublicKey = snakeOilPublicKey;
85 userData = ''
86 { pkgs, ... }:
87 {
88 imports = [
89 <nixpkgs/nixos/modules/virtualisation/openstack-config.nix>
90 <nixpkgs/nixos/modules/testing/test-instrumentation.nix>
91 <nixpkgs/nixos/modules/profiles/qemu-guest.nix>
92 ];
93 environment.etc.testFile = {
94 text = "whoa";
95 };
96 }
97 '';
98 script = ''
99 machine.start()
100 machine.wait_for_file("/etc/testFile")
101 assert "whoa" in machine.succeed("cat /etc/testFile")
102 '';
103 };
104}