-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_enterprise_backup.py
More file actions
191 lines (149 loc) · 5.82 KB
/
Copy pathtest_enterprise_backup.py
File metadata and controls
191 lines (149 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Stress tests for backup and disaster recovery."""
from __future__ import annotations
import gzip
import json
import os
import tempfile
import pytest
from hive.rust_brain import RustBrain, TimestampRegression
def test_snapshot_to_file_and_restore():
brain = RustBrain(tenant_id="backup_test")
brain.remember("key1", "value1")
brain.remember("key2", "value2", tags={"t1"})
brain.remember("key3", "value3", edges={"related_to": ["key1"]})
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
meta = brain.snapshot_to_file(path)
assert meta["node_count"] == 3
assert "sha256" in meta
# Verify file exists and is gzipped
with open(path, "rb") as fh:
raw = fh.read()
assert raw[:2] == b"\x1f\x8b" # gzip magic
# Restore into fresh brain
brain2 = RustBrain(tenant_id="backup_test")
restored = brain2.restore_from_file(path)
assert restored == 3
assert brain2.recall("key1") == "value1"
assert brain2.recall("key2") == "value2"
assert brain2.recall("key3") == "value3"
finally:
os.unlink(path)
def test_restore_clears_existing_data():
brain = RustBrain()
brain.remember("old", "data")
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
brain2 = RustBrain()
brain2.remember("new", "data")
brain2.snapshot_to_file(path)
brain.restore_from_file(path)
assert brain.recall("new") == "data"
assert brain.recall("old") is None
finally:
os.unlink(path)
def test_corruption_detection():
"""Tampering with snapshot content is caught by the embedded SHA-256."""
brain = RustBrain()
brain.remember("k", "v")
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
brain.snapshot_to_file(path)
# Tamper with a node value while keeping the gzip+JSON framing intact,
# so only the checksum can catch it.
with open(path, "rb") as fh:
data = json.loads(gzip.decompress(fh.read()).decode("utf-8"))
data["nodes"][0]["value"] = "tampered"
with open(path, "wb") as fh:
fh.write(gzip.compress(json.dumps(data).encode("utf-8")))
brain2 = RustBrain()
with pytest.raises(ValueError, match="checksum mismatch"):
brain2.restore_from_file(path)
finally:
if os.path.exists(path):
os.unlink(path)
def test_corruption_detection_leaves_existing_data_intact():
"""A failed restore must not wipe the destination store."""
brain = RustBrain()
brain.remember("k", "v")
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
brain.snapshot_to_file(path)
with open(path, "rb") as fh:
data = json.loads(gzip.decompress(fh.read()).decode("utf-8"))
data["nodes"][0]["value"] = "tampered"
with open(path, "wb") as fh:
fh.write(gzip.compress(json.dumps(data).encode("utf-8")))
target = RustBrain()
target.remember("keep", "safe")
with pytest.raises(ValueError, match="checksum mismatch"):
target.restore_from_file(path)
# Pre-existing data survives the aborted restore.
assert target.recall("keep") == "safe"
finally:
if os.path.exists(path):
os.unlink(path)
def test_truncated_snapshot_raises():
"""Raw byte corruption that breaks gzip/JSON framing also fails loudly."""
brain = RustBrain()
brain.remember("k", "v")
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
brain.snapshot_to_file(path)
with open(path, "rb+") as fh:
fh.seek(-10, os.SEEK_END)
fh.write(b"CORRUPTED!")
brain2 = RustBrain()
with pytest.raises(Exception):
brain2.restore_from_file(path)
finally:
if os.path.exists(path):
os.unlink(path)
def test_version_mismatch():
with tempfile.NamedTemporaryFile(mode="wb", delete=False, suffix=".gz") as f:
path = f.name
payload = json.dumps({"version": "hive-snapshot-v0", "nodes": []}).encode()
f.write(gzip.compress(payload))
try:
brain = RustBrain()
with pytest.raises(ValueError, match="Unsupported snapshot version"):
brain.restore_from_file(path)
finally:
os.unlink(path)
def test_restore_preserves_hlc_for_replication():
"""HLC must survive snapshot round-trip so post-restore writes stay ordered."""
brain = RustBrain()
brain.remember("k", "v1", hlc=(5000, 10, "nodeA"))
orig_hlc = brain.get("k").hlc
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
brain.snapshot_to_file(path)
brain2 = RustBrain()
brain2.restore_from_file(path)
assert brain2.get("k").hlc == orig_hlc
# Causal successor from the original writer should apply after restore.
brain2.remember("k", "v2", hlc=(5000, 11, "nodeA"))
assert brain2.recall("k") == "v2"
# Stale replay must still be rejected.
with pytest.raises(TimestampRegression):
brain2.remember("k", "stale", hlc=(5000, 5, "nodeA"))
finally:
os.unlink(path)
def test_snapshot_with_tenant_isolation():
brain = RustBrain(tenant_id="org_a", tenant_isolation=True)
brain.remember("secret", "data")
with tempfile.NamedTemporaryFile(delete=False, suffix=".gz") as f:
path = f.name
try:
brain.snapshot_to_file(path)
brain_b = RustBrain(tenant_id="org_a", tenant_isolation=True)
brain_b.restore_from_file(path)
assert brain_b.recall("secret") == "data"
finally:
os.unlink(path)