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-init-local.service")
77 unnamed.wait_for_unit("cloud-final.service")
78
79 unnamed.succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'")
80
81 # install snakeoil ssh key and provision .ssh/config file
82 unnamed.succeed("mkdir -p ~/.ssh")
83 unnamed.succeed(
84 "cat ${snakeOilPrivateKey} > ~/.ssh/id_snakeoil"
85 )
86 unnamed.succeed("chmod 600 ~/.ssh/id_snakeoil")
87
88 unnamed.wait_for_unit("sshd.service")
89
90 # we should be able to log in as the root user, as well as the created nixos user
91 unnamed.succeed(
92 "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil root@localhost 'true'"
93 )
94 unnamed.succeed(
95 "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'true'"
96 )
97
98 # test changing hostname via cloud-init worked
99 assert (
100 unnamed.succeed(
101 "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'hostname'"
102 ).strip()
103 == "test"
104 )
105
106 # check IP and route configs
107 assert "default via 12.34.56.9 dev eth0 proto static" in unnamed.succeed("ip route")
108 assert "12.34.56.0/24 dev eth0 proto kernel scope link src 12.34.56.78" in unnamed.succeed("ip route")
109
110 # check nameserver and search configs
111 assert "6.7.8.9" in unnamed.succeed("resolvectl status")
112 assert "example.com" in unnamed.succeed("resolvectl status")
113
114 '';
115}