Skip to content
Closed
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
18 changes: 12 additions & 6 deletions testcontainers/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import blindspin
import requests

from testcontainers.core.waiting_utils import wait_container_is_ready
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.waiting_utils import wait_container_is_ready


class DockerCompose(object):
def __init__(
self,
filepath,
compose_file_name="docker-compose.yml",
pull=False):
pull=False,
build=False):
self.filepath = filepath
self.compose_file_name = compose_file_name
self.pull = pull
self.build = build

def __enter__(self):
self.start()
Expand All @@ -26,11 +28,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):

def start(self):
with blindspin.spinner():
cmd = ["docker-compose", "-f", self.compose_file_name]
if self.pull:
subprocess.call(["docker-compose", "-f", self.compose_file_name, "pull"],
cwd=self.filepath)
subprocess.call(["docker-compose", "-f", self.compose_file_name, "up", "-d"],
cwd=self.filepath)
subprocess.call(cmd + ["pull"], cwd=self.filepath)

cmd += ["up", "-d"]
if self.build:
cmd += ["--build"]

subprocess.call(cmd, cwd=self.filepath)

def stop(self):
with blindspin.spinner():
Expand Down
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from os.path import realpath, dirname

TESTS_DIR = realpath(dirname(__file__))
17 changes: 13 additions & 4 deletions tests/test_docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,40 @@
from testcontainers.compose import DockerCompose
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.utils import inside_container
from tests import TESTS_DIR


def test_can_spawn_service_via_compose():
with DockerCompose("tests") as compose:
with DockerCompose(TESTS_DIR) as compose:
host = compose.get_service_host("hub", 4444)
port = compose.get_service_port("hub", 4444)
assert host == "0.0.0.0"
assert port == "4444"


def test_can_pull_images_before_spawning_service_via_compose():
with DockerCompose("tests", pull=True) as compose:
with DockerCompose(TESTS_DIR, pull=True) as compose:
host = compose.get_service_host("hub", 4444)
port = compose.get_service_port("hub", 4444)
assert host == "0.0.0.0"
assert port == "4444"


def test_can_build_images_before_spawning_service_via_compose():
with DockerCompose(TESTS_DIR, build=True) as compose:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does docker-compose do when --build is passed but the docker-compose.yml file doesn't contain any reference to a locally-built image?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question!

I just tried it with the tests/docker-compose.yml and it had no effect; it worked exactly as it would without it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tillahoffmann ☝️ is this good enough?

host = compose.get_service_host("hub", 4444)
port = compose.get_service_port("hub", 4444)
assert host == "0.0.0.0"
assert port == "4444"


def test_can_throw_exception_if_no_port_exposed():
with DockerCompose("tests") as compose:
with DockerCompose(TESTS_DIR) as compose:
with pytest.raises(NoSuchPortExposed):
compose.get_service_host("hub", 5555)


def test_compose_wait_for_container_ready():
with DockerCompose("tests") as compose:
with DockerCompose(TESTS_DIR) as compose:
host = "host.docker.internal" if inside_container() else "localhost"
compose.wait_for("http://%s:4444/wd/hub" % host)