summary refs log tree commit diff
path: root/nixos/tests/invidious.nix
diff options
context:
space:
mode:
authorSimon Bruder <simon@sbruder.de>2021-09-07 19:44:12 +0200
committerSimon Bruder <simon@sbruder.de>2021-10-30 16:33:38 +0200
commitfa0bfdbe0906d26ff29fdaf62cadf43700145ba7 (patch)
tree795bf9abf6b9d5c1753bd850360afe1aeaa32c0a /nixos/tests/invidious.nix
parentf1447fdaa852ea4d3685d3b7883af64789ab2a02 (diff)
downloadnixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.tar
nixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.tar.gz
nixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.tar.bz2
nixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.tar.lz
nixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.tar.xz
nixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.tar.zst
nixpkgs-fa0bfdbe0906d26ff29fdaf62cadf43700145ba7.zip
nixos/invidious: add test
Diffstat (limited to 'nixos/tests/invidious.nix')
-rw-r--r--nixos/tests/invidious.nix81
1 files changed, 81 insertions, 0 deletions
diff --git a/nixos/tests/invidious.nix b/nixos/tests/invidious.nix
new file mode 100644
index 00000000000..8b831715a44
--- /dev/null
+++ b/nixos/tests/invidious.nix
@@ -0,0 +1,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)
+  '';
+})