Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
nrobinaubertin committed Oct 23, 2024
1 parent bb384d7 commit 7a2a6fd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
11 changes: 8 additions & 3 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tempfile
import time

import juju
import ops
from pytest_operator.plugin import OpsTest

Expand Down Expand Up @@ -148,7 +149,7 @@ async def dispatch_to_unit(


async def generate_anycharm_relation(
app: ops.model.Application,
app: juju.application.Application,
ops_test: OpsTest,
any_charm_name: str,
dns_entries: list[models.DnsEntry],
Expand Down Expand Up @@ -247,7 +248,9 @@ async def dig_query(
return result


async def get_active_unit(app: ops.model.Application, ops_test: OpsTest) -> ops.model.Unit | None:
async def get_active_unit(
app: juju.application.Application, ops_test: OpsTest
) -> ops.model.Unit | None:
"""Get the current active unit if it exists.
Args:
Expand Down Expand Up @@ -277,7 +280,9 @@ async def get_active_unit(app: ops.model.Application, ops_test: OpsTest) -> ops.
return None


async def check_if_active_unit_exists(app: ops.model.Application, ops_test: OpsTest) -> bool:
async def check_if_active_unit_exists(
app: juju.application.Application, ops_test: OpsTest
) -> bool:
"""Check if an active unit exists and is reachable.
Args:
Expand Down
53 changes: 23 additions & 30 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import time

import juju
import ops
import pytest
import requests
Expand All @@ -22,20 +23,18 @@

@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_admin_reachable(app: ops.model.Application, ops_test: OpsTest):
async def test_admin_reachable(app: juju.application.Application, ops_test: OpsTest):
"""
arrange: build and deploy the charm.
act: nothing.
assert: that the admin of the API is reachable.
"""
# Application actually does have units
unit = app.units[0] # type: ignore
unit = app.units[0]

# Mypy has difficulty with ActiveStatus
assert unit.workload_status == ops.model.ActiveStatus.name # type: ignore
assert unit.workload_status == ops.model.ActiveStatus.name

# Mypy doesn't know that Application has set_config()
await app.set_config({"django_allowed_hosts": ""}) # type: ignore
await app.set_config({"django_allowed_hosts": ""})
time.sleep(10)

# Test that the admin is not reachable
Expand All @@ -51,8 +50,7 @@ async def test_admin_reachable(app: ops.model.Application, ops_test: OpsTest):
except requests.RequestException as e:
logger.error("Unexpected error after config change: %s", e)

# Mypy doesn't know that Application has set_config()
await app.set_config({"django_allowed_hosts": "*"}) # type: ignore
await app.set_config({"django_allowed_hosts": "*"})
time.sleep(10)

# Test that the admin is reachable
Expand All @@ -65,52 +63,50 @@ async def test_admin_reachable(app: ops.model.Application, ops_test: OpsTest):

@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_acl_actions(app: ops.model.Application):
async def test_acl_actions(app: juju.application.Application):
"""
arrange: build and deploy the charm.
act: nothing.
assert: that the acl actions do the right thing.
"""
# Application actually does have units
unit = app.units[0] # type: ignore
unit = app.units[0]

# Mypy has difficulty with ActiveStatus
assert unit.workload_status == ops.model.ActiveStatus.name # type: ignore
assert unit.workload_status == ops.model.ActiveStatus.name

zone = "test-zone"

action_create_acl: Action = await unit.run_action( # type: ignore
action_create_acl: Action = await unit.run_action(
"create-acl",
**{"service-account": "test-service-account", "zone": zone},
)
await action_create_acl.wait()
assert action_create_acl.status == "completed"
assert "ACL created successfully" in action_create_acl.results["result"]

action_check_acl: Action = await unit.run_action( # type: ignore
action_check_acl: Action = await unit.run_action(
"check-acl",
**{"service-account": "test-service-account", "zone": zone},
)
await action_check_acl.wait()
assert action_check_acl.status == "completed"
assert "ACL exists" in action_check_acl.results["result"]

action_list_acl: Action = await unit.run_action( # type: ignore
action_list_acl: Action = await unit.run_action(
"list-acl",
)
await action_list_acl.wait()
assert action_list_acl.status == "completed"
assert f"test-service-account - {zone}" in action_list_acl.results["result"]

action_delete_acl: Action = await unit.run_action( # type: ignore
action_delete_acl: Action = await unit.run_action(
"delete-acl",
**{"service-account": "test-service-account", "zone": zone},
)
await action_delete_acl.wait()
assert action_delete_acl.status == "completed"
assert "ACL deleted successfully" in action_delete_acl.results["result"]

action_check_acl: Action = await unit.run_action( # type: ignore
action_check_acl = await unit.run_action(
"check-acl",
**{"service-account": "test-service-account", "zone": zone},
)
Expand All @@ -121,17 +117,15 @@ async def test_acl_actions(app: ops.model.Application):

@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_lifecycle(app: ops.model.Application, ops_test: OpsTest):
async def test_lifecycle(app: juju.application.Application, ops_test: OpsTest):
"""
arrange: build and deploy the charm.
act: nothing.
assert: that the charm ends up in an active state.
"""
# Application actually does have units
unit = app.units[0] # type: ignore
unit = app.units[0]

# Mypy has difficulty with ActiveStatus
assert unit.workload_status == ops.model.ActiveStatus.name # type: ignore
assert unit.workload_status == ops.model.ActiveStatus.name

status = await tests.integration.helpers.dig_query(
ops_test,
Expand Down Expand Up @@ -167,14 +161,13 @@ async def test_lifecycle(app: ops.model.Application, ops_test: OpsTest):

@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_basic_dns_config(app: ops.model.Application, ops_test: OpsTest):
async def test_basic_dns_config(app: juju.application.Application, ops_test: OpsTest):
"""
arrange: build, deploy the charm and change its configuration.
act: request the test domain.
assert: the output of the dig command is the expected one
"""
# Application actually does have units
unit = app.units[0] # type: ignore
unit = app.units[0]

test_zone_def = f"""zone "dns.test" IN {{
type primary;
Expand Down Expand Up @@ -325,7 +318,7 @@ async def test_basic_dns_config(app: ops.model.Application, ops_test: OpsTest):
@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_dns_record_relation(
app: ops.model.Application,
app: juju.application.Application,
ops_test: OpsTest,
model: Model,
status: ops.model.StatusBase,
Expand Down Expand Up @@ -356,11 +349,11 @@ async def test_dns_record_relation(

await model.wait_for_idle()

await tests.integration.helpers.force_reload_bind(ops_test, app.units[0]) # type: ignore
await tests.integration.helpers.force_reload_bind(ops_test, app.units[0])
await model.wait_for_idle()

# Test the status of the bind-operator instance
assert app.units[0].workload_status == status.name # type: ignore
assert app.units[0].workload_status == status.name

# Test if the records give the correct results
# Do that only if we have an active status
Expand All @@ -370,7 +363,7 @@ async def test_dns_record_relation(

result = await tests.integration.helpers.dig_query(
ops_test,
app.units[0], # type: ignore
app.units[0],
f"@127.0.0.1 {entry.host_label}.{entry.domain} {entry.record_type} +short",
retry=True,
wait=5,
Expand Down
16 changes: 6 additions & 10 deletions tests/integration/test_multi_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging

import ops
import juju
import pytest
from pytest_operator.plugin import Model, OpsTest

Expand All @@ -19,7 +19,7 @@
@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_multi_units(
app: ops.model.Application,
app: juju.application.Application,
ops_test: OpsTest,
model: Model,
):
Expand Down Expand Up @@ -55,8 +55,7 @@ async def test_multi_units(

# Start by testing that everything is fine
assert await tests.integration.helpers.check_if_active_unit_exists(app, ops_test)
# Application actually does have units
for unit in app.units: # type: ignore
for unit in app.units:
await tests.integration.helpers.force_reload_bind(ops_test, unit)
await model.wait_for_idle()
assert (
Expand All @@ -76,8 +75,7 @@ async def test_multi_units(
await ops_test.juju(*(add_unit_cmd.split(" ")))
await model.wait_for_idle()
assert await tests.integration.helpers.check_if_active_unit_exists(app, ops_test)
# Application actually does have units
for unit in app.units: # type: ignore
for unit in app.units:
await tests.integration.helpers.force_reload_bind(ops_test, unit)
await model.wait_for_idle()
assert (
Expand Down Expand Up @@ -109,8 +107,7 @@ async def test_multi_units(
],
)
await model.wait_for_idle()
# Application actually does have units
for unit in app.units: # type: ignore
for unit in app.units:
await tests.integration.helpers.force_reload_bind(ops_test, unit)
await model.wait_for_idle()
assert (
Expand All @@ -133,8 +130,7 @@ async def test_multi_units(
await ops_test.juju(*(remove_unit_cmd.split(" ")))
await model.wait_for_idle()
assert await tests.integration.helpers.check_if_active_unit_exists(app, ops_test)
# Application actually does have units
for unit in app.units: # type: ignore
for unit in app.units:
await tests.integration.helpers.force_reload_bind(ops_test, unit)
await model.wait_for_idle()
assert (
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import time

import ops
import juju
import pytest
from pytest_operator.plugin import Model, OpsTest

Expand All @@ -24,7 +24,7 @@

async def deploy_any_charm(
*,
app: ops.model.Application,
app: juju.application.Application,
ops_test: OpsTest,
model: Model,
any_app_number: int,
Expand Down Expand Up @@ -62,7 +62,7 @@ async def deploy_any_charm(
@pytest.mark.abort_on_fail
@pytest.mark.skip(reason="Scaling test")
async def test_lots_of_applications(
app: ops.model.Application,
app: juju.application.Application,
ops_test: OpsTest,
model: Model,
):
Expand Down Expand Up @@ -139,7 +139,7 @@ async def test_lots_of_applications(
@pytest.mark.abort_on_fail
@pytest.mark.skip(reason="Scaling test")
async def test_lots_of_record_requests(
app: ops.model.Application,
app: juju.application.Application,
ops_test: OpsTest,
model: Model,
):
Expand Down

0 comments on commit 7a2a6fd

Please sign in to comment.