2020-04-24 23:36:52 +00:00
|
|
|
{ system ? builtins.currentSystem,
|
|
|
|
config ? {},
|
|
|
|
pkgs ? import ../.. { inherit system config; }
|
|
|
|
}:
|
|
|
|
|
|
|
|
with import ../lib/testing-python.nix { inherit system pkgs; };
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
2020-10-16 20:44:37 +00:00
|
|
|
inherit (import ./ssh-keys.nix pkgs)
|
|
|
|
snakeOilPrivateKey snakeOilPublicKey;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
metadataDrive = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "metadata";
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/iso
|
|
|
|
|
|
|
|
cat << EOF > $out/iso/user-data
|
|
|
|
#cloud-config
|
|
|
|
write_files:
|
|
|
|
- content: |
|
|
|
|
cloudinit
|
|
|
|
path: /tmp/cloudinit-write-file
|
2020-10-16 20:44:37 +00:00
|
|
|
|
|
|
|
users:
|
|
|
|
- default
|
|
|
|
- name: nixos
|
|
|
|
ssh_authorized_keys:
|
|
|
|
- "${snakeOilPublicKey}"
|
2020-04-24 23:36:52 +00:00
|
|
|
EOF
|
|
|
|
|
|
|
|
cat << EOF > $out/iso/meta-data
|
|
|
|
instance-id: iid-local01
|
|
|
|
local-hostname: "test"
|
|
|
|
public-keys:
|
2020-10-16 20:44:37 +00:00
|
|
|
- "${snakeOilPublicKey}"
|
2020-04-24 23:36:52 +00:00
|
|
|
EOF
|
2022-02-10 20:34:41 +00:00
|
|
|
|
|
|
|
cat << EOF > $out/iso/network-config
|
|
|
|
version: 1
|
|
|
|
config:
|
|
|
|
- type: physical
|
|
|
|
name: eth0
|
|
|
|
mac_address: '52:54:00:12:34:56'
|
|
|
|
subnets:
|
|
|
|
- type: static
|
|
|
|
address: '12.34.56.78'
|
|
|
|
netmask: '255.255.255.0'
|
|
|
|
gateway: '12.34.56.9'
|
|
|
|
- type: nameserver
|
|
|
|
address:
|
2022-10-30 15:09:59 +00:00
|
|
|
- '6.7.8.9'
|
2022-02-10 20:34:41 +00:00
|
|
|
search:
|
|
|
|
- 'example.com'
|
|
|
|
EOF
|
2020-04-24 23:36:52 +00:00
|
|
|
${pkgs.cdrkit}/bin/genisoimage -volid cidata -joliet -rock -o $out/metadata.iso $out/iso
|
|
|
|
'';
|
|
|
|
};
|
2022-10-30 15:09:59 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
in makeTest {
|
|
|
|
name = "cloud-init";
|
2022-10-30 15:09:59 +00:00
|
|
|
meta.maintainers = with pkgs.lib.maintainers; [ lewo illustris ];
|
2022-04-03 18:54:34 +00:00
|
|
|
nodes.machine = { ... }:
|
2020-10-16 20:44:37 +00:00
|
|
|
{
|
|
|
|
virtualisation.qemu.options = [ "-cdrom" "${metadataDrive}/metadata.iso" ];
|
2022-02-10 20:34:41 +00:00
|
|
|
services.cloud-init = {
|
|
|
|
enable = true;
|
|
|
|
network.enable = true;
|
|
|
|
};
|
2020-10-16 20:44:37 +00:00
|
|
|
services.openssh.enable = true;
|
|
|
|
networking.hostName = "";
|
2022-02-10 20:34:41 +00:00
|
|
|
networking.useDHCP = false;
|
2020-10-16 20:44:37 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
testScript = ''
|
2020-12-07 07:45:13 +00:00
|
|
|
# To wait until cloud-init terminates its run
|
2024-01-25 14:12:00 +00:00
|
|
|
unnamed.wait_for_unit("cloud-init-local.service")
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.wait_for_unit("cloud-final.service")
|
|
|
|
|
|
|
|
unnamed.succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'")
|
2020-10-16 20:44:37 +00:00
|
|
|
|
|
|
|
# install snakeoil ssh key and provision .ssh/config file
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.succeed("mkdir -p ~/.ssh")
|
|
|
|
unnamed.succeed(
|
2020-10-16 20:44:37 +00:00
|
|
|
"cat ${snakeOilPrivateKey} > ~/.ssh/id_snakeoil"
|
|
|
|
)
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.succeed("chmod 600 ~/.ssh/id_snakeoil")
|
2020-10-16 20:44:37 +00:00
|
|
|
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.wait_for_unit("sshd.service")
|
2020-10-16 20:44:37 +00:00
|
|
|
|
|
|
|
# we should be able to log in as the root user, as well as the created nixos user
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.succeed(
|
2022-10-30 15:09:59 +00:00
|
|
|
"timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil root@localhost 'true'"
|
2020-10-16 20:44:37 +00:00
|
|
|
)
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.succeed(
|
2022-10-30 15:09:59 +00:00
|
|
|
"timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'true'"
|
2020-10-16 20:44:37 +00:00
|
|
|
)
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
# test changing hostname via cloud-init worked
|
|
|
|
assert (
|
2020-12-07 07:45:13 +00:00
|
|
|
unnamed.succeed(
|
2022-10-30 15:09:59 +00:00
|
|
|
"timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'hostname'"
|
2020-10-16 20:44:37 +00:00
|
|
|
).strip()
|
|
|
|
== "test"
|
|
|
|
)
|
2022-02-10 20:34:41 +00:00
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
# check IP and route configs
|
2022-02-10 20:34:41 +00:00
|
|
|
assert "default via 12.34.56.9 dev eth0 proto static" in unnamed.succeed("ip route")
|
|
|
|
assert "12.34.56.0/24 dev eth0 proto kernel scope link src 12.34.56.78" in unnamed.succeed("ip route")
|
2022-10-30 15:09:59 +00:00
|
|
|
|
|
|
|
# check nameserver and search configs
|
|
|
|
assert "6.7.8.9" in unnamed.succeed("resolvectl status")
|
|
|
|
assert "example.com" in unnamed.succeed("resolvectl status")
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
}
|