Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	requirements-test.txt
#	tests/conftest.py
#	tests/container_test.py
  • Loading branch information
mcdonnnj committed Jul 28, 2023
2 parents 6224be7 + a9d6c92 commit c8c80f9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
--requirement requirements.txt
pre-commit
pytest
<<<<<<< HEAD
pytest-dockerc
semver
=======
python-on-whales
>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670
24 changes: 23 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,39 @@
"""
# Third-Party Libraries
import pytest
from python_on_whales import docker

MAIN_SERVICE_NAME = "gophish"


@pytest.fixture(scope="session")
def dockerc():
"""Start up the Docker composition."""
docker.compose.up(detach=True)
yield docker
docker.compose.down()


@pytest.fixture(scope="session")
def main_container(dockerc):
"""Return the main container from the Docker composition."""
# find the container by name even if it is stopped already
return dockerc.containers(service_names=[MAIN_SERVICE_NAME], stopped=True)[0]
return dockerc.compose.ps(services=[MAIN_SERVICE_NAME], all=True)[0]


<<<<<<< HEAD
=======
@pytest.fixture(scope="session")
def version_container(dockerc):
"""Return the version container from the Docker composition.
The version container should just output the version of its underlying contents.
"""
# find the container by name even if it is stopped already
return dockerc.compose.ps(services=[VERSION_SERVICE_NAME], all=True)[0]


>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670
def pytest_addoption(parser):
"""Add new commandline options to pytest."""
parser.addoption(
Expand Down
50 changes: 49 additions & 1 deletion tests/container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ def test_container_count(dockerc):
"""Verify the test composition and container."""
# stopped parameter allows non-running containers in results
assert (
<<<<<<< HEAD
len(dockerc.containers(stopped=True)) == 1
=======
len(dockerc.compose.ps(all=True)) == 2
>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670
), "Wrong number of containers were started."


def test_wait_for_ready(main_container):
"""Wait for container to be ready."""
TIMEOUT = 10
for i in range(TIMEOUT):
if READY_MESSAGE in main_container.logs().decode("utf-8"):
if READY_MESSAGE in main_container.logs():
break
time.sleep(1)
else:
Expand All @@ -35,6 +39,27 @@ def test_wait_for_ready(main_container):
)


<<<<<<< HEAD
=======
def test_wait_for_exits(dockerc, main_container, version_container):
"""Wait for containers to exit."""
assert (
dockerc.wait(main_container.id) == 0
), "Container service (main) did not exit cleanly"
assert (
dockerc.wait(version_container.id) == 0
), "Container service (version) did not exit cleanly"


def test_output(dockerc, main_container):
"""Verify the container had the correct output."""
# make sure container exited if running test isolated
dockerc.wait(main_container.id)
log_output = main_container.logs()
assert SECRET_QUOTE in log_output, "Secret not found in log output."


>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670
@pytest.mark.skipif(
RELEASE_TAG in [None, ""], reason="this is not a release (RELEASE_TAG not set)"
)
Expand All @@ -49,12 +74,35 @@ def test_release_version():
), "RELEASE_TAG does not match the project version"


<<<<<<< HEAD
def test_container_version_label_matches(main_container):
=======
def test_log_version(dockerc, version_container):
"""Verify the container outputs the correct version to the logs."""
# make sure container exited if running test isolated
dockerc.wait(version_container.id)
log_output = version_container.logs().strip()
pkg_vars = {}
with open(VERSION_FILE) as f:
exec(f.read(), pkg_vars) # nosec
project_version = pkg_vars["__version__"]
assert (
log_output == project_version
), f"Container version output to log does not match project version file {VERSION_FILE}"


def test_container_version_label_matches(version_container):
>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670
"""Verify the container version label is the correct version."""
pkg_vars = {}
with open(VERSION_FILE) as f:
exec(f.read(), pkg_vars) # nosec
project_version = pkg_vars["__version__"]
assert (
<<<<<<< HEAD
main_container.labels["org.opencontainers.image.version"] == project_version
=======
version_container.config.labels["org.opencontainers.image.version"]
== project_version
>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670
), "Dockerfile version label does not match project version"

0 comments on commit c8c80f9

Please sign in to comment.