1#!/usr/bin/env python3
2from contextlib import ExitStack, nullcontext
3import json
4import os
5from pathlib import Path
6import subprocess
7import tempfile
8import unittest
9from unittest.mock import patch
10
11import data
12import snowglobe
13
14
15class SnapshotPolicy(unittest.TestCase):
16 def test_pkl_policy_reaches_existing_and_new_datasets(self):
17 with tempfile.TemporaryDirectory(prefix="snowglobe-snapshot-policy-") as temporary:
18 root = Path(temporary)
19 fixture = root / "service.pkl"
20 for declaration, expected in (("", True), ("snapshots = false", False)):
21 fixture.write_text(f'''amends {json.dumps((snowglobe.REPO / "config/Service.pkl").as_uri())}
22name = "Fixture"
23container {{ image = "fixture"; volumes {{ ["/cache"] {{}} }} }}
24{declaration}
25''')
26 rendered = json.loads(subprocess.check_output([
27 os.environ.get("PKL", "pkl"), "eval", "-p", "serviceId=fixture", "-p", "uid=3132",
28 "-p", f"root={root}", "-p", "pool=fixture", str(fixture),
29 ], text=True))
30 self.assertIs(rendered["snapshots"], expected)
31 for exists in (True, False):
32 with self.subTest(declaration=declaration, exists=exists), patch.object(
33 snowglobe, "check_pool",
34 ), patch.object(snowglobe, "STATE", root), patch.object(
35 snowglobe.subprocess, "run", return_value=subprocess.CompletedProcess([], 0 if exists else 1),
36 ), patch.object(snowglobe, "command") as command:
37 snowglobe.ensure_pool([rendered])
38 self.assertEqual(command.call_args.args, (
39 "zfs", "set", f"snowglobe:snapshots={str(expected).lower()}", "fixture/prod/fixture",
40 ))
41 self.assertEqual(command.call_count, 1 if exists else 3)
42 rendered["hasManagedVolumes"] = False
43 with patch.object(snowglobe, "check_pool"), patch.object(
44 snowglobe, "STATE", root,
45 ), patch.object(snowglobe, "command") as command:
46 snowglobe.ensure_pool([rendered])
47 command.assert_not_called()
48
49 def test_backup_excludes_cache_and_preserves_database_recovery(self):
50 for policy, database in (("-", False), ("true", False), ("false", False), ("false", True)):
51 with self.subTest(policy=policy, database=database), tempfile.TemporaryDirectory(
52 prefix="snowglobe-backup-policy-",
53 ) as temporary, ExitStack() as stack:
54 root = Path(temporary)
55 (root / "managed-jobs.json").write_text('["cache", "files", "postgres"]')
56 snapshots = []
57
58 def run(*args, **kwargs):
59 if args[:2] == ("zfs", "get"):
60 value = "true" if args[-1].endswith("/files") else policy
61 return subprocess.CompletedProcess(args, 0, value + "\n")
62 if args[:2] == ("zfs", "snapshot"):
63 snapshots.extend(args[2:])
64 elif args[:4] == ("nomad", "operator", "snapshot", "save"):
65 Path(args[4]).write_bytes(b"synthetic Nomad state")
66 elif args[-2:] == ("-c", "SELECT extname FROM pg_extension WHERE extname <> 'plpgsql' ORDER BY extname"):
67 return subprocess.CompletedProcess(args, 0, "")
68 else:
69 self.fail(f"Unexpected command: {args}")
70 return subprocess.CompletedProcess(args, 0)
71
72 for name, value in {
73 "STATE": root, "BACKUPS": root / "backups", "run": run,
74 "current_release": lambda: root / ("a" * 16),
75 "dataset_for": lambda name: "fixture/prod/" + name,
76 "database_inputs": lambda name: {"db": ("fixture", "svc_fixture")} if database and name == "cache" else {},
77 "cloned_postgres": lambda *args: nullcontext("fixture-postgres"),
78 "database_dump": lambda container, name, destination, extensions: destination.write_bytes(b"synthetic database"),
79 }.items():
80 stack.enter_context(patch.object(data, name, value))
81 backup_id = data.backup("a" * 16, "b" * 16)
82 manifest = json.loads((data.BACKUPS / backup_id / "manifest.json").read_text())
83 expected = {"fixture/prod/files"}
84 if policy != "false":
85 expected |= {"fixture/prod/cache", "fixture/prod/postgres"}
86 elif database:
87 expected.add("fixture/prod/postgres")
88 self.assertEqual({item.split("@", 1)[0] for item in snapshots}, expected)
89 services = {"files"}
90 if policy != "false":
91 services |= {"cache", "postgres"}
92 elif database:
93 services.add("cache")
94 self.assertEqual(set(manifest["services"]), services)
95 if database:
96 entry = manifest["services"]["cache"]
97 self.assertNotIn("dataset", entry)
98 dump = entry["databases"]["db"]
99 self.assertEqual(dump["sha256"], data.checksum(data.BACKUPS / backup_id / dump["dump"]))
100
101
102if __name__ == "__main__":
103 unittest.main()