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