mirror of
https://github.com/Tecnativa/docker-socket-proxy
synced 2025-07-12 18:18:21 +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:
parent
b46fc70a16
commit
15071f7387
27
README.md
27
README.md
@ -153,21 +153,32 @@ poetry install
|
||||
|
||||
### 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:
|
||||
|
||||
```
|
||||
docker build -t docker-socket-proxy:local .
|
||||
```sh
|
||||
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
|
||||
```
|
||||
|
||||
_Note:_ You can use the docker tag you want, but that is the one that is picked by
|
||||
default in the tests. If you opt for a different one, set the environment variable
|
||||
`DOCKER_IMAGE_NAME` to the value you prefer before running the tests.
|
||||
If you want to use a different image, export the `DOCKER_IMAGE_NAME` env variable with
|
||||
the name you want:
|
||||
|
||||
```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
|
||||
|
||||
|
@ -2,13 +2,28 @@ 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.
|
||||
|
Loading…
Reference in New Issue
Block a user