File: test_node_tl_has_changed.py

package info (click to toggle)
check-patroni 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 628 kB
  • sloc: python: 2,779; sh: 727; makefile: 25
file content (173 lines) | stat: -rw-r--r-- 4,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from pathlib import Path
from typing import Iterator

import nagiosplugin
import pytest
from click.testing import CliRunner

from check_patroni.cli import main

from . import PatroniAPI


@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:
    result = runner.invoke(
        main,
        [
            "-e",
            patroni_api.endpoint,
            "node_tl_has_changed",
            "--timeline",
            "58",
        ],
    )
    assert result.exit_code == 0
    assert (
        result.stdout
        == "NODETLHASCHANGED OK - The timeline is still 58. | is_timeline_changed=0;;@1:1 timeline=58\n"
    )


@pytest.mark.usefixtures("node_tl_has_changed")
def test_node_tl_has_changed_ok_with_state_file(
    runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path
) -> None:
    state_file = tmp_path / "node_tl_has_changed.state_file"
    with state_file.open("w") as f:
        f.write('{"timeline": 58}')

    result = runner.invoke(
        main,
        [
            "-e",
            patroni_api.endpoint,
            "node_tl_has_changed",
            "--state-file",
            str(state_file),
        ],
    )
    assert result.exit_code == 0
    assert (
        result.stdout
        == "NODETLHASCHANGED OK - The timeline is still 58. | is_timeline_changed=0;;@1:1 timeline=58\n"
    )


@pytest.mark.usefixtures("node_tl_has_changed")
def test_node_tl_has_changed_ko_with_timeline(
    runner: CliRunner, patroni_api: PatroniAPI
) -> None:
    result = runner.invoke(
        main,
        [
            "-e",
            patroni_api.endpoint,
            "node_tl_has_changed",
            "--timeline",
            "700",
        ],
    )
    assert result.exit_code == 2
    assert (
        result.stdout
        == "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
    )


@pytest.mark.usefixtures("node_tl_has_changed")
def test_node_tl_has_changed_ko_with_state_file_and_save(
    runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path
) -> None:
    state_file = tmp_path / "node_tl_has_changed.state_file"
    with state_file.open("w") as f:
        f.write('{"timeline": 700}')

    # test without saving the new tl
    result = runner.invoke(
        main,
        [
            "-e",
            patroni_api.endpoint,
            "node_tl_has_changed",
            "--state-file",
            str(state_file),
        ],
    )
    assert result.exit_code == 2
    assert (
        result.stdout
        == "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
    )

    cookie = nagiosplugin.Cookie(state_file)
    cookie.open()
    new_tl = cookie.get("timeline")
    cookie.close()

    assert new_tl == 700

    # test when we save the hash
    result = runner.invoke(
        main,
        [
            "-e",
            patroni_api.endpoint,
            "node_tl_has_changed",
            "--state-file",
            str(state_file),
            "--save",
        ],
    )
    assert result.exit_code == 2
    assert (
        result.stdout
        == "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
    )

    cookie = nagiosplugin.Cookie(state_file)
    cookie.open()
    new_tl = cookie.get("timeline")
    cookie.close()

    assert new_tl == 58


@pytest.mark.usefixtures("node_tl_has_changed")
def test_node_tl_has_changed_params(
    runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path
) -> None:
    # This one is placed last because it seems like the exceptions are not flushed from stderr for the next tests.
    fake_state_file = tmp_path / "fake_file_name.state_file"
    result = runner.invoke(
        main,
        [
            "-e",
            patroni_api.endpoint,
            "node_tl_has_changed",
            "--timeline",
            "58",
            "--state-file",
            str(fake_state_file),
        ],
    )
    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"
    )

    result = runner.invoke(main, ["-e", patroni_api.endpoint, "node_tl_has_changed"])
    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"
    )