lol

nixos/cloud-init: fix hostname and resolvconf configuration

- Fix hostname configuration on proxmox, which uses "hostname" in user-data
instead of "local-hostname" in meta-data.
- Allow setting resolv.conf through cloud-init
- Add tests for new changes
- Add timeouts to make tests fail faster

authored by

illustris and committed by
Rick van Schijndel
79cf2115 5561dcbe

+61 -9
+2 -1
nixos/modules/services/system/cloud-init.nix
··· 81 81 - write-files 82 82 - growpart 83 83 - resizefs 84 - - update_etc_hosts 84 + - update_hostname 85 + - resolv_conf 85 86 - ca-certs 86 87 - rsyslog 87 88 - users-groups
+1
nixos/tests/all-tests.nix
··· 125 125 cjdns = handleTest ./cjdns.nix {}; 126 126 clickhouse = handleTest ./clickhouse.nix {}; 127 127 cloud-init = handleTest ./cloud-init.nix {}; 128 + cloud-init-hostname = handleTest ./cloud-init-hostname.nix {}; 128 129 cntr = handleTestOn ["aarch64-linux" "x86_64-linux"] ./cntr.nix {}; 129 130 cockroachdb = handleTestOn ["x86_64-linux"] ./cockroachdb.nix {}; 130 131 collectd = handleTest ./collectd.nix {};
+46
nixos/tests/cloud-init-hostname.nix
··· 1 + { system ? builtins.currentSystem, 2 + config ? {}, 3 + pkgs ? import ../.. { inherit system config; } 4 + }: 5 + 6 + with import ../lib/testing-python.nix { inherit system pkgs; }; 7 + with pkgs.lib; 8 + 9 + let 10 + # Hostname can also be set through "hostname" in user-data. 11 + # This is how proxmox configures hostname through cloud-init. 12 + metadataDrive = pkgs.stdenv.mkDerivation { 13 + name = "metadata"; 14 + buildCommand = '' 15 + mkdir -p $out/iso 16 + 17 + cat << EOF > $out/iso/user-data 18 + #cloud-config 19 + hostname: testhostname 20 + EOF 21 + 22 + cat << EOF > $out/iso/meta-data 23 + instance-id: iid-local02 24 + EOF 25 + 26 + ${pkgs.cdrkit}/bin/genisoimage -volid cidata -joliet -rock -o $out/metadata.iso $out/iso 27 + ''; 28 + }; 29 + 30 + in makeTest { 31 + name = "cloud-init-hostname"; 32 + meta = with pkgs.lib.maintainers; { 33 + maintainers = [ lewo illustris ]; 34 + }; 35 + 36 + nodes.machine2 = { ... }: { 37 + virtualisation.qemu.options = [ "-cdrom" "${metadataDrive}/metadata.iso" ]; 38 + services.cloud-init.enable = true; 39 + networking.hostName = ""; 40 + }; 41 + 42 + testScript = '' 43 + unnamed.wait_for_unit("cloud-final.service") 44 + assert "testhostname" in unnamed.succeed("hostname") 45 + ''; 46 + }
+12 -8
nixos/tests/cloud-init.nix
··· 49 49 gateway: '12.34.56.9' 50 50 - type: nameserver 51 51 address: 52 - - '8.8.8.8' 52 + - '6.7.8.9' 53 53 search: 54 54 - 'example.com' 55 55 EOF 56 56 ${pkgs.cdrkit}/bin/genisoimage -volid cidata -joliet -rock -o $out/metadata.iso $out/iso 57 57 ''; 58 58 }; 59 + 59 60 in makeTest { 60 61 name = "cloud-init"; 61 - meta = with pkgs.lib.maintainers; { 62 - maintainers = [ lewo ]; 63 - broken = true; # almost always times out after spending many hours 64 - }; 62 + meta.maintainers = with pkgs.lib.maintainers; [ lewo illustris ]; 65 63 nodes.machine = { ... }: 66 64 { 67 65 virtualisation.qemu.options = [ "-cdrom" "${metadataDrive}/metadata.iso" ]; ··· 90 88 91 89 # we should be able to log in as the root user, as well as the created nixos user 92 90 unnamed.succeed( 93 - "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil root@localhost 'true'" 91 + "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil root@localhost 'true'" 94 92 ) 95 93 unnamed.succeed( 96 - "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'true'" 94 + "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'true'" 97 95 ) 98 96 99 97 # test changing hostname via cloud-init worked 100 98 assert ( 101 99 unnamed.succeed( 102 - "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'hostname'" 100 + "timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentityFile=~/.ssh/id_snakeoil nixos@localhost 'hostname'" 103 101 ).strip() 104 102 == "test" 105 103 ) 106 104 105 + # check IP and route configs 107 106 assert "default via 12.34.56.9 dev eth0 proto static" in unnamed.succeed("ip route") 108 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 + 109 113 ''; 110 114 }