diff --git a/requirements-test.txt b/requirements-test.txt index 5f3337c0..8b41b2f9 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,4 +1,4 @@ --requirement requirements.txt pre-commit pytest -pytest-dockerc +python-on-whales diff --git a/tests/conftest.py b/tests/conftest.py index 879e3bda..ce6d3d4e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,17 +4,39 @@ """ # Third-Party Libraries import pytest +from python_on_whales import docker MAIN_SERVICE_NAME = "web" +@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( diff --git a/tests/container_test.py b/tests/container_test.py index e9aea226..9155abf9 100644 --- a/tests/container_test.py +++ b/tests/container_test.py @@ -14,7 +14,11 @@ def test_container_count(dockerc): """Verify correct number of containers are running.""" # 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." @@ -24,7 +28,7 @@ def test_container(main_container): # this to be shorter, so hence the longer timeout. TIMEOUT = 360 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: @@ -37,10 +41,21 @@ def test_container(main_container): # it's still running. time.sleep(10) +<<<<<<< HEAD # Make sure the container is not exiting. assert main_container.is_running is True, "Docker container is not running." assert main_container.is_restarting is False, "Docker container is restarting." assert main_container.exit_code == 0, "Docker container exited." +======= +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" +>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670 # Get project version pkg_vars = {} @@ -48,12 +63,39 @@ def test_container(main_container): exec(f.read(), pkg_vars) # nosec project_version = pkg_vars["__version__"] +<<<<<<< HEAD # Get log output log_output = main_container.logs().decode("utf-8") +======= +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 # Assert version output is in the logs. assert ( +<<<<<<< HEAD f"Running Con-PCA version {project_version}" in log_output +======= + RELEASE_TAG == f"v{project_version}" + ), "RELEASE_TAG does not match the project version" + + +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 +>>>>>>> a9d6c92ea3ca2760e4a18276d06c668058dd3670 ), f"Container version output to log does not match project version file {VERSION_FILE}" # Assert release Version Label @@ -64,5 +106,10 @@ def test_container(main_container): # Assert container version label matches 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"