|
| 1 | +""" |
| 2 | +Integration tests for AdminClient.fork_database method. |
| 3 | +
|
| 4 | +Tests the fork_database functionality against real databases, including: |
| 5 | +- Successful fork operations |
| 6 | +- Empty database fork |
| 7 | +- Fork preserves database attributes (charset, collation) |
| 8 | +- Fork creates independent databases |
| 9 | +- Error handling when destination already exists |
| 10 | +- Error handling when fork is not enabled |
| 11 | +""" |
| 12 | + |
| 13 | +import contextlib |
| 14 | +import logging |
| 15 | +import time |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class TestDatabaseFork: |
| 23 | + """Tests for AdminClient.fork_database() using real database connections.""" |
| 24 | + |
| 25 | + def _is_fork_database_enabled(self, admin_client) -> bool: |
| 26 | + """Check if fork_database is enabled for the given admin client.""" |
| 27 | + try: |
| 28 | + return admin_client._server._fork_database_enabled() |
| 29 | + except Exception: |
| 30 | + logger.exception("Failed to check if fork_database is enabled") |
| 31 | + return False |
| 32 | + |
| 33 | + def _unique_name(self, prefix: str) -> str: |
| 34 | + return f"{prefix}_{int(time.time() * 1000)}" |
| 35 | + |
| 36 | + def test_fork_database_success(self, admin_client): |
| 37 | + """ |
| 38 | + Test successful fork_database operation. |
| 39 | +
|
| 40 | + Automatically runs for: embedded, server, oceanbase |
| 41 | + Skips if fork_database is not enabled. |
| 42 | + """ |
| 43 | + if not self._is_fork_database_enabled(admin_client): |
| 44 | + pytest.skip("Fork database is not enabled for this database") |
| 45 | + |
| 46 | + source_name = self._unique_name("test_forkdb_src") |
| 47 | + dest_name = self._unique_name("test_forkdb_dst") |
| 48 | + |
| 49 | + try: |
| 50 | + admin_client.create_database(source_name) |
| 51 | + |
| 52 | + forked_db = admin_client.fork_database(source_name, dest_name) |
| 53 | + |
| 54 | + assert forked_db is not None |
| 55 | + assert forked_db.name == dest_name |
| 56 | + |
| 57 | + retrieved_db = admin_client.get_database(dest_name) |
| 58 | + assert retrieved_db.name == dest_name |
| 59 | + |
| 60 | + finally: |
| 61 | + with contextlib.suppress(Exception): |
| 62 | + admin_client.delete_database(dest_name) |
| 63 | + with contextlib.suppress(Exception): |
| 64 | + admin_client.delete_database(source_name) |
| 65 | + |
| 66 | + def test_fork_empty_database(self, admin_client): |
| 67 | + """ |
| 68 | + Test fork of an empty database (no tables). |
| 69 | +
|
| 70 | + Automatically runs for: embedded, server, oceanbase |
| 71 | + Skips if fork_database is not enabled. |
| 72 | + """ |
| 73 | + if not self._is_fork_database_enabled(admin_client): |
| 74 | + pytest.skip("Fork database is not enabled for this database") |
| 75 | + |
| 76 | + source_name = self._unique_name("test_forkdb_empty_src") |
| 77 | + dest_name = self._unique_name("test_forkdb_empty_dst") |
| 78 | + |
| 79 | + try: |
| 80 | + admin_client.create_database(source_name) |
| 81 | + |
| 82 | + forked_db = admin_client.fork_database(source_name, dest_name) |
| 83 | + assert forked_db is not None |
| 84 | + assert forked_db.name == dest_name |
| 85 | + |
| 86 | + finally: |
| 87 | + with contextlib.suppress(Exception): |
| 88 | + admin_client.delete_database(dest_name) |
| 89 | + with contextlib.suppress(Exception): |
| 90 | + admin_client.delete_database(source_name) |
| 91 | + |
| 92 | + def test_fork_database_preserves_attributes(self, admin_client): |
| 93 | + """ |
| 94 | + Test that fork preserves database attributes (charset, collation). |
| 95 | +
|
| 96 | + Automatically runs for: embedded, server, oceanbase |
| 97 | + Skips if fork_database is not enabled. |
| 98 | + """ |
| 99 | + if not self._is_fork_database_enabled(admin_client): |
| 100 | + pytest.skip("Fork database is not enabled for this database") |
| 101 | + |
| 102 | + source_name = self._unique_name("test_forkdb_attr_src") |
| 103 | + dest_name = self._unique_name("test_forkdb_attr_dst") |
| 104 | + |
| 105 | + try: |
| 106 | + admin_client.create_database(source_name) |
| 107 | + source_db = admin_client.get_database(source_name) |
| 108 | + |
| 109 | + forked_db = admin_client.fork_database(source_name, dest_name) |
| 110 | + |
| 111 | + assert forked_db.charset == source_db.charset |
| 112 | + assert forked_db.collation == source_db.collation |
| 113 | + |
| 114 | + finally: |
| 115 | + with contextlib.suppress(Exception): |
| 116 | + admin_client.delete_database(dest_name) |
| 117 | + with contextlib.suppress(Exception): |
| 118 | + admin_client.delete_database(source_name) |
| 119 | + |
| 120 | + def test_fork_database_destination_already_exists(self, admin_client): |
| 121 | + """ |
| 122 | + Test that forking to an existing database raises an error. |
| 123 | +
|
| 124 | + Automatically runs for: embedded, server, oceanbase |
| 125 | + Skips if fork_database is not enabled. |
| 126 | + """ |
| 127 | + if not self._is_fork_database_enabled(admin_client): |
| 128 | + pytest.skip("Fork database is not enabled for this database") |
| 129 | + |
| 130 | + source_name = self._unique_name("test_forkdb_dup_src") |
| 131 | + dest_name = self._unique_name("test_forkdb_dup_dst") |
| 132 | + |
| 133 | + try: |
| 134 | + admin_client.create_database(source_name) |
| 135 | + admin_client.create_database(dest_name) |
| 136 | + |
| 137 | + with pytest.raises(ValueError): |
| 138 | + admin_client.fork_database(source_name, dest_name) |
| 139 | + |
| 140 | + finally: |
| 141 | + with contextlib.suppress(Exception): |
| 142 | + admin_client.delete_database(dest_name) |
| 143 | + with contextlib.suppress(Exception): |
| 144 | + admin_client.delete_database(source_name) |
| 145 | + |
| 146 | + def test_fork_database_independent_operations(self, admin_client): |
| 147 | + """ |
| 148 | + Test that forked database is independent from the source. |
| 149 | +
|
| 150 | + Creates a table in source before fork, then verifies that modifications |
| 151 | + to the forked database do not affect the source and vice versa. |
| 152 | +
|
| 153 | + Automatically runs for: embedded, server, oceanbase |
| 154 | + Skips if fork_database is not enabled. |
| 155 | + """ |
| 156 | + if not self._is_fork_database_enabled(admin_client): |
| 157 | + pytest.skip("Fork database is not enabled for this database") |
| 158 | + |
| 159 | + source_name = self._unique_name("test_forkdb_indep_src") |
| 160 | + dest_name = self._unique_name("test_forkdb_indep_dst") |
| 161 | + |
| 162 | + try: |
| 163 | + admin_client.create_database(source_name) |
| 164 | + |
| 165 | + admin_client._server._execute(f"CREATE TABLE `{source_name}`.`t1` (id INT PRIMARY KEY, val VARCHAR(100))") |
| 166 | + admin_client._server._execute(f"INSERT INTO `{source_name}`.`t1` VALUES (1, 'original')") |
| 167 | + |
| 168 | + admin_client.fork_database(source_name, dest_name) |
| 169 | + |
| 170 | + admin_client._server._execute(f"INSERT INTO `{dest_name}`.`t1` VALUES (2, 'forked_only')") |
| 171 | + |
| 172 | + source_rows = admin_client._server._execute(f"SELECT COUNT(*) as cnt FROM `{source_name}`.`t1`") |
| 173 | + dest_rows = admin_client._server._execute(f"SELECT COUNT(*) as cnt FROM `{dest_name}`.`t1`") |
| 174 | + |
| 175 | + source_count = source_rows[0]["cnt"] if isinstance(source_rows[0], dict) else source_rows[0][0] |
| 176 | + dest_count = dest_rows[0]["cnt"] if isinstance(dest_rows[0], dict) else dest_rows[0][0] |
| 177 | + |
| 178 | + assert source_count == 1, f"Source should have 1 row, got {source_count}" |
| 179 | + assert dest_count == 2, f"Destination should have 2 rows, got {dest_count}" |
| 180 | + |
| 181 | + finally: |
| 182 | + with contextlib.suppress(Exception): |
| 183 | + admin_client.delete_database(dest_name) |
| 184 | + with contextlib.suppress(Exception): |
| 185 | + admin_client.delete_database(source_name) |
| 186 | + |
| 187 | + def test_fork_database_multiple_times(self, admin_client): |
| 188 | + """ |
| 189 | + Test that the same source database can be forked multiple times. |
| 190 | +
|
| 191 | + Automatically runs for: embedded, server, oceanbase |
| 192 | + Skips if fork_database is not enabled. |
| 193 | + """ |
| 194 | + if not self._is_fork_database_enabled(admin_client): |
| 195 | + pytest.skip("Fork database is not enabled for this database") |
| 196 | + |
| 197 | + source_name = self._unique_name("test_forkdb_multi_src") |
| 198 | + dest_name_1 = self._unique_name("test_forkdb_multi_d1") |
| 199 | + dest_name_2 = self._unique_name("test_forkdb_multi_d2") |
| 200 | + |
| 201 | + try: |
| 202 | + admin_client.create_database(source_name) |
| 203 | + |
| 204 | + forked_1 = admin_client.fork_database(source_name, dest_name_1) |
| 205 | + forked_2 = admin_client.fork_database(source_name, dest_name_2) |
| 206 | + |
| 207 | + assert forked_1.name == dest_name_1 |
| 208 | + assert forked_2.name == dest_name_2 |
| 209 | + |
| 210 | + finally: |
| 211 | + with contextlib.suppress(Exception): |
| 212 | + admin_client.delete_database(dest_name_2) |
| 213 | + with contextlib.suppress(Exception): |
| 214 | + admin_client.delete_database(dest_name_1) |
| 215 | + with contextlib.suppress(Exception): |
| 216 | + admin_client.delete_database(source_name) |
| 217 | + |
| 218 | + |
| 219 | +if __name__ == "__main__": |
| 220 | + pytest.main([__file__, "-v", "-s"]) |
0 commit comments