/ tests / transfer-node-with-relay.nix
transfer-node-with-relay.nix
  1  {
  2    pkgs,
  3    mkDistroxConfigFile,
  4    distrox-cli-no-tracy,
  5    xdg-test-setup-stable,
  6    test-utility-functions,
  7    setup-config,
  8    iroh-relay-module,
  9    ...
 10  }:
 11  
 12  let
 13    distrox-cli = distrox-cli-no-tracy;
 14  
 15    config = mkDistroxConfigFile {
 16      relay_config = {
 17        default_relays = false;
 18        custom_relays = [ { url = "http://relay:3340"; } ];
 19      };
 20    };
 21  
 22    # utility to output the xdg setup so one can interactively do
 23    # $ eval "$(echo-xdg-setup)"
 24    echo-xdg-setup = pkgs.writeShellApplication {
 25      name = "echo-xdg-setup";
 26      text = ''
 27        cat <<EOF
 28        ${xdg-test-setup-stable}
 29        EOF
 30      '';
 31    };
 32  
 33    install-distrox = pkgs.writeShellApplication {
 34      name = "install-distrox";
 35      runtimeInputs = [ distrox-cli ];
 36  
 37      text = ''
 38        ${xdg-test-setup-stable}
 39        ${test-utility-functions}
 40        ${setup-config config}
 41  
 42        export RUST_LOG=info
 43        distrox-cli --config "${config}" database setup
 44        distrox-cli --config "${config}" key generate
 45      '';
 46    };
 47  
 48    get-public-key = pkgs.writeShellApplication {
 49      name = "get-public-key";
 50      runtimeInputs = [ distrox-cli ];
 51  
 52      text = ''
 53        ${xdg-test-setup-stable}
 54        ${test-utility-functions}
 55        ${setup-config config}
 56  
 57        export RUST_LOG=info
 58        distrox-cli --config "${config}" key public -o-
 59      '';
 60    };
 61  
 62    generate-node = pkgs.writeShellApplication {
 63      name = "generate-node";
 64      runtimeInputs = [ distrox-cli ];
 65  
 66      text = ''
 67        ${xdg-test-setup-stable}
 68        export RUST_LOG=info
 69        distrox-cli --config "${config}" put node
 70      '';
 71    };
 72  
 73    serve = pkgs.writeShellApplication {
 74      name = "serve";
 75      runtimeInputs = [ distrox-cli ];
 76      text = ''
 77        ${xdg-test-setup-stable}
 78        export RUST_LOG=warn,distrox_network=trace,distrox_instance=debug,distrox_cli=info
 79        distrox-cli --config "${config}" serve everything
 80      '';
 81    };
 82  
 83    recv-node = pkgs.writeShellApplication {
 84      name = "recv-node";
 85      runtimeInputs = [ distrox-cli ];
 86      text = ''
 87        ${xdg-test-setup-stable}
 88  
 89        echo ">>> Trying to fetch '$2' from '$1'" | systemd-cat -p info
 90  
 91        export RUST_LOG=warn,distrox_network=trace,distrox_instance=debug,distrox_cli=info
 92        distrox-cli --config "${config}" fetch --connect-to "$1" node --format json "$2"
 93      '';
 94    };
 95  in
 96  pkgs.testers.runNixOSTest {
 97    name = "transfer-node-with-relay";
 98    nodes = {
 99      relay =
100        { pkgs, ... }:
101        {
102          imports = [ iroh-relay-module ];
103          virtualisation.vlans = [ 1 ];
104          services.iroh-relay = {
105            enable = true;
106            rust-log = "info";
107            openHttpPort = true;
108            openQuicPort = true;
109          };
110        };
111  
112      primary =
113        {
114          pkgs,
115          ...
116        }:
117        {
118          virtualisation.vlans = [ 1 ];
119          networking.firewall.enable = true;
120          environment.systemPackages = [
121            echo-xdg-setup
122            distrox-cli
123            install-distrox
124            generate-node
125            get-public-key
126          ];
127  
128          users.users.distrox = {
129            isSystemUser = true;
130            group = "distrox";
131          };
132  
133          users.groups.distrox = { };
134  
135          systemd.services.distrox-serve = {
136            enable = true;
137            wantedBy = [ ]; # explicitly nothing
138            wants = [
139              "network-online.target"
140              "avahi-daemon.service"
141            ];
142            after = [
143              "network-online.target"
144              "avahi-daemon.service"
145            ];
146            serviceConfig = {
147              Type = "simple";
148              ExecStart = "${pkgs.lib.getExe serve}";
149              OnFailure = "emergency.target"; # make test fail if there was an error
150            };
151          };
152          services.avahi = {
153            enable = true;
154            openFirewall = true;
155            nssmdns4 = true;
156            nssmdns6 = true;
157            publish = {
158              enable = true;
159              userServices = true;
160              addresses = true;
161              domain = true;
162              workstation = true;
163            };
164          };
165        };
166  
167      secondary =
168        {
169          pkgs,
170          ...
171        }:
172        {
173          virtualisation.vlans = [ 1 ];
174          networking.firewall.enable = true;
175          environment.systemPackages = [
176            echo-xdg-setup
177            distrox-cli
178            install-distrox
179            recv-node
180          ];
181          services.avahi = {
182            enable = true;
183            openFirewall = true;
184            nssmdns4 = true;
185            nssmdns6 = true;
186            publish = {
187              enable = true;
188              userServices = true;
189              addresses = true;
190              domain = true;
191              workstation = true;
192            };
193          };
194        };
195    };
196  
197    testScript = ''
198      relay.start()
199      primary.start()
200      secondary.start()
201  
202      primary.wait_for_unit("network.target")
203      secondary.wait_for_unit("network.target")
204      relay.wait_for_unit("iroh-relay.service")
205  
206      # check if mDNS works
207      # See https://github.com/NixOS/nixpkgs/blob/7c5cdd87661f959e05a3f2498a89a7a6d07f92a6/nixos/tests/avahi.nix
208  
209      primary.succeed("avahi-resolve-host-name primary.local | tee out >&2")
210      primary.succeed('test "`cut -f1 < out`" = primary.local')
211      primary.succeed("avahi-resolve-host-name secondary.local | tee out >&2")
212      primary.succeed('test "`cut -f1 < out`" = secondary.local')
213  
214      secondary.succeed("avahi-resolve-host-name primary.local | tee out >&2")
215      secondary.succeed('test "`cut -f1 < out`" = primary.local')
216      secondary.succeed("avahi-resolve-host-name secondary.local | tee out >&2")
217      secondary.succeed('test "`cut -f1 < out`" = secondary.local')
218  
219      #
220      # Install/setup distrox
221      #
222  
223      primary.succeed("install-distrox")
224      secondary.succeed("install-distrox")
225  
226      # Generate node
227      node_id = primary.succeed("generate-node")
228      print(f"Received node: {node_id.strip()}")
229  
230      # Start distrox serve
231      primary.systemctl("enable --now distrox-serve")
232      primary.wait_for_unit("distrox-serve.service")
233  
234      # Fetch public key of "primary"
235      primary_public_key = primary.succeed("get-public-key")
236  
237      # Fetch node from "primary" on "secondary"
238      secondary.succeed(f"recv-node {primary_public_key.strip()} {node_id.strip()}")
239    '';
240  }