{ pkgs, lib, self, }: pkgs.testers.runNixOSTest { name = "arr-init-multiple-clients"; nodes.machine = { pkgs, lib, ... }: { imports = [ self.nixosModules.default ]; system.stateVersion = "24.11"; virtualisation.memorySize = 4096; environment.systemPackages = with pkgs; [ curl jq gnugrep ]; # Mock qBittorrent on port 6011 systemd.services.mock-qbittorrent = let mockQbitScript = pkgs.writeScript "mock-qbittorrent.py" '' import json from http.server import HTTPServer, BaseHTTPRequestHandler from urllib.parse import parse_qs, urlparse CATEGORIES = { "tv": {"name": "tv", "savePath": "/downloads"}, } class QBitMock(BaseHTTPRequestHandler): def _respond(self, code=200, body=b"Ok.", content_type="text/plain"): self.send_response(code) self.send_header("Content-Type", content_type) self.send_header("Set-Cookie", "SID=mock_session_id; Path=/") self.end_headers() self.wfile.write(body if isinstance(body, bytes) else body.encode()) def do_GET(self): path = self.path.split("?")[0] if path == "/api/v2/app/webapiVersion": self._respond(body=b"2.9.3") elif path == "/api/v2/app/version": self._respond(body=b"v5.0.0") elif path == "/api/v2/torrents/info": self._respond(body=b"[]", content_type="application/json") elif path == "/api/v2/torrents/categories": body = json.dumps(CATEGORIES).encode() self._respond(body=body, content_type="application/json") elif path == "/api/v2/app/preferences": body = json.dumps({"save_path": "/tmp"}).encode() self._respond(body=body, content_type="application/json") else: self._respond() def do_POST(self): content_length = int(self.headers.get("Content-Length", 0)) body = self.rfile.read(content_length).decode() path = urlparse(self.path).path query = parse_qs(urlparse(self.path).query) form = parse_qs(body) params = {**query, **form} if path == "/api/v2/torrents/createCategory": name = params.get("category", [""])[0] save_path = params.get("savePath", params.get("save_path", [""]))[0] or "/downloads" if name: CATEGORIES[name] = {"name": name, "savePath": save_path} self._respond() def log_message(self, format, *args): pass HTTPServer(("0.0.0.0", 6011), QBitMock).serve_forever() ''; in { description = "Mock qBittorrent API"; wantedBy = [ "multi-user.target" ]; serviceConfig = { ExecStart = "${pkgs.python3}/bin/python3 ${mockQbitScript}"; Type = "simple"; }; }; # Mock SABnzbd on port 6012 systemd.services.mock-sabnzbd = let mockSabScript = pkgs.writeScript "mock-sabnzbd.py" '' import json from http.server import HTTPServer, BaseHTTPRequestHandler from urllib.parse import parse_qs, urlparse class SabMock(BaseHTTPRequestHandler): def _respond(self, code=200, body=b'{"config": {}}', content_type="application/json"): self.send_response(code) self.send_header("Content-Type", content_type) self.end_headers() self.wfile.write(body if isinstance(body, bytes) else body.encode()) def do_GET(self): path = self.path.split("?")[0] if "mode=config" in self.path or "mode=version" in self.path: self._respond(body=b'{"config": {"misc": {"api_key": "test"}}, "version": "4.0.0"}') elif "mode=get_config" in self.path: self._respond(body=b'{"config": {"misc": {"complete_dir": "/downloads/usenet", "pre_check": false}, "categories": [{"name": "tv", "order": 0, "pp": "", "script": "Default", "dir": "tv"}], "sorters": []}}') else: self._respond() def do_POST(self): self._respond() def log_message(self, format, *args): pass HTTPServer(("0.0.0.0", 6012), SabMock).serve_forever() ''; in { description = "Mock SABnzbd API"; wantedBy = [ "multi-user.target" ]; serviceConfig = { ExecStart = "${pkgs.python3}/bin/python3 ${mockSabScript}"; Type = "simple"; }; }; systemd.tmpfiles.rules = [ "d /media/tv 0755 sonarr sonarr -" ]; services.sonarr = { enable = true; dataDir = "/var/lib/sonarr/.config/NzbDrone"; settings.server.port = lib.mkDefault 8989; }; # Sonarr with TWO download clients: qBittorrent + SABnzbd services.arrInit.sonarr = { enable = true; serviceName = "sonarr"; dataDir = "/var/lib/sonarr/.config/NzbDrone"; port = 8989; downloadClients = [ { name = "qBittorrent"; implementation = "QBittorrent"; configContract = "QBittorrentSettings"; protocol = "torrent"; fields = { host = "127.0.0.1"; port = 6011; useSsl = false; tvCategory = "tv"; }; } { name = "SABnzbd"; implementation = "Sabnzbd"; configContract = "SabnzbdSettings"; protocol = "usenet"; fields = { host = "127.0.0.1"; port = 6012; useSsl = false; apiKey = "test-api-key"; tvCategory = "tv"; }; } ]; rootFolders = [ "/media/tv" ]; }; }; testScript = '' start_all() # Wait for mock services machine.wait_for_unit("mock-qbittorrent.service") machine.wait_for_unit("mock-sabnzbd.service") machine.wait_until_succeeds("curl -sf http://localhost:6011/api/v2/app/version", timeout=30) # Wait for Sonarr machine.wait_for_unit("sonarr.service") machine.wait_until_succeeds( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/system/status -H \"X-Api-Key: $API_KEY\"", timeout=120, ) # Trigger init machine.succeed("systemctl restart sonarr-init.service") machine.wait_for_unit("sonarr-init.service") # Verify both download clients exist result = machine.succeed( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/downloadclient -H \"X-Api-Key: $API_KEY\" | " "jq '. | length'" ).strip() assert result == "2", f"Expected 2 Sonarr download clients, got {result}" # Verify qBittorrent client machine.succeed( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/downloadclient -H \"X-Api-Key: $API_KEY\" | " "jq -e '.[] | select(.name == \"qBittorrent\")'" ) # Verify SABnzbd client machine.succeed( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/downloadclient -H \"X-Api-Key: $API_KEY\" | " "jq -e '.[] | select(.name == \"SABnzbd\")'" ) # Idempotency test: restart init and verify still exactly 2 clients machine.succeed("systemctl restart sonarr-init.service") machine.wait_for_unit("sonarr-init.service") result = machine.succeed( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/downloadclient -H \"X-Api-Key: $API_KEY\" | " "jq '. | length'" ).strip() assert result == "2", f"Expected 2 Sonarr download clients after idempotency test, got {result}" # Verify qBittorrent has correct configuration machine.succeed( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/downloadclient -H \"X-Api-Key: $API_KEY\" | " "jq -e '.[] | select(.name == \"qBittorrent\") | .protocol == \"torrent\"'" ) # Verify SABnzbd has correct configuration machine.succeed( "API_KEY=$(grep -oP '(?<=)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && " "curl -sf http://localhost:8989/api/v3/downloadclient -H \"X-Api-Key: $API_KEY\" | " "jq -e '.[] | select(.name == \"SABnzbd\") | .protocol == \"usenet\"'" ) ''; }