1
0
mirror of https://github.com/Tecnativa/docker-socket-proxy synced 2025-07-12 10:08:08 +00:00

Restore fixture allowing usage for local testing

This reverts commit dc0b60e63f and allows using `--prebuild` CLI flag for pytest when doing local tests.
This commit is contained in:
Jairo Llopis 2020-12-04 17:18:44 +00:00
parent b46fc70a16
commit 15071f7387
No known key found for this signature in database
GPG Key ID: 8B8A6900E4831A9B
2 changed files with 34 additions and 8 deletions

View File

@ -153,21 +153,32 @@ poetry install
### Testing ### Testing
To run the tests locally, you first need to build the image locally: To run the tests locally, add `--prebuild` to autobuild the image before testing:
``` ```sh
docker build -t docker-socket-proxy:local . poetry run pytest --prebuild
``` ```
You can then run them with: By default, the image that the tests use (and optionally prebuild) is named
`docker-socket-proxy:local`. If you prefer, you can build it separately before testing,
and remove the `--prebuild` flag, to run the tests with that image you built:
``` ```sh
docker image build -t docker-socket-proxy:local .
poetry run pytest poetry run pytest
``` ```
_Note:_ You can use the docker tag you want, but that is the one that is picked by If you want to use a different image, export the `DOCKER_IMAGE_NAME` env variable with
default in the tests. If you opt for a different one, set the environment variable the name you want:
`DOCKER_IMAGE_NAME` to the value you prefer before running the tests.
```sh
# To build it automatically
env DOCKER_IMAGE_NAME=my_custom_image poetry run pytest --prebuild
# To prebuild it separately
docker image build -t my_custom_image .
env DOCKER_IMAGE_NAME=my_custom_image poetry run pytest
```
## Logging ## Logging

View File

@ -2,13 +2,28 @@ import json
import os import os
from contextlib import contextmanager from contextlib import contextmanager
from logging import info from logging import info
from pathlib import Path
import pytest
from plumbum import local from plumbum import local
from plumbum.cmd import docker from plumbum.cmd import docker
DOCKER_IMAGE_NAME = os.environ.get("DOCKER_IMAGE_NAME", "docker-socket-proxy:local") 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 @contextmanager
def proxy(**env_vars): def proxy(**env_vars):
"""A context manager that starts the proxy with the specified env. """A context manager that starts the proxy with the specified env.