|
| 1 | +import time |
| 2 | +from collections.abc import Callable |
| 3 | +from pathlib import Path |
| 4 | +from typing import TypeAlias |
| 5 | + |
| 6 | +from cbltest import CBLPyTest, CouchbaseServer |
| 7 | +from cbltest.api.cbltestclass import CBLTestClass |
| 8 | +from cbltest.api.database import Database, GetDocumentResult |
| 9 | +from cbltest.api.database_types import DocumentEntry |
| 10 | +from cbltest.api.error import CblSyncGatewayBadResponseError |
| 11 | +from cbltest.api.replicator import Replicator |
| 12 | +from cbltest.api.replicator_types import ( |
| 13 | + ReplicatorActivityLevel, |
| 14 | + ReplicatorBasicAuthenticator, |
| 15 | + ReplicatorCollectionEntry, |
| 16 | + ReplicatorConflictResolver, |
| 17 | + ReplicatorType, |
| 18 | + WaitForDocumentEventEntry, |
| 19 | +) |
| 20 | +from cbltest.api.syncgateway import RemoteDocument |
| 21 | +from cbltest.api.test_functions import compare_local_and_remote |
| 22 | +from cbltest.logging import cbl_info |
| 23 | + |
| 24 | + |
| 25 | +class DocSnapshot: |
| 26 | + def __init__(self, local: GetDocumentResult, remote: RemoteDocument): |
| 27 | + self.local = local |
| 28 | + self.remote = remote |
| 29 | + |
| 30 | + |
| 31 | +DocValidator: TypeAlias = Callable[[DocSnapshot, DocSnapshot], None] |
| 32 | + |
| 33 | + |
| 34 | +def tools_path() -> Path: |
| 35 | + # client/src/cbltest/api/upgrade_test_helpers.py -> parents[4] is repo root |
| 36 | + return Path(__file__).resolve().parents[4] / "tests" / ".tools" |
| 37 | + |
| 38 | + |
| 39 | +async def setup_upgrade_env( |
| 40 | + test_case: CBLTestClass, cblpytest: CBLPyTest, dataset_path: Path |
| 41 | +) -> Database: |
| 42 | + await test_case.skip_if_cbl_not(cblpytest.test_servers[0], ">= 4.0.0") |
| 43 | + |
| 44 | + dataset_ver = cblpytest.test_servers[0].dataset_version |
| 45 | + test_case.skip_if_not( |
| 46 | + dataset_ver == "4.0", f"Requires dataset v4.0 (current: {dataset_ver})." |
| 47 | + ) |
| 48 | + |
| 49 | + test_case.mark_test_step("Delete Sync Gateway 'upgrade' database if exists") |
| 50 | + sg = cblpytest.sync_gateways[0] |
| 51 | + try: |
| 52 | + await sg.delete_database("upgrade") |
| 53 | + except CblSyncGatewayBadResponseError as e: |
| 54 | + if e.code != 403: |
| 55 | + raise |
| 56 | + |
| 57 | + test_case.mark_test_step("Restore Couchbase Server Bucket using `upgrade` dataset") |
| 58 | + cbs: CouchbaseServer = cblpytest.couchbase_servers[0] |
| 59 | + cbs.drop_bucket("upgrade") |
| 60 | + cbs.restore_bucket("upgrade", tools_path(), dataset_path, "upgrade") |
| 61 | + |
| 62 | + test_case.mark_test_step("Wait 2s to ensure SG picks up the restored database.") |
| 63 | + time.sleep(2) |
| 64 | + |
| 65 | + test_case.mark_test_step("Reset local database, and load `upgrade` dataset.") |
| 66 | + dbs = await cblpytest.test_servers[0].create_and_reset_db( |
| 67 | + ["db1"], dataset="upgrade" |
| 68 | + ) |
| 69 | + return dbs[0] |
| 70 | + |
| 71 | + |
| 72 | +async def do_upgrade_replication_test( |
| 73 | + test_case: CBLTestClass, |
| 74 | + cblpytest: CBLPyTest, |
| 75 | + db: Database, |
| 76 | + doc_id: str, |
| 77 | + replicator_type: ReplicatorType, |
| 78 | + conflict_resolver: ReplicatorConflictResolver | None = None, |
| 79 | + doc_events: set[WaitForDocumentEventEntry] | None = None, |
| 80 | + compare_docs: bool | None = True, |
| 81 | + validator: DocValidator | None = None, |
| 82 | +) -> None: |
| 83 | + sg = cblpytest.sync_gateways[0] |
| 84 | + |
| 85 | + pre_local_doc = await db.get_document(DocumentEntry("_default._default", doc_id)) |
| 86 | + pre_remote_doc = await sg.get_document("upgrade", doc_id) |
| 87 | + |
| 88 | + assert pre_local_doc is not None |
| 89 | + assert pre_remote_doc is not None |
| 90 | + cbl_info(f"Revision Info before Replication ({replicator_type}):") |
| 91 | + cbl_info(f"Local : RevID = {pre_local_doc.revid}, HLV = {pre_local_doc.cv}") |
| 92 | + cbl_info(f"Remote : RevID = {pre_remote_doc.revid}, HLV = {pre_remote_doc.cv}") |
| 93 | + |
| 94 | + wait_for_doc_events = bool(doc_events) |
| 95 | + |
| 96 | + conflict_resolver_name = ( |
| 97 | + f"{conflict_resolver.name}" if conflict_resolver else "None" |
| 98 | + ) |
| 99 | + |
| 100 | + test_case.mark_test_step(f""" |
| 101 | + Start a replicator: |
| 102 | + * endpoint: '/upgrade' |
| 103 | + * collections : '_default._default' |
| 104 | + * type: {replicator_type} |
| 105 | + * document_ids: ['{doc_id}'] |
| 106 | + * continuous: {wait_for_doc_events} |
| 107 | + * conflict_resolver: {conflict_resolver_name} |
| 108 | + """) |
| 109 | + replicator = Replicator( |
| 110 | + db, |
| 111 | + cblpytest.sync_gateways[0].replication_url("upgrade"), |
| 112 | + collections=[ |
| 113 | + ReplicatorCollectionEntry( |
| 114 | + names=["_default._default"], |
| 115 | + document_ids=[doc_id], |
| 116 | + conflict_resolver=conflict_resolver, |
| 117 | + ) |
| 118 | + ], |
| 119 | + replicator_type=replicator_type, |
| 120 | + continuous=wait_for_doc_events, |
| 121 | + authenticator=ReplicatorBasicAuthenticator("user1", "pass"), |
| 122 | + pinned_server_cert=cblpytest.sync_gateways[0].tls_cert(), |
| 123 | + enable_document_listener=wait_for_doc_events, |
| 124 | + ) |
| 125 | + |
| 126 | + await replicator.start() |
| 127 | + |
| 128 | + if doc_events: |
| 129 | + test_case.mark_test_step("Wait until receiving all document replication events") |
| 130 | + await replicator.wait_for_all_doc_events( |
| 131 | + events=doc_events, |
| 132 | + max_retries=100, |
| 133 | + ) |
| 134 | + else: |
| 135 | + test_case.mark_test_step("Wait until the replicator is stopped.") |
| 136 | + status = await replicator.wait_for(ReplicatorActivityLevel.STOPPED) |
| 137 | + assert status.error is None, ( |
| 138 | + f"Error waiting for replicator: ({status.error.domain} / {status.error.code}) {status.error.message}" |
| 139 | + ) |
| 140 | + |
| 141 | + if compare_docs: |
| 142 | + test_case.mark_test_step("Check that the doc is replicated correctly.") |
| 143 | + await compare_local_and_remote( |
| 144 | + db, sg, replicator_type, "upgrade", ["_default._default"], [doc_id] |
| 145 | + ) |
| 146 | + |
| 147 | + local_doc = await db.get_document(DocumentEntry("_default._default", doc_id)) |
| 148 | + remote_doc = await sg.get_document("upgrade", doc_id) |
| 149 | + |
| 150 | + assert local_doc is not None |
| 151 | + assert remote_doc is not None |
| 152 | + cbl_info(f"Revision Info after Replication ({replicator_type}):") |
| 153 | + cbl_info(f"Local : RevID = {local_doc.revid}, HLV = {local_doc.cv}") |
| 154 | + cbl_info(f"Remote : RevID = {remote_doc.revid}, HLV = {remote_doc.cv}") |
| 155 | + |
| 156 | + if validator: |
| 157 | + test_case.mark_test_step("Validate revid and HLV of local and remote doc.") |
| 158 | + validator( |
| 159 | + DocSnapshot(pre_local_doc, pre_remote_doc), |
| 160 | + DocSnapshot(local_doc, remote_doc), |
| 161 | + ) |
0 commit comments