Skip to content

Commit bc11d21

Browse files
committed
test: for branch clone and backup restore
1 parent 395dfb8 commit bc11d21

3 files changed

Lines changed: 147 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ testpaths = [
8888
"tests/test_projects.py",
8989
"tests/test_resources.py",
9090
"tests/test_branches.py",
91+
"tests/test_backup_and_clone.py",
9192
]
9293

9394
[tool.ruff]

tests/conftest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ def client():
8484
def make_org(client):
8585
created: list[ULID] = []
8686

87-
def _factory(name: str) -> ULID:
88-
r = client.post("organizations/", json={"name": name, "max_backups": 0, "environments": ""})
87+
def _factory(name: str, **overrides: object) -> ULID:
88+
payload = {"name": name, "max_backups": 0, "environments": "", **overrides}
89+
r = client.post("organizations/", json=payload)
8990
assert r.status_code == 201
9091
uid = _id(r.headers["Location"])
9192
created.append(uid)
@@ -100,10 +101,11 @@ def _factory(name: str) -> ULID:
100101
def make_project(client):
101102
created: list[tuple[ULID, ULID]] = []
102103

103-
def _factory(org_id: ULID, name: str) -> ULID:
104+
def _factory(org_id: ULID, name: str, **overrides: object) -> ULID:
105+
payload = {"name": name, "max_backups": 0, "project_limits": {}, "per_branch_limits": {}, **overrides}
104106
r = client.post(
105107
f"organizations/{org_id}/projects/",
106-
json={"name": name, "max_backups": 0, "project_limits": {}, "per_branch_limits": {}},
108+
json=payload,
107109
)
108110
assert r.status_code == 201
109111
uid = _id(r.headers["Location"])

tests/test_backup_and_clone.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import pytest
2+
from conftest import BRANCH_TIMEOUT_SEC, _id, wait_for_status
3+
4+
pytestmark = pytest.mark.backup
5+
6+
_BRANCH_NAME = "test-branch-backup"
7+
_BRANCH_PASSWORD = "SecurePass1!"
8+
_BRANCH_CREATE_PAYLOAD = {
9+
"name": _BRANCH_NAME,
10+
"deployment": {
11+
"database_password": _BRANCH_PASSWORD,
12+
"database_size": 5000000000,
13+
"storage_size": 5000000000,
14+
"milli_vcpu": 500,
15+
"memory_bytes": 1073741824,
16+
"iops": 1000,
17+
"database_image_tag": "18.1-velaos",
18+
"enable_file_storage": False,
19+
},
20+
}
21+
22+
# ---------------------------------------------------------------------------
23+
# Fixtures
24+
# ---------------------------------------------------------------------------
25+
26+
27+
@pytest.fixture(scope="module")
28+
def org(make_org):
29+
return make_org("test-org-backup", max_backups=10)
30+
31+
32+
@pytest.fixture(scope="module")
33+
def project(make_project, org):
34+
return make_project(org, "test-project-backup", max_backups=10)
35+
36+
37+
@pytest.fixture(scope="module")
38+
def branch_id(client, org, project):
39+
r = client.post(
40+
f"organizations/{org}/projects/{project}/branches/",
41+
json=_BRANCH_CREATE_PAYLOAD,
42+
timeout=60,
43+
)
44+
assert r.status_code == 201
45+
bid = _id(r.headers["Location"])
46+
wait_for_status(
47+
client,
48+
f"organizations/{org}/projects/{project}/branches/{bid}/",
49+
"ACTIVE_HEALTHY",
50+
BRANCH_TIMEOUT_SEC,
51+
)
52+
53+
yield bid
54+
55+
r = client.delete(f"organizations/{org}/projects/{project}/branches/{bid}/")
56+
assert r.status_code == 204
57+
58+
59+
@pytest.fixture(scope="module")
60+
def backup_id(client, branch_id):
61+
"""Trigger a manual backup and return the backup_id."""
62+
r = client.post(f"backup/branches/{branch_id}/", timeout=120)
63+
assert r.status_code == 200
64+
data = r.json()
65+
assert data["status"] == "manual backup created"
66+
assert "backup_id" in data
67+
return data["backup_id"]
68+
69+
70+
# ---------------------------------------------------------------------------
71+
# Branch Clone
72+
# ---------------------------------------------------------------------------
73+
74+
75+
def test_branch_clone(client, org, project, branch_id):
76+
"""Clone a branch and verify the clone reaches ACTIVE_HEALTHY."""
77+
r = client.post(
78+
f"organizations/{org}/projects/{project}/branches/",
79+
json={
80+
"name": "test-branch-clone",
81+
"source": {
82+
"branch_id": str(branch_id),
83+
"data_copy": True,
84+
},
85+
},
86+
timeout=60,
87+
)
88+
assert r.status_code == 201
89+
clone_id = _id(r.headers["Location"])
90+
91+
try:
92+
wait_for_status(
93+
client,
94+
f"organizations/{org}/projects/{project}/branches/{clone_id}/",
95+
"ACTIVE_HEALTHY",
96+
BRANCH_TIMEOUT_SEC,
97+
)
98+
finally:
99+
r = client.delete(f"organizations/{org}/projects/{project}/branches/{clone_id}/")
100+
assert r.status_code == 204
101+
102+
103+
# ---------------------------------------------------------------------------
104+
# Manual Backup
105+
# ---------------------------------------------------------------------------
106+
107+
108+
def test_manual_backup(backup_id):
109+
"""Trigger an on-demand backup for the branch and verify a backup_id is returned."""
110+
assert backup_id, "backup_id fixture must return a valid id"
111+
112+
113+
# ---------------------------------------------------------------------------
114+
# Restore a new branch from backup
115+
# ---------------------------------------------------------------------------
116+
117+
118+
def test_restore_branch_from_backup(client, org, project, backup_id):
119+
"""Create a new branch from the manual backup and verify it reaches ACTIVE_HEALTHY."""
120+
r = client.post(
121+
f"organizations/{org}/projects/{project}/branches/",
122+
json={
123+
"name": "test-branch-restored",
124+
"restore": {"backup_id": backup_id},
125+
},
126+
timeout=60,
127+
)
128+
assert r.status_code == 201
129+
restored_id = _id(r.headers["Location"])
130+
131+
try:
132+
wait_for_status(
133+
client,
134+
f"organizations/{org}/projects/{project}/branches/{restored_id}/",
135+
"ACTIVE_HEALTHY",
136+
BRANCH_TIMEOUT_SEC,
137+
)
138+
finally:
139+
r = client.delete(f"organizations/{org}/projects/{project}/branches/{restored_id}/")
140+
assert r.status_code == 204

0 commit comments

Comments
 (0)