|
| 1 | +# Copyright (c) 2021 Aiven, Helsinki, Finland. https://aiven.io/ |
| 2 | + |
| 3 | +from aiven_db_migrate.migrate.errors import PGMigrateValidationFailedError |
| 4 | +from aiven_db_migrate.migrate.pgmigrate import PGMigrate |
| 5 | +from test.conftest import PGRunner |
| 6 | +from test.utils import random_string |
| 7 | +from typing import Tuple |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +def test_dbs_max_total_size_check(pg_source_and_target: Tuple[PGRunner, PGRunner]): |
| 14 | + source, target = pg_source_and_target |
| 15 | + dbnames = {random_string() for _ in range(3)} |
| 16 | + |
| 17 | + for dbname in dbnames: |
| 18 | + source.create_db(dbname=dbname) |
| 19 | + target.create_db(dbname=dbname) |
| 20 | + |
| 21 | + # This DB seems to be created outside the tests |
| 22 | + dbnames.add("postgres") |
| 23 | + |
| 24 | + # Create few tables and insert some data |
| 25 | + tables = [f'table_{i}' for i in range(4)] |
| 26 | + for dbname in dbnames: |
| 27 | + with source.cursor(dbname=dbname) as c: |
| 28 | + for t in tables: |
| 29 | + c.execute(f"DROP TABLE IF EXISTS {t}") |
| 30 | + c.execute(f"CREATE TABLE {t} (foo INT)") |
| 31 | + c.execute(f"INSERT INTO {t} (foo) VALUES (1), (2), (3)") |
| 32 | + |
| 33 | + pg_mig = PGMigrate( |
| 34 | + source_conn_info=source.conn_info(), |
| 35 | + target_conn_info=target.conn_info(), |
| 36 | + createdb=False, |
| 37 | + verbose=True, |
| 38 | + ) |
| 39 | + |
| 40 | + with patch( |
| 41 | + "aiven_db_migrate.migrate.pgmigrate.PGMigrate._check_database_size", side_effect=pg_mig._check_database_size |
| 42 | + ) as mock_db_size_check: |
| 43 | + # DB size check is not run |
| 44 | + pg_mig.validate() |
| 45 | + mock_db_size_check.assert_not_called() |
| 46 | + |
| 47 | + mock_db_size_check.reset_mock() |
| 48 | + |
| 49 | + # DB size check with max size of zero |
| 50 | + with pytest.raises(PGMigrateValidationFailedError) as e: |
| 51 | + pg_mig.validate(dbs_max_total_size=0) |
| 52 | + assert "Databases do not fit to the required maximum size" in str(e) |
| 53 | + mock_db_size_check.assert_called_once_with(max_size=0) |
| 54 | + |
| 55 | + mock_db_size_check.reset_mock() |
| 56 | + |
| 57 | + # DB size check with enough size |
| 58 | + pg_mig.validate(dbs_max_total_size=1073741824) |
| 59 | + mock_db_size_check.assert_called_once_with(max_size=1073741824) |
| 60 | + |
| 61 | + # Test with DB name filtering |
| 62 | + pg_mig = PGMigrate( |
| 63 | + source_conn_info=source.conn_info(), |
| 64 | + target_conn_info=target.conn_info(), |
| 65 | + createdb=False, |
| 66 | + verbose=True, |
| 67 | + filtered_db=",".join(dbnames), |
| 68 | + ) |
| 69 | + |
| 70 | + with patch( |
| 71 | + "aiven_db_migrate.migrate.pgmigrate.PGMigrate._check_database_size", side_effect=pg_mig._check_database_size |
| 72 | + ) as mock_db_size_check: |
| 73 | + # Should pass as all DBs are filtered out from size calculations |
| 74 | + pg_mig.validate(dbs_max_total_size=0) |
| 75 | + mock_db_size_check.assert_called_once_with(max_size=0) |
| 76 | + |
| 77 | + # Test with table filtering |
| 78 | + |
| 79 | + # Include all tables in "skip_tables" |
| 80 | + pg_mig = PGMigrate( |
| 81 | + source_conn_info=source.conn_info(), |
| 82 | + target_conn_info=target.conn_info(), |
| 83 | + createdb=False, |
| 84 | + verbose=True, |
| 85 | + skip_tables=tables, # skip all tables |
| 86 | + ) |
| 87 | + |
| 88 | + with patch( |
| 89 | + "aiven_db_migrate.migrate.pgmigrate.PGMigrate._check_database_size", side_effect=pg_mig._check_database_size |
| 90 | + ) as mock_db_size_check: |
| 91 | + # Should pass as all tables are filtered out from size calculations |
| 92 | + pg_mig.validate(dbs_max_total_size=0) |
| 93 | + mock_db_size_check.assert_called_once_with(max_size=0) |
| 94 | + |
| 95 | + # Only the first table is included |
| 96 | + pg_mig = PGMigrate( |
| 97 | + source_conn_info=source.conn_info(), |
| 98 | + target_conn_info=target.conn_info(), |
| 99 | + createdb=False, |
| 100 | + verbose=True, |
| 101 | + with_tables=tables[:1], # include only one table |
| 102 | + ) |
| 103 | + with patch( |
| 104 | + "aiven_db_migrate.migrate.pgmigrate.PGMigrate._check_database_size", side_effect=pg_mig._check_database_size |
| 105 | + ) as mock_db_size_check: |
| 106 | + # This fails as one table is included in check and it should have data |
| 107 | + with pytest.raises(PGMigrateValidationFailedError) as e: |
| 108 | + pg_mig.validate(dbs_max_total_size=0) |
| 109 | + assert "Databases do not fit to the required maximum size" in str(e) |
| 110 | + mock_db_size_check.assert_called_once_with(max_size=0) |
| 111 | + |
| 112 | + # Should easily fit |
| 113 | + pg_mig.validate(dbs_max_total_size=1073741824) |
0 commit comments