|
| 1 | +"""Retry selection must cover discovered and stale groups without pruning history.""" |
| 2 | +import importlib.machinery |
| 3 | +import importlib.util |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import sys |
| 8 | +import tempfile |
| 9 | +import time |
| 10 | +import types |
| 11 | +import unittest |
| 12 | +from unittest.mock import patch |
| 13 | + |
| 14 | +ROOT = Path(__file__).parent |
| 15 | + |
| 16 | + |
| 17 | +def load_backup(): |
| 18 | + dependency = types.ModuleType('app_backup_retention') |
| 19 | + dependency.ResticArchive = lambda _: None |
| 20 | + dependency.digest = lambda _: 'fixture' |
| 21 | + dependency.STAGING_MARKER = '.host-backup-managed' |
| 22 | + spec = importlib.util.spec_from_file_location('retry_fixture', ROOT / 'host-backup.py') |
| 23 | + module = importlib.util.module_from_spec(spec) |
| 24 | + with patch.dict(sys.modules, {'app_backup_retention': dependency}), patch.object(importlib.machinery.SourceFileLoader, 'load_module', return_value=types.SimpleNamespace()): |
| 25 | + spec.loader.exec_module(module) |
| 26 | + return module |
| 27 | + |
| 28 | + |
| 29 | +class RetryTests(unittest.TestCase): |
| 30 | + def test_only_fresh_verified_backups_can_be_skipped(self): |
| 31 | + backup = load_backup() |
| 32 | + now = 2_000_000 |
| 33 | + fresh = {'timestamp': now - 60, 'restore_verified': True} |
| 34 | + for label, state, due in [ |
| 35 | + ('new discovery', {}, True), |
| 36 | + ('one provider missing', {'aws': fresh}, True), |
| 37 | + ('both verified', {'aws': fresh, 'hetzner': fresh}, False), |
| 38 | + ('explicit failure', {'aws': fresh, 'hetzner': fresh, 'error': 'failed'}, True), |
| 39 | + ('stale provider', {'aws': fresh, 'hetzner': {**fresh, 'timestamp': now - 36 * 3600 - 1}}, True), |
| 40 | + ('unverified restore', {'aws': fresh, 'hetzner': {**fresh, 'restore_verified': False}}, True), |
| 41 | + ('future timestamp', {'aws': fresh, 'hetzner': {**fresh, 'timestamp': now + 1}}, True), |
| 42 | + ]: |
| 43 | + with self.subTest(label=label): |
| 44 | + self.assertEqual(backup.project_backup_due(state, ['aws', 'hetzner'], now), due) |
| 45 | + self.assertEqual(backup.all_projects_recovered({'projects': {'app': {}}, 'repositories': ['aws', 'hetzner']}, {'projects': {'app': state}}, now), not due) |
| 46 | + self.assertFalse(backup.all_projects_recovered({'projects': {'new-app': {}}, 'repositories': ['aws', 'hetzner']}, {'projects': {}}, now)) |
| 47 | + |
| 48 | + @unittest.skipUnless(os.geteuid() == 0, 'real root-owned staging integration') |
| 49 | + def test_retry_creates_missing_and_stale_backups_and_preserves_every_snapshot(self): |
| 50 | + backup = load_backup() |
| 51 | + now = int(time.time()) |
| 52 | + fresh = {name: {'timestamp': now, 'restore_verified': True} for name in ['aws', 'hetzner']} |
| 53 | + stale = {name: {**value, 'timestamp': now - 37 * 3600} for name, value in fresh.items()} |
| 54 | + calls, archived = [], [] |
| 55 | + backup.ResticArchive = lambda _: types.SimpleNamespace(run=lambda *args: archived.append(args)) |
| 56 | + backup.command = lambda args: None |
| 57 | + def recover(project, *_): |
| 58 | + calls.append(project) |
| 59 | + return fresh, None |
| 60 | + backup.project_backup = recover |
| 61 | + with tempfile.TemporaryDirectory() as directory: |
| 62 | + root = Path(directory) |
| 63 | + config = root / 'config.json' |
| 64 | + config.write_text(json.dumps({'repositories': {'aws': '/unused', 'hetzner': '/unused'}, 'projects': {name: {} for name in ['fresh', 'new', 'stale', 'failed']}, 'staging': str(root / 'stage')})) |
| 65 | + backup.STATE = root / 'state.json' |
| 66 | + backup.STATE.write_text(json.dumps({'projects': {'fresh': fresh, 'stale': stale, 'failed': {**fresh, 'error': 'previous failure'}}})) |
| 67 | + real_open = open |
| 68 | + def locked_open(file, *args, **kwargs): |
| 69 | + return real_open(root / 'lock' if file == '/run/lock/host-backup.lock' else file, *args, **kwargs) |
| 70 | + with patch('sys.argv', ['host-backup.py', '--config', str(config), '--retry-failed', '--preserve-backups']), patch('builtins.open', side_effect=locked_open): |
| 71 | + backup.main() |
| 72 | + self.assertEqual(calls, ['new', 'stale', 'failed']) |
| 73 | + self.assertEqual(archived, [], 'retry must never forget or prune an existing snapshot') |
| 74 | + self.assertNotIn('error', json.loads(backup.STATE.read_text())['projects']['failed']) |
| 75 | + backup.main() |
| 76 | + self.assertEqual(calls, ['new', 'stale', 'failed'], 'fresh recovered groups must not be repeated') |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + unittest.main() |
0 commit comments