Skip to content

Commit

Permalink
Add integration tests with mysql-router(-k8s) <> data-integrator
Browse files Browse the repository at this point in the history
  • Loading branch information
taurus-forever committed Mar 22, 2023
1 parent 15e0dc4 commit f10b683
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/integration/app-charm/charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parts:
charm:
charm-binary-python-packages:
- psycopg2-binary
- mysql-connector-python == 8.0.31
- mysql-connector-python ~= 8.0.32

bases:
- build-on:
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

DATA_INTEGRATOR = "data-integrator"
MYSQL = {"localhost": "mysql", "microk8s": "mysql-k8s"}
MYSQL_ROUTER = {"localhost": "mysql-router", "microk8s": "mysql-router-k8s"}
MYSQL_TEST_APP = "mysql-test-app"
POSTGRESQL = {"localhost": "postgresql", "microk8s": "postgresql-k8s"}
MONGODB = {"localhost": "mongodb", "microk8s": "mongodb-k8s"}
DATABASE_NAME = "test_database"
Expand Down
118 changes: 108 additions & 10 deletions tests/integration/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ async def test_deploy(ops_test: OpsTest, app_charm: PosixPath, data_integrator_c
),
ops_test.model.deploy(app_charm, application_name=APP, num_units=1, series="jammy"),
)
logger.info(f"Wait for blocked status for {DATA_INTEGRATOR}")
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, APP])
assert ops_test.model.applications[DATA_INTEGRATOR].status == "blocked"

# config database name

logger.info(f"Configure database name: {DATABASE_NAME}")
config = {"database-name": DATABASE_NAME}
await ops_test.model.applications[DATA_INTEGRATOR].set_config(config)

# test the active/waiting status for relation
logger.info("Test the blocked status for relation with database name set")
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR])
assert ops_test.model.applications[DATA_INTEGRATOR].status == "blocked"

Expand All @@ -49,13 +49,13 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest):
trust=True,
)
)
await ops_test.model.wait_for_idle(apps=[MYSQL[ops_test.cloud_name]], wait_for_active=True)
await ops_test.model.wait_for_idle(apps=[MYSQL[ops_test.cloud_name]], status="active")
assert ops_test.model.applications[MYSQL[ops_test.cloud_name]].status == "active"
await ops_test.model.add_relation(DATA_INTEGRATOR, MYSQL[ops_test.cloud_name])
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL[ops_test.cloud_name]])
assert ops_test.model.applications[DATA_INTEGRATOR].status == "active"

# get credential for MYSQL
logger.info(f"Get credential for {MYSQL[ops_test.cloud_name]}")
credentials = await fetch_action_get_credentials(
ops_test.model.applications[DATA_INTEGRATOR].units[0]
)
Expand Down Expand Up @@ -87,7 +87,7 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest):
DATABASE_NAME,
)
assert result["ok"]
# remove relation and test connection again
logger.info("Remove relation and test connection again")
await ops_test.model.applications[DATA_INTEGRATOR].remove_relation(
f"{DATA_INTEGRATOR}:mysql", f"{MYSQL[ops_test.cloud_name]}:database"
)
Expand All @@ -96,15 +96,13 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest):
await ops_test.model.add_relation(DATA_INTEGRATOR, MYSQL[ops_test.cloud_name])
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL[ops_test.cloud_name]])

# join with another relation and check the accessibility of the previously created database
logger.info("Join with new relation and check the previously created database")
new_credentials = await fetch_action_get_credentials(
ops_test.model.applications[DATA_INTEGRATOR].units[0]
)

assert credentials != new_credentials
logger.info(
f"Check assessibility of inserted data on {MYSQL[ops_test.cloud_name]} with new credentials"
)
logger.info(f"Check inserted data on {MYSQL[ops_test.cloud_name]} with new credentials")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"check-inserted-data",
Expand All @@ -113,3 +111,103 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest):
DATABASE_NAME,
)
assert result["ok"]
logger.info(f"Unlock (unreleate) {DATA_INTEGRATOR} for mysql-router tests")
await ops_test.model.applications[DATA_INTEGRATOR].remove_relation(
f"{DATA_INTEGRATOR}:mysql", f"{MYSQL[ops_test.cloud_name]}:database"
)


