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
|
|
|
|
|
|
|
|
2023-08-22 11:08:38 +02:00
|
|
|
def my_mock(
|
|
|
|
mocker: MockerFixture,
|
|
|
|
json_file: str,
|
|
|
|
status: int,
|
|
|
|
use_old_replica_state: bool = False,
|
|
|
|
) -> 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-22 11:08:38 +02:00
|
|
|
if json_file:
|
|
|
|
if use_old_replica_state and (
|
|
|
|
json_file.startswith("cluster_has_replica")
|
|
|
|
or json_file.startswith("cluster_node_count")
|
|
|
|
):
|
|
|
|
return cluster_api_set_replica_running(getjson(json_file))
|
|
|
|
return getjson(json_file)
|
|
|
|
return 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)
|
2023-08-22 11:08:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def cluster_api_set_replica_running(js: Any) -> Any:
|
|
|
|
# starting from 3.0.4 the state of replicas is streaming instead of running
|
|
|
|
for node in js["members"]:
|
|
|
|
if node["role"] in ["replica", "sync_standby"]:
|
|
|
|
if node["state"] == "streaming":
|
|
|
|
node["state"] = "running"
|
|
|
|
return js
|