Skip to content

Commit a108150

Browse files
committed
Added an if statement to catch when there are no containers
1 parent c0011a0 commit a108150

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/pytest_docker_compose/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ def __init__(self, docker_project: Project) -> None:
205205
self.docker_project = docker_project
206206

207207
def get(self, key: str) -> Container:
208-
container = self.docker_project.containers(service_names=[key])[0]
208+
containers = self.docker_project.containers(service_names=[key])
209+
if not containers:
210+
warnings.warn(UserWarning(
211+
"The service %s has no running containers containers" % key
212+
))
213+
return []
214+
container = containers[0]
209215
container.network_info = create_network_info_for_container(container)
210216
return container

tests/pytest_docker_compose_tests/my_network/docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
version: '3'
22

33
services:
4+
my_service_without_containers:
5+
build: ./a_buildable_container
6+
entrypoint: /bin/echo
7+
command: "Echoing"
8+
49
my_api_service:
510
build: ./a_buildable_container
611
ports:

tests/pytest_docker_compose_tests/test_module_scoping_fixtures.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def wait_for_api(module_scoped_container_getter):
1919
service = module_scoped_container_getter.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)
22+
# The container that only echoes should have run and disappeared by now:
23+
assert not module_scoped_container_getter.get("my_service_without_containers")
2224
return request_session, api_url
2325

2426

0 commit comments

Comments
 (0)