-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Added ServerContainer (#595)
As part of the effort described, detailed and presented on #559 This is the seconds PR (out of 4) that should provide all the groundwork to support containers running a server. This would allow users to use custom images: ```python with DockerImage(path=".", tag="test:latest") as image: with ServerContainer(port=9000, image=image) as srv: # Test something with/on the server using port 9000 ``` Next in line are: `feat(core): Added FastAPI module` `feat(core): Added AWS Lambda module` --- Based on the work done on #585 Expended from issue #83 --------- Co-authored-by: David Ankin <daveankin@gmail.com>
- Loading branch information
1 parent
59cb6fc
commit 0768490
Showing
11 changed files
with
178 additions
and
56 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
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
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
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 @@ | ||
FROM python:3-alpine | ||
EXPOSE 9000 | ||
CMD ["python", "-m", "http.server", "9000"] |
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,47 @@ | ||
import re | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
import pytest | ||
from httpx import get | ||
|
||
from testcontainers.core.waiting_utils import wait_for_logs | ||
from testcontainers.core.image import DockerImage | ||
from testcontainers.core.generic import ServerContainer | ||
|
||
TEST_DIR = Path(__file__).parent | ||
|
||
|
||
@pytest.mark.parametrize("test_image_cleanup", [True, False]) | ||
@pytest.mark.parametrize("test_image_tag", [None, "custom-image:test"]) | ||
def test_srv_container(test_image_tag: Optional[str], test_image_cleanup: bool, check_for_image, port=9000): | ||
with ( | ||
DockerImage( | ||
path=TEST_DIR / "image_fixtures/python_server", | ||
tag=test_image_tag, | ||
clean_up=test_image_cleanup, | ||
# | ||
) as docker_image, | ||
ServerContainer(port=port, image=docker_image) as srv, | ||
): | ||
image_short_id = docker_image.short_id | ||
image_build_logs = docker_image.get_logs() | ||
# check if dict is in any of the logs | ||
assert {"stream": f"Step 2/3 : EXPOSE {port}"} in image_build_logs, "Image logs mismatch" | ||
assert (port, None) in srv.ports.items(), "Port mismatch" | ||
with pytest.raises(NotImplementedError): | ||
srv.get_api_url() | ||
test_url = srv._create_connection_url() | ||
assert re.match(r"http://localhost:\d+", test_url), "Connection URL mismatch" | ||
|
||
check_for_image(image_short_id, test_image_cleanup) | ||
|
||
|
||
def test_like_doctest(): | ||
with DockerImage(path=TEST_DIR / "image_fixtures/python_server", tag="test-srv:latest") as image: | ||
with ServerContainer(port=9000, image=image) as srv: | ||
url = srv._create_connection_url() | ||
response = get(f"{url}", timeout=5) | ||
assert response.status_code == 200, "Response status code is not 200" | ||
delay = wait_for_logs(srv, "GET / HTTP/1.1") | ||
print(delay) |
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,11 @@ | ||
Community Modules | ||
================= | ||
|
||
.. | ||
glob: | ||
https://stackoverflow.com/a/44572883/4971476 | ||
.. toctree:: | ||
:glob: | ||
|
||
*/README |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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