Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_backup_completeness.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 18 minutes ago History
test_backup_completeness.py · 5.5 KB · 121 lines
1"""A backup holds what docs/http-api-contract.md ("Backups") says it holds.
2
3Found by simulation: the contract said "everything -- records, identity,
4credentials, service keys", the archive omitted user_files (the bytes
5behind every `files` record), the permissions policy and identity, and a
6restored file record answered 404 "File bytes missing" while `verify`
7said ok.
8"""
9
10import tarfile
11from pathlib import Path
12
13import object_backup
14from test_object_backup import archive_names, make_runtime_tree, write_file
15
16FILES_TSV = ("id\tname\towner_id\n"
17 "f1\tphoto.png\tada\n"
18 "f2\tlost.png\tada\n")
19
20
21def _tree(tmp_path):
22 objects_dir, data_dir = make_runtime_tree(tmp_path)
23 write_file(data_dir / "collections" / "files" / "records.tsv", FILES_TSV)
24 write_file(data_dir / "user_files" / "ada" / "f1", b"\x89PNG bytes")
25 write_file(data_dir / "permissions" / "policy.json", '{"rules": []}\n')
26 write_file(data_dir / "identity" / "users.tsv",
27 "user_id\taccount_id\temail\tdisplay_name\tstatus\troles\tsubscriptions\t"
28 "created_at\tupdated_at\nada\tacct1\tada@example.com\tAda\tactive\tadmin\t\t\t\n")
29 write_file(data_dir / "identity" / "accounts.tsv",
30 "account_id\tname\tstatus\tsubscriptions\tcreated_at\tupdated_at\n"
31 "acct1\tAcme\tactive\t\t\t\n")
32 for secret in ("sessions.tsv", "credentials.tsv", "api_keys.tsv", "service_keys.tsv"):
33 write_file(data_dir / "identity" / secret, "id\tsecret\nx\tHUNTER2\n")
34 return objects_dir, data_dir
35
36
37def test_a_backup_carries_user_file_bytes_permissions_and_users(tmp_path):
38 objects_dir, data_dir = _tree(tmp_path)
39 backup = tmp_path / "b.tar.gz"
40 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
41 names = archive_names(backup)
42 assert "data/user_files/ada/f1" in names
43 assert "data/permissions/policy.json" in names
44 assert "data/identity/users.tsv" in names
45 assert "data/identity/accounts.tsv" in names
46
47
48def test_secrets_stay_out_of_the_archive(tmp_path):
49 objects_dir, data_dir = _tree(tmp_path)
50 backup = tmp_path / "b.tar.gz"
51 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
52 names = archive_names(backup)
53 for secret in ("sessions", "credentials", "api_keys", "service_keys"):
54 assert f"data/identity/{secret}.tsv" not in names
55 with tarfile.open(backup) as archive:
56 for member in archive.getmembers():
57 if member.isfile():
58 assert b"HUNTER2" not in archive.extractfile(member).read()
59
60
61def test_a_round_trip_restores_the_bytes_and_the_users(tmp_path):
62 objects_dir, data_dir = _tree(tmp_path)
63 backup = tmp_path / "b.tar.gz"
64 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
65 new_objects, new_data = tmp_path / "o2", tmp_path / "d2"
66 object_backup.restore_runtime_backup(backup, objects_dir=new_objects, data_dir=new_data)
67 assert (new_data / "user_files" / "ada" / "f1").read_bytes() == b"\x89PNG bytes"
68 assert (new_data / "permissions" / "policy.json").exists()
69 assert "ada@example.com" in (new_data / "identity" / "users.tsv").read_text()
70 assert not (new_data / "identity" / "credentials.tsv").exists()
71
72
73def test_prune_extra_does_not_delete_the_credentials_a_backup_never_held(tmp_path):
74 objects_dir, data_dir = _tree(tmp_path)
75 backup = tmp_path / "b.tar.gz"
76 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
77 object_backup.restore_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir,
78 overwrite=True, prune_extra=True)
79 assert (data_dir / "identity" / "credentials.tsv").exists()
80 assert (data_dir / "identity" / "sessions.tsv").exists()
81
82
83def test_verify_names_file_records_whose_bytes_are_missing(tmp_path):
84 objects_dir, data_dir = _tree(tmp_path)
85 backup = tmp_path / "b.tar.gz"
86 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
87
88 result = object_backup.verify_runtime_backup(backup)
89
90 assert result.ok # the archive is sound and restorable...
91 assert result.missing_file_bytes == ["files/f2 (owner ada)"] # ...and incomplete
92 assert any("f2" in warning for warning in result.warnings)
93
94
95def test_a_backup_made_before_user_files_was_included_is_reported(tmp_path):
96 """Exactly the archive the old code produced."""
97 objects_dir, data_dir = _tree(tmp_path)
98 import shutil
99 shutil.rmtree(data_dir / "user_files")
100 backup = tmp_path / "old.tar.gz"
101 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
102 result = object_backup.verify_runtime_backup(backup)
103 assert result.ok
104 assert result.missing_file_bytes == ["files/f1 (owner ada)", "files/f2 (owner ada)"]
105
106
107def test_the_cli_exits_nonzero_for_an_incomplete_backup(tmp_path):
108 objects_dir, data_dir = _tree(tmp_path)
109 backup = tmp_path / "b.tar.gz"
110 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
111 assert object_backup.main(["verify", str(backup)]) == 2
112
113
114def test_verify_is_quiet_when_every_record_has_its_bytes(tmp_path):
115 objects_dir, data_dir = _tree(tmp_path)
116 write_file(data_dir / "user_files" / "ada" / "f2", b"x")
117 backup = tmp_path / "b.tar.gz"
118 object_backup.create_runtime_backup(backup, objects_dir=objects_dir, data_dir=data_dir)
119 result = object_backup.verify_runtime_backup(backup)
120 assert result.ok and result.missing_file_bytes == []
121 assert object_backup.main(["verify", str(backup)]) == 0