Release NixOS 23.11
[NixPkgs.git] / nixos / tests / invidious.nix
blob701e8e5e7a3fc4c70948abe045faec8f13eedb71
1 import ./make-test-python.nix ({ pkgs, ... }: {
2   name = "invidious";
4   meta = with pkgs.lib.maintainers; {
5     maintainers = [ sbruder ];
6   };
8   nodes.machine = { config, lib, pkgs, ... }: {
9     services.invidious = {
10       enable = true;
11     };
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 OWNER kemal;
48             '';
49           };
50       };
51     };
52   };
54   testScript = { nodes, ... }: ''
55     def curl_assert_status_code(url, code, form=None):
56         assert int(machine.succeed(f"curl -s -o /dev/null -w %{{http_code}} {'-F ' + form + ' ' if form else '''}{url}")) == code
59     def activate_specialisation(name: str):
60         machine.succeed(f"${nodes.machine.config.system.build.toplevel}/specialisation/{name}/bin/switch-to-configuration test >&2")
63     url = "http://localhost:${toString nodes.machine.config.services.invidious.port}"
64     port = ${toString nodes.machine.config.services.invidious.port}
66     machine.wait_for_open_port(port)
67     curl_assert_status_code(f"{url}/search", 200)
69     activate_specialisation("nginx")
70     machine.wait_for_open_port(80)
71     curl_assert_status_code("http://invidious.example.com/search", 200)
73     # Remove the state so the `initialScript` gets run
74     machine.succeed("systemctl stop postgresql")
75     machine.succeed("rm -r /var/lib/postgresql")
76     activate_specialisation("postgres-tcp")
77     machine.wait_for_open_port(port)
78     curl_assert_status_code(f"{url}/search", 200)
79   '';