Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BaseImage #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.177.0/containers/python-3
{
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"extensions": [
"github.vscode-pull-request-github",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-vscode.makefile-tools",
"ryanluker.vscode-coverage-gutters",
"visualstudioexptteam.vscodeintellicode"
],
// Set *default* container specific settings.json values on container create.
"settings": {
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.tabSize": 4,
"files.eol": "\n",
"files.trimTrailingWhitespace": true,
"python.analysis.autoImportCompletions": true,
"python.analysis.autoSearchPaths": false,
"python.analysis.extraPaths": ["/home/vscode/.local/lib/python3.11/"],
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.provider": "black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.languageServer": "Pylance",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.enabled": true,
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyEnabled": true,
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintArgs": ["--disable", "import-error"],
"python.linting.pylintEnabled": true,
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint",
"python.pythonPath": "/usr/local/python/bin/python",
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
}
}
}
},
// Add the IDs of extensions you want installed when the container is created.
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},
"image": "mcr.microsoft.com/devcontainers/python:3",
"name": "Pytest-docker-fixtures",
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip install -e .",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"runArgs": ["-e", "GIT_EDITOR=code --wait"]
}
40 changes: 40 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
hooks:
- id: pyupgrade
args:
- --py38-plus
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args:
- --quiet
- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
- id: codespell
args:
- --ignore-words-list=hass,deebot
- --skip="./.*,*.csv,*.json"
- --quiet-level=2
exclude_types:
- csv
- json
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-merge-conflict
- id: detect-private-key
- id: no-commit-to-branch
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
hooks:
- id: mypy
additional_dependencies:
- types-requests
19 changes: 11 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,24 @@ PRs welcome!
- kafka (require to be installed with `pip install pytest-docker-fixtures[kafka]`)
- memcached (require to be installed with `pip install pytest-docker-fixtures[memcached]`)
- minio
- mosquitto (MQTT)
- mysql (require to be installed with `pip install pytest-docker-fixtures[mysql]`)
- pg (require to be installed with `pip install pytest-docker-fixtures[pg]`)
- rabbitmq (require to be installed with `pip install pytest-docker-fixtures[rabbitmq]`)
- redis
- stripe (stripemock)

Configuring custom images
Configuring custom container
-------------------------

You can also configure custom images to use::
You can also configure custom container to use::

