-
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 servers in tests (WIP)
We introduce a few fixtures, defined in tests/conftest.py: - http_server_factory, a factory to create HTTP server serving files in specified directory - rest_api_server, an HTTP server serving files in tests/json directory - 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. The direct advantage of this is that PatroniResource.rest_api() method is now covered by the test suite. In test_api.py, we either use the main 'rest_api_server' or build a specific one to check 404 case. In test_cluster_config_has_changed.py, we build a specific server serving only one file. The test suite is slower however now, as we start and stop an HTTP server for each test case. This might be improved later by using another scope (e.g. module or package).
- Loading branch information
Showing
5 changed files
with
115 additions
and
37 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,12 @@ | ||
from http.server import HTTPServer | ||
from pathlib import Path | ||
from typing import Callable | ||
|
||
|
||
class RESTAPIServer(HTTPServer): | ||
@property | ||
def endpoint(self) -> str: | ||
return f"http://{self.server_name}:{self.server_port}" | ||
|
||
|
||
ServerFactory = Callable[[Path], RESTAPIServer] |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"pending_restart": false | ||
} |
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,23 @@ | ||
from pathlib import Path | ||
|
||
from click.testing import CliRunner | ||
|
||
from check_patroni.cli import main | ||
|
||
from . import RESTAPIServer, ServerFactory | ||
|
||
def test_api_status_code_200(fake_restapi) -> None: | ||
runner = CliRunner() | ||
|
||
fake_restapi("node_is_pending_restart_ok") | ||
def test_api_status_code_200(runner: CliRunner, rest_api_server: RESTAPIServer) -> None: | ||
result = runner.invoke( | ||
main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] | ||
main, ["-e", rest_api_server.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) | ||
result = runner.invoke( | ||
main, ["-e", "https://10.20.199.3:8008", "node_is_pending_restart"] | ||
) | ||
def test_api_status_code_404( | ||
tmp_path: Path, http_server_factory: ServerFactory, runner: CliRunner | ||
) -> None: | ||
# HTTP server using an empty (tmp) directory, thus responding with 404s. | ||
httpd = http_server_factory(tmp_path) | ||
result = runner.invoke(main, ["-e", httpd.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