Skip to content

Commit

Permalink
add support for Stopping a Docker Container (#25)
Browse files Browse the repository at this point in the history
* add support for Stopping a Docker Container

* Update CHANGES.rst

* move update on top
  • Loading branch information
chicco785 committed Aug 18, 2021
1 parent df8567b commit 3955020
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changes for Lovely Pytest Docker
unreleased
==========

- add support for `docker-compose stop`
- allow to set the timeout and retry interval when waiting for services
- added explicit dependency to six, which is no more required in newer pytest versions

Expand Down
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ the ``exec`` method of the ``docker_services`` fixture::
res = docker_services.execute('crate', 'ls', '-a')


Stopping a Docker Container
=============================

It's possible to stop single Docker containers. Use
the ``stop`` method of the ``docker_services`` fixture::

def test_stop(docker_services):
# the first argument is the service name of the compose file,
# the following arguments build the command to run
res = docker_services.stop('crate')


Wait for Service
================

Expand Down
9 changes: 9 additions & 0 deletions src/lovely/pytest/docker/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def start(self, *services):
"""
self._docker_compose.execute('up', '--build', '-d', *services)


def stop(self, *services):
"""Ensures that the given services are stopped via docker compose.
:param services: the names of the services as defined in compose file
"""
self._docker_compose.execute('stop', *services)


def execute(self, service, *cmd):
"""Execute a command inside a docker container.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_hello.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from six.moves.urllib.request import urlopen
import time


def test_hello_world(docker_hello_world):
Expand All @@ -21,6 +22,22 @@ def test_single_container(docker_hello_world, docker_services):
assert 'hello2' not in res


def test_stop_single_container(docker_hello_world, docker_hello_world2, docker_services):
"""Test if only the requested containers are stope.
The container for hello2 is started by the fixture and stopped via function.
"""
res = docker_services._docker_compose.execute("ps")
assert 'hello' in res
assert 'hello2' in res
docker_services.stop("hello2")
res = docker_services._docker_compose.execute('ps', '--services', '--filter', 'status=running')
assert 'hello2' not in res
docker_services.start("hello2")
res = docker_services._docker_compose.execute('ps', '--services', '--filter', 'status=running')
assert 'hello2' in res


def test_execute(docker_services):
"""Test the exec method.
Expand Down

0 comments on commit 3955020

Please sign in to comment.