Skip to content

Commit

Permalink
Merge branch 'main' into test-multiple-models-status-and-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloneppel authored Jun 19, 2024
2 parents 8bd4baf + 2745070 commit c1d369e
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
jobs:
lint:
name: Lint
uses: canonical/data-platform-workflows/.github/workflows/lint.yaml@v14.0.0
uses: canonical/data-platform-workflows/.github/workflows/lint.yaml@v14.1.0

unit-test:
name: Unit test charm
Expand All @@ -44,7 +44,7 @@ jobs:

build:
name: Build charm
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v14.0.0
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v14.1.0
with:
cache: true

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ jobs:

build:
name: Build charm
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v14.0.0
uses: canonical/data-platform-workflows/.github/workflows/build_charm.yaml@v14.1.0

release:
name: Release charm
needs:
- ci-tests
- build
uses: canonical/data-platform-workflows/.github/workflows/release_charm.yaml@v14.0.0
uses: canonical/data-platform-workflows/.github/workflows/release_charm.yaml@v14.1.0
with:
channel: 14/edge
artifact-prefix: ${{ needs.build.outputs.artifact-prefix }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync_issue_to_jira.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
sync:
name: Sync GitHub issue to Jira
uses: canonical/data-platform-workflows/.github/workflows/sync_issue_to_jira.yaml@v14.0.0
uses: canonical/data-platform-workflows/.github/workflows/sync_issue_to_jira.yaml@v14.1.0
with:
jira-base-url: https://warthogs.atlassian.net
jira-project-key: DPE
Expand Down
29 changes: 9 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ optional = true

[tool.poetry.group.integration.dependencies]
pytest = "^8.2.2"
pytest-github-secrets = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.0.0", subdirectory = "python/pytest_plugins/github_secrets"}
pytest-github-secrets = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.1.0", subdirectory = "python/pytest_plugins/github_secrets"}
pytest-operator = "^0.35.0"
pytest-operator-cache = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.0.0", subdirectory = "python/pytest_plugins/pytest_operator_cache"}
pytest-operator-groups = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.0.0", subdirectory = "python/pytest_plugins/pytest_operator_groups"}
pytest-operator-cache = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.1.0", subdirectory = "python/pytest_plugins/pytest_operator_cache"}
pytest-operator-groups = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.1.0", subdirectory = "python/pytest_plugins/pytest_operator_groups"}
# renovate caret doesn't work: https://github.com/renovatebot/renovate/issues/26940
juju = "<=3.5.0.0"
boto3 = "*"
Expand All @@ -80,7 +80,7 @@ landscape-api-py3 = "^0.9.0"
mailmanclient = "^3.3.5"
psycopg2-binary = "^2.9.9"
allure-pytest = "^2.13.5"
allure-pytest-collection-report = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.0.0", subdirectory = "python/pytest_plugins/allure_pytest_collection_report"}
allure-pytest-collection-report = {git = "https://github.com/canonical/data-platform-workflows", tag = "v14.1.0", subdirectory = "python/pytest_plugins/allure_pytest_collection_report"}

# Testing tools configuration
[tool.coverage.run]
Expand Down
161 changes: 161 additions & 0 deletions tests/integration/new_relations/test_relations_coherence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
import logging
import secrets
import string

import psycopg2
import pytest
from pytest_operator.plugin import OpsTest

from ..helpers import CHARM_SERIES, DATABASE_APP_NAME
from .helpers import build_connection_string
from .test_new_relations import DATA_INTEGRATOR_APP_NAME

logger = logging.getLogger(__name__)

APPLICATION_APP_NAME = "postgresql-test-app"
APP_NAMES = [DATABASE_APP_NAME, DATA_INTEGRATOR_APP_NAME]
FIRST_DATABASE_RELATION_NAME = "first-database"


@pytest.mark.group(1)
@pytest.mark.abort_on_fail
async def test_relations(ops_test: OpsTest, charm):
"""Test that check relation data."""
async with ops_test.fast_forward():
await ops_test.model.deploy(
charm,
application_name=DATABASE_APP_NAME,
num_units=1,
series=CHARM_SERIES,
config={"profile": "testing"},
)
await ops_test.model.wait_for_idle(apps=[DATABASE_APP_NAME], status="active", timeout=3000)

# Creating first time relation with user role
await ops_test.model.deploy(DATA_INTEGRATOR_APP_NAME)
await ops_test.model.applications[DATA_INTEGRATOR_APP_NAME].set_config({
"database-name": DATA_INTEGRATOR_APP_NAME.replace("-", "_"),
})
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR_APP_NAME], status="blocked")
await ops_test.model.add_relation(DATA_INTEGRATOR_APP_NAME, DATABASE_APP_NAME)
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")

