Skip to content

Commit

Permalink
Fix container-in-container testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Dec 19, 2024
1 parent e1cc77d commit fed6604
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
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
27 changes: 22 additions & 5 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,38 @@ 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.
"""
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",
" -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

0 comments on commit fed6604

Please sign in to comment.