summaryrefslogtreecommitdiffstats
path: root/nixos/tests/containers-gateway.nix
blob: afef1e1a0a313a66c2bdbab28a78147fbc533bd9 (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
let
  hostIp4 = "192.168.0.1";
  containerIp4 = "192.168.0.100/24";
  hostIp6 = "fc00::1";
  containerIp6 = "fc00::2/7";
in

{ lib, ... }:
{
  name = "containers-gateway";
  meta = {
    maintainers = with lib.maintainers; [
      rnhmjoj
    ];
  };

  nodes.machine = {
    networking.bridges = {
      br0.interfaces = [ ];
    };
    networking.interfaces = {
      br0.ipv4.addresses = [
        {
          address = hostIp4;
          prefixLength = 24;
        }
      ];
      br0.ipv6.addresses = [
        {
          address = hostIp6;
          prefixLength = 7;
        }
      ];
    };

    containers.test = {
      autoStart = true;
      privateNetwork = true;
      hostBridge = "br0";
      localAddress = containerIp4;
      localAddress6 = containerIp6;
      config = {
        networking = {
          defaultGateway.address = hostIp4;
          defaultGateway6.address = hostIp6;
        };
        nix.enable = false; # disabled by default on the test's host. See all-tests.nix / tag(no-nix-by-default)
      };
    };
  };

  testScript = ''
    def container_succeed(command: str):
        machine.succeed(f"nixos-container run test -- {command}")

    machine.wait_for_unit("default.target")
    assert "test" in machine.succeed("nixos-container list")

    with subtest("Container has started"):
        assert "up" in machine.succeed("nixos-container status test")

    with subtest("Container can ping the host"):
        container_succeed("ping -n -c 1 ${hostIp4}")
        container_succeed("ping -n -c 1 ${hostIp6}")

    with subtest("Container default gateways are set"):
        container_succeed("ip -4 route show default | grep 'via ${hostIp4}'")
        container_succeed("ip -6 route show default | grep 'via ${hostIp6}'")
  '';
}