-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(image) : add new image ubuntu (#26)
- Loading branch information
1 parent
ea5249f
commit d4a1557
Showing
11 changed files
with
282 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.