Skip to content

Commit

Permalink
Merge pull request #699 from SUSE/tomcat-no-curl
Browse files Browse the repository at this point in the history
[tomcat] stop needing curl for the tests
  • Loading branch information
dirkmueller authored Dec 12, 2024
2 parents 0d76fa5 + 1ef7659 commit 9db04a5
Showing 1 changed file with 68 additions and 35 deletions.
103 changes: 68 additions & 35 deletions tests/test_tomcat.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
"""Tests for the tomcat containers."""

from pathlib import Path

import pytest
import requests
import tenacity
from pytest_container import DerivedContainer
from pytest_container import container_and_marks_from_pytest_param
from pytest_container import OciRuntimeBase
from pytest_container.container import ContainerData
from pytest_container.container import ImageFormat

from bci_tester.data import TOMCAT_CONTAINERS

TOMCAT_WITH_SAMPLE = []

for tomcat_ctr in TOMCAT_CONTAINERS:
ctr, marks = container_and_marks_from_pytest_param(tomcat_ctr)
TOMCAT_WITH_SAMPLE.append(
pytest.param(
DerivedContainer(
base=ctr,
forwarded_ports=ctr.forwarded_ports,
containerfile="""RUN cd $CATALINA_HOME/webapps; curl -sfO https://tomcat.apache.org/tomcat-10.1-doc/appdev/sample/sample.war
HEALTHCHECK --interval=5s --timeout=5s --retries=5 CMD ["/usr/bin/curl", "-sf", "http://localhost:8080/sample"]
""",
image_format=ImageFormat.DOCKER,
),
marks=marks,
)
)


@pytest.mark.parametrize("container", TOMCAT_CONTAINERS, indirect=True)
def test_tomcat_launches(container: ContainerData) -> None:
def _tomcat_launch_test_fn(container: ContainerData) -> requests.Response:
"""Simple smoke test verifying that the entrypoint launches tomcat and the
server is responding to a ``GET /`` on the exposed port.
Expand All @@ -47,29 +28,81 @@ def _fetch_tomcat_root() -> requests.Response:
)

resp = _fetch_tomcat_root()
return resp


@pytest.mark.parametrize("container", TOMCAT_CONTAINERS, indirect=True)
def test_tomcat_launches(container: ContainerData) -> None:
"""Simple smoke test verifying that the entrypoint launches tomcat and the
server is responding to a ``GET /`` on the exposed port.
"""

resp = _tomcat_launch_test_fn(container)

baseurl = container.container.baseurl
assert baseurl
ver = baseurl.rpartition(":")[2].partition("-")[0]
assert resp.status_code == 404
assert f"Apache Tomcat/{ver}" in resp.text


@pytest.mark.parametrize("container", TOMCAT_CONTAINERS, indirect=True)
def test_tomcat_logs(container: ContainerData) -> None:
""""""
_tomcat_launch_test_fn(container)
logs = container.read_container_logs()
assert (
"[main] org.apache.catalina.startup.Catalina.start Server startup in"
in logs
), "startup logging message not found"
assert (
"-Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties"
in logs
), "expected logfile CLI argument not found"


@pytest.mark.parametrize(
"container_per_test", TOMCAT_WITH_SAMPLE, indirect=True
"container_per_test", TOMCAT_CONTAINERS, indirect=True
)
def test_tomcat_launches_sample_app(container_per_test: ContainerData) -> None:
"""Launches a container derived from the tomcat containers that includes the
`Tomcat sample application
<https://tomcat.apache.org/tomcat-10.1-doc/appdev/sample/>`_ in the
:file:`$CATALINA_HOME/webapps` directory. Tomcat should serve this sample
app automatically via the `GET /sample` route. We test that Tomcat responds
to `GET /sample` with a response containing `Sample "Hello, World"
Application`.
def test_tomcat_launches_sample_app(
container_per_test: ContainerData,
host,
tmp_path: Path,
container_runtime: OciRuntimeBase,
) -> None:
"""Launches a tomcat container. The test downloads the `Tomcat sample
application <https://tomcat.apache.org/tomcat-10.1-doc/appdev/sample/>`_ and
copies it into the :file:`$CATALINA_HOME/webapps` directory, which then
tomcat should serve automatically via the `GET /sample` route. We test that
Tomcat responds to `GET /sample` with a response containing ``Sample "Hello,
World" Application``.
"""
resp = requests.get(
f"http://localhost:{container_per_test.forwarded_ports[0].host_port}/sample",
timeout=2,

sample_req = requests.get(
"https://tomcat.apache.org/tomcat-10.1-doc/appdev/sample/sample.war"
)
sample_req.raise_for_status()

sample_war_dest = str(tmp_path / "sample.war")
with open(sample_war_dest, "wb") as sample_war:
for chunk in sample_req.iter_content():
sample_war.write(chunk)

host.check_output(
f"{container_runtime.runner_binary} cp {sample_war_dest} {container_per_test.container_id}:/srv/tomcat/webapps/"
)

@tenacity.retry(
stop=tenacity.stop_after_attempt(5), wait=tenacity.wait_exponential()
)
def _fetch_tomcat_sample() -> requests.Response:
return requests.get(
f"http://localhost:{container_per_test.forwarded_ports[0].host_port}/sample",
timeout=2,
)

resp = _fetch_tomcat_sample()
assert resp.status_code == 200
assert 'Sample "Hello, World" Application' in resp.text

0 comments on commit 9db04a5

Please sign in to comment.