async def test_deploy_and_relate_mysql_router(ops_test: OpsTest):
"""Test the relation with mysql-router and database accessibility."""
logger.info(f"Test the relation with {MYSQL_ROUTER[ops_test.cloud_name]}.")
await asyncio.gather(
ops_test.model.deploy(
MYSQL_ROUTER[ops_test.cloud_name],
channel="edge",
application_name=MYSQL_ROUTER[ops_test.cloud_name],
num_units=1,
series="focal",
trust=True,
),
ops_test.model.deploy(
MYSQL_TEST_APP,
channel="edge",
application_name=MYSQL_TEST_APP,
num_units=1,
series="focal",
),
)
await ops_test.model.add_relation(
MYSQL[ops_test.cloud_name], MYSQL_ROUTER[ops_test.cloud_name]
)
await ops_test.model.add_relation(MYSQL_ROUTER[ops_test.cloud_name], MYSQL_TEST_APP)
await ops_test.model.wait_for_idle(
apps=[MYSQL[ops_test.cloud_name], MYSQL_ROUTER[ops_test.cloud_name], MYSQL_TEST_APP],
status="active",
raise_on_error=False, # TODO: remove after MYSQL_TEST_APP fix to stop error'ing
)
await ops_test.model.add_relation(
f"{DATA_INTEGRATOR}:mysql", f"{MYSQL_ROUTER[ops_test.cloud_name]}:database"
)
await ops_test.model.wait_for_idle(
apps=[DATA_INTEGRATOR, MYSQL_ROUTER[ops_test.cloud_name], MYSQL_TEST_APP],
status="active",
)
assert ops_test.model.applications[DATA_INTEGRATOR].status == "active"

logger.info(f"Get credential for {MYSQL_ROUTER[ops_test.cloud_name]}")
credentials = await fetch_action_get_credentials(
ops_test.model.applications[DATA_INTEGRATOR].units[0]
)

logger.info(f"Create table on {MYSQL_ROUTER[ops_test.cloud_name]}")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"create-table",
MYSQL_ROUTER[ops_test.cloud_name],
json.dumps(credentials),
DATABASE_NAME,
)
assert result["ok"]
logger.info(f"Insert data in the table on {MYSQL_ROUTER[ops_test.cloud_name]}")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"insert-data",
MYSQL_ROUTER[ops_test.cloud_name],
json.dumps(credentials),
DATABASE_NAME,
)
assert result["ok"]
logger.info(f"Check assessibility of inserted data on {MYSQL_ROUTER[ops_test.cloud_name]}")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"check-inserted-data",
MYSQL_ROUTER[ops_test.cloud_name],
json.dumps(credentials),
DATABASE_NAME,
)
assert result["ok"]
logger.info("Remove relation and test connection again")
await ops_test.model.applications[DATA_INTEGRATOR].remove_relation(
f"{DATA_INTEGRATOR}:mysql", f"{MYSQL_ROUTER[ops_test.cloud_name]}:database"
)

await ops_test.model.wait_for_idle(apps=[MYSQL_ROUTER[ops_test.cloud_name], DATA_INTEGRATOR])
await ops_test.model.add_relation(DATA_INTEGRATOR, MYSQL_ROUTER[ops_test.cloud_name])
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL_ROUTER[ops_test.cloud_name]])

logger.info("Relate and check the accessibility of the previously created database")
new_credentials = await fetch_action_get_credentials(
ops_test.model.applications[DATA_INTEGRATOR].units[0]
)

assert credentials != new_credentials
logger.info(f"Check inserted data on {MYSQL_ROUTER[ops_test.cloud_name]} with new credentials")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"check-inserted-data",
MYSQL_ROUTER[ops_test.cloud_name],
json.dumps(new_credentials),
DATABASE_NAME,
)
assert result["ok"]

0 comments on commit f10b683

Please sign in to comment.