Skip to content

Commit

Permalink
Add property extra_entrypoint_args to support appending arguments to …
Browse files Browse the repository at this point in the history
…the container launch command

This fixes #168
  • Loading branch information
dcermak committed Nov 14, 2023
1 parent dc5ec05 commit 1d6eccf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Breaking changes:

Improvements and new features:

- Add property
:py:attr:`~pytest_container.container.ContainerBase.extra_entrypoint_args` to
support appending arguments to the container launch command

Documentation:

Expand Down
17 changes: 15 additions & 2 deletions pytest_container/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,21 @@ class ContainerBase:
custom_entry_point: Optional[str] = None

#: List of additional flags that will be inserted after
#: `docker/podman run -d`. The list must be properly escaped, e.g. as
#: created by `shlex.split`
#: `docker/podman run -d` and before the image name (i.e. these arguments
#: are not passed to the entrypoint or ``CMD``). The list must be properly
#: escaped, e.g. as created by ``shlex.split``.
extra_launch_args: List[str] = field(default_factory=list)

#: List of additional arguments that are passed to the ``CMD`` or
#: entrypoint. These arguments are inserted after the :command:`docker/podman
#: run -d $image` on launching the image.
#: The list must be properly escaped, e.g. by passing the string through
#: ``shlex.split``.
#: The arguments must not cause the container to exit early. It must remain
#: active in the background, otherwise this library will not function
#: properly.
extra_entrypoint_args: List[str] = field(default_factory=list)

#: Time for the container to become healthy (the timeout is ignored
#: when the container image defines no ``HEALTHCHECK`` or when the timeout
#: is below zero).
Expand Down Expand Up @@ -571,6 +582,8 @@ def get_launch_cmd(
else: # pragma: no cover
assert False, "This branch must be unreachable" # pragma: no cover

cmd.extend(self.extra_entrypoint_args)

return cmd

@property
Expand Down
12 changes: 10 additions & 2 deletions source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ follows:
:command:`podman`.
Entrypoint and stop signal handling
-----------------------------------
Entrypoint, launch command and stop signal handling
---------------------------------------------------

``pytest_container`` will by default (when
:py:attr:`~pytest_container.container.ContainerBase.entry_point` is set to
Expand All @@ -419,6 +419,14 @@ image without specifying one
(:py:attr:`~pytest_container.container.EntrypointSelection.IMAGE`).


The container under test is launched by default with no further
arguments. Additional arguments can be passed to the entrypoint via the
parameter
:py:attr:`~pytest_container.container.ContainerBase.extra_entrypoint_args`. The
list of arguments/parameters is appended to the container launch command line
after the container image.


Changing the container entrypoint can have a catch with respect to the
``STOPSIGNAL`` defined by a container image. Container images that have
non-shell entry points sometimes use a different signal for stopping the main
Expand Down
35 changes: 35 additions & 0 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
custom_entry_point="/bin/sh",
)

PYTHON_LEAP = DerivedContainer(
base=LEAP,
containerfile="""
RUN set -euxo pipefail; zypper -n ref; zypper -n in python3 curl;
ENTRYPOINT ["/usr/bin/python3"]
CMD ["-m", "http.server"]
""",
)


def _test_func(con: Any) -> None:
sleep(5)
Expand Down Expand Up @@ -268,3 +278,28 @@ def try_launch():
assert not Path(
tempfile.gettempdir(), container_with_wrong_url.filelock_filename
).exists()


@pytest.mark.parametrize(
"container,port_num",
[
(
DerivedContainer(
base=PYTHON_LEAP,
extra_entrypoint_args=[
"-m",
"http.server",
"8080"
],
),
8080,
),
(PYTHON_LEAP, 8000),
],
indirect=["container"],
)
def test_extra_command_args(container: ContainerData, port_num: int) -> None:
print(id(container.container))
assert container.connection.check_output(
f"curl http://localhost:{port_num}"
)

0 comments on commit 1d6eccf

Please sign in to comment.