|
15 | 15 | DATA_INTEGRATOR_APP_NAME,
|
16 | 16 | DATABASE_APP_NAME,
|
17 | 17 | check_connected_user,
|
| 18 | + db_connect, |
| 19 | + get_password, |
| 20 | + get_primary, |
| 21 | + get_unit_address, |
18 | 22 | )
|
19 | 23 | from .new_relations.helpers import build_connection_string
|
20 | 24 |
|
@@ -56,6 +60,31 @@ async def test_charmed_dba_role(ops_test: OpsTest):
|
56 | 60 | apps=[DATA_INTEGRATOR_APP_NAME, DATABASE_APP_NAME], status="active"
|
57 | 61 | )
|
58 | 62 |
|
| 63 | + primary = await get_primary(ops_test, f"{DATABASE_APP_NAME}/0") |
| 64 | + primary_address = get_unit_address(ops_test, primary) |
| 65 | + operator_password = await get_password(ops_test, "operator") |
| 66 | + |
| 67 | + # Create a test database to check the dblink functionality. |
| 68 | + connection = None |
| 69 | + cursor = None |
| 70 | + try: |
| 71 | + connection = db_connect( |
| 72 | + primary_address, |
| 73 | + operator_password, |
| 74 | + username="operator", |
| 75 | + database="charmed_dba_database", |
| 76 | + ) |
| 77 | + connection.autocommit = True |
| 78 | + cursor = connection.cursor() |
| 79 | + cursor.execute("CREATE EXTENSION IF NOT EXISTS dblink;") |
| 80 | + cursor.execute("DROP DATABASE IF EXISTS test;") |
| 81 | + cursor.execute("CREATE DATABASE test;") |
| 82 | + finally: |
| 83 | + if cursor is not None: |
| 84 | + cursor.close() |
| 85 | + if connection is not None: |
| 86 | + connection.close() |
| 87 | + |
59 | 88 | action = await ops_test.model.units[f"{DATA_INTEGRATOR_APP_NAME}/0"].run_action(
|
60 | 89 | action_name="get-credentials"
|
61 | 90 | )
|
@@ -91,6 +120,33 @@ async def test_charmed_dba_role(ops_test: OpsTest):
|
91 | 120 | logger.info(f"Resetting the user to the {username} user in the {instance}")
|
92 | 121 | cursor.execute("SELECT reset_user();")
|
93 | 122 | check_connected_user(cursor, username, username, primary=read_write_endpoint)
|
| 123 | + logger.info( |
| 124 | + f"Testing connection to another database through the same session in the {instance}" |
| 125 | + ) |
| 126 | + other_database_connection_string = ( |
| 127 | + await build_connection_string( |
| 128 | + ops_test, |
| 129 | + DATA_INTEGRATOR_APP_NAME, |
| 130 | + "postgresql", |
| 131 | + database="test", |
| 132 | + read_only_endpoint=(not read_write_endpoint), |
| 133 | + ) |
| 134 | + ).replace("'", "") |
| 135 | + cursor.execute( |
| 136 | + f"SELECT * FROM dblink('{other_database_connection_string}', 'SELECT current_database() AS database') AS t1(database TEXT);" |
| 137 | + ) |
| 138 | + assert cursor.fetchone()[0] == "test" |
94 | 139 | finally:
|
95 | 140 | if connection is not None:
|
96 | 141 | connection.close()
|
| 142 | + |
| 143 | + connection = psycopg2.connect(other_database_connection_string) |
| 144 | + try: |
| 145 | + with connection.cursor() as cursor: |
| 146 | + logger.info( |
| 147 | + f"Testing connection to another database through another session in the {instance}" |
| 148 | + ) |
| 149 | + cursor.execute("SELECT current_database();") |
| 150 | + assert cursor.fetchone()[0] == "test" |
| 151 | + finally: |
| 152 | + connection.close() |
0 commit comments