From 7256c1894a86be2809b75d783ce9549bc699fe2a Mon Sep 17 00:00:00 2001 From: benoit Date: Sun, 12 Mar 2023 19:57:52 +0100 Subject: [PATCH] Fix tests for the urllib3 to requests change --- tests/test_api.py | 26 ++++++++++++++++++++++++ tests/test_cluster_config_has_changed.py | 12 +++++------ tests/test_node_is_pending_restart.py | 2 +- tests/tools.py | 21 ++++++++----------- 4 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 tests/test_api.py diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..048e0d8 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,26 @@ +from pytest_mock import MockerFixture + +from click.testing import CliRunner + +from check_patroni.cli import main +from tools import my_mock + + +def test_api_status_code_200(mocker: MockerFixture) -> None: + runner = CliRunner() + + my_mock(mocker, "node_is_pending_restart_ok", 200) + result = runner.invoke( + main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] + ) + assert result.exit_code == 0 + + +def test_api_status_code_404(mocker: MockerFixture) -> None: + runner = CliRunner() + + my_mock(mocker, "Fake test", 404) + result = runner.invoke( + main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] + ) + assert result.exit_code == 3 diff --git a/tests/test_cluster_config_has_changed.py b/tests/test_cluster_config_has_changed.py index 516c052..e279274 100644 --- a/tests/test_cluster_config_has_changed.py +++ b/tests/test_cluster_config_has_changed.py @@ -42,7 +42,7 @@ def test_cluster_config_has_changed_ok_with_hash(mocker: MockerFixture) -> None: "https://10.20.199.3:8008", "cluster_config_has_changed", "--hash", - "640df9f0211c791723f18fc3ed9dbb95", + "96b12d82571473d13e890b893734e731", ], ) print(result.output) @@ -53,7 +53,7 @@ def test_cluster_config_has_changed_ok_with_state_file(mocker: MockerFixture) -> runner = CliRunner() with open(here / "cluster_config_has_changed.state_file", "w") as f: - f.write('{"hash": "640df9f0211c791723f18fc3ed9dbb95"}') + f.write('{"hash": "96b12d82571473d13e890b893734e731"}') my_mock(mocker, "cluster_config_has_changed", 200) result = runner.invoke( @@ -80,7 +80,7 @@ def test_cluster_config_has_changed_ko_with_hash(mocker: MockerFixture) -> None: "https://10.20.199.3:8008", "cluster_config_has_changed", "--hash", - "640df9f0211c791723f18fc3edffffff", + "96b12d82571473d13e890b8937ffffff", ], ) assert result.exit_code == 2 @@ -92,7 +92,7 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save( runner = CliRunner() with open(here / "cluster_config_has_changed.state_file", "w") as f: - f.write('{"hash": "640df9f0211c791723f18fc3edffffff"}') + f.write('{"hash": "96b12d82571473d13e890b8937ffffff"}') my_mock(mocker, "cluster_config_has_changed", 200) # test without saving the new hash @@ -113,7 +113,7 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save( new_config_hash = cookie.get("hash") cookie.close() - assert new_config_hash == "640df9f0211c791723f18fc3edffffff" + assert new_config_hash == "96b12d82571473d13e890b8937ffffff" # test when we save the hash result = runner.invoke( @@ -134,4 +134,4 @@ def test_cluster_config_has_changed_ko_with_state_file_and_save( new_config_hash = cookie.get("hash") cookie.close() - assert new_config_hash == "640df9f0211c791723f18fc3ed9dbb95" + assert new_config_hash == "96b12d82571473d13e890b893734e731" diff --git a/tests/test_node_is_pending_restart.py b/tests/test_node_is_pending_restart.py index 3d89735..a3e652f 100644 --- a/tests/test_node_is_pending_restart.py +++ b/tests/test_node_is_pending_restart.py @@ -19,7 +19,7 @@ def test_node_is_pending_restart_ok(mocker: MockerFixture) -> None: def test_node_is_pending_restart_ko(mocker: MockerFixture) -> None: runner = CliRunner() - my_mock(mocker, "node_is_pending_restart_ko", 404) + my_mock(mocker, "node_is_pending_restart_ko", 200) result = runner.invoke( main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] ) diff --git a/tests/tools.py b/tests/tools.py index da0c1cd..76f2ded 100644 --- a/tests/tools.py +++ b/tests/tools.py @@ -1,30 +1,27 @@ -import attr +import json import pathlib from pytest_mock import MockerFixture +from typing import Any -from check_patroni.types import PatroniResource +from check_patroni.types import APIError, PatroniResource here = pathlib.Path(__file__).parent -def getjson(name: str) -> bytes: +def getjson(name: str) -> Any: path = here / "json" / f"{name}.json" if not path.exists(): raise Exception(f"path does not exist : {path}") with path.open() as f: - return f.read().encode("utf-8") - - -@attr.s(auto_attribs=True, frozen=True, slots=True) -class MockApiReturnCode: - data: bytes - status: int + return json.load(f) def my_mock(mocker: MockerFixture, json_file: str, status: int) -> None: - def mock_rest_api(self: PatroniResource, service: str) -> MockApiReturnCode: - return MockApiReturnCode(getjson(json_file), status) + def mock_rest_api(self: PatroniResource, service: str) -> Any: + if status != 200: + raise APIError("Test en erreur pour status code 200") + return getjson(json_file) mocker.resetall() mocker.patch("check_patroni.types.PatroniResource.rest_api", mock_rest_api)