Skip to content

Commit

Permalink
Turn --use-old-replica-state into a parametrized fixture
Browse files Browse the repository at this point in the history
Instead of requiring the user to run the test suite with and without the
--use-old-replica-state flag, we introduce an 'old_replica_state()'
parametrized fixture that is used only when needed (i.e. in
test_cluster_{has_replica,node_count}.py).
  • Loading branch information
dlax committed Oct 3, 2023
1 parent fea8904 commit 597f07e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 60 deletions.
14 changes: 4 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,15 @@ erroneously.
The tests are executed automatically for each PR using the ci (see
`.github/workflow/lint.yml` and `.github/workflow/tests.yml`).

Running the tests manually:
Running the tests,

* Using patroni's nominal replica state of `streaming` (since v3.0.4):
* manually:

```bash
pytest ./tests
pytest tests
```

* Using patroni's nominal replica state of `running` (before v3.0.4):
```bash
pytest --use-old-replica-state ./tests
```
* Using tox:
* or using tox:

```bash
tox -e lint # mypy + flake8 + black + isort ° codespell
Expand Down
24 changes: 8 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@
from .tools import my_mock


def pytest_addoption(parser: Any) -> None:
"""
Add CLI options to `pytest` to pass those options to the test cases.
These options are used in `pytest_generate_tests`.
"""
parser.addoption("--use-old-replica-state", action="store_true", default=False)


def pytest_generate_tests(metafunc: Any) -> None:
metafunc.parametrize(
"use_old_replica_state", [metafunc.config.getoption("use_old_replica_state")]
)
@pytest.fixture(
params=[False, True],
ids=lambda v: "new-replica-state" if v else "old-replica-state",
)
def old_replica_state(request: Any) -> Any:
return request.param


@pytest.fixture
def fake_restapi(
mocker: MockerFixture, use_old_replica_state: bool
) -> Callable[..., Any]:
return partial(my_mock, mocker, use_old_replica_state=use_old_replica_state)
def fake_restapi(mocker: MockerFixture) -> Callable[..., Any]:
return partial(my_mock, mocker)


@pytest.fixture
Expand Down
30 changes: 16 additions & 14 deletions tests/test_cluster_has_replica.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@


# TODO Lag threshold tests
def test_cluster_has_relica_ok(runner: CliRunner, fake_restapi) -> None:
fake_restapi("cluster_has_replica_ok")
def test_cluster_has_relica_ok(
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ok", use_old_replica_state=old_replica_state)
result = runner.invoke(
main, ["-e", "https://10.20.199.3:8008", "cluster_has_replica"]
)
Expand All @@ -17,9 +19,9 @@ def test_cluster_has_relica_ok(runner: CliRunner, fake_restapi) -> None:


def test_cluster_has_replica_ok_with_count_thresholds(
runner: CliRunner, fake_restapi
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ok")
fake_restapi("cluster_has_replica_ok", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -40,9 +42,9 @@ def test_cluster_has_replica_ok_with_count_thresholds(


def test_cluster_has_replica_ok_with_sync_count_thresholds(
runner: CliRunner, fake_restapi
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ok")
fake_restapi("cluster_has_replica_ok", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -61,9 +63,9 @@ def test_cluster_has_replica_ok_with_sync_count_thresholds(


def test_cluster_has_replica_ok_with_count_thresholds_lag(
runner: CliRunner, fake_restapi
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ok_lag")
fake_restapi("cluster_has_replica_ok_lag", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -86,9 +88,9 @@ def test_cluster_has_replica_ok_with_count_thresholds_lag(


def test_cluster_has_replica_ko_with_count_thresholds(
runner: CliRunner, fake_restapi
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ko")
fake_restapi("cluster_has_replica_ko", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -109,9 +111,9 @@ def test_cluster_has_replica_ko_with_count_thresholds(


def test_cluster_has_replica_ko_with_sync_count_thresholds(
runner: CliRunner, fake_restapi
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ko")
fake_restapi("cluster_has_replica_ko", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -132,9 +134,9 @@ def test_cluster_has_replica_ko_with_sync_count_thresholds(


def test_cluster_has_replica_ko_with_count_thresholds_and_lag(
runner: CliRunner, fake_restapi
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_has_replica_ko_lag")
fake_restapi("cluster_has_replica_ko_lag", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand Down
42 changes: 26 additions & 16 deletions tests/test_cluster_node_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

from check_patroni.cli import main

# use_old_replica_state


def test_cluster_node_count_ok(
runner: CliRunner, fake_restapi, use_old_replica_state: bool
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_node_count_ok")
fake_restapi("cluster_node_count_ok", use_old_replica_state=old_replica_state)
result = runner.invoke(
main, ["-e", "https://10.20.199.3:8008", "cluster_node_count"]
)
assert result.exit_code == 0
if use_old_replica_state:
if old_replica_state:
assert (
result.output
== "CLUSTERNODECOUNT OK - members is 3 | healthy_members=3 members=3 role_leader=1 role_replica=2 state_running=3\n"
Expand All @@ -24,9 +26,9 @@ def test_cluster_node_count_ok(


def test_cluster_node_count_ok_with_thresholds(
runner: CliRunner, fake_restapi, use_old_replica_state: bool
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_node_count_ok")
fake_restapi("cluster_node_count_ok", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -44,7 +46,7 @@ def test_cluster_node_count_ok_with_thresholds(
],
)
assert result.exit_code == 0
if use_old_replica_state:
if old_replica_state:
assert (
result.output
== "CLUSTERNODECOUNT OK - members is 3 | healthy_members=3;@2;@1 members=3;@1;@2 role_leader=1 role_replica=2 state_running=3\n"
Expand All @@ -57,9 +59,11 @@ def test_cluster_node_count_ok_with_thresholds(


def test_cluster_node_count_healthy_warning(
runner: CliRunner, fake_restapi, use_old_replica_state: bool
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_node_count_healthy_warning")
fake_restapi(
"cluster_node_count_healthy_warning", use_old_replica_state=old_replica_state
)
result = runner.invoke(
main,
[
Expand All @@ -73,7 +77,7 @@ def test_cluster_node_count_healthy_warning(
],
)
assert result.exit_code == 1
if use_old_replica_state:
if old_replica_state:
assert (
result.output
== "CLUSTERNODECOUNT WARNING - healthy_members is 2 (outside range @0:2) | healthy_members=2;@2;@1 members=2 role_leader=1 role_replica=1 state_running=2\n"
Expand All @@ -85,8 +89,12 @@ def test_cluster_node_count_healthy_warning(
)


def test_cluster_node_count_healthy_critical(runner: CliRunner, fake_restapi) -> None:
fake_restapi("cluster_node_count_healthy_critical")
def test_cluster_node_count_healthy_critical(
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi(
"cluster_node_count_healthy_critical", use_old_replica_state=old_replica_state
)
result = runner.invoke(
main,
[
Expand All @@ -107,9 +115,9 @@ def test_cluster_node_count_healthy_critical(runner: CliRunner, fake_restapi) ->


def test_cluster_node_count_warning(
runner: CliRunner, fake_restapi, use_old_replica_state: bool
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_node_count_warning")
fake_restapi("cluster_node_count_warning", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand All @@ -123,7 +131,7 @@ def test_cluster_node_count_warning(
],
)
assert result.exit_code == 1
if use_old_replica_state:
if old_replica_state:
assert (
result.stdout
== "CLUSTERNODECOUNT WARNING - members is 2 (outside range @0:2) | healthy_members=2 members=2;@2;@1 role_leader=1 role_replica=1 state_running=2\n"
Expand All @@ -135,8 +143,10 @@ def test_cluster_node_count_warning(
)


def test_cluster_node_count_critical(runner: CliRunner, fake_restapi) -> None:
fake_restapi("cluster_node_count_critical")
def test_cluster_node_count_critical(
runner: CliRunner, fake_restapi, old_replica_state: bool
) -> None:
fake_restapi("cluster_node_count_critical", use_old_replica_state=old_replica_state)
result = runner.invoke(
main,
[
Expand Down
8 changes: 4 additions & 4 deletions tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def mock_rest_api(self: PatroniResource, service: str) -> Any:
if status != 200:
raise APIError("Test en erreur pour status code 200")
if json_file:
if use_old_replica_state and (
json_file.startswith("cluster_has_replica")
or json_file.startswith("cluster_node_count")
):
if use_old_replica_state:
assert json_file.startswith(
"cluster_has_replica"
) or json_file.startswith("cluster_node_count")
return cluster_api_set_replica_running(getjson(json_file))
return getjson(json_file)
return None
Expand Down

0 comments on commit 597f07e

Please sign in to comment.