Skip to content

Commit

Permalink
feat!: V2 as default (#94)
Browse files Browse the repository at this point in the history
* feat!: V2 as default

* Python 3.6 removed from the test matrix

* chore: Python 3.10 and 3.11 added to test matrix
  • Loading branch information
augi committed Jul 19, 2023
1 parent 519b155 commit 052e08b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
pytest-version: [4, 5, 6, 7]

steps:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 2.0.0
Breaking changes:
- Default command changed from `docker-compose` to `docker compose`, so the V2 is the default one.

## Version 1.0.1
Chore:
- Set dependency on `attrs` to be the same as in `pytest`
Expand Down
39 changes: 18 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Docker-based integration tests

# Description
Simple [pytest](http://doc.pytest.org/) fixtures that help you write integration
tests with Docker and [docker-compose](https://docs.docker.com/compose/).
tests with Docker and [Docker Compose](https://docs.docker.com/compose/).
Specify all necessary containers in a `docker-compose.yml` file and and
`pytest-docker` will spin them up for the duration of your tests.

Expand All @@ -18,30 +18,27 @@ This package is tested with Python versions `3.6`, `3.7`, `3.8` and
`pytest-docker` was originally created by André Caron.

# Installation
Install `pytest-docker` with `pip` or add it to your test requirements. It is
recommended to install `docker-compose` python package directly in your
environment to ensure that it is available during tests. This will prevent
potential dependency conflicts that can occur when the system wide
`docker-compose` is used in tests.
Install `pytest-docker` with `pip` or add it to your test requirements.

The default behavior is not to install `docker-compose` with `pytest-docker`. If you
want to, you install `pytest-docker` with the `docker-compose-v1`
extra. You can use the following command:
By default, it uses the `docker compose` command, so it relies on the Compose plugin for Docker (also called Docker Compose V2).

## Docker Compose V1 compatibility

If you want to use the old `docker-compose` command (deprecated since July 2023, not receiving updates since 2021)
then you can do it using the [`docker-compose-command`](#docker_compose_command) fixture:

```python
@pytest.fixture(scope="session")
def docker_compose_command() -> str:
return "docker-compose"
```

If you want to use the pip-distributed version of `docker-compose` command, you can install it using
```
pip install pytest-docker[docker-compose-v1]
```

## Docker Compose v2 compatiblity

`pytest-docker` will work with Docker Compose v2 out of the box if
[`compose-switch`](https://github.com/docker/compose-switch)
is installed.

If you want to use the real Docker Compose v2, it has to be installed
system wide ([more information](https://github.com/docker/compose#linux))
and you have to modify the [`docker-compose-command`](#docker_compose_command)
fixture (this behavior might change in the future versions).
Another option could be usage of [`compose-switch`](https://github.com/docker/compose-switch).

# Usage
Here is an example of a test that depends on a HTTP service.
Expand Down Expand Up @@ -135,8 +132,8 @@ After test are finished, shutdown all services (`docker-compose down`).
### `docker_compose_command`

Docker Compose command to use to execute Dockers. Default is to use
Docker Compose v1 (command is `docker-compose`). If you want to use
Docker Compose v2, change this fixture to return `docker compose`.
Docker Compose V2 (command is `docker compose`). If you want to use
Docker Compose V1, change this fixture to return `docker-compose`.

### `docker_setup`

Expand Down
7 changes: 4 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[metadata]
name = pytest-docker
version = 1.0.1
description = Simple pytest fixtures for Docker and docker-compose based tests
version = 2.0.0
description = Simple pytest fixtures for Docker and Docker Compose based tests
long_description = file: README.md
long_description_content_type = text/markdown
keywords = docker,docker-compose,pytest
Expand All @@ -13,10 +13,11 @@ author_email = maxim.kovykov@avast.com
license = MIT
classifiers =
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
License :: OSI Approved :: MIT License
Topic :: Utilities
Intended Audience :: Developers
Expand Down
8 changes: 4 additions & 4 deletions src/pytest_docker/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ def execute(self, subcommand):

@pytest.fixture(scope="session")
def docker_compose_command():
"""Docker Compose command to use, it could be either `docker-compose`
for Docker Compose v1 or `docker compose` for Docker Compose
v2."""
"""Docker Compose command to use, it could be either `docker compose`
for Docker Compose V2 or `docker-compose` for Docker Compose
V1."""

return "docker-compose"
return "docker compose"


@pytest.fixture(scope="session")
Expand Down
22 changes: 11 additions & 11 deletions tests/test_docker_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_docker_services():

# The fixture is a context-manager.
with get_docker_services(
"docker-compose",
"docker compose",
"docker-compose.yml",
docker_compose_project_name="pytest123",
docker_setup=get_setup_command(),
Expand All @@ -49,17 +49,17 @@ def test_docker_services():
# Both should have been called.
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" up --build -d',
'docker compose -f "docker-compose.yml" -p "pytest123" up --build -d',
stderr=subprocess.STDOUT,
shell=True,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" port abc 123',
'docker compose -f "docker-compose.yml" -p "pytest123" port abc 123',
stderr=subprocess.STDOUT,
shell=True,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" down -v',
'docker compose -f "docker-compose.yml" -p "pytest123" down -v',
stderr=subprocess.STDOUT,
shell=True,
),
Expand All @@ -77,7 +77,7 @@ def test_docker_services_unused_port():

# The fixture is a context-manager.
with get_docker_services(
"docker-compose",
"docker compose",
"docker-compose.yml",
docker_compose_project_name="pytest123",
docker_setup=get_setup_command(),
Expand All @@ -101,17 +101,17 @@ def test_docker_services_unused_port():
# Both should have been called.
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" ' "up --build -d",
'docker compose -f "docker-compose.yml" -p "pytest123" ' "up --build -d",
shell=True,
stderr=subprocess.STDOUT,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" ' "port abc 123",
'docker compose -f "docker-compose.yml" -p "pytest123" ' "port abc 123",
shell=True,
stderr=subprocess.STDOUT,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" down -v',
'docker compose -f "docker-compose.yml" -p "pytest123" down -v',
shell=True,
stderr=subprocess.STDOUT,
),
Expand All @@ -130,7 +130,7 @@ def test_docker_services_failure():
# The fixture is a context-manager.
with pytest.raises(Exception) as exc:
with get_docker_services(
"docker-compose",
"docker compose",
"docker-compose.yml",
docker_compose_project_name="pytest123",
docker_setup=get_setup_command(),
Expand All @@ -148,7 +148,7 @@ def test_docker_services_failure():
# Tear down code should not be called.
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" ' "up --build -d",
'docker compose -f "docker-compose.yml" -p "pytest123" ' "up --build -d",
shell=True,
stderr=subprocess.STDOUT,
)
Expand All @@ -161,7 +161,7 @@ def test_wait_until_responsive_timeout():

with mock.patch("time.sleep") as sleep:
docker_compose = DockerComposeExecutor(
compose_command="docker-compose",
compose_command="docker compose",
compose_files="docker-compose.yml",
compose_project_name="pytest123",
)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_dockercomposeexecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

def test_execute():
docker_compose = DockerComposeExecutor(
"docker-compose", "docker-compose.yml", "pytest123"
"docker compose", "docker-compose.yml", "pytest123"
)
with mock.patch("subprocess.check_output") as check_output:
docker_compose.execute("up")
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" up',
'docker compose -f "docker-compose.yml" -p "pytest123" up',
shell=True,
stderr=subprocess.STDOUT,
)
Expand All @@ -37,12 +37,12 @@ def test_execute_docker_compose_v2():

def test_pypath_compose_files():
compose_file = py.path.local("/tmp/docker-compose.yml")
docker_compose = DockerComposeExecutor("docker-compose", compose_file, "pytest123")
docker_compose = DockerComposeExecutor("docker compose", compose_file, "pytest123")
with mock.patch("subprocess.check_output") as check_output:
docker_compose.execute("up")
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "/tmp/docker-compose.yml"' ' -p "pytest123" up',
'docker compose -f "/tmp/docker-compose.yml"' ' -p "pytest123" up',
shell=True,
stderr=subprocess.STDOUT,
)
Expand All @@ -51,13 +51,13 @@ def test_pypath_compose_files():

def test_multiple_compose_files():
docker_compose = DockerComposeExecutor(
"docker-compose", ["docker-compose.yml", "other-compose.yml"], "pytest123"
"docker compose", ["docker-compose.yml", "other-compose.yml"], "pytest123"
)
with mock.patch("subprocess.check_output") as check_output:
docker_compose.execute("up")
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -f "other-compose.yml"'
'docker compose -f "docker-compose.yml" -f "other-compose.yml"'
' -p "pytest123" up',
shell=True,
stderr=subprocess.STDOUT,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_docker_setup(docker_setup):
assert docker_setup == "up --build -d"

def test_docker_compose_comand(docker_compose_command):
assert docker_compose_command == "docker-compose"
assert docker_compose_command == "docker compose"

0 comments on commit 052e08b

Please sign in to comment.