Skip to content

Commit

Permalink
Add pytest configuration and utilities
Browse files Browse the repository at this point in the history
Add tests for Docker container
  • Loading branch information
felddy committed Jan 17, 2024
1 parent a3df5cc commit 4ccd87a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .bandit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ tests:
# - B102

skips:
# - B101 # skip "assert used" check since assertions are required in pytests
- B101 # skip "assert used" check since assertions are required in pytests
8 changes: 8 additions & 0 deletions pytest.ini
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"')
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""pytest configurations and utilities."""
73 changes: 73 additions & 0 deletions tests/conftest.py
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)
23 changes: 23 additions & 0 deletions tests/container_test.py
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"

0 comments on commit 4ccd87a

Please sign in to comment.