-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pytest configuration and utilities
Add tests for Docker container
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[pytest] | ||
addopts = --capture=no --color=yes --runslow --verbose -rA | ||
|
||
log_cli = true | ||
log_cli_level = INFO | ||
|
||
markers = | ||
slow: marks tests as slow (deselect with '-m "not slow"') |
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 @@ | ||
"""pytest configurations and utilities.""" |
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,73 @@ | ||
"""pytest configuration.""" | ||
|
||
# Standard Python Libraries | ||
import os | ||
|
||
# Third-Party Libraries | ||
import docker | ||
import pytest | ||
|
||
MAIN_SERVICE_NAME = "main" | ||
|
||
client = docker.from_env() | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def group_github_log_lines(request): | ||
"""Group log lines when running in GitHub actions.""" | ||
# Group output from each test with workflow log groups | ||
# https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#grouping-log-lines | ||
|
||
if os.environ.get("GITHUB_ACTIONS") != "true": | ||
# Not running in GitHub actions | ||
yield | ||
return | ||
# Group using the current test name | ||
print() | ||
print(f"::group::{request.node.name}") | ||
yield | ||
print() | ||
print("::endgroup::") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def main_container(image_tag): | ||
"""Fixture for the main Foundry container.""" | ||
container = client.containers.run( | ||
image_tag, | ||
detach=True, | ||
environment={}, | ||
name=MAIN_SERVICE_NAME, | ||
) | ||
yield container | ||
container.remove(force=True) | ||
|
||
|
||
def pytest_addoption(parser): | ||
"""Add new commandline options to pytest.""" | ||
parser.addoption( | ||
"--runslow", action="store_true", default=False, help="run slow tests" | ||
) | ||
parser.addoption( | ||
"--image-tag", | ||
action="store", | ||
default="local/test-image:latest", | ||
help="image tag to test", | ||
) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def image_tag(request): | ||
"""Get the image tag to test.""" | ||
return request.config.getoption("--image-tag") | ||
|
||
|
||
def pytest_collection_modifyitems(config, items): | ||
"""Modify collected tests based on custom marks and commandline options.""" | ||
if config.getoption("--runslow"): | ||
# --runslow given in cli: do not skip slow tests | ||
return | ||
skip_slow = pytest.mark.skip(reason="need --runslow option to run") | ||
for item in items: | ||
if "slow" in item.keywords: | ||
item.add_marker(skip_slow) |
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,23 @@ | ||
#!/usr/bin/env pytest -vs | ||
"""Tests for Docker container.""" | ||
|
||
# Standard Python Libraries | ||
import time | ||
|
||
|
||
def test_container_running(main_container): | ||
"""Test that the container has started.""" | ||
# Wait until the container is running or timeout. | ||
for _ in range(10): | ||
main_container.reload() | ||
if main_container.status != "created": | ||
break | ||
time.sleep(1) | ||
assert main_container.status in ("exited", "running") | ||
|
||
|
||
def test_wait_for_container_exit(main_container): | ||
"""Wait for version container to exit cleanly.""" | ||
assert ( | ||
main_container.wait()["StatusCode"] == 0 | ||
), "The container did not exit cleanly" |