Fix tests for the urllib3 to requests change
This commit is contained in:
parent
7815f3379c
commit
7256c1894a
26
tests/test_api.py
Normal file
26
tests/test_api.py
Normal file
|
@ -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
|
|
@ -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"
|
||||
|
|
|
@ -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"]
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue