summaryrefslogtreecommitdiffstats
path: root/nixos/tests/initrd-network-ssh/default.nix
blob: 60b888b69a861583e393a2b3e88f9660092172df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# This tests SSH in scripted stage 1. Remove in 26.11.

import ../make-test-python.nix (
  { lib, pkgs, ... }:

  {
    name = "initrd-network-ssh";
    meta.maintainers = with lib.maintainers; [
      emily
    ];

    nodes = {
      server =
        { config, ... }:
        {
          boot.kernelParams = [
            "ip=${config.networking.primaryIPAddress}:::255.255.255.0::eth1:none"
          ];
          boot.initrd.systemd.enable = false;
          boot.initrd.network = {
            enable = true;
            ssh = {
              enable = true;
              authorizedKeys = [ (lib.readFile ./id_ed25519.pub) ];
              port = 22;
              hostKeys = [ ./ssh_host_ed25519_key ];
            };
          };
          boot.initrd.preLVMCommands = ''
            while true; do
              if [ -f fnord ]; then
                poweroff
              fi
              sleep 1
            done
          '';
        };

      client =
        { config, ... }:
        {
          environment.etc = {
            knownHosts = {
              text = lib.concatStrings [
                "server,"
                "${toString (lib.head (lib.splitString " " (toString (lib.elemAt (lib.splitString "\n" config.networking.extraHosts) 2))))} "
                "${lib.readFile ./ssh_host_ed25519_key.pub}"
              ];
            };
            sshKey = {
              source = ./id_ed25519;
              mode = "0600";
            };
          };
        };
    };

    testScript = ''
      start_all()
      client.wait_for_unit("network.target")


      def ssh_is_up(_) -> bool:
          status, _ = client.execute("nc -z server 22")
          return status == 0


      with client.nested("waiting for SSH server to come up"):
          retry(ssh_is_up)


      client.succeed(
          "ssh -i /etc/sshKey -o UserKnownHostsFile=/etc/knownHosts server 'touch /fnord'"
      )
      client.shutdown()
    '';
  }
)