mirror of
https://github.com/Tecnativa/docker-socket-proxy
synced 2025-01-06 13:50:59 +00:00
e84babd1c4
* Add first version of tests
From https://github.com/Tecnativa/docker-socket-proxy/pull/14
* Expand tests
* Add GH CI
* Apply suggestions
* Apply autopretty template + fix prettier
* Fix isort
* Apply autoprettier
* Fix VSCode settings
* Make tests run in parallel
* Build docker image before testing
* Update workspace settings
* Try multi-platform builds and push to ghcr.io
* Push to docker hub as well from ci
* Upgrade autopretty
* Update pyproject configurations
* Improve test configuration and execution
TT26468
* Provide initial conftest
* Improve tests
* Add python3 in image
* Remove POST rule from proxy
* Build image before testing and push at the end
Builds the image (in single arch) before testing
Loads the image into local docker (See https://github.com/docker/build-push-action#export-image-to-docker)
Rebuilds and pushes the final image in multi-arch at the end.
* Fix python path
* Remove build fixture from tests to see if image is built in CI
* Organize docker tests definition and document
* Restore fixture allowing usage for local testing
This reverts commit dc0b60e63f
and allows using `--prebuild` CLI flag for pytest when doing local tests.
Co-authored-by: Jairo Llopis <jairo.llopis@tecnativa.com>
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import json
|
|
import os
|
|
from contextlib import contextmanager
|
|
from logging import info
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from plumbum import local
|
|
from plumbum.cmd import docker
|
|
|
|
DOCKER_IMAGE_NAME = os.environ.get("DOCKER_IMAGE_NAME", "docker-socket-proxy:local")
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
"""Allow prebuilding image for local testing."""
|
|
parser.addoption("--prebuild", action="store_const", const=True)
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope="session")
|
|
def prebuild_docker_image(request):
|
|
"""Build local docker image once before starting test suite."""
|
|
if request.config.getoption("--prebuild"):
|
|
info(f"Building {DOCKER_IMAGE_NAME}...")
|
|
docker("build", "-t", DOCKER_IMAGE_NAME, Path(__file__).parent.parent)
|
|
|
|
|
|
@contextmanager
|
|
def proxy(**env_vars):
|
|
"""A context manager that starts the proxy with the specified env.
|
|
|
|
While inside the block, `$DOCKER_HOST` will be modified to talk to the proxy
|
|
instead of the raw docker socket.
|
|
"""
|
|
container_id = None
|
|
env_list = [f"--env={key}={value}" for key, value in env_vars.items()]
|
|
info(f"Starting {DOCKER_IMAGE_NAME} container with: {env_list}")
|
|
try:
|
|
container_id = docker(
|
|
"container",
|
|
"run",
|
|
"--detach",
|
|
"--privileged",
|
|
"--publish=2375",
|
|
"--volume=/var/run/docker.sock:/var/run/docker.sock",
|
|
*env_list,
|
|
DOCKER_IMAGE_NAME,
|
|
).strip()
|
|
container_data = json.loads(
|
|
docker("container", "inspect", container_id.strip())
|
|
)
|
|
socket_port = container_data[0]["NetworkSettings"]["Ports"]["2375/tcp"][0][
|
|
"HostPort"
|
|
]
|
|
with local.env(DOCKER_HOST=f"tcp://localhost:{socket_port}"):
|
|
yield container_id
|
|
finally:
|
|
if container_id:
|
|
info(f"Removing {container_id}...")
|
|
docker(
|
|
"container",
|
|
"rm",
|
|
"-f",
|
|
container_id,
|
|
)
|