8000 Fix/issue 144 by singerjess · Pull Request #1 · singerjess/testcontainers-python · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
6 changes: 6 additions & 0 deletions testcontainers/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ def get_wrapped_container(self) -> Container:
def get_docker_client(self) -> DockerClient:
return self._docker

def get_logs(self):
if not self._container:
raise ContainerStartException("Container should be started before")
return self._container.logs(stderr=False), self._container.logs(stdout=False)

Choose a reason for hiding this comment

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

What happens if container variable is not initialized? (see line 134 from exec function)


def exec(self, command):
if not self._container:
raise ContainerStartException("Container should be started before")
return self.get_wrapped_container().exec_run(command)

4 changes: 3 additions & 1 deletion testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def wait_for_logs(container, predicate, timeout=None, interval=1):
start = time.time()
while True:
duration = time.time() - start
if predicate(container._container.logs().decode()):
stdout = container.get_logs()[0].decode()
stderr = container.get_logs()[1].decode()
if predicate(stdout) or predicate(stderr):
return duration
if timeout and duration > timeout:
raise TimeoutError("container did not emit logs satisfying predicate in %.3f seconds"
Expand Down
2 changes: 2 additions & 0 deletions tests/docker-compose-4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello-world:
image: "hello-world"
7 changes: 7 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ def test_raise_timeout():
def test_wait_for_hello():
with DockerContainer("hello-world") as container:
wait_for_logs(container, "Hello from Docker!")


def test_can_get_logs():
with DockerContainer("hello-world") as container:
wait_for_logs(container, "Hello from Docker!")
stdout, stderr = container.get_logs()
assert stdout, 'There should be something on stdout'
2 changes: 1 addition & 1 deletion tests/test_db_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_docker_run_mysql():
assert row[0] == '5.7.17'


def test_docker_run_postgress():
def test_docker_run_postgres():
postgres_container = PostgresContainer("postgres:9.5")
with postgres_container as postgres:
e = sqlalchemy.create_engine(postgres.get_connection_url())
Expand Down
6 changes: 6 additions & 0 deletions tests/test_docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from testcontainers.compose import DockerCompose
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.waiting_utils import wait_for_logs


def test_can_spawn_service_via_compose():
Expand Down Expand Up @@ -34,6 +35,11 @@ def test_compose_wait_for_container_ready():
compose.wait_for("http://%s:4444/wd/hub" % docker.host())


def test_compose_can_wait_for_logs():
with DockerCompose(filepath="tests", compose_file_name="docker-compose-4.yml") as compose:
wait_for_logs(compose, "Hello from Docker!")


def test_can_parse_multiple_compose_files():
with DockerCompose(filepath="tests",
compose_file_name=["docker-compose.yml", "docker-compose-2.yml"]) as compose:
Expand Down
0