summary refs log tree commit diff
path: root/nixos/tests/invidious.nix
blob: 8b831715a441fae87f9a2f18e25384373627f1d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import ./make-test-python.nix ({ pkgs, ... }: {
  name = "invidious";

  meta = with pkgs.lib.maintainers; {
    maintainers = [ sbruder ];
  };

  machine = { config, lib, pkgs, ... }: {
    services.invidious = {
      enable = true;
    };

    specialisation = {
      nginx.configuration = {
        services.invidious = {
          nginx.enable = true;
          domain = "invidious.example.com";
        };
        services.nginx.virtualHosts."invidious.example.com" = {
          forceSSL = false;
          enableACME = false;
        };
        networking.hosts."127.0.0.1" = [ "invidious.example.com" ];
      };
      postgres-tcp.configuration = {
        services.invidious = {
          database = {
            createLocally = false;
            host = "127.0.0.1";
            passwordFile = toString (pkgs.writeText "database-password" "correct horse battery staple");
          };
        };
        # Normally not needed because when connecting to postgres over TCP/IP
        # the database is most likely on another host.
        systemd.services.invidious = {
          after = [ "postgresql.service" ];
          requires = [ "postgresql.service" ];
        };
        services.postgresql =
          let
            inherit (config.services.invidious.settings.db) dbname user;
          in
          {
            enable = true;
            initialScript = pkgs.writeText "init-postgres-with-password" ''
              CREATE USER kemal WITH PASSWORD 'correct horse battery staple';
              CREATE DATABASE invidious;
              GRANT ALL PRIVILEGES ON DATABASE invidious TO kemal;
            '';
          };
      };
    };
  };

  testScript = { nodes, ... }: ''
    def curl_assert_status_code(url, code, form=None):
        assert int(machine.succeed(f"curl -s -o /dev/null -w %{{http_code}} {'-F ' + form + ' ' if form else '''}{url}")) == code


    def activate_specialisation(name: str):
        machine.succeed(f"${nodes.machine.config.system.build.toplevel}/specialisation/{name}/bin/switch-to-configuration test >&2")


    url = "http://localhost:${toString nodes.machine.config.services.invidious.port}"
    port = ${toString nodes.machine.config.services.invidious.port}

    machine.wait_for_open_port(port)
    curl_assert_status_code(f"{url}/search", 200)

    activate_specialisation("nginx")
    machine.wait_for_open_port(80)
    curl_assert_status_code("http://invidious.example.com/search", 200)

    # Remove the state so the `initialScript` gets run
    machine.succeed("systemctl stop postgresql")
    machine.succeed("rm -r /var/lib/postgresql")
    activate_specialisation("postgres-tcp")
    machine.wait_for_open_port(port)
    curl_assert_status_code(f"{url}/search", 200)
  '';
})