Skip to content

Commit

Permalink
Merge pull request #170 from dcermak/entrypoint-args
Browse files Browse the repository at this point in the history
Entrypoint args
  • Loading branch information
dcermak authored Nov 28, 2023
2 parents 71ad8e2 + 41af565 commit 64aba0d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python_version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python_version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
container_runtime: ["podman", "docker"]
update_runtime: [ true, false ]

Expand All @@ -127,6 +127,9 @@ jobs:
- container_runtime: "docker"
python_version: "3.11"
update_runtime: true
- container_runtime: "docker"
python_version: "3.12"
update_runtime: true

steps:
- uses: actions/checkout@v4
Expand All @@ -145,12 +148,12 @@ jobs:
if: ${{ matrix.update_runtime }}
run: |
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_$(lsb_release -rs)/Release.key \
curl -fsSL https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/unstable/xUbuntu_$(lsb_release -rs)/Release.key \
| gpg --dearmor \
| sudo tee /etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg > /dev/null
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/devel_kubic_libcontainers_unstable.gpg]\
https://download.opensuse.org/repositories/devel:kubic:libcontainers:unstable/xUbuntu_$(lsb_release -rs)/ /" \
https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/unstable/xUbuntu_$(lsb_release -rs)/ /" \
| sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:unstable.list > /dev/null
sudo apt-get update -qq
sudo apt-get -qq -y install podman buildah
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ 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

- Add support for Python 3.12

- Add property :py:attr:`~pytest_container.inspect.PortForwarding.bind_ip`
to support binding to arbitrary IP addresses.

- Fix :py:attr:`~pytest_container.inspect.PortForwarding.host_port` being
ignored when picking the host port

Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from nox_poetry import session


@session(python=["3.11", "3.10", "3.9", "3.8", "3.7", "3.6"])
@session(python=["3.12", "3.11", "3.10", "3.9", "3.8", "3.7", "3.6"])
@nox.parametrize(
"container_runtime",
[nox.param(runtime, id=runtime) for runtime in ("podman", "docker")],
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Software Development :: Quality Assurance",
"Topic :: Software Development :: Testing"
]
Expand Down
17 changes: 15 additions & 2 deletions pytest_container/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,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 @@ -576,6 +587,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
31 changes: 31 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,24 @@ 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 64aba0d

Please sign in to comment.