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

Fix container-in-container testing #505

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
9 changes: 1 addition & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/asottile/add-trailing-comma.git
rev: v3.1.0
hooks:
- id: add-trailing-comma
args:
- --py36-plus

- repo: https://github.com/Lucas-C/pre-commit-hooks.git
rev: v1.5.5
hooks:
Expand Down Expand Up @@ -100,7 +93,7 @@ repos:
hooks:
- id: pydoclint
# This allows automatic reduction of the baseline file when needed.
entry: sh -ec "pydoclint . && pydoclint --generate-baseline=1 ."
entry: sh -ec "pydoclint -q . && pydoclint --generate-baseline=1 ."
pass_filenames: false

- repo: https://github.com/pycqa/pylint.git
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ external = [
]
ignore = [
"COM812", # conflicts with ISC001 on format
"E501", # line-too-long / rely on black
"ISC001" # conflicts with COM812 on format
]
select = ["ALL"]
Expand Down
26 changes: 19 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import pty
import re
import select
import shlex
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -310,10 +311,14 @@ def _start_container() -> None:
err = f"Container engine {INFRASTRUCTURE.container_engine} not found."
raise ValueError(err)

cmd = cmd.replace("\n", " ").format(
container_engine=INFRASTRUCTURE.container_engine,
container_name=INFRASTRUCTURE.container_name,
image_name=INFRASTRUCTURE.image_name,
cmd = (
cmd.replace("\n", " ")
.format(
container_engine=INFRASTRUCTURE.container_engine,
container_name=INFRASTRUCTURE.container_name,
image_name=INFRASTRUCTURE.image_name,
)
.replace(" ", " ")
)
LOGGER.warning("Running: %s", cmd)
try:
Expand Down Expand Up @@ -395,9 +400,16 @@ def _exec_container(command: str) -> subprocess.CompletedProcess[str]:
Returns:
subprocess.CompletedProcess: The completed process.
"""
cmd = (
f"{INFRASTRUCTURE.container_engine} exec -t"
f" {INFRASTRUCTURE.container_name} bash -c '{command}'"
cmd = shlex.join(
[
INFRASTRUCTURE.container_engine,
"exec",
"-t",
INFRASTRUCTURE.container_name,
"bash",
"-c",
command,
]
)
result = subprocess.run(
cmd,
Expand Down
32 changes: 26 additions & 6 deletions tests/integration/test_container.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Run tests against the container."""

# cspell: ignore cinc
from __future__ import annotations

import shlex

from typing import TYPE_CHECKING

import pytest
Expand Down Expand Up @@ -48,24 +51,41 @@ def test_podman(exec_container: Callable[[str], subprocess.CompletedProcess[str]


@pytest.mark.container
def test_container_in_container(
def test_cinc(
infrastructure: Infrastructure,
exec_container: Callable[[str], subprocess.CompletedProcess[str]],
) -> None:
"""Test podman container-in-container functionality for plugin copy.

Args:
infrastructure: The testing infrastructure.
exec_container: The container executor.
"""
# We do not want to accidentally pull here because we expected to load
# the tar image into the container. Our scope is to test the image we did
# not publish yet to any registry.
podman_run_container = exec_container(
"podman run -i --rm -d -e ANSIBLE_DEV_TOOLS_CONTAINER=1"
" -e ANSIBLE_FORCE_COLOR=0 --name ghcr_io_ansible_community_ansible_dev_tools_latest"
" ghcr.io/ansible/community-ansible-dev-tools:latest bash",
"podman run --pull=never -i --rm -d -e ANSIBLE_DEV_TOOLS_CONTAINER=1"
" -e ANSIBLE_FORCE_COLOR=0 --name test_cinc"
f" {infrastructure.image_name} bash",
)
assert podman_run_container.returncode == 0

test_path_access = exec_container(
"podman exec ghcr_io_ansible_community_ansible_dev_tools_latest"
" ls /usr/local/lib/python3.12/site-packages/ansible/plugins/",
shlex.join(
[
"podman",
"exec",
"test_cinc",
"python3",
"-c",
(
"import pathlib, sys; sys.exit(0 if"
" pathlib.Path(f'/usr/local/lib/python{sys.version_info[0]}.{sys.version_info[1]}/site-packages/ansible/plugins/').exists()"
" else 1);"
),
],
),
)
assert "OCI permission denied" not in test_path_access.stdout
assert test_path_access.returncode == 0
Expand Down
Loading