1
0
Fork 0
selfhostblocks/test/vm/nextcloud.nix

392 lines
13 KiB
Nix
Raw Normal View History

2023-12-30 18:51:55 +01:00
{ pkgs, lib, ... }:
let
2024-03-20 06:50:41 +01:00
pkgs' = pkgs;
2023-12-30 18:51:55 +01:00
adminUser = "root";
adminPass = "rootpw";
2024-03-04 08:01:05 +01:00
commonTestScript = { nodes, ... }:
let
hasSSL = !(isNull nodes.server.shb.nextcloud.ssl);
fqdn = if hasSSL then "https://n.example.com" else "http://n.example.com";
in
''
import json
import os
import pathlib
import time
2023-12-30 18:51:55 +01:00
start_all()
server.wait_for_unit("phpfpm-nextcloud.service")
server.wait_for_unit("nginx.service")
2024-03-04 08:01:05 +01:00
server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.nextcloud.socket}")
if ${if hasSSL then "True" else "False"}:
server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt")
client.succeed("rm -r /etc/ssl/certs")
client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt")
2023-12-30 18:51:55 +01:00
def find_in_logs(unit, text):
return server.systemctl("status {}".format(unit))[1].find(text) != -1
2024-03-04 08:01:05 +01:00
def curl(target, format, endpoint, succeed=True):
return json.loads(target.succeed(
"curl --fail-with-body --silent --show-error --output /dev/null --location"
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ f" --write-out '{format}'"
+ " " + endpoint
))
with subtest("access"):
response = curl(client, """{"code":%{response_code}}""", "${fqdn}")
if response['code'] != 200:
raise Exception(f"Code is {response['code']}")
2023-12-30 18:51:55 +01:00
with subtest("cron job succeeds"):
# This calls blocks until the service is done.
server.systemctl("start nextcloud-cron.service")
# If the service failed, then we're not happy.
status = "active"
while status == "active":
status = server.get_unit_info("nextcloud-cron")["ActiveState"]
time.sleep(5)
if status != "inactive":
raise Exception("Cron job did not finish correctly")
2023-12-30 18:51:55 +01:00
if not find_in_logs("nextcloud-cron", "nextcloud-cron.service: Deactivated successfully."):
raise Exception("Nextcloud cron job did not finish successfully.")
with subtest("fails with incorrect authentication"):
client.fail(
2024-03-04 08:01:05 +01:00
"curl -f -s --location -X PROPFIND"
2023-12-30 18:51:55 +01:00
+ """ -H "Depth: 1" """
+ """ -u ${adminUser}:other """
2024-03-04 08:01:05 +01:00
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ " ${fqdn}/remote.php/dav/files/${adminUser}/"
)
client.fail(
"curl -f -s --location -X PROPFIND"
+ """ -H "Depth: 1" """
+ """ -u root:rootpw """
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ " ${fqdn}/remote.php/dav/files/other/"
2023-12-30 18:51:55 +01:00
)
with subtest("fails with incorrect path"):
client.fail(
2024-03-04 08:01:05 +01:00
"curl -f -s --location -X PROPFIND"
2023-12-30 18:51:55 +01:00
+ """ -H "Depth: 1" """
+ """ -u ${adminUser}:${adminPass} """
2024-03-04 08:01:05 +01:00
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ " ${fqdn}/remote.php/dav/files/other/"
2023-12-30 18:51:55 +01:00
)
with subtest("can access webdav"):
client.succeed(
2024-03-04 08:01:05 +01:00
"curl -f -s --location -X PROPFIND"
2023-12-30 18:51:55 +01:00
+ """ -H "Depth: 1" """
+ """ -u ${adminUser}:${adminPass} """
2024-03-04 08:01:05 +01:00
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ " ${fqdn}/remote.php/dav/files/${adminUser}/"
2023-12-30 18:51:55 +01:00
)
with subtest("can create and retrieve file"):
client.fail(
2024-03-04 08:01:05 +01:00
"curl -f -s --location -X GET"
2023-12-30 18:51:55 +01:00
+ """ -H "Depth: 1" """
+ """ -u ${adminUser}:${adminPass} """
2024-03-04 08:01:05 +01:00
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
2023-12-30 18:51:55 +01:00
+ """ -T file """
2024-03-04 08:01:05 +01:00
+ " ${fqdn}/remote.php/dav/files/${adminUser}/file"
2023-12-30 18:51:55 +01:00
)
client.succeed("echo 'hello' > file")
client.succeed(
2024-03-04 08:01:05 +01:00
"curl -f -s --location -X PUT"
2023-12-30 18:51:55 +01:00
+ """ -H "Depth: 1" """
+ """ -u ${adminUser}:${adminPass} """
2024-03-04 08:01:05 +01:00
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
2023-12-30 18:51:55 +01:00
+ """ -T file """
2024-03-04 08:01:05 +01:00
+ " ${fqdn}/remote.php/dav/files/${adminUser}/"
2023-12-30 18:51:55 +01:00
)
content = client.succeed(
2024-03-04 08:01:05 +01:00
"curl -f -s --location -X GET"
2023-12-30 18:51:55 +01:00
+ """ -H "Depth: 1" """
+ """ -u ${adminUser}:${adminPass} """
2024-03-04 08:01:05 +01:00
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
2023-12-30 18:51:55 +01:00
+ """ -T file """
2024-03-04 08:01:05 +01:00
+ " ${fqdn}/remote.php/dav/files/${adminUser}/file"
2023-12-30 18:51:55 +01:00
)
if content != "hello\n":
raise Exception("Got incorrect content for file, expected 'hello\n' but got:\n{}".format(content))
'';
2024-03-04 08:01:05 +01:00
in
{
2024-03-20 06:50:41 +01:00
basic = pkgs.testers.runNixOSTest {
2024-03-04 08:01:05 +01:00
name = "nextcloud-basic";
nodes.server = { config, pkgs, ... }: {
imports = [
2024-03-20 06:50:41 +01:00
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
2024-03-04 08:01:05 +01:00
{
options = {
shb.backup = lib.mkOption { type = lib.types.anything; };
shb.authelia = lib.mkOption { type = lib.types.anything; };
};
}
../../modules/services/nextcloud-server.nix
];
shb.nextcloud = {
enable = true;
domain = "example.com";
subdomain = "n";
dataDir = "/var/lib/nextcloud";
tracing = null;
defaultPhoneRegion = "US";
# This option is only needed because we do not access Nextcloud at the default port in the VM.
externalFqdn = "n.example.com:8080";
adminUser = adminUser;
adminPassFile = pkgs.writeText "adminPassFile" adminPass;
debug = true;
};
# Nginx port.
networking.firewall.allowedTCPPorts = [ 80 ];
# VM needs a bit more memory than default.
virtualisation.memorySize = 4096;
};
nodes.client = {};
testScript = commonTestScript;
};
2024-03-20 06:50:41 +01:00
cert = pkgs.testers.runNixOSTest {
2024-03-04 08:01:05 +01:00
name = "nextcloud-cert";
nodes.server = { config, pkgs, ... }: {
imports = [
2024-03-20 06:50:41 +01:00
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
2024-03-04 08:01:05 +01:00
{
options = {
shb.backup = lib.mkOption { type = lib.types.anything; };
shb.authelia = lib.mkOption { type = lib.types.anything; };
};
}
../../modules/blocks/nginx.nix
# ../../modules/blocks/postgresql.nix
../../modules/blocks/ssl.nix
../../modules/services/nextcloud-server.nix
];
shb.certs = {
cas.selfsigned.myca = {
name = "My CA";
};
certs.selfsigned = {
n = {
ca = config.shb.certs.cas.selfsigned.myca;
domain = "*.example.com";
group = "nginx";
};
};
};
systemd.services.nginx.after = [ config.shb.certs.certs.selfsigned.n.systemdService ];
systemd.services.nginx.requires = [ config.shb.certs.certs.selfsigned.n.systemdService ];
shb.nextcloud = {
enable = true;
domain = "example.com";
subdomain = "n";
dataDir = "/var/lib/nextcloud";
tracing = null;
defaultPhoneRegion = "US";
ssl = config.shb.certs.certs.selfsigned.n;
# This option is only needed because we do not access Nextcloud at the default port in the VM.
externalFqdn = "n.example.com:8080";
adminUser = adminUser;
adminPassFile = pkgs.writeText "adminPassFile" adminPass;
debug = true;
};
# Nginx port.
networking.firewall.allowedTCPPorts = [ 80 443 ];
shb.nginx.accessLog = true;
};
nodes.client = {};
# TODO: Test login
testScript = commonTestScript;
2023-12-30 18:51:55 +01:00
};
previewGenerator = pkgs.testers.runNixOSTest {
name = "nextcloud-previewGenerator";
nodes.server = { config, pkgs, ... }: {
imports = [
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
{
options = {
shb.backup = lib.mkOption { type = lib.types.anything; };
shb.authelia = lib.mkOption { type = lib.types.anything; };
};
}
../../modules/services/nextcloud-server.nix
];
systemd.tmpfiles.rules = [
"d '/srv/nextcloud' 0750 nextcloud nextcloud - -"
];
shb.nextcloud = {
enable = true;
domain = "example.com";
subdomain = "n";
dataDir = "/var/lib/nextcloud";
tracing = null;
defaultPhoneRegion = "US";
# This option is only needed because we do not access Nextcloud at the default port in the VM.
externalFqdn = "n.example.com:8080";
adminUser = adminUser;
adminPassFile = pkgs.writeText "adminPassFile" adminPass;
debug = true;
apps.previewgenerator.enable = true;
};
# Nginx port.
networking.firewall.allowedTCPPorts = [ 80 ];
# VM needs a bit more memory than default.
virtualisation.memorySize = 4096;
};
nodes.client = {};
testScript = { nodes, ... }:
''
import json
start_all()
server.wait_for_unit("phpfpm-nextcloud.service")
server.wait_for_unit("nginx.service")
server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.nextcloud.socket}")
def find_in_logs(unit, text):
return server.systemctl("status {}".format(unit))[1].find(text) != -1
def curl(target, format, endpoint, succeed=True):
return json.loads(target.succeed(
"curl --fail-with-body --silent --show-error --output /dev/null --location"
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ f" --write-out '{format}'"
+ " " + endpoint
))
with subtest("access"):
response = curl(client, """{"code":%{response_code}}""", "http://n.example.com")
if response['code'] != 200:
raise Exception(f"Code is {response['code']}")
'';
};
externalStorage = pkgs.testers.runNixOSTest {
name = "nextcloud-externalStorage";
nodes.server = { config, pkgs, ... }: {
imports = [
(pkgs'.path + "/nixos/modules/profiles/headless.nix")
(pkgs'.path + "/nixos/modules/profiles/qemu-guest.nix")
{
options = {
shb.backup = lib.mkOption { type = lib.types.anything; };
shb.authelia = lib.mkOption { type = lib.types.anything; };
};
}
../../modules/services/nextcloud-server.nix
];
systemd.tmpfiles.rules = [
"d '/srv/nextcloud' 0750 nextcloud nextcloud - -"
];
shb.nextcloud = {
enable = true;
domain = "example.com";
subdomain = "n";
dataDir = "/var/lib/nextcloud";
tracing = null;
defaultPhoneRegion = "US";
# This option is only needed because we do not access Nextcloud at the default port in the VM.
externalFqdn = "n.example.com:8080";
adminUser = adminUser;
adminPassFile = pkgs.writeText "adminPassFile" adminPass;
debug = true;
apps.externalStorage = {
enable = true;
userLocalMount.directory = "/srv/nextcloud/$user";
userLocalMount.mountName = "home";
};
};
# Nginx port.
networking.firewall.allowedTCPPorts = [ 80 ];
# VM needs a bit more memory than default.
virtualisation.memorySize = 4096;
};
nodes.client = {};
testScript = { nodes, ... }:
''
import json
start_all()
server.wait_for_unit("phpfpm-nextcloud.service")
server.wait_for_unit("nginx.service")
server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.nextcloud.socket}")
def find_in_logs(unit, text):
return server.systemctl("status {}".format(unit))[1].find(text) != -1
def curl(target, format, endpoint, succeed=True):
return json.loads(target.succeed(
"curl --fail-with-body --silent --show-error --output /dev/null --location"
+ " --connect-to n.example.com:443:server:443"
+ " --connect-to n.example.com:80:server:80"
+ f" --write-out '{format}'"
+ " " + endpoint
))
with subtest("access"):
response = curl(client, """{"code":%{response_code}}""", "http://n.example.com")
if response['code'] != 200:
raise Exception(f"Code is {response['code']}")
'';
};
2023-12-30 18:51:55 +01:00
}