233 lines
9.9 KiB
Nix
233 lines
9.9 KiB
Nix
{ system, inputs, pkgs, ... }:
|
|
let
|
|
lib = inputs.nixpkgs.lib;
|
|
clock = pkgs.writeShellScriptBin "date" ''
|
|
case "$*" in
|
|
+%s) echo 1700000000 ;;
|
|
"-u +%Y%m%dT%H%M%SZ") echo 20231114T221320Z ;;
|
|
*) exit 97 ;;
|
|
esac
|
|
'';
|
|
sleep = pkgs.writeShellScriptBin "sleep" ''
|
|
set -eu
|
|
now="$(cat /build/control/elapsed)"
|
|
echo "$((now + $1))" > /build/control/elapsed
|
|
'';
|
|
coreutils = pkgs.symlinkJoin {
|
|
name = "minecraft-backup-test-coreutils";
|
|
paths = [ pkgs.coreutils ];
|
|
postBuild = ''
|
|
rm "$out/bin/date"
|
|
ln -s ${clock}/bin/date "$out/bin/date"
|
|
rm "$out/bin/sleep"
|
|
ln -s ${sleep}/bin/sleep "$out/bin/sleep"
|
|
'';
|
|
};
|
|
systemctl = pkgs.writeShellScriptBin "systemctl" ''
|
|
set -eu
|
|
case "$1" in
|
|
is-active) test "$(cat /build/control/state)" = active ;;
|
|
stop)
|
|
cat /build/control/elapsed > /build/control/stopped-at
|
|
echo inactive > /build/control/state
|
|
echo "''${STOP_RESULT:-success}" > /build/control/result
|
|
;;
|
|
start) echo active > /build/control/state ;;
|
|
show) cat /build/control/result ;;
|
|
*) exit 99 ;;
|
|
esac
|
|
'';
|
|
announce = pkgs.writeShellScript "minecraft-backup-test-announce" ''
|
|
set -eu
|
|
test "$(cat /build/control/state)" = active
|
|
number="$(($(wc -l < /build/control/messages) + 1))"
|
|
test "''${FAIL_ANNOUNCEMENT:-0}" != "$number" || exit 33
|
|
${pkgs.jq}/bin/jq -cn \
|
|
--arg message "$1" --argjson at "$(cat /build/control/elapsed)" \
|
|
'{at: $at, message: $message}' >> /build/control/messages
|
|
'';
|
|
rsync = pkgs.writeShellScriptBin "rsync" ''
|
|
set -eu
|
|
test "$(cat /build/control/state)" = inactive
|
|
test "''${FAIL_COPY:-0}" = 0 || exit 31
|
|
exec ${pkgs.rsync}/bin/rsync "$@"
|
|
'';
|
|
aws = pkgs.writeShellScriptBin "aws" ''
|
|
set -eu
|
|
case "$1 $2" in
|
|
"s3 cp")
|
|
test "''${FAIL_UPLOAD:-0}" = 0 || exit 32
|
|
cp "$3" "/build/bucket/''${3##*/}"
|
|
;;
|
|
"s3api list-objects-v2")
|
|
cat /build/objects.json
|
|
;;
|
|
"s3api delete-objects")
|
|
shift 2
|
|
while [ "$1" != --delete ]; do shift; done
|
|
manifest="''${2#file://}"
|
|
if [ "''${FAIL_DELETE:-0}" = 1 ]; then
|
|
echo '{"Errors":[{"Code":"AccessDenied"}]}'
|
|
else
|
|
${pkgs.jq}/bin/jq -r '.Objects[].Key' "$manifest" |
|
|
while IFS= read -r key; do rm -f "/build/bucket/''${key##*/}"; done
|
|
echo '{"Errors":null}'
|
|
fi
|
|
;;
|
|
*) exit 98 ;;
|
|
esac
|
|
'';
|
|
mkConfig = announcementCommand: (lib.nixosSystem {
|
|
inherit system;
|
|
inherit pkgs;
|
|
modules = [
|
|
inputs.nix-minecraft.nixosModules.minecraft-servers
|
|
({ config, lib, ... }: import ../../nixos/module/generic/minecraft-backups.nix {
|
|
inherit config lib;
|
|
pkgs = pkgs // { systemd = systemctl; inherit rsync coreutils; awscli2 = aws; };
|
|
})
|
|
{
|
|
system.stateVersion = "25.05";
|
|
services.minecraft-servers = {
|
|
enable = true;
|
|
eula = true;
|
|
servers.auto = { enable = true; package = pkgs.hello; };
|
|
servers.disabled = { enable = false; package = pkgs.hello; };
|
|
};
|
|
services.minecraft-backups = {
|
|
enable = true;
|
|
stateDir = "/build/backups";
|
|
s3 = {
|
|
bucket = "test";
|
|
endpoint = "https://s3.invalid";
|
|
region = "test";
|
|
prefix = "minecraft";
|
|
credentialsFile = "/run/test-credentials";
|
|
};
|
|
servers.test = {
|
|
directory = "/build/source";
|
|
service = "minecraft-test.service";
|
|
inherit announcementCommand;
|
|
};
|
|
};
|
|
}
|
|
];
|
|
}).config;
|
|
cfg = mkConfig null;
|
|
service = cfg.systemd.services.minecraft-backup-test;
|
|
announcedService = (mkConfig announce).systemd.services.minecraft-backup-test;
|
|
runCase = name: test: pkgs.runCommand "minecraft-backup-${name}" {
|
|
nativeBuildInputs = [ pkgs.jq pkgs.gnutar pkgs.zstd ];
|
|
} ''
|
|
set -eu
|
|
mkdir -p /build/source/world /build/control /build/bucket /build/backups/test
|
|
echo active > /build/control/state
|
|
echo success > /build/control/result
|
|
echo 0 > /build/control/elapsed
|
|
: > /build/control/messages
|
|
echo world-data > /build/source/world/level.dat
|
|
echo secret > /build/source/server.properties
|
|
touch /build/bucket/minecraft-test-20200101T000000Z.tar.zst /build/bucket/manual.tar.zst
|
|
touch /build/bucket/minecraft-test-20990101T000000Z.tar.zst /build/bucket/minecraft-other-20200101T000000Z.tar.zst
|
|
echo '{"Contents":[
|
|
{"Key":"minecraft/test/minecraft-test-20200101T000000Z.tar.zst","LastModified":"2020-01-01T00:00:00Z"},
|
|
{"Key":"minecraft/test/manual.tar.zst","LastModified":"2020-01-01T00:00:00Z"},
|
|
{"Key":"minecraft/test/minecraft-test-20990101T000000Z.tar.zst","LastModified":"2099-01-01T00:00:00Z"},
|
|
{"Key":"minecraft/other/minecraft-other-20200101T000000Z.tar.zst","LastModified":"2020-01-01T00:00:00Z"}
|
|
]}' > /build/objects.json
|
|
export AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test
|
|
${test}
|
|
touch "$out"
|
|
'';
|
|
in {
|
|
minecraft-backup-enabled-server-timers =
|
|
assert cfg.services.minecraft-backups.servers.auto.directory == "/srv/minecraft/auto";
|
|
assert cfg.services.minecraft-backups.servers.auto.service == "minecraft-server-auto.service";
|
|
assert builtins.filter (name: lib.hasPrefix "minecraft-backup-" name)
|
|
(builtins.attrNames cfg.systemd.timers) == [ "minecraft-backup-auto" "minecraft-backup-test" ];
|
|
assert cfg.systemd.timers.minecraft-backup-auto.timerConfig.OnCalendar == "*-*-* 00/6:00:00";
|
|
pkgs.runCommand "minecraft-backup-enabled-server-timers" {} ''touch "$out"'';
|
|
minecraft-backup-upload-and-retention = runCase "upload-and-retention" ''
|
|
# A warm staging copy can have stale bytes despite identical size and mtime.
|
|
mkdir -p /build/backups/test/staging/world
|
|
printf 'stale-data\n' > /build/backups/test/staging/world/level.dat
|
|
touch -r /build/source/world/level.dat /build/backups/test/staging/world/level.dat
|
|
${service.serviceConfig.ExecStart}
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/restart-needed
|
|
test ! -e /build/bucket/minecraft-test-20200101T000000Z.tar.zst
|
|
test -e /build/bucket/manual.tar.zst
|
|
test -e /build/bucket/minecraft-test-20990101T000000Z.tar.zst
|
|
test -e /build/bucket/minecraft-other-20200101T000000Z.tar.zst
|
|
test -z "$(find /build/backups/test/archive -name '*.tar.zst' -print)"
|
|
mkdir /build/restored
|
|
tar --zstd -xf /build/bucket/minecraft-test-20231114T221320Z.tar.zst -C /build/restored
|
|
cmp /build/source/world/level.dat /build/restored/world/level.dat
|
|
test ! -e /build/restored/server.properties
|
|
'';
|
|
minecraft-backup-copy-failure-resumes-server = runCase "copy-failure-resumes-server" ''
|
|
if FAIL_COPY=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/restart-needed
|
|
'';
|
|
minecraft-backup-upload-failure-keeps-archive = runCase "upload-failure-keeps-archive" ''
|
|
if FAIL_UPLOAD=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test "$(find /build/backups/test/archive -name '*.tar.zst' | wc -l)" -eq 1
|
|
test "$(stat -c %a /build/backups/test/archive/*.tar.zst)" = 600
|
|
# A subsequent copy failure still allows the pending archive to be uploaded first.
|
|
if FAIL_COPY=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test -z "$(find /build/backups/test/archive -name '*.tar.zst' -print)"
|
|
test -e /build/bucket/minecraft-test-20231114T221320Z.tar.zst
|
|
'';
|
|
minecraft-backup-inactive-server-stays-inactive = runCase "inactive-server-stays-inactive" ''
|
|
echo inactive > /build/control/state
|
|
${service.serviceConfig.ExecStart}
|
|
test "$(cat /build/control/state)" = inactive
|
|
'';
|
|
minecraft-backup-unclean-stop-refuses-snapshot = runCase "unclean-stop-refuses-snapshot" ''
|
|
if STOP_RESULT=timeout ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/staging/world/level.dat
|
|
'';
|
|
minecraft-backup-timeout-resumes-server = runCase "timeout-resumes-server" ''
|
|
echo inactive > /build/control/state
|
|
touch /build/backups/test/restart-needed
|
|
${service.serviceConfig.ExecStopPost}
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/backups/test/restart-needed
|
|
'';
|
|
minecraft-backup-delete-errors-fail-service = runCase "delete-errors-fail-service" ''
|
|
if FAIL_DELETE=1 ${service.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test -e /build/bucket/minecraft-test-20200101T000000Z.tar.zst
|
|
'';
|
|
minecraft-backup-announces-before-stop = runCase "announces-before-stop" ''
|
|
${announcedService.serviceConfig.ExecStart}
|
|
jq -s -e 'map(.at) == [0, 240, 300]
|
|
and all(.[]; (.message | length) > 0)' /build/control/messages
|
|
test "$(cat /build/control/stopped-at)" = 300
|
|
test "$(cat /build/control/state)" = active
|
|
test -e /build/bucket/minecraft-test-20231114T221320Z.tar.zst
|
|
'';
|
|
minecraft-backup-inactive-skips-announcements = runCase "inactive-skips-announcements" ''
|
|
echo inactive > /build/control/state
|
|
${announcedService.serviceConfig.ExecStart}
|
|
test ! -s /build/control/messages
|
|
test "$(cat /build/control/elapsed)" = 0
|
|
test ! -e /build/control/stopped-at
|
|
test "$(cat /build/control/state)" = inactive
|
|
'';
|
|
minecraft-backup-announcement-failure-keeps-server = runCase "announcement-failure-keeps-server" ''
|
|
for stage in 1 2 3; do
|
|
: > /build/control/messages
|
|
echo 0 > /build/control/elapsed
|
|
if FAIL_ANNOUNCEMENT="$stage" ${announcedService.serviceConfig.ExecStart}; then exit 1; fi
|
|
test "$(cat /build/control/state)" = active
|
|
test ! -e /build/control/stopped-at
|
|
test ! -e /build/backups/test/restart-needed
|
|
test ! -e /build/backups/test/staging/world/level.dat
|
|
done
|
|
'';
|
|
}
|