2023-09-28 09:41:33 +02:00
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
import pytest
|
2022-02-07 15:11:05 +01:00
|
|
|
from click.testing import CliRunner
|
2021-08-11 19:09:14 +02:00
|
|
|
|
2022-02-07 15:11:05 +01:00
|
|
|
from check_patroni.cli import main
|
2023-03-20 11:44:19 +01:00
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
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
|
|
|
|
|
2021-08-11 19:09:14 +02:00
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@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"])
|
2021-08-11 19:09:14 +02:00
|
|
|
assert result.exit_code == 0
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODEISREPLICA OK - This node is a running replica with no noloadbalance tag. | is_replica=1;;@0\n"
|
|
|
|
)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_node_is_replica_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
|
|
|
result = runner.invoke(main, ["-e", patroni_api.endpoint, "node_is_replica"])
|
2021-08-11 19:09:14 +02:00
|
|
|
assert result.exit_code == 2
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODEISREPLICA CRITICAL - This node is not a running replica with no noloadbalance tag. | is_replica=0;;@0\n"
|
|
|
|
)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_node_is_replica_ko_lag(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
2021-08-11 19:09:14 +02:00
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
|
|
result = runner.invoke(
|
2023-09-28 09:41:33 +02:00
|
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--max-lag", "100"]
|
2021-08-11 19:09:14 +02:00
|
|
|
)
|
|
|
|
assert result.exit_code == 2
|
2023-08-22 11:08:38 +02:00
|
|
|
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"
|
|
|
|
)
|
2023-08-23 14:25:21 +02:00
|
|
|
|
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2023-08-23 14:25:21 +02:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
|
|
def test_node_is_replica_sync_ok(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
2023-08-23 14:25:21 +02:00
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
|
|
result = runner.invoke(
|
2023-09-28 09:41:33 +02:00
|
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-sync"]
|
2023-08-23 14:25:21 +02:00
|
|
|
)
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_node_is_replica_sync_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
2023-08-23 14:25:21 +02:00
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
|
|
result = runner.invoke(
|
2023-09-28 09:41:33 +02:00
|
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-sync"]
|
2023-08-23 14:25:21 +02:00
|
|
|
)
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
|
|
def test_node_is_replica_async_ok(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
2023-08-23 14:25:21 +02:00
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
|
|
result = runner.invoke(
|
2023-09-28 09:41:33 +02:00
|
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-async"]
|
2023-08-23 14:25:21 +02:00
|
|
|
)
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_node_is_replica_async_ko(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
2023-08-23 14:25:21 +02:00
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
|
|
result = runner.invoke(
|
2023-09-28 09:41:33 +02:00
|
|
|
main, ["-e", patroni_api.endpoint, "node_is_replica", "--is-async"]
|
2023-08-23 14:25:21 +02:00
|
|
|
)
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_is_replica_ok")
|
|
|
|
def test_node_is_replica_params(runner: CliRunner, patroni_api: PatroniAPI) -> None:
|
2023-08-23 14:25:21 +02:00
|
|
|
# We don't do the check ourselves, patroni does it and changes the return code
|
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2023-08-23 14:25:21 +02:00
|
|
|
"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",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2023-08-23 14:25:21 +02:00
|
|
|
"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"
|
|
|
|
)
|