Skip to content

Commit

Permalink
Implement __repr__ for FRRoutingPrefix & CabourotteHealthcheck (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SRv6d authored Dec 5, 2023
1 parent 1640f7d commit fa816e4
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/anycastd/healthcheck/_cabourotte/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def __init__(self, name: str, *, url: str, interval: datetime.timedelta):
self.name = name
self.url = url

def __repr__(self) -> str:
return (
f"CabourotteHealthcheck(name={self.name!r}, url={self.url!r}, "
f"interval={self.interval!r})"
)

async def _check(self) -> bool:
"""Return whether the healthcheck is healthy or not."""
result = await get_result(self.name, url=self.url)
Expand Down
3 changes: 0 additions & 3 deletions src/anycastd/healthcheck/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ def __init__(self, interval: datetime.timedelta):
raise TypeError("Interval must be a timedelta.")
self.interval = interval

def __repr__(self) -> str:
return f"{self.__class__.__name__}(interval={self.interval!r})"

@final
async def is_healthy(self) -> bool:
"""Whether the healthcheck is healthy.
Expand Down
6 changes: 6 additions & 0 deletions src/anycastd/prefix/_frrouting/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def __init__(
self.vtysh = vtysh
self.executor = executor

def __repr__(self) -> str:
return (
f"FRRoutingPrefix(prefix={self.prefix!r}, vrf={self.vrf!r}, "
f"vtysh={self.vtysh!r}, executor={self.executor!r})"
)

async def is_announced(self) -> bool:
"""Returns True if the prefix is announced.
Expand Down
3 changes: 0 additions & 3 deletions src/anycastd/prefix/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ def __init__(self, prefix: IPv4Network | IPv6Network):
raise TypeError("Prefix must be an IPv4 or IPv6 network.")
self.prefix = prefix

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.prefix!r})"

@abstractmethod
async def is_announced(self) -> bool:
"""Whether the prefix is currently announced.
Expand Down
18 changes: 18 additions & 0 deletions tests/healthcheck/cabourotte/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ def test__init__non_string_url_raises_type_error():
)


@pytest.mark.parametrize(
"attributes",
[
{
"name": "test",
"url": "https://example.com",
"interval": datetime.timedelta(seconds=30),
}
],
)
def test_repr(attributes: dict):
"""The repr of a Cabourotte healthcheck is correct."""
healthcheck = CabourotteHealthcheck(**attributes)
assert repr(healthcheck) == "CabourotteHealthcheck({})".format(
", ".join(f"{k}={v!r}" for (k, v) in attributes.items())
)


@pytest.mark.asyncio
async def test__check_awaits_get_result(mocker: MockerFixture):
"""The check method awaits the result of get_result."""
Expand Down
6 changes: 0 additions & 6 deletions tests/healthcheck/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ def test__init___non_timedelta_interval_raises_type_error():
Healthcheck(interval="not a timedelta") # type: ignore


def test__repr__():
"""The repr of a subclassed healthcheck is correct."""
check = DummyHealthcheck(interval=datetime.timedelta(seconds=1))
assert repr(check) == "DummyHealthcheck(interval=datetime.timedelta(seconds=1))"


class TestHealthStatus:
"""Tests for the healthchecks status."""

Expand Down
20 changes: 20 additions & 0 deletions tests/prefix/frrouting/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pathlib import Path

from anycastd._executor import LocalExecutor
from anycastd.prefix._frrouting.main import FRRoutingPrefix


def test_repr(example_networks, example_vrfs):
"""The repr of a FRRouting prefix is correct."""
vtysh = Path("/usr/bin/vtysh")
executor = LocalExecutor()
prefix = FRRoutingPrefix(
example_networks,
vrf=example_vrfs,
vtysh=vtysh,
executor=executor,
)
assert repr(prefix) == (
f"FRRoutingPrefix(prefix={example_networks!r}, vrf={example_vrfs!r}, "
f"vtysh={vtysh!r}, executor={executor!r})"
)
6 changes: 0 additions & 6 deletions tests/prefix/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ def test__init___non_network_raises_type_error():
def test__init__ip_network(example_networks: IPv4Network | IPv6Network):
"""IPv4Network and IPv6Network prefixes are accepted."""
DummyPrefix(example_networks)


def test__repr__(example_networks: IPv4Network | IPv6Network):
"""The repr of a subclassed prefix is correct."""
prefix = DummyPrefix(example_networks)
assert repr(prefix) == f"DummyPrefix({example_networks!r})"

0 comments on commit fa816e4

Please sign in to comment.