summary refs log blame commit diff
path: root/tests/nat.nix
blob: 6b0c7306bd9eed793d4b1a336d5ba999848bd186 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                                      
                                     
                                       

                                                                 




                                         


                                                        










                                                       
                   




                                                    


















                                                                             


       
# This is a simple distributed test involving a topology with two
# separate virtual networks - the "inside" and the "outside" - with a
# client on the inside network, a server on the outside network, and a
# router connected to both that performs Network Address Translation
# for the client.

{ pkgs, ... }:

{

  nodes =
    { client = 
        { config, pkgs, nodes, ... }:
        { virtualisation.vlans = [ 1 ];
          networking.defaultGateway =
            nodes.router.config.networking.ifaces.eth2.ipAddress;
        };

      router = 
        { config, pkgs, ... }:
        { virtualisation.vlans = [ 2 1 ];
          networking.nat.enable = true;
          networking.nat.internalIPs = "192.168.1.0/24";
          networking.nat.externalInterface = "eth1";
        };

      server = 
        { config, pkgs, ... }:
        { virtualisation.vlans = [ 2 ];
          services.httpd.enable = true;
          services.httpd.adminAddr = "foo@example.org";
        };
    };

  testScript =
    { nodes, ... }:
    ''
      startAll;

      # The router should have access to the server.
      $server->waitForJob("httpd");
      $router->succeed("curl --fail http://server/ >&2");

      # The client should be also able to connect via the NAT router.
      $router->waitForJob("nat");
      $client->succeed("curl --fail http://server/ >&2");
      $client->succeed("ping -c 1 server >&2");
      
      # If we turn off NAT, the client shouldn't be able to reach the server.
      $router->succeed("stop nat");
      $client->fail("curl --fail --connect-timeout 5 http://server/ >&2");
      $client->fail("ping -c 1 server >&2");

      # And make sure that restarting the NAT job works.
      $router->succeed("start nat");
      $client->succeed("curl --fail http://server/ >&2");
      $client->succeed("ping -c 1 server >&2");

      $client->succeed("ping -c 1 router >&2");
      $router->succeed("ping -c 1 client >&2");
    '';

}