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, withConntrackHelpers ? false, ... }:
8 unit = if withFirewall then "firewall" else "nat";
12 { virtualisation.vlans = [ 2 1 ];
13 networking.firewall.enable = withFirewall;
14 networking.nat.internalIPs = [ "192.168.1.0/24" ];
15 networking.nat.externalInterface = "eth1";
17 (lib.optionalAttrs withConntrackHelpers {
18 networking.firewall.connectionTrackingModules = [ "ftp" ];
19 networking.firewall.autoLoadConntrackHelpers = true;
24 name = "nat" + (if withFirewall then "WithFirewall" else "Standalone")
25 + (lib.optionalString withConntrackHelpers "withConntrackHelpers");
26 meta = with pkgs.stdenv.lib.maintainers; {
27 maintainers = [ eelco rob ];
34 { virtualisation.vlans = [ 1 ];
35 networking.defaultGateway =
36 (pkgs.lib.head nodes.router.config.networking.interfaces.eth2.ipv4.addresses).address;
38 (lib.optionalAttrs withConntrackHelpers {
39 networking.firewall.connectionTrackingModules = [ "ftp" ];
40 networking.firewall.autoLoadConntrackHelpers = true;
45 { ... }: lib.mkMerge [
47 { networking.nat.enable = true; }
51 { ... }: lib.mkMerge [
53 { networking.nat.enable = false; }
58 { virtualisation.vlans = [ 2 ];
59 networking.firewall.enable = false;
60 services.httpd.enable = true;
61 services.httpd.adminAddr = "foo@example.org";
62 services.vsftpd.enable = true;
63 services.vsftpd.anonymousUser = true;
69 routerDummyNoNatClosure = nodes.routerDummyNoNat.config.system.build.toplevel;
70 routerClosure = nodes.router.config.system.build.toplevel;
76 # The router should have access to the server.
77 server.wait_for_unit("network.target")
78 server.wait_for_unit("httpd")
79 router.wait_for_unit("network.target")
80 router.succeed("curl --fail http://server/ >&2")
82 # The client should be also able to connect via the NAT router.
83 router.wait_for_unit("${unit}")
84 client.wait_for_unit("network.target")
85 client.succeed("curl --fail http://server/ >&2")
86 client.succeed("ping -c 1 server >&2")
88 # Test whether passive FTP works.
89 server.wait_for_unit("vsftpd")
90 server.succeed("echo Hello World > /home/ftp/foo.txt")
91 client.succeed("curl -v ftp://server/foo.txt >&2")
93 # Test whether active FTP works.
94 client.${if withConntrackHelpers then "succeed" else "fail"}("curl -v -P - ftp://server/foo.txt >&2")
97 client.succeed("ping -c 1 router >&2")
98 router.succeed("ping -c 1 client >&2")
100 # If we turn off NAT, the client shouldn't be able to reach the server.
102 "${routerDummyNoNatClosure}/bin/switch-to-configuration test 2>&1"
104 client.fail("curl --fail --connect-timeout 5 http://server/ >&2")
105 client.fail("ping -c 1 server >&2")
107 # And make sure that reloading the NAT job works.
109 "${routerClosure}/bin/switch-to-configuration test 2>&1"
111 # FIXME: this should not be necessary, but nat.service is not started because
112 # network.target is not triggered
113 # (https://github.com/NixOS/nixpkgs/issues/16230#issuecomment-226408359)
114 ${lib.optionalString (!withFirewall) ''
115 router.succeed("systemctl start nat.service")
117 client.succeed("curl --fail http://server/ >&2")
118 client.succeed("ping -c 1 server >&2")