"""get_running_backups: the host's own file-level backups that a restart would abort. A restic, borg, borgmatic, duplicity ... run -- from a systemd timer, a cron line or by hand -- is a process of a backup tool. The driver reads the processes, the host's clock and each process's cgroup in one round trip, and names a backup by the systemd service it runs in, which is the scheduled job netOrk shows for the timer (NetOrk/netork#2318, #1933). A failed read raises: an empty list would say nothing runs. """ from __future__ import annotations from unittest.mock import MagicMock import pytest from napalm_linux import LinuxDriver from napalm_linux._running_backups import RUNNING_BACKUPS_COMMAND, parse_running_backups NOW = 1793664000 def _reading(*rows: str, now: int = NOW, end: bool = True) -> str: lines = [f"NOW {now}", *rows] if end: lines.append("END") return "\n".join(lines) def _row(pid: int, elapsed: int, cgroup: str, args: str) -> str: return f"{pid}\t{elapsed}\t{cgroup}\t{args}" def test_nothing_runs(): out = _reading( _row(1, 99999, "0::/init.scope", "/sbin/init"), _row(812, 3600, "0::/system.slice/ssh.service", "sshd: /usr/sbin/sshd -D"), ) assert parse_running_backups(out) == [] def test_a_timers_restic_backup_is_named_by_its_service(): out = _reading( _row(4242, 600, "0::/system.slice/restic-backup.service", "/usr/bin/restic backup /srv") ) assert parse_running_backups(out) == [ { "kind": "backup", "job": "restic-backup.service", "task": "4242", "guests": [], "since": NOW - 600, } ] def test_a_cron_run_has_no_job_of_its_own(): out = _reading(_row(77, 30, "0::/system.slice/cron.service", "borg create ::{now} /etc")) [backup] = parse_running_backups(out) assert backup["job"] is None assert backup["kind"] == "backup" def test_cgroup_v1_is_read_too(): out = _reading(_row(9, 5, "1:name=systemd:/system.slice/borgmatic.service", "borgmatic")) assert parse_running_backups(out)[0]["job"] == "borgmatic.service" @pytest.mark.parametrize( ("args", "kind"), [ ("/usr/bin/restic backup --files-from /etc/restic/files", "backup"), ("restic forget --prune --keep-daily 7", "prune"), ("restic -r s3:example check", "verify"), ("restic copy --from-repo /srv/a", "sync"), ("/usr/bin/borg create --stats ::{now} /home", "backup"), ("borg --lock-wait 60 prune --keep-daily 7", "prune"), ("borg compact", "prune"), ("borg check --verify-data", "verify"), ("borg serve --restrict-to-path /srv/borg", "backup"), ("/usr/bin/python3 /usr/bin/borgmatic --verbosity 1", "backup"), ("duplicity incremental /home file:///mnt/b", "backup"), ("/bin/bash /usr/bin/duply home backup", "backup"), ("rsnapshot daily", "backup"), ("rdiff-backup /home /mnt/b", "backup"), ("proxmox-backup-client backup root.pxar:/", "backup"), ("kopia snapshot create /home", "backup"), ("btrbk run", "backup"), ("resticprofile backup", "backup"), ("autorestic backup -a", "backup"), ], ) def test_the_tools_and_what_they_do(args, kind): [backup] = parse_running_backups(_reading(_row(1, 1, "0::/user.slice", args))) assert backup["kind"] == kind @pytest.mark.parametrize( "args", [ "restic mount /mnt/restic", "restic snapshots", "restic restore latest --target /", "borg mount ::archive /mnt", "borg list", "kopia server start", "vim /etc/restic/backup.sh", "grep borg /var/log/syslog", ], ) def test_what_is_no_backup(args): assert parse_running_backups(_reading(_row(1, 1, "0::/user.slice", args))) == [] def test_a_session_or_scope_is_no_job(): out = _reading(_row(5, 1, "0::/user.slice/user-1000.slice/session-3.scope", "restic backup /")) assert parse_running_backups(out)[0]["job"] is None def test_a_reading_cut_short_raises(): with pytest.raises(RuntimeError): parse_running_backups(_reading(_row(1, 1, "", "restic backup /"), end=False)) def test_no_clock_raises(): with pytest.raises(RuntimeError): parse_running_backups("END") def test_the_driver_reads_as_the_login_user_in_one_round_trip(): drv = LinuxDriver.__new__(LinuxDriver) drv._send = MagicMock(return_value=_reading()) assert drv.get_running_backups() == [] drv._send.assert_called_once() assert drv._send.call_args.args[0] == RUNNING_BACKUPS_COMMAND def test_the_command_says_when_ps_failed(): """Busybox's ps knows no etimes: the reading must not end as if nothing ran.""" assert "|| exit" in RUNNING_BACKUPS_COMMAND assert RUNNING_BACKUPS_COMMAND.rstrip().endswith("echo END")