Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests with mysql-router(-k8s) <> data-integrator #14

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.33

bases:
- name: ubuntu
Expand Down
1 change: 1 addition & 0 deletions tests/integration/app-charm/src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def create_table_mysql(credentials: Dict[str, str], database_name: str) -> bool:
config = get_mysql_config(credentials, database_name)
with MysqlConnector(config) as cursor:
try:
cursor.execute(f"DROP TABLE IF EXISTS {TABLE_NAME};")
cursor.execute(
(
f"CREATE TABLE IF NOT EXISTS {TABLE_NAME} ("
Expand Down
1 change: 1 addition & 0 deletions tests/integration/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
TLS_CERTIFICATES_APP_NAME = "self-signed-certificates"

MYSQL = {"localhost": "mysql", "microk8s": "mysql-k8s"}
MYSQL_ROUTER = {"localhost": "mysql-router", "microk8s": "mysql-router-k8s"}
POSTGRESQL = {"localhost": "postgresql", "microk8s": "postgresql-k8s"}
PGBOUNCER = {"localhost": "pgbouncer", "microk8s": "pgbouncer-k8s"}
MONGODB = {"localhost": "mongodb", "microk8s": "mongodb-k8s"}
Expand Down
105 changes: 98 additions & 7 deletions tests/integration/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest
from pytest_operator.plugin import OpsTest

from .constants import APP, DATA_INTEGRATOR, DATABASE_NAME, MYSQL
from .constants import APP, DATA_INTEGRATOR, DATABASE_NAME, MYSQL, MYSQL_ROUTER
from .helpers import (
check_secrets_usage_matching_juju_version,
fetch_action_database,
Expand All @@ -29,15 +29,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 @@ -53,9 +53,10 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest, cloud_name: str):
num_units=1,
series="jammy",
trust=True,
config={"profile": "testing"},
)
)
await ops_test.model.wait_for_idle(apps=[MYSQL[cloud_name]], wait_for_active=True)
await ops_test.model.wait_for_idle(apps=[MYSQL[cloud_name]], status="active")
assert ops_test.model.applications[MYSQL[cloud_name]].status == "active"
integrator_relation = await ops_test.model.add_relation(DATA_INTEGRATOR, MYSQL[cloud_name])
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL[cloud_name]])
Expand All @@ -69,6 +70,7 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest, cloud_name: str):
)

# get credential for MYSQL
logger.info(f"Get credential for {MYSQL[cloud_name]}")
credentials = await fetch_action_get_credentials(
ops_test.model.applications[DATA_INTEGRATOR].units[0]
)
Expand Down Expand Up @@ -100,7 +102,7 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest, cloud_name: str):
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[cloud_name]}:database"
)
Expand All @@ -109,7 +111,7 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest, cloud_name: str):
await ops_test.model.add_relation(DATA_INTEGRATOR, MYSQL[cloud_name])
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL[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]
)
Expand All @@ -126,3 +128,92 @@ async def test_deploy_and_relate_mysql(ops_test: OpsTest, cloud_name: str):
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[cloud_name]}:database"
)
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL[ops_test.cloud_name]])


@pytest.mark.group(1)
async def test_deploy_and_relate_mysql_router(ops_test: OpsTest, cloud_name: str):
"""Test the relation with mysql-router and database accessibility."""
logger.info(f"Test the relation with {MYSQL_ROUTER[cloud_name]}.")
num_units = 0 if cloud_name == "localhost" else 1
channel = "dpe/edge" if cloud_name == "localhost" else "8.0/edge"
await asyncio.gather(
ops_test.model.deploy(
MYSQL_ROUTER[cloud_name],
application_name=MYSQL_ROUTER[cloud_name],
channel=channel,
num_units=num_units,
series="jammy",
trust=True,
),
)
await ops_test.model.add_relation(MYSQL[cloud_name], MYSQL_ROUTER[cloud_name])
await ops_test.model.add_relation(
f"{DATA_INTEGRATOR}:mysql", f"{MYSQL_ROUTER[cloud_name]}:database"
)
await ops_test.model.wait_for_idle(
apps=[DATA_INTEGRATOR, MYSQL[cloud_name], MYSQL_ROUTER[cloud_name]],
status="active",
)
assert ops_test.model.applications[DATA_INTEGRATOR].status == "active"

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

logger.info(f"Create table on {MYSQL_ROUTER[cloud_name]}")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"create-table",
MYSQL_ROUTER[cloud_name],
json.dumps(credentials),
DATABASE_NAME,
)
assert result["ok"]
logger.info(f"Insert data in the table on {MYSQL_ROUTER[cloud_name]}")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"insert-data",
MYSQL_ROUTER[cloud_name],
json.dumps(credentials),
DATABASE_NAME,
)
assert result["ok"]
logger.info(f"Check assessibility of inserted data on {MYSQL_ROUTER[cloud_name]}")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"check-inserted-data",
MYSQL_ROUTER[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[cloud_name]}:database"
)

await ops_test.model.wait_for_idle(apps=[MYSQL_ROUTER[cloud_name], DATA_INTEGRATOR])
await ops_test.model.add_relation(DATA_INTEGRATOR, MYSQL_ROUTER[cloud_name])
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR, MYSQL_ROUTER[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[cloud_name]} with new credentials")
result = await fetch_action_database(
ops_test.model.applications[APP].units[0],
"check-inserted-data",
MYSQL_ROUTER[cloud_name],
json.dumps(new_credentials),
DATABASE_NAME,
)
assert result["ok"]
Loading