903b83e211
We introduce a patroni_api fixture, defined in tests/conftest.py, which sets up an HTTP server serving files in a temporary directory. The server is itself defined by the PatroniAPI class; it has a 'routes()' context manager method to be used in actual tests to setup expected responses based on specified JSON files. We set up some logging in order to improve debugging. The direct advantage of this is that PatroniResource.rest_api() method is now covered by the test suite. Coverage before this commit: Name Stmts Miss Cover ----------------------------------------------- check_patroni/__init__.py 3 0 100% check_patroni/cli.py 193 18 91% check_patroni/cluster.py 113 0 100% check_patroni/convert.py 23 5 78% check_patroni/node.py 146 1 99% check_patroni/types.py 50 23 54% ----------------------------------------------- TOTAL 528 47 91% and after this commit: Name Stmts Miss Cover ----------------------------------------------- check_patroni/__init__.py 3 0 100% check_patroni/cli.py 193 18 91% check_patroni/cluster.py 113 0 100% check_patroni/convert.py 23 5 78% check_patroni/node.py 146 1 99% check_patroni/types.py 50 9 82% ----------------------------------------------- TOTAL 528 33 94% In actual test functions, we either invoke patroni_api.routes() to configure which JSON file(s) should be served for each endpoint, or we define dedicated fixtures (e.g. cluster_config_has_changed()) to configure this for several test functions or the whole module. The 'old_replica_state' parametrized fixture is used when needed to adjust such fixtures, e.g. in cluster_has_replica_ok(), to modify the JSON content using cluster_api_set_replica_running() (previously in tests/tools.py, now in tests/__init__.py). The dependency on pytest-mock is no longer needed.
156 lines
5.2 KiB
Python
156 lines
5.2 KiB
Python
from typing import Iterator
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from check_patroni.cli import main
|
|
|
|
from . import PatroniAPI
|
|
|
|
|
|
@pytest.fixture
|
|
def node_is_replica_ok(patroni_api: PatroniAPI) -> Iterator[None]:
|
|
with patroni_api.routes(
|
|
{
|
|
k: "node_is_replica_ok.json"
|
|
for k in ("replica", "synchronous", "asynchronous")
|
|
}
|
|
):
|
|
yield None
|
|
|
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
def test_node_is_replica_ok(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
result = runner.invoke(main, ["-e", patroni_api.endpoint, "node_is_replica"])
|
|
assert result.exit_code == 0
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA OK - This node is a running replica with no noloadbalance tag. | is_replica=1;;@0\n"
|
|
)
|
|
|
|
|
|
def test_node_is_replica_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
result = runner.invoke(main, ["-e", patroni_api.endpoint, "node_is_replica"])
|
|
assert result.exit_code == 2
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA CRITICAL - This node is not a running replica with no noloadbalance tag. | is_replica=0;;@0\n"
|
|
)
|
|
|
|
|
|
def test_node_is_replica_ko_lag(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--max-lag", "100"]
|
|
)
|
|
assert result.exit_code == 2
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA CRITICAL - This node is not a running replica with no noloadbalance tag and a lag under 100. | is_replica=0;;@0\n"
|
|
)
|
|
|
|
result = runner.invoke(
|
|
main,
|
|
[
|
|
"-e",
|
|
patroni_api.endpoint,
|
|
"node_is_replica",
|
|
"--is-async",
|
|
"--max-lag",
|
|
"100",
|
|
],
|
|
)
|
|
assert result.exit_code == 2
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA CRITICAL - This node is not a running asynchronous replica with no noloadbalance tag and a lag under 100. | is_replica=0;;@0\n"
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
def test_node_is_replica_sync_ok(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-sync"]
|
|
)
|
|
assert result.exit_code == 0
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA OK - This node is a running synchronous replica with no noloadbalance tag. | is_replica=1;;@0\n"
|
|
)
|
|
|
|
|
|
def test_node_is_replica_sync_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-sync"]
|
|
)
|
|
assert result.exit_code == 2
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA CRITICAL - This node is not a running synchronous replica with no noloadbalance tag. | is_replica=0;;@0\n"
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
def test_node_is_replica_async_ok(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-async"]
|
|
)
|
|
assert result.exit_code == 0
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA OK - This node is a running asynchronous replica with no noloadbalance tag. | is_replica=1;;@0\n"
|
|
)
|
|
|
|
|
|
def test_node_is_replica_async_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-async"]
|
|
)
|
|
assert result.exit_code == 2
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA CRITICAL - This node is not a running asynchronous replica with no noloadbalance tag. | is_replica=0;;@0\n"
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
def test_node_is_replica_params(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main,
|
|
[
|
|
"-e",
|
|
patroni_api.endpoint,
|
|
"node_is_replica",
|
|
"--is-async",
|
|
"--is-sync",
|
|
],
|
|
)
|
|
assert result.exit_code == 3
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA UNKNOWN: click.exceptions.UsageError: --is-sync and --is-async cannot be provided at the same time for this service\n"
|
|
)
|
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
result = runner.invoke(
|
|
main,
|
|
[
|
|
"-e",
|
|
patroni_api.endpoint,
|
|
"node_is_replica",
|
|
"--is-sync",
|
|
"--max-lag",
|
|
"1MB",
|
|
],
|
|
)
|
|
assert result.exit_code == 3
|
|
assert (
|
|
result.stdout
|
|
== "NODEISREPLICA UNKNOWN: click.exceptions.UsageError: --is-sync and --max-lag cannot be provided at the same time for this service\n"
|
|
)
|