Skip to content

Commit

Permalink
refactor: clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Oct 25, 2023
1 parent 3b58897 commit 0b21135
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
15 changes: 3 additions & 12 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,23 @@ async def test_build_and_deploy(ops_test: OpsTest, charm: str, test_charm: str)
),
)

logger.debug("waiting for postgresql")
await ops_test.model.wait_for_idle(
apps=["postgresql"],
status="active",
raise_on_blocked=True,
timeout=1000,
)

logger.debug("adding postgresql relation")
await ops_test.model.integrate(APP_NAME, "postgresql:database")
await ops_test.model.wait_for_idle(
apps=[APP_NAME],
apps=[APP_NAME, "postgresql"],
status="active",
timeout=60,
timeout=1000,
)

assert ops_test.model.applications[APP_NAME].status == "active"

await ops_test.model.integrate(APP_NAME, "openfga-requires")

async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(
apps=["openfga-requires"],
status="active",
timeout=60,
)

openfga_requires_unit = ops_test.model.units[0]
openfga_requires_unit = ops_test.model.applications["openfga-requires"].units[0]
assert "running with store" in openfga_requires_unit.workload_status_message
57 changes: 27 additions & 30 deletions tests/integration/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import pytest
import yaml
from juju.action import Action
from pytest_operator.plugin import OpsTest

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -43,46 +42,32 @@ async def test_upgrade_running_application(ops_test: OpsTest, charm: str, test_c
),
)

logger.debug("waiting for postgresql")
logger.debug("adding postgresql relation")
await ops_test.model.wait_for_idle(
apps=["postgresql"],
status="active",
raise_on_blocked=True,
timeout=1000,
)

logger.debug("adding postgresql relation")
await ops_test.model.integrate(APP_NAME, "postgresql:database")

logger.debug("running schema-upgrade action")
openfga_unit = ops_test.model.units[0]
openfga_unit = ops_test.model.applications[APP_NAME].units[0]
for i in range(10):
action: Action = await openfga_unit.run_action("schema-upgrade")
action = await openfga_unit.run_action("schema-upgrade")
result = await action.wait()
logger.info("attempt {} -> action result {} {}".format(i, result.status, result.results))
if result.results == {"result": "done", "return-code": 0}:
break
time.sleep(2)

async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(
apps=[APP_NAME],
status="active",
timeout=60,
)

assert ops_test.model.applications[APP_NAME].status == "active"

await ops_test.model.integrate(APP_NAME, "openfga-requires")

async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(
apps=["openfga-requires"],
status="active",
timeout=60,
)

openfga_requires_unit = ops_test.model.units[0]
openfga_requires_unit = ops_test.model.applications["openfga-requires"].units[0]
assert "running with store" in openfga_requires_unit.workload_status_message

# Starting upgrade/refresh
Expand All @@ -91,27 +76,39 @@ async def test_upgrade_running_application(ops_test: OpsTest, charm: str, test_c
# Build and deploy charm from local source folder
logger.debug("building local charm")

charm_path = await ops_test.build_charm(".")
resources = {"openfga-image": METADATA["resources"]["openfga-image"]["upstream-source"]}
resources = {"oci-image": METADATA["resources"]["oci-image"]["upstream-source"]}

# Deploy the charm and wait for active/idle status
# Deploy the charm and wait for active/blocked status
logger.debug("refreshing running application with the new local charm")

await ops_test.model.applications[APP_NAME].refresh(
path=charm_path,
path=charm,
resources=resources,
)
await ops_test.model.block_until(
lambda: ops_test.model.applications[APP_NAME].units[0].workload_status in ["blocked", "active"],
timeout=60,
)

async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(
apps=[APP_NAME],
status="active",
timeout=60,
)
logger.debug("running schema-upgrade action")
openfga_unit = ops_test.model.applications[APP_NAME].units[0]
for i in range(10):
action = await openfga_unit.run_action("schema-upgrade")
result = await action.wait()
logger.info("attempt {} -> action result {} {}".format(i, result.status, result.results))
if result.results == {"result": "done", "return-code": 0}:
break
time.sleep(2)

await ops_test.model.wait_for_idle(
apps=[APP_NAME],
status="active",
timeout=60,
)

assert ops_test.model.applications[APP_NAME].status == "active"

upgraded_openfga_unit = ops_test.model.units[0]
upgraded_openfga_unit = ops_test.model.applications[APP_NAME].units[0]

health = await upgraded_openfga_unit.run("curl -s http://localhost:8080/healthz")
await health.wait()
Expand Down

0 comments on commit 0b21135

Please sign in to comment.