-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use fake HTTP server for the Patroni API in tests (WIP)
We introduce a few fixtures, defined in tests/conftest.py: - patroni_api, an HTTP server serving files in a temporary directory and with an 'installed()' context manager method to be used in actual tests to setup expected responses based on specified JSON files, - runner, a CliRunner, configured with stdout and stderr separated as logs of the HTTP server thread would appear in stderr, and if mixed with stdout, the assertions in test case will fail. Fixtures use some logging in order to improve debugging. The direct advantage of this is that PatroniResource.rest_api() method is now covered by the test suite. For test_api.py, there was no 'patroni.json' file in tests/json, so we use a temporary file when serving the API.
- Loading branch information
Showing
6 changed files
with
187 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
import shutil | ||
from contextlib import contextmanager | ||
from functools import partial | ||
from http.server import HTTPServer, SimpleHTTPRequestHandler | ||
from pathlib import Path | ||
from typing import Any, Iterator, Union | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
|
||
class PatroniAPI(HTTPServer): | ||
def __init__(self, directory: Path, *, datadir: Path) -> None: | ||
self.directory = directory | ||
self.datadir = datadir | ||
handler_cls = partial(SimpleHTTPRequestHandler, directory=str(directory)) | ||
super().__init__(("", 0), handler_cls) | ||
|
||
def serve_forever(self, *args: Any) -> None: | ||
logger.info( | ||
"starting fake Patroni API at %s (directory=%s)", | ||
self.endpoint, | ||
self.directory, | ||
) | ||
return super().serve_forever(*args) | ||
|
||
@property | ||
def endpoint(self) -> str: | ||
return f"http://{self.server_name}:{self.server_port}" | ||
|
||
@contextmanager | ||
def installed(self, **installed: Union[Path, str]) -> Iterator[None]: | ||
"""Temporarilly install specified files in served directory.""" | ||
for dest, src in installed.items(): | ||
if isinstance(src, str): | ||
src = self.datadir / src | ||
shutil.copy(src, self.directory / dest) | ||
try: | ||
yield None | ||
finally: | ||
for i in installed: | ||
(self.directory / i).unlink() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
from pathlib import Path | ||
|
||
from click.testing import CliRunner | ||
|
||
from check_patroni.cli import main | ||
|
||
from . import PatroniAPI | ||
|
||
def test_api_status_code_200(fake_restapi) -> None: | ||
runner = CliRunner() | ||
|
||
fake_restapi("node_is_pending_restart_ok") | ||
result = runner.invoke( | ||
main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] | ||
) | ||
def test_api_status_code_200( | ||
runner: CliRunner, patroni_api: PatroniAPI, tmp_path: Path | ||
) -> None: | ||
patroni_json = tmp_path / "patroni.json" | ||
patroni_json.write_text('{"pending_restart": false}') | ||
with patroni_api.installed(patroni=patroni_json): | ||
result = runner.invoke( | ||
main, ["-e", patroni_api.endpoint, "node_is_pending_restart"] | ||
) | ||
assert result.exit_code == 0 | ||
|
||
|
||
def test_api_status_code_404(fake_restapi) -> None: | ||
runner = CliRunner() | ||
|
||
fake_restapi("Fake test", status=404) | ||
def test_api_status_code_404(runner: CliRunner, patroni_api: PatroniAPI) -> None: | ||
result = runner.invoke( | ||
main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] | ||
main, ["-e", patroni_api.endpoint, "node_is_pending_restart"] | ||
) | ||
assert result.exit_code == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.