Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional platform argument to ContainerBase when docker pull is executed #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 2 additions & 85 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,98 +26,18 @@ jobs:
pip install --upgrade poetry nox nox-poetry
nox -s format -- --check

generate_requirements:
name: Generate requirements.txt for Python 3.6 CI
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.x'
- uses: actions/cache@v4
with:
path: ~/.cache/pypoetry/virtualenvs
key: poetry-${{ hashFiles('poetry.lock') }}

- run: |
pipx install poetry
poetry export --with=dev --without-hashes > requirements.txt

- uses: actions/upload-artifact@v4
with:
name: requirements
path: requirements.txt
if-no-files-found: error

ci_36:
name: Run the integration tests for Python 3.6
runs-on: ubuntu-20.04
needs: generate_requirements

strategy:
fail-fast: false
matrix:
container_runtime: ["podman", "docker"]

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.6

- uses: actions/download-artifact@v4
with:
name: requirements
path: .

- run: pip install -r test-requirements.txt

- run: |
export CUR_USER="$(whoami)"
sudo loginctl enable-linger ${CUR_USER}

- run: |
mkdir ./tmp/
chmod 1777 ./tmp
export TMPDIR="$(pwd)/tmp"
# duplication of noxfile.py because we can't use that one with python 3.6 :-/
coverage run -m pytest -vv tests -p pytest_container -x -n auto --reruns 3 --pytest-container-log-level DEBUG
coverage run -m pytest -vv tests -p pytest_container -x --reruns 3 --pytest-container-log-level DEBUG
coverage combine
coverage report -m
coverage xml

- name: verify that no stray containers are left
run: |
[[ $(${{ matrix.container_runtime }} ps -aq|wc -l) = "0" ]]

- name: verify that no stray volumes are left
run: |
[[ $(${{ matrix.container_runtime }} volume ls -q|wc -l) = "0" ]]

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true

ci:
name: Run the integration tests
runs-on: "ubuntu-latest"
strategy:
fail-fast: false
matrix:
python_version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
python_version: ["3.9", "3.10", "3.11", "3.12"]
container_runtime: ["podman", "docker"]
update_runtime: [ true, false ]

exclude:
- container_runtime: "docker"
python_version: "3.7"
update_runtime: true
- container_runtime: "docker"
python_version: "3.8"
update_runtime: true
- container_runtime: "docker"
python_version: "3.9"
update_runtime: true
Expand Down Expand Up @@ -217,10 +137,7 @@ jobs:
fail-fast: false
matrix:
os_version: ["ubuntu-latest"]
python_version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
include:
- os_version: "ubuntu-20.04"
python_version: "3.6"
python_version: ["3.9", "3.10", "3.11"]

steps:
- uses: actions/setup-python@v5
Expand Down
15 changes: 14 additions & 1 deletion pytest_container/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@
default_factory=list
)

#: optional target platform for the container image
platform: Optional[str] = None

_is_local: bool = False

def __post_init__(self) -> None:
Expand Down Expand Up @@ -654,7 +657,14 @@
container runtime"""
runtime = get_selected_runtime()
_logger.debug("Pulling %s via %s", self.url, runtime.runner_binary)
check_output([runtime.runner_binary, "pull", self.url])

# Construct the args passed to the Docker CLI
cmd_args = [runtime.runner_binary, "pull"]
if self.platform is not None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.platform is not None:
if self.platform:

cmd_args += ["--platform", self.platform]

Check warning on line 664 in pytest_container/container.py

View check run for this annotation

Codecov / codecov/patch

pytest_container/container.py#L664

Added line #L664 was not covered by tests
cmd_args.append(self.url)

check_output(cmd_args)

def prepare_container(
self, rootdir: Path, extra_build_args: Optional[List[str]] = None
Expand Down Expand Up @@ -1067,6 +1077,9 @@
if self.container_name:
extra_run_args.extend(("--name", self.container_name))

if self.container.platform:
extra_run_args.extend(("--platform", self.container.platform))

Check warning on line 1081 in pytest_container/container.py

View check run for this annotation

Codecov / codecov/patch

pytest_container/container.py#L1081

Added line #L1081 was not covered by tests

extra_run_args.append(f"--cidfile={self._cidfile}")

# We must perform the launches in separate branches, as containers with
Expand Down
14 changes: 10 additions & 4 deletions pytest_container/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,15 +621,21 @@
podman_exists = LOCALHOST.exists("podman") and LOCALHOST.exists("buildah")
docker_exists = LOCALHOST.exists("docker")

runtime_choice = getenv("CONTAINER_RUNTIME", "podman").lower()
if runtime_choice not in ("podman", "docker"):
runtime_choice = getenv("CONTAINER_RUNTIME", None)
if runtime_choice is not None and runtime_choice.lower() not in (
"podman",
"docker",
):
raise ValueError(f"Invalid CONTAINER_RUNTIME {runtime_choice}")

if runtime_choice == "podman" and podman_exists:
runtime_choice = runtime_choice and runtime_choice.lower() or None
if runtime_choice in ("podman", None) and podman_exists:
return PodmanRuntime()
if runtime_choice == "docker" and docker_exists:

if runtime_choice in ("docker", None) and docker_exists:
return DockerRuntime()

runtime_choice = runtime_choice or "(podman or docker)"

Check warning on line 638 in pytest_container/runtime.py

View check run for this annotation

Codecov / codecov/patch

pytest_container/runtime.py#L638

Added line #L638 was not covered by tests
raise ValueError(
"Selected runtime " + runtime_choice + " does not exist on the system"
)
2 changes: 1 addition & 1 deletion tests/test_container_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_multistage_build_target(
== "foobar"
)

for (distro, target) in (
for distro, target in (
("Leap", first_target),
("Alpine", second_target),
):
Expand Down
1 change: 0 additions & 1 deletion tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def test_launcher_creates_and_cleanes_up_volumes(
assert container.volume_mounts

for vol in container.volume_mounts:

if isinstance(vol, BindMount):
assert vol.host_path and os.path.exists(vol.host_path)
elif isinstance(vol, ContainerVolume):
Expand Down