Use the tmp_path fixture instead of writing files to tests/

This commit is contained in:
Denis Laxalde 2023-09-28 11:22:30 +02:00
parent bc2d2917c3
commit d34e597e61
3 changed files with 39 additions and 27 deletions

1
.gitignore vendored
View file

@ -1,6 +1,5 @@
__pycache__/
check_patroni.egg-info
tests/*.state_file
tests/config.ini
vagrant/.vagrant
vagrant/*.state_file

View file

@ -1,10 +1,10 @@
from pathlib import Path
import nagiosplugin
from click.testing import CliRunner
from check_patroni.cli import main
from .tools import here
def test_cluster_config_has_changed_ok_with_hash(fake_restapi) -> None:
runner = CliRunner()
@ -27,10 +27,13 @@ def test_cluster_config_has_changed_ok_with_hash(fake_restapi) -> None:
)
def test_cluster_config_has_changed_ok_with_state_file(fake_restapi) -> None:
def test_cluster_config_has_changed_ok_with_state_file(
fake_restapi, tmp_path: Path
) -> None:
runner = CliRunner()
with open(here / "cluster_config_has_changed.state_file", "w") as f:
state_file = tmp_path / "cluster_config_has_changed.state_file"
with state_file.open("w") as f:
f.write('{"hash": "96b12d82571473d13e890b893734e731"}')
fake_restapi("cluster_config_has_changed")
@ -41,7 +44,7 @@ def test_cluster_config_has_changed_ok_with_state_file(fake_restapi) -> None:
"https://10.20.199.3:8008",
"cluster_config_has_changed",
"--state-file",
str(here / "cluster_config_has_changed.state_file"),
str(state_file),
],
)
assert result.exit_code == 0
@ -72,10 +75,13 @@ def test_cluster_config_has_changed_ko_with_hash(fake_restapi) -> None:
)
def test_cluster_config_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
def test_cluster_config_has_changed_ko_with_state_file_and_save(
fake_restapi, tmp_path: Path
) -> None:
runner = CliRunner()
with open(here / "cluster_config_has_changed.state_file", "w") as f:
state_file = tmp_path / "cluster_config_has_changed.state_file"
with state_file.open("w") as f:
f.write('{"hash": "96b12d82571473d13e890b8937ffffff"}')
fake_restapi("cluster_config_has_changed")
@ -87,7 +93,7 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save(fake_restapi) ->
"https://10.20.199.3:8008",
"cluster_config_has_changed",
"--state-file",
str(here / "cluster_config_has_changed.state_file"),
str(state_file),
],
)
assert result.exit_code == 2
@ -96,7 +102,8 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save(fake_restapi) ->
== "CLUSTERCONFIGHASCHANGED CRITICAL - The hash of patroni's dynamic configuration has changed. The old hash was 96b12d82571473d13e890b8937ffffff. | is_configuration_changed=1;;@1:1\n"
)
cookie = nagiosplugin.Cookie(here / "cluster_config_has_changed.state_file")
state_file = tmp_path / "cluster_config_has_changed.state_file"
cookie = nagiosplugin.Cookie(state_file)
cookie.open()
new_config_hash = cookie.get("hash")
cookie.close()
@ -111,7 +118,7 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save(fake_restapi) ->
"https://10.20.199.3:8008",
"cluster_config_has_changed",
"--state-file",
str(here / "cluster_config_has_changed.state_file"),
str(state_file),
"--save",
],
)
@ -121,7 +128,7 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save(fake_restapi) ->
== "CLUSTERCONFIGHASCHANGED CRITICAL - The hash of patroni's dynamic configuration has changed. The old hash was 96b12d82571473d13e890b8937ffffff. | is_configuration_changed=1;;@1:1\n"
)
cookie = nagiosplugin.Cookie(here / "cluster_config_has_changed.state_file")
cookie = nagiosplugin.Cookie(state_file)
cookie.open()
new_config_hash = cookie.get("hash")
cookie.close()
@ -129,10 +136,11 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save(fake_restapi) ->
assert new_config_hash == "96b12d82571473d13e890b893734e731"
def test_cluster_config_has_changed_params(fake_restapi) -> None:
def test_cluster_config_has_changed_params(fake_restapi, tmp_path: Path) -> None:
# This one is placed last because it seems like the exceptions are not flushed from stderr for the next tests.
runner = CliRunner()
fake_state_file = tmp_path / "fake_file_name.state_file"
fake_restapi("cluster_config_has_changed")
result = runner.invoke(
main,
@ -143,7 +151,7 @@ def test_cluster_config_has_changed_params(fake_restapi) -> None:
"--hash",
"640df9f0211c791723f18fc3ed9dbb95",
"--state-file",
str(here / "fake_file_name.state_file"),
str(fake_state_file),
],
)
assert result.exit_code == 3

View file

@ -1,10 +1,10 @@
from pathlib import Path
import nagiosplugin
from click.testing import CliRunner
from check_patroni.cli import main
from .tools import here
def test_node_tl_has_changed_ok_with_timeline(fake_restapi) -> None:
runner = CliRunner()
@ -27,10 +27,11 @@ def test_node_tl_has_changed_ok_with_timeline(fake_restapi) -> None:
)
def test_node_tl_has_changed_ok_with_state_file(fake_restapi) -> None:
def test_node_tl_has_changed_ok_with_state_file(fake_restapi, tmp_path: Path) -> None:
runner = CliRunner()
with open(here / "node_tl_has_changed.state_file", "w") as f:
state_file = tmp_path / "node_tl_has_changed.state_file"
with state_file.open("w") as f:
f.write('{"timeline": 58}')
fake_restapi("node_tl_has_changed")
@ -41,7 +42,7 @@ def test_node_tl_has_changed_ok_with_state_file(fake_restapi) -> None:
"https://10.20.199.3:8008",
"node_tl_has_changed",
"--state-file",
str(here / "node_tl_has_changed.state_file"),
str(state_file),
],
)
assert result.exit_code == 0
@ -72,10 +73,13 @@ def test_node_tl_has_changed_ko_with_timeline(fake_restapi) -> None:
)
def test_node_tl_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
def test_node_tl_has_changed_ko_with_state_file_and_save(
fake_restapi, tmp_path: Path
) -> None:
runner = CliRunner()
with open(here / "node_tl_has_changed.state_file", "w") as f:
state_file = tmp_path / "node_tl_has_changed.state_file"
with state_file.open("w") as f:
f.write('{"timeline": 700}')
fake_restapi("node_tl_has_changed")
@ -87,7 +91,7 @@ def test_node_tl_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
"https://10.20.199.3:8008",
"node_tl_has_changed",
"--state-file",
str(here / "node_tl_has_changed.state_file"),
str(state_file),
],
)
assert result.exit_code == 2
@ -96,7 +100,7 @@ def test_node_tl_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
== "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
)
cookie = nagiosplugin.Cookie(here / "node_tl_has_changed.state_file")
cookie = nagiosplugin.Cookie(state_file)
cookie.open()
new_tl = cookie.get("timeline")
cookie.close()
@ -111,7 +115,7 @@ def test_node_tl_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
"https://10.20.199.3:8008",
"node_tl_has_changed",
"--state-file",
str(here / "node_tl_has_changed.state_file"),
str(state_file),
"--save",
],
)
@ -121,7 +125,7 @@ def test_node_tl_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
== "NODETLHASCHANGED CRITICAL - The expected timeline was 700 got 58. | is_timeline_changed=1;;@1:1 timeline=58\n"
)
cookie = nagiosplugin.Cookie(here / "node_tl_has_changed.state_file")
cookie = nagiosplugin.Cookie(state_file)
cookie.open()
new_tl = cookie.get("timeline")
cookie.close()
@ -129,10 +133,11 @@ def test_node_tl_has_changed_ko_with_state_file_and_save(fake_restapi) -> None:
assert new_tl == 58
def test_node_tl_has_changed_params(fake_restapi) -> None:
def test_node_tl_has_changed_params(fake_restapi, tmp_path: Path) -> None:
# This one is placed last because it seems like the exceptions are not flushed from stderr for the next tests.
runner = CliRunner()
fake_state_file = tmp_path / "fake_file_name.state_file"
fake_restapi("node_tl_has_changed")
result = runner.invoke(
main,
@ -143,7 +148,7 @@ def test_node_tl_has_changed_params(fake_restapi) -> None:
"--timeline",
"58",
"--state-file",
str(here / "fake_file_name.state_file"),
str(fake_state_file),
],
)
assert result.exit_code == 3