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 ${pkgs.cdrkit}/bin/genisoimage -volid cidata -joliet -rock -o $out/metadata.iso $out/iso
39 '';
40 };
41in makeTest {
42 name = "cloud-init";
43 meta = with pkgs.lib.maintainers; {
44 maintainers = [ lewo ];
45 };
46 machine = { ... }:
47 {
48 virtualisation.qemu.options = [ "-cdrom" "${metadataDrive}/metadata.iso" ];
49 services.cloud-init.enable = true;
50 services.openssh.enable = true;
51 networking.hostName = "";
52 };
53 testScript = ''
54 # To wait until cloud-init terminates its run
55 unnamed.wait_for_unit("cloud-final.service")
56
57 unnamed.succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'")
58
59 # install snakeoil ssh key and provision .ssh/config file
60 unnamed.succeed("mkdir -p ~/.ssh")
61 unnamed.succeed(
62 "cat ${snakeOilPrivateKey} > ~/.ssh/id_snakeoil"
63 )
64 unnamed.succeed("chmod 600 ~/.ssh/id_snakeoil")
65
66 unnamed.wait_for_unit("sshd.service")
67
68 # we should be able to log in as the root user, as well as the created nixos user
69 unnamed.succeed(
70 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil root@localhost 'true'"
71 )
72 unnamed.succeed(
73 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'true'"
74 )
75
76 # test changing hostname via cloud-init worked
77 assert (
78 unnamed.succeed(
79 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'hostname'"
80 ).strip()
81 == "test"
82 )
83 '';
84}