connection_string = await build_connection_string(
ops_test,
DATA_INTEGRATOR_APP_NAME,
"postgresql",
database=DATA_INTEGRATOR_APP_NAME.replace("-", "_"),
)

connection = psycopg2.connect(connection_string)
connection.autocommit = True
cursor = connection.cursor()
try:
random_name = (
f"test_{''.join(secrets.choice(string.ascii_lowercase) for _ in range(10))}"
)
cursor.execute(f"CREATE DATABASE {random_name};")
assert False, "user role was able to create database"
except psycopg2.errors.InsufficientPrivilege:
pass
finally:
connection.close()

with psycopg2.connect(connection_string) as connection:
connection.autocommit = True
with connection.cursor() as cursor:
# Check that it's possible to write and read data from the database that
# was created for the application.
cursor.execute("DROP TABLE IF EXISTS test;")
cursor.execute("CREATE TABLE test(data TEXT);")
cursor.execute("INSERT INTO test(data) VALUES('some data');")
cursor.execute("SELECT data FROM test;")
data = cursor.fetchone()
assert data[0] == "some data"
connection.close()

await ops_test.model.applications[DATABASE_APP_NAME].remove_relation(
f"{DATABASE_APP_NAME}:database", f"{DATA_INTEGRATOR_APP_NAME}:postgresql"
)

# Re-relation with admin role with checking permission
await ops_test.model.applications[DATA_INTEGRATOR_APP_NAME].set_config({
"database-name": DATA_INTEGRATOR_APP_NAME.replace("-", "_"),
"extra-user-roles": "admin",
})
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR_APP_NAME], status="blocked")
await ops_test.model.add_relation(DATA_INTEGRATOR_APP_NAME, DATABASE_APP_NAME)
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")

connection_string = await build_connection_string(
ops_test,
DATA_INTEGRATOR_APP_NAME,
"postgresql",
database=DATA_INTEGRATOR_APP_NAME.replace("-", "_"),
)
try:
connection = psycopg2.connect(connection_string)
connection.autocommit = True
cursor = connection.cursor()
random_name = (
f"test_{''.join(secrets.choice(string.ascii_lowercase) for _ in range(10))}"
)
cursor.execute(f"CREATE DATABASE {random_name};")
except psycopg2.errors.InsufficientPrivilege:
assert (
False
), f"failed connect to {random_name} or run a statement in the following database"
finally:
connection.close()

await ops_test.model.applications[DATABASE_APP_NAME].remove_relation(
f"{DATABASE_APP_NAME}:database", f"{DATA_INTEGRATOR_APP_NAME}:postgresql"
)

# Re-relation again with user role and checking write data
await ops_test.model.applications[DATA_INTEGRATOR_APP_NAME].set_config({
"database-name": DATA_INTEGRATOR_APP_NAME.replace("-", "_"),
"extra-user-roles": "",
})
await ops_test.model.wait_for_idle(apps=[DATA_INTEGRATOR_APP_NAME], status="blocked")
await ops_test.model.add_relation(DATA_INTEGRATOR_APP_NAME, DATABASE_APP_NAME)
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")

for database in [
DATA_INTEGRATOR_APP_NAME.replace("-", "_"),
"postgres",
]:
logger.info(f"connecting to the following database: {database}")
connection_string = await build_connection_string(
ops_test, DATA_INTEGRATOR_APP_NAME, "postgresql", database=database
)
connection = None
should_fail = database == "postgres"
try:
with psycopg2.connect(
connection_string
) as connection, connection.cursor() as cursor:
cursor.execute("SELECT data FROM test;")
data = cursor.fetchone()
assert data[0] == "some data"

# Write some data (it should fail in the "postgres" database).
random_name = f"test_{''.join(secrets.choice(string.ascii_lowercase) for _ in range(10))}"
cursor.execute(f"CREATE TABLE {random_name}(data TEXT);")
if should_fail:
assert (
False
), f"failed to run a statement in the following database: {database}"
except psycopg2.errors.InsufficientPrivilege as e:
if not should_fail:
logger.exception(e)
assert False, f"failed to connect to or run a statement in the following database: {database}"
except psycopg2.OperationalError as e:
if not should_fail:
logger.exception(e)
finally:
if connection is not None:
connection.close()

0 comments on commit c1d369e

Please sign in to comment.