summary refs log tree commit diff
path: root/nixos/tests/wireguard/namespaces.nix
blob: 94f993d9475dd7b62596f113a9337d72c918a1d8 (plain) (blame)
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
let
  listenPort = 12345;
  socketNamespace = "foo";
  interfaceNamespace = "bar";
  node = {
    networking.wireguard.interfaces.wg0 = {
      listenPort = listenPort;
      ips = [ "10.10.10.1/24" ];
      privateKeyFile = "/etc/wireguard/private";
      generatePrivateKeyFile = true;
    };
  };

in

import ../make-test.nix ({ pkgs, ...} : {
  name = "wireguard-with-namespaces";
  meta = with pkgs.stdenv.lib.maintainers; {
    maintainers = [ asymmetric ];
  };

  nodes = {
    # interface should be created in the socketNamespace
    # and not moved from there
    peer0 = pkgs.lib.attrsets.recursiveUpdate node {
      networking.wireguard.interfaces.wg0 = {
        preSetup = ''
          ip netns add ${socketNamespace}
        '';
        inherit socketNamespace;
      };
    };
    # interface should be created in the init namespace
    # and moved to the interfaceNamespace
    peer1 = pkgs.lib.attrsets.recursiveUpdate node {
      networking.wireguard.interfaces.wg0 = {
        preSetup = ''
          ip netns add ${interfaceNamespace}
        '';
        inherit interfaceNamespace;
      };
    };
    # interface should be created in the socketNamespace
    # and moved to the interfaceNamespace
    peer2 = pkgs.lib.attrsets.recursiveUpdate node {
      networking.wireguard.interfaces.wg0 = {
        preSetup = ''
          ip netns add ${socketNamespace}
          ip netns add ${interfaceNamespace}
        '';
        inherit socketNamespace interfaceNamespace;
      };
    };
    # interface should be created in the socketNamespace
    # and moved to the init namespace
    peer3 = pkgs.lib.attrsets.recursiveUpdate node {
      networking.wireguard.interfaces.wg0 = {
        preSetup = ''
          ip netns add ${socketNamespace}
        '';
        inherit socketNamespace;
        interfaceNamespace = "init";
      };
    };
  };

  testScript = ''
    startAll();

    $peer0->waitForUnit("wireguard-wg0.service");
    $peer1->waitForUnit("wireguard-wg0.service");
    $peer2->waitForUnit("wireguard-wg0.service");
    $peer3->waitForUnit("wireguard-wg0.service");

    $peer0->succeed("ip -n ${socketNamespace} link show wg0");
    $peer1->succeed("ip -n ${interfaceNamespace} link show wg0");
    $peer2->succeed("ip -n ${interfaceNamespace} link show wg0");
    $peer3->succeed("ip link show wg0");
  '';
})