|
| 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