def test_does_not_remove_the_last_snapshot_left(): subprocess.call("zfs destroy -r data/src", shell=True) subprocess.check_call("zfs create data/src", shell=True) subprocess.check_call("zfs snapshot data/src@2020-05-07_00-00", shell=True) subprocess.check_call("zfs snapshot data/src@2020-05-23_00-00", shell=True) data = yaml.safe_load( textwrap.dedent("""\ timezone: "UTC" periodic-snapshot-tasks: src: dataset: data/src recursive: false naming-schema: "%Y-%m-%d_%H-%M" schedule: minute: "*" hour: "*" day-of-month: "*" month: "*" day-of-week: "*" lifetime: P30D """)) definition = Definition.from_data(data) local_shell = LocalShell() zettarepl = Zettarepl(Mock(), local_shell) zettarepl.set_tasks(definition.tasks) zettarepl._run_local_retention(datetime(2020, 6, 25, 0, 0)) assert list_snapshots(local_shell, "data/src", False) == [Snapshot("data/src", "2020-05-23_00-00")]
def test_hold_pending_snapshots(hold_pending_snapshots, remains): subprocess.call("zfs destroy -r data/src", shell=True) subprocess.call("zfs destroy -r data/dst", shell=True) subprocess.check_call("zfs create data/src", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_00-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_01-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_02-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_03-00", shell=True) subprocess.check_call("zfs create data/dst", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_00-00", shell=True) definition = Definition.from_data( yaml.load( textwrap.dedent("""\ timezone: "UTC" periodic-snapshot-tasks: - id: src dataset: data/src recursive: true lifetime: PT1H naming-schema: "%Y-%m-%d_%H-%M" schedule: minute: "0" replication-tasks: - id: src direction: push transport: type: local source-dataset: data/src target-dataset: data/dst recursive: true periodic-snapshot-tasks: - src auto: true retention-policy: source hold-pending-snapshots: """ + yaml.dump(hold_pending_snapshots) + """ """))) local_shell = LocalShell() zettarepl = Zettarepl(Mock(), local_shell) zettarepl.set_tasks(definition.tasks) zettarepl._run_local_retention(datetime(2018, 10, 1, 3, 0)) assert list_snapshots(local_shell, "data/src", False) == remains
def test_does_not_remove_the_last_snapshot_left(snapshots__removal_dates__result): snapshots, removal_dates, result = snapshots__removal_dates__result subprocess.call("zfs destroy -r data/src", shell=True) subprocess.call("zfs destroy -r data/src2", shell=True) subprocess.check_call("zfs create data/src", shell=True) subprocess.check_call("zfs create data/src/child", shell=True) subprocess.check_call("zfs create data/src2", shell=True) for snapshot in snapshots: subprocess.check_call(f"zfs snapshot {snapshot}", shell=True) data = yaml.safe_load(textwrap.dedent("""\ timezone: "UTC" periodic-snapshot-tasks: src: dataset: data/src recursive: false naming-schema: "%Y-%m-%d-%H-%M" schedule: minute: "*" hour: "*" day-of-month: "*" month: "*" day-of-week: "*" lifetime: P30D """)) definition = Definition.from_data(data) local_shell = LocalShell() zettarepl = Zettarepl(Mock(), local_shell, use_removal_dates=True) zettarepl.set_tasks(definition.tasks) with patch("zettarepl.zettarepl.get_removal_dates", Mock(return_value=removal_dates)): zettarepl._run_local_retention(datetime(2021, 4, 19, 17, 0)) assert list_snapshots(local_shell, "data/src", False) + list_snapshots(local_shell, "data/src2", False) == [ snapshots[i] for i in result ]
def test_hold_pending_snapshots(retention_policy, remains): subprocess.call("zfs destroy -r data/src", shell=True) subprocess.call("zfs destroy -r data/dst", shell=True) subprocess.check_call("zfs create data/src", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_01-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_02-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_03-00", shell=True) subprocess.check_call("zfs create data/dst", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_00-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_01-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_02-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_03-00", shell=True) data = yaml.safe_load( textwrap.dedent("""\ timezone: "UTC" replication-tasks: src: direction: pull transport: type: local source-dataset: data/src target-dataset: data/dst naming-schema: "%Y-%m-%d_%H-%M" recursive: true auto: false """)) data["replication-tasks"]["src"].update(**retention_policy) definition = Definition.from_data(data) local_shell = LocalShell() zettarepl = Zettarepl(Mock(), local_shell) zettarepl.set_tasks(definition.tasks) zettarepl._run_local_retention(datetime(2018, 10, 1, 3, 0)) assert list_snapshots(local_shell, "data/dst", False) == remains
def test_hold_pending_snapshots__does_not_delete_orphan_snapshots(): subprocess.call("zfs destroy -r data/src", shell=True) subprocess.call("zfs destroy -r data/dst", shell=True) subprocess.check_call("zfs create data/src", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_00-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_01-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_02-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-01_03-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-02_00-00", shell=True) subprocess.check_call("zfs snapshot data/src@2018-10-03_00-00", shell=True) subprocess.check_call("zfs create data/dst", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_00-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_01-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_02-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-01_03-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-02_00-00", shell=True) subprocess.check_call("zfs snapshot data/dst@2018-10-03_00-00", shell=True) definition = Definition.from_data( yaml.safe_load( textwrap.dedent("""\ timezone: "UTC" periodic-snapshot-tasks: src: dataset: data/src recursive: true lifetime: PT48H naming-schema: "%Y-%m-%d_%H-%M" schedule: minute: "0" hour: "0" replication-tasks: src: direction: push transport: type: local source-dataset: data/src target-dataset: data/dst recursive: true periodic-snapshot-tasks: - src auto: true retention-policy: source hold-pending-snapshots: true """))) local_shell = LocalShell() zettarepl = Zettarepl(Mock(), local_shell) zettarepl.set_tasks(definition.tasks) zettarepl._run_local_retention(datetime(2018, 10, 4, 0, 0)) assert list_snapshots(local_shell, "data/src", False) == [ Snapshot("data/src", "2018-10-01_01-00"), Snapshot("data/src", "2018-10-01_02-00"), Snapshot("data/src", "2018-10-01_03-00"), Snapshot("data/src", "2018-10-02_00-00"), Snapshot("data/src", "2018-10-03_00-00"), ]