Merge pull request #308829 from r-ryantm/auto-update/qlog
[NixPkgs.git] / nixos / tests / nextcloud / with-declarative-redis-and-secrets.nix
blobb09ee1276a136654428eff4587a79f89646e0992
1 args@{ nextcloudVersion ? 27, ... }:
2 (import ../make-test-python.nix ({ pkgs, ...}: let
3   adminuser = "custom_admin_username";
4   # This will be used both for redis and postgresql
5   pass = "hunter2";
6   # Don't do this at home, use a file outside of the nix store instead
7   passFile = toString (pkgs.writeText "pass-file" ''
8     ${pass}
9   '');
10 in {
11   name = "nextcloud-with-declarative-redis";
12   meta = with pkgs.lib.maintainers; {
13     maintainers = [ eqyiel ma27 ];
14   };
16   nodes = {
17     # The only thing the client needs to do is download a file.
18     client = { ... }: {};
20     nextcloud = { config, pkgs, ... }: {
21       networking.firewall.allowedTCPPorts = [ 80 ];
23       services.nextcloud = {
24         enable = true;
25         hostName = "nextcloud";
26         package = pkgs.${"nextcloud" + (toString nextcloudVersion)};
27         caching = {
28           apcu = false;
29           redis = true;
30           memcached = false;
31         };
32         # This test also validates that we can use an "external" database
33         database.createLocally = false;
34         config = {
35           dbtype = "pgsql";
36           dbname = "nextcloud";
37           dbuser = adminuser;
38           dbpassFile = passFile;
39           adminuser = adminuser;
40           adminpassFile = passFile;
41         };
42         secretFile = "/etc/nextcloud-secrets.json";
44         settings = {
45           allow_local_remote_servers = true;
46           redis = {
47             dbindex = 0;
48             timeout = 1.5;
49             # password handled via secretfile below
50           };
51         };
52         configureRedis = true;
53       };
55       services.redis.servers."nextcloud" = {
56         enable = true;
57         port = 6379;
58         requirePass = "secret";
59       };
61       systemd.services.nextcloud-setup= {
62         requires = ["postgresql.service"];
63         after = [ "postgresql.service" ];
64       };
66       services.postgresql = {
67         enable = true;
68         package = pkgs.postgresql_14;
69       };
70       systemd.services.postgresql.postStart = pkgs.lib.mkAfter ''
71         password=$(cat ${passFile})
72         ${config.services.postgresql.package}/bin/psql <<EOF
73           CREATE ROLE ${adminuser} WITH LOGIN PASSWORD '$password' CREATEDB;
74           CREATE DATABASE nextcloud;
75           GRANT ALL PRIVILEGES ON DATABASE nextcloud TO ${adminuser};
76         EOF
77       '';
79       # This file is meant to contain secret options which should
80       # not go into the nix store. Here it is just used to set the
81       # redis password.
82       environment.etc."nextcloud-secrets.json".text = ''
83         {
84           "redis": {
85             "password": "secret"
86           }
87         }
88       '';
89     };
90   };
92   testScript = let
93     withRcloneEnv = pkgs.writeScript "with-rclone-env" ''
94       #!${pkgs.runtimeShell}
95       export RCLONE_CONFIG_NEXTCLOUD_TYPE=webdav
96       export RCLONE_CONFIG_NEXTCLOUD_URL="http://nextcloud/remote.php/dav/files/${adminuser}"
97       export RCLONE_CONFIG_NEXTCLOUD_VENDOR="nextcloud"
98       export RCLONE_CONFIG_NEXTCLOUD_USER="${adminuser}"
99       export RCLONE_CONFIG_NEXTCLOUD_PASS="$(${pkgs.rclone}/bin/rclone obscure ${pass})"
100       "''${@}"
101     '';
102     copySharedFile = pkgs.writeScript "copy-shared-file" ''
103       #!${pkgs.runtimeShell}
104       echo 'hi' | ${pkgs.rclone}/bin/rclone rcat nextcloud:test-shared-file
105     '';
107     diffSharedFile = pkgs.writeScript "diff-shared-file" ''
108       #!${pkgs.runtimeShell}
109       diff <(echo 'hi') <(${pkgs.rclone}/bin/rclone cat nextcloud:test-shared-file)
110     '';
111   in ''
112     start_all()
113     nextcloud.wait_for_unit("multi-user.target")
114     nextcloud.succeed("curl -sSf http://nextcloud/login")
115     nextcloud.succeed(
116         "${withRcloneEnv} ${copySharedFile}"
117     )
118     client.wait_for_unit("multi-user.target")
119     client.succeed(
120         "${withRcloneEnv} ${diffSharedFile}"
121     )
123     # redis cache should not be empty
124     nextcloud.fail('test "[]" = "$(redis-cli --json KEYS "*")"')
125   '';
126 })) args