Skip to content

Commit

Permalink
chore: better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Dec 17, 2024
1 parent fda9a90 commit 6710b0c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 62 deletions.
6 changes: 3 additions & 3 deletions juju/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from ..tag import application as application_tag
from ..url import URL, Schema
from ..version import DEFAULT_ARCHITECTURE
from . import idle
from . import _idle

if TYPE_CHECKING:
from ..application import Application
Expand Down Expand Up @@ -3301,15 +3301,15 @@ async def new_wait_for_idle(
deadline = None if timeout is None else time.monotonic() + timeout

async def status_on_demand():
yield idle._check(
yield _idle.check(
await self.get_status(),
apps=apps,
raise_on_error=raise_on_error,
raise_on_blocked=raise_on_blocked,
status=status,
)

async for done in idle._loop(
async for done in _idle.loop(
status_on_demand(),
apps=apps,
wait_for_exact_units=wait_for_exact_units,
Expand Down
16 changes: 8 additions & 8 deletions juju/model/idle.py → juju/model/_idle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@


@dataclass
class _CheckStatus:
"""Return type for a single interaction, that is _check()."""
class CheckStatus:
"""Return type check(), represents single loop iteration."""

units: set[str]
"""All units visible at this point."""
Expand All @@ -32,8 +32,8 @@ class _CheckStatus:
"""Units with stable agent status (FIXME details)."""


async def _loop(
foo: AsyncIterable[_CheckStatus | None],
async def loop(
foo: AsyncIterable[CheckStatus | None],
*,
apps: frozenset[str],
wait_for_exact_units: int | None = None,
Expand Down Expand Up @@ -99,15 +99,15 @@ async def _loop(
yield rv


def _check(
def check(
full_status: FullStatus,
*,
apps: frozenset[str],
raise_on_error: bool,
raise_on_blocked: bool,
status: str | None,
) -> _CheckStatus | None:
"""A single interaction of a wait_for_idle loop."""
) -> CheckStatus | None:
"""A single iteration of a wait_for_idle loop."""
for app_name in apps:
if not full_status.applications.get(app_name):
logger.info("Waiting for app %r", app_name)
Expand Down Expand Up @@ -172,7 +172,7 @@ def _check(
if app.status.status == "blocked" and raise_on_blocked:
raise JujuAppError(f"{app_name!r} is blocked: {app.status.info!r}")

rv = _CheckStatus(set(), set(), set())
rv = CheckStatus(set(), set(), set())

for app_name in apps:
ready_units = []
Expand Down
65 changes: 30 additions & 35 deletions tests/unit/test_idle_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Licensed under the Apache V2, see LICENCE file for details.
from __future__ import annotations

# pyright: reportPrivateUsage=false
import copy
import json
from typing import Any
Expand All @@ -14,12 +13,12 @@
)
from juju.client.facade import _convert_response
from juju.errors import JujuAgentError, JujuAppError, JujuMachineError, JujuUnitError
from juju.model.idle import _check, _CheckStatus
from juju.model._idle import CheckStatus, check


def test_check_status(full_status: FullStatus, kwargs):
status = _check(full_status, **kwargs)
assert status == _CheckStatus(
status = check(full_status, **kwargs)
assert status == CheckStatus(
{
"grafana-agent-k8s/0",
"hexanator/0",
Expand All @@ -38,33 +37,33 @@ def test_check_status(full_status: FullStatus, kwargs):

def test_check_status_missing_app(full_status: FullStatus, kwargs):
kwargs["apps"] = ["missing", "hexanator"]
status = _check(full_status, **kwargs)
status = check(full_status, **kwargs)
assert status is None


def test_check_status_is_selective(full_status: FullStatus, kwargs):
kwargs["apps"] = ["hexanator"]
status = _check(full_status, **kwargs)
assert status == _CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())
status = check(full_status, **kwargs)
assert status == CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())


def test_no_apps(full_status: FullStatus, kwargs):
kwargs["apps"] = []
status = _check(full_status, **kwargs)
assert status == _CheckStatus(set(), set(), set())
status = check(full_status, **kwargs)
assert status == CheckStatus(set(), set(), set())


def test_missing_app(full_status: FullStatus, kwargs):
kwargs["apps"] = ["missing"]
status = _check(full_status, **kwargs)
status = check(full_status, **kwargs)
assert status is None


def test_no_units(response: dict[str, Any], kwargs):
response["response"]["applications"]["hexanator"]["units"].clear()
kwargs["apps"] = ["hexanator"]
status = _check(convert(response), **kwargs)
assert status == _CheckStatus(set(), set(), set())
status = check(convert(response), **kwargs)
assert status == CheckStatus(set(), set(), set())


def test_app_error(response: dict[str, Any], kwargs):
Expand All @@ -76,7 +75,7 @@ def test_app_error(response: dict[str, Any], kwargs):
kwargs["raise_on_error"] = True

with pytest.raises(JujuAppError) as e:
_check(convert(response), **kwargs)
check(convert(response), **kwargs)

assert "big problem" in str(e)

Expand All @@ -87,16 +86,16 @@ def test_exact_count(response: dict[str, Any], kwargs):

kwargs["apps"] = ["hexanator"]

status = _check(convert(response), **kwargs)
assert status == _CheckStatus(
status = check(convert(response), **kwargs)
assert status == CheckStatus(
{"hexanator/0", "hexanator/1"}, {"hexanator/0", "hexanator/1"}, set()
)


def test_ready_units(full_status: FullStatus, kwargs):
kwargs["apps"] = ["mysql-test-app"]
status = _check(full_status, **kwargs)
assert status == _CheckStatus(
status = check(full_status, **kwargs)
assert status == CheckStatus(
{"mysql-test-app/0", "mysql-test-app/1"},
{"mysql-test-app/0", "mysql-test-app/1"},
set(),
Expand All @@ -106,10 +105,8 @@ def test_ready_units(full_status: FullStatus, kwargs):
def test_active_units(full_status: FullStatus, kwargs):
kwargs["apps"] = ["mysql-test-app"]
kwargs["status"] = "active"
status = _check(full_status, **kwargs)
assert status == _CheckStatus(
{"mysql-test-app/0", "mysql-test-app/1"}, set(), set()
)
status = check(full_status, **kwargs)
assert status == CheckStatus({"mysql-test-app/0", "mysql-test-app/1"}, set(), set())


def test_ready_unit_requires_idle_agent(response: dict[str, Any], kwargs):
Expand All @@ -120,7 +117,7 @@ def test_ready_unit_requires_idle_agent(response: dict[str, Any], kwargs):
kwargs["apps"] = ["hexanator"]
kwargs["status"] = "active"

status = _check(convert(response), **kwargs)
status = check(convert(response), **kwargs)
assert status
assert status.units == {"hexanator/0", "hexanator/1"}
assert status.ready_units == {"hexanator/0"}
Expand All @@ -134,10 +131,8 @@ def test_ready_unit_requires_workload_status(response: dict[str, Any], kwargs):
kwargs["apps"] = ["hexanator"]
kwargs["status"] = "active"

status = _check(convert(response), **kwargs)
assert status == _CheckStatus(
{"hexanator/0", "hexanator/1"}, {"hexanator/0"}, set()
)
status = check(convert(response), **kwargs)
assert status == CheckStatus({"hexanator/0", "hexanator/1"}, {"hexanator/0"}, set())


def test_agent_error(response: dict[str, Any], kwargs):
Expand All @@ -149,7 +144,7 @@ def test_agent_error(response: dict[str, Any], kwargs):
kwargs["raise_on_error"] = True

with pytest.raises(JujuAgentError) as e:
_check(convert(response), **kwargs)
check(convert(response), **kwargs)

assert "hexanator/0" in str(e)
assert "agent problem" in str(e)
Expand All @@ -164,7 +159,7 @@ def test_workload_error(response: dict[str, Any], kwargs):
kwargs["raise_on_error"] = True

with pytest.raises(JujuUnitError) as e:
_check(convert(response), **kwargs)
check(convert(response), **kwargs)

assert "hexanator/0" in str(e)
assert "workload problem" in str(e)
Expand All @@ -186,8 +181,8 @@ def test_machine_ok(response: dict[str, Any], kwargs):
kwargs["apps"] = ["hexanator"]
kwargs["raise_on_error"] = True

status = _check(convert(response), **kwargs)
assert status == _CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())
status = check(convert(response), **kwargs)
assert status == CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())


def test_machine_error(response: dict[str, Any], kwargs):
Expand All @@ -206,7 +201,7 @@ def test_machine_error(response: dict[str, Any], kwargs):
kwargs["raise_on_error"] = True

with pytest.raises(JujuMachineError) as e:
_check(convert(response), **kwargs)
check(convert(response), **kwargs)

assert "potato" in str(e)

Expand All @@ -220,7 +215,7 @@ def test_app_blocked(response: dict[str, Any], kwargs):
kwargs["raise_on_blocked"] = True

with pytest.raises(JujuAppError) as e:
_check(convert(response), **kwargs)
check(convert(response), **kwargs)

assert "big problem" in str(e)

Expand All @@ -234,7 +229,7 @@ def test_unit_blocked(response: dict[str, Any], kwargs):
kwargs["raise_on_blocked"] = True

with pytest.raises(JujuUnitError) as e:
_check(convert(response), **kwargs)
check(convert(response), **kwargs)

assert "small problem" in str(e)

Expand All @@ -248,8 +243,8 @@ def test_maintenance(response: dict[str, Any], kwargs):
kwargs["apps"] = ["hexanator"]
kwargs["status"] = "maintenance"

status = _check(convert(response), **kwargs)
assert status == _CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())
status = check(convert(response), **kwargs)
assert status == CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())


@pytest.fixture
Expand Down
11 changes: 5 additions & 6 deletions tests/unit/test_idle_check_subordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Licensed under the Apache V2, see LICENCE file for details.
from __future__ import annotations

# pyright: reportPrivateUsage=false
import json
from typing import Any

Expand All @@ -12,21 +11,21 @@
FullStatus,
)
from juju.client.facade import _convert_response
from juju.model.idle import _check, _CheckStatus
from juju.model._idle import CheckStatus, check


def test_subordinate_apps(response: dict[str, Any], kwargs):
status = _check(convert(response), **kwargs)
assert status == _CheckStatus({"ntp/0", "ubuntu/0"}, {"ntp/0", "ubuntu/0"}, set())
status = check(convert(response), **kwargs)
assert status == CheckStatus({"ntp/0", "ubuntu/0"}, {"ntp/0", "ubuntu/0"}, set())


def test_subordinate_is_selective(response, kwargs):
subordinates = response["response"]["applications"]["ubuntu"]["units"]["ubuntu/0"][
"subordinates"
]
subordinates["some-other/0"] = subordinates["ntp/0"]
status = _check(convert(response), **kwargs)
assert status == _CheckStatus({"ntp/0", "ubuntu/0"}, {"ntp/0", "ubuntu/0"}, set())
status = check(convert(response), **kwargs)
assert status == CheckStatus({"ntp/0", "ubuntu/0"}, {"ntp/0", "ubuntu/0"}, set())


@pytest.fixture
Expand Down
18 changes: 8 additions & 10 deletions tests/unit/test_idle_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from __future__ import annotations

import pytest

# pyright: reportPrivateUsage=false
from freezegun import freeze_time

from juju.model.idle import _CheckStatus, _loop
from juju.model._idle import CheckStatus, loop


# Missing tests
Expand All @@ -24,14 +22,14 @@
@pytest.mark.xfail(reason="FIXME I misunderstood what 'idle' means")
async def test_at_least_units():
async def checks():
yield _CheckStatus({"u/0", "u/1", "u/2"}, {"u/0"}, set())
yield _CheckStatus({"u/0", "u/1", "u/2"}, {"u/0", "u/1"}, set())
yield _CheckStatus({"u/0", "u/1", "u/2"}, {"u/0", "u/1", "u/2"}, set())
yield CheckStatus({"u/0", "u/1", "u/2"}, {"u/0"}, set())
yield CheckStatus({"u/0", "u/1", "u/2"}, {"u/0", "u/1"}, set())
yield CheckStatus({"u/0", "u/1", "u/2"}, {"u/0", "u/1", "u/2"}, set())

with freeze_time():
assert [
v
async for v in _loop(
async for v in loop(
checks(),
apps=frozenset(["u"]),
wait_for_units=2,
Expand All @@ -41,8 +39,8 @@ async def checks():


async def test_ping_pong():
good = _CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())
bad = _CheckStatus({"hexanator/0"}, set(), set())
good = CheckStatus({"hexanator/0"}, {"hexanator/0"}, set())
bad = CheckStatus({"hexanator/0"}, set(), set())

async def checks():
with freeze_time() as clock:
Expand All @@ -54,7 +52,7 @@ async def checks():

assert [
v
async for v in _loop(
async for v in loop(
checks(),
apps=frozenset(["hexanator"]),
wait_for_units=1,
Expand Down

0 comments on commit 6710b0c

Please sign in to comment.