summaryrefslogtreecommitdiffstats
path: root/nixos/tests/predictable-interface-names.nix
blob: f1f0f80fea0eeacfb209371ea6e0722b62490081 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
  system ? builtins.currentSystem,
  config ? { },
  pkgs ? import ../.. { inherit system config; },
}:

let
  inherit (import ../lib/testing-python.nix { inherit system pkgs; }) makeTest;
  testCombinations = pkgs.lib.cartesianProduct {
    predictable = [
      true
      false
    ];
    withNetworkd = [
      true
      false
    ];
    systemdStage1 = [
      true
      false
    ];
  };
in
pkgs.lib.listToAttrs (
  map (
    {
      predictable,
      withNetworkd,
      systemdStage1,
    }:
    {
      name =
        pkgs.lib.optionalString (!predictable) "un"
        + "predictable"
        + pkgs.lib.optionalString withNetworkd "Networkd"
        + pkgs.lib.optionalString systemdStage1 "SystemdStage1";
      value = makeTest {
        name =
          pkgs.lib.optionalString (!predictable) "un"
          + "predictableInterfaceNames"
          + pkgs.lib.optionalString withNetworkd "-with-networkd"
          + pkgs.lib.optionalString systemdStage1 "-systemd-stage-1";
        meta = { };

        nodes.machine =
          {
            config,
            pkgs,
            lib,
            ...
          }:
          let
            script = ''
              ${lib.getExe' config.boot.initrd.systemd.package "udevadm"} settle --timeout=180
              ip link
              if ${lib.optionalString predictable "!"} ip link show eth0; then
                echo Success
              else
                exit 1
              fi
            '';
          in
          {
            networking.usePredictableInterfaceNames = lib.mkForce predictable;
            networking.useNetworkd = withNetworkd;
            networking.dhcpcd.enable = !withNetworkd;
            networking.useDHCP = !withNetworkd;

            # Check if predictable interface names are working in stage-1
            boot.initrd.postDeviceCommands = lib.mkIf (!systemdStage1) script;

            boot.initrd.systemd = lib.mkMerge [
              { enable = systemdStage1; }
              (lib.mkIf systemdStage1 {
                initrdBin = [ pkgs.iproute2 ];
                services.systemd-udev-settle.wantedBy = [ "initrd.target" ];
                services.check-interfaces = {
                  requiredBy = [ "initrd.target" ];
                  after = [ "systemd-udev-settle.service" ];
                  serviceConfig.Type = "oneshot";
                  path = [ pkgs.iproute2 ];
                  inherit script;
                };
              })
            ];
          };

        testScript = ''
          print(machine.succeed("ip link"))
          machine.${if predictable then "fail" else "succeed"}("ip link show eth0")
        '';
      };
    }
  ) testCombinations
)