2023-03-12 19:57:52 +01:00
|
|
|
import json
|
2021-08-11 19:09:14 +02:00
|
|
|
import pathlib
|
2023-03-12 19:57:52 +01:00
|
|
|
from typing import Any
|
2021-08-11 19:09:14 +02:00
|
|
|
|
2023-03-20 11:44:19 +01:00
|
|
|
from pytest_mock import MockerFixture
|
|
|
|
|
2023-03-12 19:57:52 +01:00
|
|
|
from check_patroni.types import APIError, PatroniResource
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
here = pathlib.Path(__file__).parent
|
|
|
|
|
|
|
|
|
2023-03-12 19:57:52 +01:00
|
|
|
def getjson(name: str) -> Any:
|
2021-08-11 19:09:14 +02:00
|
|
|
path = here / "json" / f"{name}.json"
|
2021-08-13 10:59:44 +02:00
|
|
|
if not path.exists():
|
2023-03-01 17:08:04 +01:00
|
|
|
raise Exception(f"path does not exist : {path}")
|
2021-08-13 10:59:44 +02:00
|
|
|
|
2021-08-11 19:09:14 +02:00
|
|
|
with path.open() as f:
|
2023-03-12 19:57:52 +01:00
|
|
|
return json.load(f)
|
2021-08-11 19:09:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
def my_mock(mocker: MockerFixture, json_file: str, status: int) -> None:
|
2023-03-12 19:57:52 +01:00
|
|
|
def mock_rest_api(self: PatroniResource, service: str) -> Any:
|
|
|
|
if status != 200:
|
|
|
|
raise APIError("Test en erreur pour status code 200")
|
2023-08-21 13:36:29 +02:00
|
|
|
return getjson(json_file) if json_file else None
|
2021-08-11 19:09:14 +02:00
|
|
|
|
2021-08-13 10:59:44 +02:00
|
|
|
mocker.resetall()
|
2021-08-11 19:09:14 +02:00
|
|
|
mocker.patch("check_patroni.types.PatroniResource.rest_api", mock_rest_api)
|