Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten and rename service tests #34

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 0 additions & 112 deletions tests/test_main.py

This file was deleted.

92 changes: 92 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import pytest
from anycastd.core import Service
from pytest_mock import MockerFixture

from tests.dummy import DummyHealthcheck, DummyPrefix


@pytest.fixture
def example_service(ipv4_example_network, ipv6_example_network):
return Service(
name="Example Service",
prefixes=(DummyPrefix(ipv4_example_network), DummyPrefix(ipv6_example_network)),
health_checks=(DummyHealthcheck(), DummyHealthcheck()),
)


@pytest.mark.asyncio
async def test_run_awaits_status(mocker: MockerFixture, example_service):
"""When run, the service awaits the status of the health checks."""
mock_is_healthy = mocker.patch.object(example_service, "is_healthy")
await example_service.run(_only_once=True)
mock_is_healthy.assert_awaited_once()


@pytest.mark.asyncio
async def test_run_announces_when_healthy(mocker: MockerFixture, example_service):
"""When run, all prefixes are announced when the service is healthy."""
mocker.patch.object(example_service, "is_healthy", return_value=True)
mock_prefixes = tuple(
mocker.create_autospec(_, spec_set=True) for _ in example_service.prefixes
)
mocker.patch.object(example_service, "prefixes", mock_prefixes)
await example_service.run(_only_once=True)
for mock_prefix in mock_prefixes:
mock_prefix.announce.assert_awaited_once()


@pytest.mark.asyncio
async def test_run_denounces_when_unhealthy(mocker: MockerFixture, example_service):
"""When run, all prefixes are denounced when the service is unhealthy."""
mocker.patch.object(example_service, "is_healthy", return_value=False)
mock_prefixes = tuple(
mocker.create_autospec(_, spec_set=True) for _ in example_service.prefixes
)
mocker.patch.object(example_service, "prefixes", mock_prefixes)
await example_service.run(_only_once=True)
for mock_prefix in mock_prefixes:
mock_prefix.denounce.assert_awaited_once()


@pytest.mark.asyncio
async def test_healthy_when_all_checks_healthy(mocker: MockerFixture, example_service):
"""The service is healthy if all healthchecks are healthy."""
mock_health_checks = tuple(
mocker.create_autospec(_, spec_set=True) for _ in example_service.health_checks
)
mocker.patch.object(example_service, "health_checks", mock_health_checks)
for mock_health_check in mock_health_checks:
mock_health_check.is_healthy.return_value = True
result = await example_service.is_healthy()
assert result is True


@pytest.mark.asyncio
async def test_unhealthy_when_one_check_unhealthy(
mocker: MockerFixture, example_service
):
"""The service is unhealthy if one healthcheck is unhealthy."""
mock_health_checks = tuple(
mocker.create_autospec(_, spec_set=True) for _ in example_service.health_checks
)
mocker.patch.object(example_service, "health_checks", mock_health_checks)
for mock_health_check in mock_health_checks:
mock_health_check.is_healthy.return_value = True
mock_health_checks[1].is_healthy.return_value = False
result = await example_service.is_healthy()
assert result is False


@pytest.mark.asyncio
async def test_unhealthy_when_all_checks_unhealthy(
mocker: MockerFixture, example_service
):
"""The service is unhealthy if all healthchecks are unhealthy."""
mock_health_checks = tuple(
mocker.create_autospec(_, spec_set=True) for _ in example_service.health_checks
)
mocker.patch.object(example_service, "health_checks", mock_health_checks)
for mock_health_check in mock_health_checks:
mock_health_check.is_healthy.return_value = False
result = await example_service.is_healthy()
assert result is False
Loading