Skip to content

Commit f3b6526

Browse files
committed
Changed how containers are supplied by fixture as discussed in pytest-docker-compose#8
1 parent 1b210ea commit f3b6526

File tree

5 files changed

+19
-27
lines changed

5 files changed

+19
-27
lines changed

README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
pytest-docker-compose
22
=====================
3+
.. image:: https://circleci.com/gh/pytest-docker-compose/pytest-docker-compose/tree/master.svg?style=svg
4+
:target: https://circleci.com/gh/pytest-docker-compose/pytest-docker-compose/tree/master
5+
36
This package contains a `pytest`_ plugin for integrating Docker Compose into your automated integration tests.
47

58
Given a path to a ``docker-compose.yml`` file, it will automatically build the project at the start of the test run, bring the containers up before each test starts, and tear them down after each test ends.
@@ -38,10 +41,7 @@ See `Installing and Using Plugins`_ for more information.
3841
To interact with Docker containers in your tests, use the following fixtures:
3942

4043
``function_scoped_containers``
41-
A dictionary of the Docker ``compose.container.Container`` objects
42-
running during the test. These containers each have an extra attribute
43-
called ``network_info`` added to them. This attribute has a list of
44-
``pytest_docker_compose.NetworkInfo`` objects.
44+
An object that fetches containers of the Docker ``compose.container.Container`` objects running during the test. The containers are fetched using ``function_scoped_containers.get('service_name')`` These containers each have an extra attribute called ``network_info`` added to them. This attribute has a list of ``pytest_docker_compose.NetworkInfo`` objects.
4545

4646
This information can be used to configure API clients and other objects that
4747
will connect to services exposed by the Docker containers in your tests.
@@ -103,7 +103,7 @@ Here's an example of a fixture called ``wait_for_api`` that waits for an HTTP se
103103
status_forcelist=[500, 502, 503, 504])
104104
request_session.mount('http://', HTTPAdapter(max_retries=retries))
105105
106-
service = function_scoped_containers["my_network_my_api_service_1"].network_info[0]
106+
service = function_scoped_containers.get("my_api_service").network_info[0]
107107
api_url = "http://%s:%s/" % (service.hostname, service.host_port)
108108
assert request_session.get(api_url)
109109
return request_session, api_url

src/pytest_docker_compose/__init__.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,13 @@ def scoped_containers_fixture(docker_project: Project, request):
160160
containers = docker_project.up() # type: List[Container]
161161
if not containers:
162162
raise ValueError("`docker-compose` didn't launch any containers!")
163+
else:
164+
containers = docker_project.containers()
163165

164-
container_dict = ContainerDict(docker_project)
165-
yield container_dict
166+
container_fetcher = ContainerFetcher(docker_project)
167+
yield container_fetcher
166168

167-
for container in sorted(container_dict.values(), key=lambda c: c.name):
169+
for container in sorted(containers, key=lambda c: c.name):
168170
header = "Logs from {name}:".format(name=container.name)
169171
print(header, '\n', "=" * len(header))
170172
print(container.logs(since=now).decode("utf-8", errors="replace")
@@ -182,28 +184,18 @@ def scoped_containers_fixture(docker_project: Project, request):
182184
return scoped_containers_fixture
183185

184186

185-
class ContainerDict(dict):
187+
class ContainerFetcher:
186188
"""
187-
A read-only dictionary that re-fetches containers from a docker-compose
188-
project dynamically every time they are accessed.
189+
A class that retrieves containers from the docker project and adds a
190+
convenience wrapper for the available ports
189191
"""
190192
def __init__(self, docker_project: Project) -> None:
191-
super().__init__()
192193
self.docker_project = docker_project
193-
for container in docker_project.containers():
194-
super().__setitem__(container.name, container)
195194

196-
def __getitem__(self, key: str) -> Container:
197-
container = [container for container in self.docker_project.containers()
198-
if container.name == key][0]
195+
def get(self, key: str) -> Container:
196+
container = self.docker_project.containers(service_names=[key])[0]
199197
container.network_info = create_network_info_for_container(container)
200198
return container
201199

202-
def __setitem__(self, k, v) -> None:
203-
raise TypeError('ContainerDict does not allow item assignment')
204-
205-
def __delitem__(self, v) -> None:
206-
raise TypeError('ContainerDict does not allow item deletion')
207-
208200

209201
plugin = DockerComposePlugin()

tests/pytest_docker_compose_tests/test_function_scoping_fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def wait_for_api(function_scoped_containers):
1616
status_forcelist=[500, 502, 503, 504])
1717
request_session.mount('http://', HTTPAdapter(max_retries=retries))
1818

19-
service = function_scoped_containers["my_network_my_api_service_1"].network_info[0]
19+
service = function_scoped_containers.get("my_api_service").network_info[0]
2020
api_url = "http://%s:%s/" % (service.hostname, service.host_port)
2121
assert request_session.get(api_url)
2222
return request_session, api_url

tests/pytest_docker_compose_tests/test_module_scoping_fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def wait_for_api(module_scoped_containers):
1616
status_forcelist=[500, 502, 503, 504])
1717
request_session.mount('http://', HTTPAdapter(max_retries=retries))
1818

19-
service = module_scoped_containers["my_network_my_api_service_1"].network_info[0]
19+
service = module_scoped_containers.get("my_api_service").network_info[0]
2020
api_url = "http://%s:%s/" % (service.hostname, service.host_port)
2121
assert request_session.get(api_url)
2222
return request_session, api_url

tests/pytest_docker_compose_tests/test_wrong_scoping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
@pytest.mark.should_fail
66
def test_read_all_module(module_scoped_containers):
7-
assert module_scoped_containers["my_network_my_api_service_1"].network_info[0]
7+
assert module_scoped_containers.get("my_api_service").network_info[0]
88

99

1010
@pytest.mark.should_fail
1111
def test_read_all_function(function_scoped_containers):
12-
assert function_scoped_containers["my_network_my_api_service_1"].network_info[0]
12+
assert function_scoped_containers.get("my_api_service").network_info[0]

0 commit comments

Comments
 (0)