2023-09-28 11:22:30 +02:00
|
|
|
from pathlib import Path
|
2023-09-28 09:41:33 +02:00
|
|
|
from typing import Iterator
|
2023-09-28 11:22:30 +02:00
|
|
|
|
2023-03-20 11:44:19 +01:00
|
|
|
import nagiosplugin
|
2023-09-28 09:41:33 +02:00
|
|
|
import pytest
|
2021-08-11 19:09:14 +02:00
|
|
|
from click.testing import CliRunner
|
|
|
|
|
|
|
|
from check_patroni.cli import main
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
from . import PatroniAPI
|
2021-08-11 19:09:14 +02:00
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def node_tl_has_changed(patroni_api: PatroniAPI) -> Iterator[None]:
|
|
|
|
with patroni_api.routes({"patroni": "node_tl_has_changed.json"}):
|
|
|
|
yield None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("node_tl_has_changed")
|
|
|
|
def test_node_tl_has_changed_ok_with_timeline(
|
|
|
|
runner: CliRunner, patroni_api: PatroniAPI
|
|
|
|
) -> None:
|
2021-08-11 19:09:14 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2021-08-11 19:09:14 +02:00
|
|
|
"node_tl_has_changed",
|
|
|
|
"--timeline",
|
|
|
|
"58",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 0
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED OK - The timeline is still 58. | is_timeline_changed=0;;@1:1 timeline=58\n"
|
|
|
|
)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_tl_has_changed")
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_node_tl_has_changed_ok_with_state_file(
|
2023-09-28 09:41:33 +02:00
|
|
|
runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-09-28 11:22:30 +02:00
|
|
|
state_file = tmp_path / "node_tl_has_changed.state_file"
|
|
|
|
with state_file.open("w") as f:
|
2021-08-11 19:09:14 +02:00
|
|
|
f.write('{"timeline": 58}')
|
|
|
|
|
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2021-08-11 19:09:14 +02:00
|
|
|
"node_tl_has_changed",
|
|
|
|
"--state-file",
|
2023-09-28 11:22:30 +02:00
|
|
|
str(state_file),
|
2021-08-11 19:09:14 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 0
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED OK - The timeline is still 58. | is_timeline_changed=0;;@1:1 timeline=58\n"
|
|
|
|
)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_tl_has_changed")
|
|
|
|
def test_node_tl_has_changed_ko_with_timeline(
|
|
|
|
runner: CliRunner, patroni_api: PatroniAPI
|
|
|
|
) -> None:
|
2021-08-11 19:09:14 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2021-08-11 19:09:14 +02:00
|
|
|
"node_tl_has_changed",
|
|
|
|
"--timeline",
|
|
|
|
"700",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 2
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
|
|
|
|
)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_tl_has_changed")
|
2023-09-28 11:22:30 +02:00
|
|
|
def test_node_tl_has_changed_ko_with_state_file_and_save(
|
2023-09-28 09:41:33 +02:00
|
|
|
runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path
|
2023-09-28 11:22:30 +02:00
|
|
|
) -> None:
|
|
|
|
state_file = tmp_path / "node_tl_has_changed.state_file"
|
|
|
|
with state_file.open("w") as f:
|
2021-08-11 19:09:14 +02:00
|
|
|
f.write('{"timeline": 700}')
|
|
|
|
|
2023-03-01 16:46:55 +01:00
|
|
|
# test without saving the new tl
|
2021-08-11 19:09:14 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2021-08-11 19:09:14 +02:00
|
|
|
"node_tl_has_changed",
|
|
|
|
"--state-file",
|
2023-09-28 11:22:30 +02:00
|
|
|
str(state_file),
|
2021-08-11 19:09:14 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 2
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
|
|
|
|
)
|
2022-02-07 15:01:50 +01:00
|
|
|
|
2023-09-28 11:22:30 +02:00
|
|
|
cookie = nagiosplugin.Cookie(state_file)
|
2023-03-01 16:46:55 +01:00
|
|
|
cookie.open()
|
|
|
|
new_tl = cookie.get("timeline")
|
|
|
|
cookie.close()
|
|
|
|
|
|
|
|
assert new_tl == 700
|
|
|
|
|
|
|
|
# test when we save the hash
|
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2023-03-01 16:46:55 +01:00
|
|
|
"node_tl_has_changed",
|
|
|
|
"--state-file",
|
2023-09-28 11:22:30 +02:00
|
|
|
str(state_file),
|
2023-03-01 16:46:55 +01:00
|
|
|
"--save",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 2
|
2023-08-22 11:08:38 +02:00
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
|
|
|
|
)
|
2023-03-01 16:46:55 +01:00
|
|
|
|
2023-09-28 11:22:30 +02:00
|
|
|
cookie = nagiosplugin.Cookie(state_file)
|
2022-02-07 15:01:50 +01:00
|
|
|
cookie.open()
|
|
|
|
new_tl = cookie.get("timeline")
|
|
|
|
cookie.close()
|
|
|
|
|
|
|
|
assert new_tl == 58
|
2023-08-22 11:08:38 +02:00
|
|
|
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
@pytest.mark.usefixtures("node_tl_has_changed")
|
2023-09-28 09:41:33 +02:00
|
|
|
def test_node_tl_has_changed_params(
|
2023-09-28 09:41:33 +02:00
|
|
|
runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path
|
2023-09-28 09:41:33 +02:00
|
|
|
) -> None:
|
2023-08-22 11:08:38 +02:00
|
|
|
# This one is placed last because it seems like the exceptions are not flushed from stderr for the next tests.
|
2023-09-28 11:22:30 +02:00
|
|
|
fake_state_file = tmp_path / "fake_file_name.state_file"
|
2023-08-22 11:08:38 +02:00
|
|
|
result = runner.invoke(
|
|
|
|
main,
|
|
|
|
[
|
|
|
|
"-e",
|
2023-09-28 09:41:33 +02:00
|
|
|
patroni_api.endpoint,
|
2023-08-22 11:08:38 +02:00
|
|
|
"node_tl_has_changed",
|
|
|
|
"--timeline",
|
|
|
|
"58",
|
|
|
|
"--state-file",
|
2023-09-28 11:22:30 +02:00
|
|
|
str(fake_state_file),
|
2023-08-22 11:08:38 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
assert result.exit_code == 3
|
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED UNKNOWN: click.exceptions.UsageError: Either --timeline or --state-file should be provided for this service\n"
|
|
|
|
)
|
|
|
|
|
2023-09-28 09:41:33 +02:00
|
|
|
result = runner.invoke(main, ["-e", patroni_api.endpoint, "node_tl_has_changed"])
|
2023-08-22 11:08:38 +02:00
|
|
|
assert result.exit_code == 3
|
|
|
|
assert (
|
|
|
|
result.stdout
|
|
|
|
== "NODETLHASCHANGED UNKNOWN: click.exceptions.UsageError: Either --timeline or --state-file should be provided for this service\n"
|
|
|
|
)
|