at 23.05-pre 2.8 kB view raw
1import ./make-test-python.nix ({ pkgs, ... }: { 2 name = "invidious"; 3 4 meta = with pkgs.lib.maintainers; { 5 maintainers = [ sbruder ]; 6 }; 7 8 nodes.machine = { config, lib, pkgs, ... }: { 9 services.invidious = { 10 enable = true; 11 }; 12 13 specialisation = { 14 nginx.configuration = { 15 services.invidious = { 16 nginx.enable = true; 17 domain = "invidious.example.com"; 18 }; 19 services.nginx.virtualHosts."invidious.example.com" = { 20 forceSSL = false; 21 enableACME = false; 22 }; 23 networking.hosts."127.0.0.1" = [ "invidious.example.com" ]; 24 }; 25 postgres-tcp.configuration = { 26 services.invidious = { 27 database = { 28 createLocally = false; 29 host = "127.0.0.1"; 30 passwordFile = toString (pkgs.writeText "database-password" "correct horse battery staple"); 31 }; 32 }; 33 # Normally not needed because when connecting to postgres over TCP/IP 34 # the database is most likely on another host. 35 systemd.services.invidious = { 36 after = [ "postgresql.service" ]; 37 requires = [ "postgresql.service" ]; 38 }; 39 services.postgresql = 40 let 41 inherit (config.services.invidious.settings.db) dbname user; 42 in 43 { 44 enable = true; 45 initialScript = pkgs.writeText "init-postgres-with-password" '' 46 CREATE USER kemal WITH PASSWORD 'correct horse battery staple'; 47 CREATE DATABASE invidious; 48 GRANT ALL PRIVILEGES ON DATABASE invidious TO kemal; 49 ''; 50 }; 51 }; 52 }; 53 }; 54 55 testScript = { nodes, ... }: '' 56 def curl_assert_status_code(url, code, form=None): 57 assert int(machine.succeed(f"curl -s -o /dev/null -w %{{http_code}} {'-F ' + form + ' ' if form else '''}{url}")) == code 58 59 60 def activate_specialisation(name: str): 61 machine.succeed(f"${nodes.machine.config.system.build.toplevel}/specialisation/{name}/bin/switch-to-configuration test >&2") 62 63 64 url = "http://localhost:${toString nodes.machine.config.services.invidious.port}" 65 port = ${toString nodes.machine.config.services.invidious.port} 66 67 machine.wait_for_open_port(port) 68 curl_assert_status_code(f"{url}/search", 200) 69 70 activate_specialisation("nginx") 71 machine.wait_for_open_port(80) 72 curl_assert_status_code("http://invidious.example.com/search", 200) 73 74 # Remove the state so the `initialScript` gets run 75 machine.succeed("systemctl stop postgresql") 76 machine.succeed("rm -r /var/lib/postgresql") 77 activate_specialisation("postgres-tcp") 78 machine.wait_for_open_port(port) 79 curl_assert_status_code(f"{url}/search", 200) 80 ''; 81})