1 # This is a simple distributed test involving a topology with two
2 # separate virtual networks - the "inside" and the "outside" - with a
3 # client on the inside network, a server on the outside network, and a
4 # router connected to both that performs Network Address Translation
6 import ./make-test-python.nix ({ pkgs, lib, withFirewall, nftables ? false, ... }:
8 unit = if nftables then "nftables" else (if withFirewall then "firewall" else "nat");
11 name = "nat" + (lib.optionalString nftables "Nftables")
12 + (if withFirewall then "WithFirewall" else "Standalone");
13 meta = with pkgs.lib.maintainers; {
14 maintainers = [ rob ];
19 client = { lib, nodes, ... }: {
20 virtualisation.vlans = [ 1 ];
21 networking.defaultGateway =
22 (lib.head nodes.router.networking.interfaces.eth2.ipv4.addresses).address;
23 networking.nftables.enable = nftables;
26 router = { lib, ... }: {
27 virtualisation.vlans = [ 2 1 ];
28 networking.firewall.enable = withFirewall;
29 networking.firewall.filterForward = nftables;
30 networking.nftables.enable = nftables;
31 networking.nat.enable = true;
32 networking.nat.internalIPs = [ "192.168.1.0/24" ];
33 networking.nat.externalInterface = "eth1";
35 specialisation.no-nat.configuration = {
36 networking.nat.enable = lib.mkForce false;
42 { virtualisation.vlans = [ 2 ];
43 networking.firewall.enable = false;
44 services.httpd.enable = true;
45 services.httpd.adminAddr = "foo@example.org";
46 services.vsftpd.enable = true;
47 services.vsftpd.anonymousUser = true;
56 # The router should have access to the server.
57 server.wait_for_unit("network.target")
58 server.wait_for_unit("httpd")
59 router.wait_for_unit("network.target")
60 router.succeed("curl -4 --fail http://server/ >&2")
62 # The client should be also able to connect via the NAT router.
63 router.wait_for_unit("${unit}")
64 client.wait_for_unit("network.target")
65 client.succeed("curl --fail http://server/ >&2")
66 client.succeed("ping -4 -c 1 server >&2")
68 # Test whether passive FTP works.
69 server.wait_for_unit("vsftpd")
70 server.succeed("echo Hello World > /home/ftp/foo.txt")
71 client.succeed("curl -v ftp://server/foo.txt >&2")
73 # Test whether active FTP works.
74 client.fail("curl -v -P - ftp://server/foo.txt >&2")
77 client.succeed("ping -4 -c 1 router >&2")
78 router.succeed("ping -4 -c 1 client >&2")
80 # If we turn off NAT, the client shouldn't be able to reach the server.
82 "/run/booted-system/specialisation/no-nat/bin/switch-to-configuration test 2>&1"
84 client.fail("curl -4 --fail --connect-timeout 5 http://server/ >&2")
85 client.fail("ping -4 -c 1 server >&2")
87 # And make sure that reloading the NAT job works.
89 "/run/booted-system/bin/switch-to-configuration test 2>&1"
91 # FIXME: this should not be necessary, but nat.service is not started because
92 # network.target is not triggered
93 # (https://github.com/NixOS/nixpkgs/issues/16230#issuecomment-226408359)
94 ${lib.optionalString (!withFirewall && !nftables) ''
95 router.succeed("systemctl start nat.service")
97 client.succeed("curl -4 --fail http://server/ >&2")
98 client.succeed("ping -4 -c 1 server >&2")