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
9let
10 inherit (import ./ssh-keys.nix pkgs)
11 snakeOilPrivateKey snakeOilPublicKey;
12
13 metadataDrive = pkgs.stdenv.mkDerivation {
14 name = "metadata";
15 buildCommand = ''
16 mkdir -p $out/iso
17
18 cat << EOF > $out/iso/user-data
19 #cloud-config
20 write_files:
21 - content: |
22 cloudinit
23 path: /tmp/cloudinit-write-file
24
25 users:
26 - default
27 - name: nixos
28 ssh_authorized_keys:
29 - "${snakeOilPublicKey}"
30 EOF
31
32 cat << EOF > $out/iso/meta-data
33 instance-id: iid-local01
34 local-hostname: "test"
35 public-keys:
36 - "${snakeOilPublicKey}"
37 EOF
38
39 cat << EOF > $out/iso/network-config
40 version: 1
41 config:
42 - type: physical
43 name: eth0
44 mac_address: '52:54:00:12:34:56'
45 subnets:
46 - type: static
47 address: '12.34.56.78'
48 netmask: '255.255.255.0'
49 gateway: '12.34.56.9'
50 - type: nameserver
51 address:
52 - '6.7.8.9'
53 search:
54 - 'example.com'
55 EOF
56 ${pkgs.cdrkit}/bin/genisoimage -volid cidata -joliet -rock -o $out/metadata.iso $out/iso
57 '';
58 };
59
60in makeTest {
61 name = "cloud-init";
62 meta.maintainers = with pkgs.lib.maintainers; [ lewo illustris ];
63 nodes.machine = { ... }:
64 {
65 virtualisation.qemu.options = [ "-cdrom" "${metadataDrive}/metadata.iso" ];
66 services.cloud-init = {
67 enable = true;
68 network.enable = true;
69 };
70 services.openssh.enable = true;
71 networking.hostName = "";
72 networking.useDHCP = false;
73 };
74 testScript = ''
75 # To wait until cloud-init terminates its run
76 unnamed.wait_for_unit("cloud-final.service")
77
78 unnamed.succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'")
79
80 # install snakeoil ssh key and provision .ssh/config file
81 unnamed.succeed("mkdir -p ~/.ssh")
82 unnamed.succeed(
83 "cat ${snakeOilPrivateKey} > ~/.ssh/id_snakeoil"
84 )
85 unnamed.succeed("chmod 600 ~/.ssh/id_snakeoil")
86
87 unnamed.wait_for_unit("sshd.service")
88
89 # we should be able to log in as the root user, as well as the created nixos user
90 unnamed.succeed(
91 "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil root@localhost 'true'"
92 )
93 unnamed.succeed(
94 "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'true'"
95 )
96
97 # test changing hostname via cloud-init worked
98 assert (
99 unnamed.succeed(
100 "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'hostname'"
101 ).strip()
102 == "test"
103 )
104
105 # check IP and route configs
106 assert "default via 12.34.56.9 dev eth0 proto static" in unnamed.succeed("ip route")
107 assert "12.34.56.0/24 dev eth0 proto kernel scope link src 12.34.56.78" in unnamed.succeed("ip route")
108
109 # check nameserver and search configs
110 assert "6.7.8.9" in unnamed.succeed("resolvectl status")
111 assert "example.com" in unnamed.succeed("resolvectl status")
112
113 '';
114}