1
0
mirror of https://github.com/Tecnativa/docker-socket-proxy synced 2025-01-03 04:10:55 +00:00
docker-socket-proxy/tests/conftest.py
João Marques d9d5b44235 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.
2020-12-04 09:06:07 +00:00

61 lines
1.8 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_REPO = os.environ.get("DOCKER_REPO", "docker-socket-proxy")
IMAGE_NAME = f"{DOCKER_REPO}:local"
@pytest.fixture(autouse=True, scope="session")
def docker_image():
"""Build local docker image once before starting test suite."""
info(f"Building {IMAGE_NAME}...")
docker("build", "-t", IMAGE_NAME, Path(__file__).parent.parent)
return IMAGE_NAME
@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 {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,
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,
)