vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / restic-rest-server.nix
blob1d38ddbe513c96c3606217b3e48a29ca217d4597
1 import ./make-test-python.nix (
2   { pkgs, ... }:
4   let
5     remoteRepository = "rest:http://restic_rest_server:8001/";
7     backupPrepareCommand = ''
8       touch /root/backupPrepareCommand
9       test ! -e /root/backupCleanupCommand
10     '';
12     backupCleanupCommand = ''
13       rm /root/backupPrepareCommand
14       touch /root/backupCleanupCommand
15     '';
17     testDir = pkgs.stdenvNoCC.mkDerivation {
18       name = "test-files-to-backup";
19       unpackPhase = "true";
20       installPhase = ''
21         mkdir $out
22         echo some_file > $out/some_file
23         echo some_other_file > $out/some_other_file
24         mkdir $out/a_dir
25         echo a_file > $out/a_dir/a_file
26       '';
27     };
29     passwordFile = "${pkgs.writeText "password" "correcthorsebatterystaple"}";
30     paths = [ "/opt" ];
31     exclude = [ "/opt/excluded_file_*" ];
32     pruneOpts = [
33       "--keep-daily 2"
34       "--keep-weekly 1"
35       "--keep-monthly 1"
36       "--keep-yearly 99"
37     ];
38   in
39   {
40     name = "restic-rest-server";
42     nodes = {
43       restic_rest_server = {
44         services.restic.server = {
45           enable = true;
46           extraFlags = [ "--no-auth" ];
47           listenAddress = "8001";
48         };
49         networking.firewall.allowedTCPPorts = [ 8001 ];
50       };
51       server = {
52         services.restic.backups = {
53           remotebackup = {
54             inherit passwordFile paths exclude pruneOpts backupPrepareCommand backupCleanupCommand;
55             repository = remoteRepository;
56             initialize = true;
57             timerConfig = null; # has no effect here, just checking that it doesn't break the service
58           };
59           remoteprune = {
60             inherit passwordFile;
61             repository = remoteRepository;
62             pruneOpts = [ "--keep-last 1" ];
63           };
64         };
65       };
66     };
68     testScript = ''
69       restic_rest_server.start()
70       server.start()
71       restic_rest_server.wait_for_unit("restic-rest-server.socket")
72       restic_rest_server.wait_for_open_port(8001)
73       server.wait_for_unit("dbus.socket")
74       server.fail(
75           "restic-remotebackup snapshots",
76       )
77       server.succeed(
78           # set up
79           "cp -rT ${testDir} /opt",
80           "touch /opt/excluded_file_1 /opt/excluded_file_2",
82           # test that remotebackup runs custom commands and produces a snapshot
83           "timedatectl set-time '2016-12-13 13:45'",
84           "systemctl start restic-backups-remotebackup.service",
85           "rm /root/backupCleanupCommand",
86           'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
88           # test that restoring that snapshot produces the same directory
89           "mkdir /tmp/restore-1",
90           "restic-remotebackup restore latest -t /tmp/restore-1",
91           "diff -ru ${testDir} /tmp/restore-1/opt",
93           # test that we can create four snapshots in remotebackup and rclonebackup
94           "timedatectl set-time '2017-12-13 13:45'",
95           "systemctl start restic-backups-remotebackup.service",
96           "rm /root/backupCleanupCommand",
98           "timedatectl set-time '2018-12-13 13:45'",
99           "systemctl start restic-backups-remotebackup.service",
100           "rm /root/backupCleanupCommand",
102           "timedatectl set-time '2018-12-14 13:45'",
103           "systemctl start restic-backups-remotebackup.service",
104           "rm /root/backupCleanupCommand",
106           "timedatectl set-time '2018-12-15 13:45'",
107           "systemctl start restic-backups-remotebackup.service",
108           "rm /root/backupCleanupCommand",
110           "timedatectl set-time '2018-12-16 13:45'",
111           "systemctl start restic-backups-remotebackup.service",
112           "rm /root/backupCleanupCommand",
114           'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 4"',
116           # test that remoteprune brings us back to 1 snapshot in remotebackup
117           "systemctl start restic-backups-remoteprune.service",
118           'restic-remotebackup snapshots --json | ${pkgs.jq}/bin/jq "length | . == 1"',
119       )
120     '';
121   }