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