from pytest_docker_fixtures import images
images.configure(
'elasticsearch',
'docker.elastic.co/elasticsearch/elasticsearch-platinum', '6.2.4',
env={},
options={}
Example: Bind mosquitto to fixed port

from pytest_docker_fixtures.containers.mosquitto import mosquitto_container

mosquitto_container.config.options.update(
{
"ports": {"1883/tcp": 1883},
}
)
143 changes: 76 additions & 67 deletions pytest_docker_fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,139 +1,142 @@
from .containers.cockroach import cockroach_image
from .containers.es import es_image
from .containers.etcd import etcd_image
from .containers.pg import pg_image
from .containers.redis import redis_image
from .containers.rabbitmq import rabbitmq_image
from .containers.kafka import kafka_image
from .containers.minio import minio_image
from .containers.mysql import mysql_image
from .containers.memcached import memcached_image
from .containers.stripe import stripe_image
from .containers.cockroach import cockroach_container
from .containers.es import es_container
from .containers.etcd import etcd_container
from .containers.kafka import kafka_container
from .containers.memcached import memcached_container
from .containers.minio import minio_container
from .containers.mosquitto import mosquitto_container
from .containers.mysql import mysql_container
from .containers.pg import pg_container
from .containers.rabbitmq import rabbitmq_container
from .containers.redis import redis_container
from .containers.stripe import stripe_container
from pytest_docker_fixtures.containers.base import HostPort
from typing import Generator

import os
import pytest


IS_TRAVIS = 'TRAVIS' in os.environ
IS_TRAVIS = "TRAVIS" in os.environ


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def redis():
"""
detect travis, use travis's postgres; otherwise, use docker
"""
if os.environ.get('REDIS'):
yield os.environ['REDIS'].split(':')
if os.environ.get("REDIS"):
yield os.environ["REDIS"].split(":")
else:
if IS_TRAVIS:
host = 'localhost'
host = "localhost"
port = 6379
else:
host, port = redis_image.run()
host, port = redis_container.run()

yield host, port # provide the fixture value

if not IS_TRAVIS:
redis_image.stop()
redis_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def cockroach():
if os.environ.get('COCKROACH'):
yield os.environ['COCKROACH'].split(':')
if os.environ.get("COCKROACH"):
yield os.environ["COCKROACH"].split(":")
else:
yield cockroach_image.run()
cockroach_image.stop()
yield cockroach_container.run()
cockroach_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def pg():
if os.environ.get('POSTGRESQL'):
yield os.environ['POSTGRESQL'].split(':')
if os.environ.get("POSTGRESQL"):
yield os.environ["POSTGRESQL"].split(":")
else:
if IS_TRAVIS:
host = 'localhost'
host = "localhost"
port = 6379
else:
host, port = pg_image.run()
host, port = pg_container.run()

yield host, port # provide the fixture value

if not IS_TRAVIS:
pg_image.stop()
pg_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def etcd():
if os.environ.get('ETCD'):
yield os.environ['ETCD'].split(':')
if os.environ.get("ETCD"):
yield os.environ["ETCD"].split(":")
else:
yield etcd_image.run()
etcd_image.stop()
yield etcd_container.run()
etcd_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def es():
if os.environ.get('ELASTICSEARCH'):
yield os.environ['ELASTICSEARCH'].split(':')
if os.environ.get("ELASTICSEARCH"):
yield os.environ["ELASTICSEARCH"].split(":")
else:
yield es_image.run()
es_image.stop()
yield es_container.run()
es_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def rabbitmq():
if os.environ.get('RABBITMQ'):
yield os.environ['RABBITMQ'].split(':')
if os.environ.get("RABBITMQ"):
yield os.environ["RABBITMQ"].split(":")
else:
yield rabbitmq_image.run()
rabbitmq_image.stop()
yield rabbitmq_container.run()
rabbitmq_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def kafka():
if os.environ.get('KAFKA'):
yield os.environ['KAFKA'].split(':')
if os.environ.get("KAFKA"):
yield os.environ["KAFKA"].split(":")
else:
yield kafka_image.run()
kafka_image.stop()
yield kafka_container.run()
kafka_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def minio():
if os.environ.get('MINIO'):
yield os.environ['MINIO'].split(':')
if os.environ.get("MINIO"):
yield os.environ["MINIO"].split(":")
else:
if IS_TRAVIS:
host = 'localhost'
host = "localhost"
port = 19000
else:
host, port = minio_image.run()
host, port = minio_container.run()

yield host, port

if not IS_TRAVIS:
minio_image.stop()
minio_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def mysql():
if os.environ.get('MYSQL'):
yield os.environ['MYSQL'].split(':')
if os.environ.get("MYSQL"):
yield os.environ["MYSQL"].split(":")
else:
yield mysql_image.run()
mysql_image.stop()
yield mysql_container.run()
mysql_container.stop()


@pytest.fixture(scope='session')
@pytest.fixture(scope="session")
def memcached():
if os.environ.get('MEMCACHED'):
host, port = os.environ['MEMCACHED'].split(':')
if os.environ.get("MEMCACHED"):
host, port = os.environ["MEMCACHED"].split(":")
yield host, port
else:
host, port = memcached_image.run()
host, port = memcached_container.run()
yield host, port
memcached_image.stop()
memcached_container.stop()


@pytest.fixture(scope="session")
Expand All @@ -142,6 +145,12 @@ def stripe():
host, port = os.environ["STRIPE"].split(":")
yield host, port
else:
host, port = stripe_image.run()
host, port = stripe_container.run()
yield host, port
stripe_image.stop()
stripe_container.stop()


@pytest.fixture(scope="session")
def mosquitto() -> Generator[HostPort, None, None]:
yield mosquitto_container.run()
mosquitto_container.stop()
Loading