Skip to content

Commit c24f9a7

Browse files
committed
Adjust tests for docker in docker.
1 parent cd117bb commit c24f9a7

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

testcontainers/compose.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def __init__(self, filepath, compose_file_name="docker-compose.yml"):
1313
self.compose_file_name = compose_file_name
1414

1515
def __enter__(self):
16-
return self.start()
16+
self.start()
17+
return self
1718

1819
def __exit__(self, exc_type, exc_val, exc_tb):
1920
self.stop()

tests/test_docker_compose.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
import os
2-
31
import pytest
42

53
from testcontainers.compose import DockerCompose
64
from testcontainers.core.exceptions import NoSuchPortExposed
5+
from testcontainers.core.utils import inside_container
76

87

98
def test_can_spawn_service_via_compose():
10-
compose = DockerCompose(os.path.dirname(__file__))
11-
12-
try:
13-
compose.start()
9+
with DockerCompose("tests") as compose:
1410
host = compose.get_service_host("hub", 4444)
1511
port = compose.get_service_port("hub", 4444)
1612
assert host == "0.0.0.0"
1713
assert port == "4444"
18-
finally:
19-
compose.stop()
2014

2115

2216
def test_can_throw_exception_if_no_port_exposed():
23-
compose = DockerCompose(os.path.dirname(__file__))
24-
25-
compose.start()
26-
with pytest.raises(NoSuchPortExposed):
27-
compose.get_service_host("hub", 5555)
28-
29-
compose.stop()
17+
with DockerCompose("tests") as compose:
18+
with pytest.raises(NoSuchPortExposed):
19+
compose.get_service_host("hub", 5555)
3020

3121

3222
def test_compose_wait_for_container_ready():
33-
compose = DockerCompose(os.path.dirname(__file__))
34-
with compose:
35-
compose.wait_for("http://localhost:4444/wd/hub")
23+
with DockerCompose("tests") as compose:
24+
host = "host.docker.internal" if inside_container() else "localhost"
25+
compose.wait_for("http://%s:4444/wd/hub" % host)

tests/test_new_docker_api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
2+
import re
23
from pathlib import Path
34

45
from testcontainers import mysql
56

67
from testcontainers.core.generic import GenericContainer
7-
8+
from testcontainers.core.utils import inside_container
89
from importlib import reload
910

1011

@@ -30,7 +31,10 @@ def test_docker_env_variables():
3031
db.with_bind_ports(3306, 32785)
3132
with db:
3233
url = db.get_connection_url()
33-
assert url == 'mysql+pymysql://demo:test@localhost:32785/custom_db'
34+
if inside_container():
35+
assert re.match(r'mysql\+pymysql://demo:test@(\d+\.){3}\d+:3306/custom_db', url)
36+
else:
37+
assert url == 'mysql+pymysql://demo:test@localhost:32785/custom_db'
3438

3539

3640
def test_docker_kargs():

0 commit comments

Comments
 (0)