Skip to content

Commit

Permalink
feat(image) : add new image ubuntu (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainmouquet authored Nov 30, 2024
1 parent ea5249f commit d4a1557
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 113 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
python-version: '3.13'
- name: Extract version from tag
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v4
- name: Use uv with Python version
run: uv venv --python 3.12
run: uv venv --python 3.13
- run: make install
- run: make build
- run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/update-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12
python-version: 3.13

- name: Install uv
uses: astral-sh/setup-uv@v3
uses: astral-sh/setup-uv@v4
- name: Use uv with Python version
run: uv venv --python 3.12
run: uv venv --python 3.13

# - name: Install dependencies
# run: make install
Expand Down
9 changes: 9 additions & 0 deletions pydocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"redis_clean_all_containers",
"redis_container",
"redis_container_session",
"ubuntu_clean_all_containers",
"ubuntu_container",
"ubuntu_container_session",
)

# pytest_plugins = ["pydocks.conftest"]
Expand All @@ -32,3 +35,9 @@
redis_container,
redis_container_session,
)

from pydocks.ubuntu import (
ubuntu_clean_all_containers,
ubuntu_container,
ubuntu_container_session,
)
91 changes: 91 additions & 0 deletions pydocks/ubuntu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import pytest
import os


import pytest_asyncio
from python_on_whales import docker as libdocker
import logging
import uuid

from pydocks.plugin import (
clean_containers,
wait_and_run_container,
)


logger = logging.getLogger("pydocks")
logger.addHandler(logging.NullHandler())


# https://hub.docker.com/_/ubuntu/tags
TEST_UBUNTU_DOCKER_IMAGE: str = "docker.io/ubuntu:24.04"


@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def ubuntu_clean_all_containers(docker):
container_name: str = "test-ubuntu"
# clean before

await clean_containers(docker, container_name)
yield
# clean after
await clean_containers(docker, container_name)


@pytest.fixture(scope="function")
async def ubuntu_container(docker: libdocker, mocker): # type: ignore
mocker.patch(
"logging.exception",
lambda *args, **kwargs: logger.warning(f"Exception raised {args}"),
)

container_name = f"test-ubuntu-{uuid.uuid4()}"
# optional : await clean_containers(docker, container_name)

async for container in setup_ubuntu_container(docker, container_name):
yield container


@pytest_asyncio.fixture(scope="session", loop_scope="session")
async def ubuntu_container_session(docker: libdocker, session_mocker): # type: ignore
session_mocker.patch(
"logging.exception",
lambda *args, **kwargs: logger.warning(f"Exception raised {args}"),
)

await clean_containers(docker, "test-ubuntu-session")

container_name = f"test-ubuntu-session-{uuid.uuid4()}"

async for container in setup_ubuntu_container(docker, container_name):
yield container


async def setup_ubuntu_container(docker: libdocker, container_name): # type: ignore
ubuntu_image = (
TEST_UBUNTU_DOCKER_IMAGE
if "TEST_UBUNTU_DOCKER_IMAGE" not in os.environ
else os.environ["TEST_UBUNTU_DOCKER_IMAGE"]
)
logger.debug(f"pull docker image : {ubuntu_image}")

def run_container(container_name: str):
return docker.run(
image=ubuntu_image,
name=container_name,
detach=True,
entrypoint="/bin/sh",
command=["-c", "sleep 60"],
)

# Select the container with the given name if exists, else create a new one
containers = docker.ps(all=True, filters={"name": f"^{container_name}$"})
if containers and len(containers) > 0:
container = containers[0] # type: ignore
logger.debug(f"found existing container: {container_name}")
else:
logger.debug(f"no existing container found, creating new one: {container_name}")
container = run_container(container_name)

async for instance in wait_and_run_container(docker, container, container_name):
yield instance
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions tests/test_ubuntu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
import os
from loguru import logger
import pytest_asyncio


@pytest_asyncio.fixture(scope="session", loop_scope="session", autouse=True)
async def begin_clean_all_containers(ubuntu_clean_all_containers):
logger.info("Begin - clean all containers")


@pytest.mark.asyncio
async def test_ubuntu_default_version(ubuntu_container):
version_output = ubuntu_container.execute(["cat", "/etc/lsb-release"])
assert (
"DISTRIB_RELEASE=24.04" in version_output
), f"Unexpected version output: {version_output}"


@pytest.fixture
def custom_ubuntu_version():
os.environ["TEST_UBUNTU_DOCKER_IMAGE"] = "docker.io/ubuntu:22.04"
yield
del os.environ["TEST_UBUNTU_DOCKER_IMAGE"]


@pytest.mark.asyncio
async def test_ubuntu_custom_version(custom_ubuntu_version, ubuntu_container):
version_output = ubuntu_container.execute(["cat", "/etc/lsb-release"])
assert (
"DISTRIB_RELEASE=22.04" in version_output
), f"Unexpected version output: {version_output}"


@pytest.mark.asyncio
async def test_ubuntu_execute_command(ubuntu_container):
# Execute Redis CLI command
result = ubuntu_container.execute(["echo", "Hello World"])
assert result.strip() == "Hello World"
File renamed without changes.
Loading

0 comments on commit d4a1557

Please sign in to comment.