refactor: split module.nix into per-service modules
Replace the 1301-line monolithic module.nix with focused modules: - modules/servarr.nix (Sonarr/Radarr/Prowlarr) - modules/bazarr.nix (Bazarr provider connections) - modules/jellyseerr.nix (Jellyseerr quality profiles) - modules/default.nix (import aggregator) Python scripts (from prior commit) are referenced as standalone files via PYTHONPATH, with config passed as a JSON file argument. New options: - Add bindAddress option to all services (default 127.0.0.1) - Replace hardcoded wg.service dependency with configurable networkNamespaceService option - Add systemd hardening: PrivateTmp, NoNewPrivileges, ProtectHome, ProtectKernelTunables/Modules, ProtectControlGroups, RestrictSUIDSGID, SystemCallArchitectures=native Test updates: - Extract mock qBittorrent/SABnzbd servers into tests/lib/mocks.nix - Fix duplicate wait_for_unit calls in integration test
This commit is contained in:
@@ -9,6 +9,9 @@ pkgs.testers.runNixOSTest {
|
||||
|
||||
nodes.machine =
|
||||
{ pkgs, lib, ... }:
|
||||
let
|
||||
mocks = import ./lib/mocks.nix { inherit pkgs; };
|
||||
in
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
|
||||
@@ -22,71 +25,7 @@ pkgs.testers.runNixOSTest {
|
||||
gnugrep
|
||||
];
|
||||
|
||||
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 = {}
|
||||
|
||||
|
||||
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";
|
||||
};
|
||||
};
|
||||
systemd.services.mock-qbittorrent = mocks.mkMockQbittorrent { };
|
||||
|
||||
# Create directories including one with spaces
|
||||
systemd.tmpfiles.rules = [
|
||||
|
||||
@@ -9,6 +9,9 @@ pkgs.testers.runNixOSTest {
|
||||
|
||||
nodes.machine =
|
||||
{ pkgs, lib, ... }:
|
||||
let
|
||||
mocks = import ./lib/mocks.nix { inherit pkgs; };
|
||||
in
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
|
||||
@@ -22,77 +25,12 @@ pkgs.testers.runNixOSTest {
|
||||
gnugrep
|
||||
];
|
||||
|
||||
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"},
|
||||
"movies": {"name": "movies", "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}
|
||||
if path in ["/api/v2/torrents/editCategory", "/api/v2/torrents/removeCategory"]:
|
||||
self._respond()
|
||||
return
|
||||
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";
|
||||
};
|
||||
systemd.services.mock-qbittorrent = mocks.mkMockQbittorrent {
|
||||
initialCategories = {
|
||||
tv = { name = "tv"; savePath = "/downloads"; };
|
||||
movies = { name = "movies"; savePath = "/downloads"; };
|
||||
};
|
||||
};
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /media/tv 0755 sonarr sonarr -"
|
||||
|
||||
@@ -9,6 +9,9 @@ pkgs.testers.runNixOSTest {
|
||||
|
||||
nodes.machine =
|
||||
{ pkgs, lib, ... }:
|
||||
let
|
||||
mocks = import ./lib/mocks.nix { inherit pkgs; };
|
||||
in
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
|
||||
@@ -22,81 +25,13 @@ pkgs.testers.runNixOSTest {
|
||||
gnugrep
|
||||
];
|
||||
|
||||
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"},
|
||||
"movies": {"name": "movies", "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}
|
||||
if path in ["/api/v2/torrents/editCategory", "/api/v2/torrents/removeCategory"]:
|
||||
self._respond()
|
||||
return
|
||||
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" ];
|
||||
before = [
|
||||
"sonarr-init.service"
|
||||
"radarr-init.service"
|
||||
];
|
||||
serviceConfig = {
|
||||
ExecStart = "${pkgs.python3}/bin/python3 ${mockQbitScript}";
|
||||
Type = "simple";
|
||||
};
|
||||
systemd.services.mock-qbittorrent = mocks.mkMockQbittorrent {
|
||||
initialCategories = {
|
||||
tv = { name = "tv"; savePath = "/downloads"; };
|
||||
movies = { name = "movies"; savePath = "/downloads"; };
|
||||
};
|
||||
before = [ "sonarr-init.service" "radarr-init.service" ];
|
||||
};
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /media/tv 0755 sonarr sonarr -"
|
||||
@@ -255,10 +190,6 @@ pkgs.testers.runNixOSTest {
|
||||
machine.wait_for_unit("sonarr-init.service")
|
||||
machine.wait_for_unit("radarr-init.service")
|
||||
|
||||
# Wait for init services to complete
|
||||
machine.wait_for_unit("sonarr-init.service")
|
||||
machine.wait_for_unit("radarr-init.service")
|
||||
|
||||
# Verify Sonarr download clients
|
||||
machine.succeed(
|
||||
"API_KEY=$(grep -oP '(?<=<ApiKey>)[^<]+' /var/lib/sonarr/.config/NzbDrone/config.xml) && "
|
||||
|
||||
126
tests/lib/mocks.nix
Normal file
126
tests/lib/mocks.nix
Normal file
@@ -0,0 +1,126 @@
|
||||
# Shared mock service generators for arr-init NixOS tests.
|
||||
#
|
||||
# Usage (from a test file):
|
||||
# let mocks = import ./lib/mocks.nix { inherit pkgs; };
|
||||
# in { systemd.services.mock-qbittorrent = mocks.mkMockQbittorrent { }; }
|
||||
{ pkgs }:
|
||||
{
|
||||
# Mock qBittorrent WebUI API.
|
||||
#
|
||||
# Args:
|
||||
# port - TCP port (default 6011)
|
||||
# initialCategories - Nix attrset seeded as the CATEGORIES dict
|
||||
# before - systemd Before= list
|
||||
mkMockQbittorrent =
|
||||
{
|
||||
port ? 6011,
|
||||
initialCategories ? { },
|
||||
before ? [ ],
|
||||
}:
|
||||
let
|
||||
categoriesJson = builtins.toJSON initialCategories;
|
||||
mockScript = pkgs.writeScript "mock-qbittorrent.py" ''
|
||||
import json
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
CATEGORIES = json.loads('${categoriesJson}')
|
||||
|
||||
|
||||
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", ${toString port}), QBitMock).serve_forever()
|
||||
'';
|
||||
in
|
||||
{
|
||||
description = "Mock qBittorrent API";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
inherit before;
|
||||
serviceConfig = {
|
||||
ExecStart = "${pkgs.python3}/bin/python3 ${mockScript}";
|
||||
Type = "simple";
|
||||
};
|
||||
};
|
||||
|
||||
# Mock SABnzbd API.
|
||||
mkMockSabnzbd =
|
||||
{ port ? 6012 }:
|
||||
let
|
||||
mockScript = pkgs.writeScript "mock-sabnzbd.py" ''
|
||||
import json
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
|
||||
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):
|
||||
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", ${toString port}), SabMock).serve_forever()
|
||||
'';
|
||||
in
|
||||
{
|
||||
description = "Mock SABnzbd API";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
serviceConfig = {
|
||||
ExecStart = "${pkgs.python3}/bin/python3 ${mockScript}";
|
||||
Type = "simple";
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -9,6 +9,9 @@ pkgs.testers.runNixOSTest {
|
||||
|
||||
nodes.machine =
|
||||
{ pkgs, lib, ... }:
|
||||
let
|
||||
mocks = import ./lib/mocks.nix { inherit pkgs; };
|
||||
in
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
|
||||
@@ -23,117 +26,14 @@ pkgs.testers.runNixOSTest {
|
||||
];
|
||||
|
||||
# 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";
|
||||
};
|
||||
systemd.services.mock-qbittorrent = mocks.mkMockQbittorrent {
|
||||
initialCategories = {
|
||||
tv = { name = "tv"; savePath = "/downloads"; };
|
||||
};
|
||||
};
|
||||
|
||||
# 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.services.mock-sabnzbd = mocks.mkMockSabnzbd { };
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /media/tv 0755 sonarr sonarr -"
|
||||
|
||||
@@ -9,6 +9,9 @@ pkgs.testers.runNixOSTest {
|
||||
|
||||
nodes.machine =
|
||||
{ pkgs, lib, ... }:
|
||||
let
|
||||
mocks = import ./lib/mocks.nix { inherit pkgs; };
|
||||
in
|
||||
{
|
||||
imports = [ self.nixosModules.default ];
|
||||
|
||||
@@ -22,71 +25,7 @@ pkgs.testers.runNixOSTest {
|
||||
gnugrep
|
||||
];
|
||||
|
||||
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 = {}
|
||||
|
||||
|
||||
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";
|
||||
};
|
||||
};
|
||||
systemd.services.mock-qbittorrent = mocks.mkMockQbittorrent { };
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d /media/tv 0755 sonarr sonarr -"
|
||||
|
||||
Reference in New Issue
Block a user