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
|
|
|
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
# TODO Lag threshold tests
|
2023-10-03 14:29:21 +02:00
|
|
|
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)
|
2021-08-11 19:09:14 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main, ["-e", "https://10.20.199.3:8008", "cluster_has_replica"]
|
|
|
|
)
|
|
|
|
assert result.exit_code == 0
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
2023-08-24 15:43:35 +02:00
|
|
|
== "CLUSTERHASREPLICA OK - healthy_replica is 2 | healthy_replica=2 srv2_lag=0 srv2_sync=0 srv3_lag=0 srv3_sync=1 sync_replica=1 unhealthy_replica=0\n"
|
2023-08-22 11:08:38 +02:00
|
|
|
)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_cluster_has_replica_ok_with_count_thresholds(
|
2023-10-03 14:29:21 +02:00
|
|
|
runner: CliRunner, fake_restapi, old_replica_state: bool
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-10-03 14:29:21 +02:00
|
|
|
fake_restapi("cluster_has_replica_ok", use_old_replica_state=old_replica_state)
|
2021-08-13 11:00:43 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
|
|
|
"https://10.20.199.3:8008",
|
|
|
|
"cluster_has_replica",
|
|
|
|
"--warning",
|
|
|
|
"@1",
|
|
|
|
"--critical",
|
|
|
|
"@0",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 0
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
2023-08-24 15:43:35 +02:00
|
|
|
== "CLUSTERHASREPLICA OK - healthy_replica is 2 | healthy_replica=2;@1;@0 srv2_lag=0 srv2_sync=0 srv3_lag=0 srv3_sync=1 sync_replica=1 unhealthy_replica=0\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_cluster_has_replica_ok_with_sync_count_thresholds(
|
2023-10-03 14:29:21 +02:00
|
|
|
runner: CliRunner, fake_restapi, old_replica_state: bool
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-10-03 14:29:21 +02:00
|
|
|
fake_restapi("cluster_has_replica_ok", use_old_replica_state=old_replica_state)
|
2023-08-24 15:43:35 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
|
|
|
"https://10.20.199.3:8008",
|
|
|
|
"cluster_has_replica",
|
|
|
|
"--sync-warning",
|
|
|
|
"1:",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "CLUSTERHASREPLICA OK - healthy_replica is 2 | healthy_replica=2 srv2_lag=0 srv2_sync=0 srv3_lag=0 srv3_sync=1 sync_replica=1;1: unhealthy_replica=0\n"
|
2023-08-22 11:08:38 +02:00
|
|
|
)
|
2021-08-13 11:00:43 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_cluster_has_replica_ok_with_count_thresholds_lag(
|
2023-10-03 14:29:21 +02:00
|
|
|
runner: CliRunner, fake_restapi, old_replica_state: bool
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-10-03 14:29:21 +02:00
|
|
|
fake_restapi("cluster_has_replica_ok_lag", use_old_replica_state=old_replica_state)
|
2021-08-13 11:00:43 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
|
|
|
"https://10.20.199.3:8008",
|
|
|
|
"cluster_has_replica",
|
|
|
|
"--warning",
|
|
|
|
"@1",
|
|
|
|
"--critical",
|
|
|
|
"@0",
|
|
|
|
"--max-lag",
|
|
|
|
"1MB",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 0
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
2023-08-24 15:43:35 +02:00
|
|
|
== "CLUSTERHASREPLICA OK - healthy_replica is 2 | healthy_replica=2;@1;@0 srv2_lag=1024 srv2_sync=0 srv3_lag=0 srv3_sync=0 sync_replica=0 unhealthy_replica=0\n"
|
2023-08-22 11:08:38 +02:00
|
|
|
)
|
2021-08-13 11:00:43 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_cluster_has_replica_ko_with_count_thresholds(
|
2023-10-03 14:29:21 +02:00
|
|
|
runner: CliRunner, fake_restapi, old_replica_state: bool
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-10-03 14:29:21 +02:00
|
|
|
fake_restapi("cluster_has_replica_ko", use_old_replica_state=old_replica_state)
|
2021-08-11 19:09:14 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
|
|
|
"https://10.20.199.3:8008",
|
|
|
|
"cluster_has_replica",
|
2021-08-13 11:00:43 +02:00
|
|
|
"--warning",
|
|
|
|
"@1",
|
|
|
|
"--critical",
|
|
|
|
"@0",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 1
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
2023-08-24 15:43:35 +02:00
|
|
|
== "CLUSTERHASREPLICA WARNING - healthy_replica is 1 (outside range @0:1) | healthy_replica=1;@1;@0 srv3_lag=0 srv3_sync=0 sync_replica=0 unhealthy_replica=1\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_cluster_has_replica_ko_with_sync_count_thresholds(
|
2023-10-03 14:29:21 +02:00
|
|
|
runner: CliRunner, fake_restapi, old_replica_state: bool
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-10-03 14:29:21 +02:00
|
|
|
fake_restapi("cluster_has_replica_ko", use_old_replica_state=old_replica_state)
|
2023-08-24 15:43:35 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
|
|
|
"https://10.20.199.3:8008",
|
|
|
|
"cluster_has_replica",
|
|
|
|
"--sync-warning",
|
|
|
|
"2:",
|
|
|
|
"--sync-critical",
|
|
|
|
"1:",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 2
|
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "CLUSTERHASREPLICA CRITICAL - sync_replica is 0 (outside range 1:) | healthy_replica=1 srv3_lag=0 srv3_sync=0 sync_replica=0;2:;1: unhealthy_replica=1\n"
|
2023-08-22 11:08:38 +02:00
|
|
|
)
|
2021-08-13 11:00:43 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_cluster_has_replica_ko_with_count_thresholds_and_lag(
|
2023-10-03 14:29:21 +02:00
|
|
|
runner: CliRunner, fake_restapi, old_replica_state: bool
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-10-03 14:29:21 +02:00
|
|
|
fake_restapi("cluster_has_replica_ko_lag", use_old_replica_state=old_replica_state)
|
2021-08-13 11:00:43 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
|
|
|
"https://10.20.199.3:8008",
|
|
|
|
"cluster_has_replica",
|
|
|
|
"--warning",
|
|
|
|
"@1",
|
2021-08-11 19:09:14 +02:00
|
|
|
"--critical",
|
2021-08-13 11:00:43 +02:00
|
|
|
"@0",
|
|
|
|
"--max-lag",
|
|
|
|
"1MB",
|
2021-08-11 19:09:14 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 2
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
2023-08-24 15:43:35 +02:00
|
|
|
== "CLUSTERHASREPLICA CRITICAL - healthy_replica is 0 (outside range @0:0) | healthy_replica=0;@1;@0 srv2_lag=10241024 srv2_sync=0 srv3_lag=20000000 srv3_sync=0 sync_replica=0 unhealthy_replica=2\n"
|
2023-08-22 11:08:38 +02:00
|
|
|
)
|