Skip to content

Commit 0c31d54

Browse files
committed
Use docker networks for integration tests
This is possible now we don't have file upload server. Closes replicate#262 Closes replicate#263 Signed-off-by: Ben Firshman <[email protected]>
1 parent b3d34e7 commit 0c31d54

File tree

3 files changed

+51
-69
lines changed

3 files changed

+51
-69
lines changed
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
import subprocess
22
import pytest
3+
import redis
34

4-
from .util import random_string, find_free_port, docker_run, get_local_ip, wait_for_port
5+
from .util import random_string, find_free_port, docker_run, wait_for_port
56

67

78
@pytest.fixture
8-
def redis_port():
9-
container_name = "cog-test-redis-" + random_string(10)
9+
def redis_port(docker_network):
10+
"""Start a redis server inside the Docker network.
11+
Inside the network, it is available at redis:6379
12+
Outside the network, it is available at localhost:redis_port
13+
"""
1014
port = find_free_port()
1115
with docker_run(
1216
"redis",
13-
name=container_name,
17+
net_alias="redis",
18+
network=docker_network,
1419
publish=[{"host": port, "container": 6379}],
1520
detach=True,
1621
):
17-
wait_for_port(get_local_ip(), port)
22+
wait_for_port("localhost", port)
1823
yield port
1924

2025

26+
@pytest.fixture
27+
def redis_client(redis_port):
28+
yield redis.Redis("localhost", redis_port)
29+
30+
2131
@pytest.fixture
2232
def docker_image():
2333
image = "cog-test-" + random_string(10)
2434
yield image
2535
subprocess.run(["docker", "rmi", "-f", image], check=False)
36+
37+
38+
@pytest.fixture
39+
def docker_network():
40+
name = "cog-test-" + random_string(10)
41+
subprocess.run(["docker", "network", "create", name])
42+
yield name
43+
subprocess.run(["docker", "network", "rm", name])

test-integration/test_integration/test_redis_queue.py

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
import json
22
from pathlib import Path
33

4-
import redis
54
import subprocess
65

7-
from .util import (
8-
docker_run,
9-
get_local_ip,
10-
random_string,
11-
)
6+
from .util import docker_run, random_string
127

138

14-
def test_queue_worker_yielding(docker_image, redis_port):
9+
def test_queue_worker_yielding(docker_image, docker_network, redis_client):
1510
project_dir = Path(__file__).parent / "fixtures/yielding-project"
1611
subprocess.run(["cog", "build", "-t", docker_image], check=True, cwd=project_dir)
1712

18-
local_ip = get_local_ip()
19-
redis_host = local_ip
20-
21-
redis_client = redis.Redis(host=redis_host, port=redis_port, db=0)
22-
2313
with docker_run(
2414
image=docker_image,
2515
interactive=True,
16+
network=docker_network,
2617
command=[
2718
"python",
2819
"-m",
2920
"cog.server.redis_queue",
30-
redis_host,
31-
str(redis_port),
21+
"redis",
22+
"6379",
3223
"predict-queue",
3324
"",
3425
"test-worker",
@@ -69,24 +60,20 @@ def test_queue_worker_yielding(docker_image, redis_port):
6960
assert response == None
7061

7162

72-
def test_queue_worker_error(docker_image, redis_port, request):
63+
def test_queue_worker_error(docker_image, docker_network, redis_client):
7364
project_dir = Path(__file__).parent / "fixtures/failing-project"
7465
subprocess.run(["cog", "build", "-t", docker_image], check=True, cwd=project_dir)
7566

76-
local_ip = get_local_ip()
77-
redis_host = local_ip
78-
79-
redis_client = redis.Redis(host=redis_host, port=redis_port, db=0)
80-
8167
with docker_run(
8268
image=docker_image,
8369
interactive=True,
70+
network=docker_network,
8471
command=[
8572
"python",
8673
"-m",
8774
"cog.server.redis_queue",
88-
redis_host,
89-
str(redis_port),
75+
"redis",
76+
"6379",
9077
"predict-queue",
9178
"",
9279
"test-worker",
@@ -121,24 +108,20 @@ def test_queue_worker_error(docker_image, redis_port, request):
121108
assert response == None
122109

123110

124-
def test_queue_worker_logging(docker_image, redis_port, request):
111+
def test_queue_worker_logging(docker_image, docker_network, redis_client):
125112
project_dir = Path(__file__).parent / "fixtures/logging-project"
126113
subprocess.run(["cog", "build", "-t", docker_image], check=True, cwd=project_dir)
127114

128-
local_ip = get_local_ip()
129-
redis_host = local_ip
130-
131-
redis_client = redis.Redis(host=redis_host, port=redis_port, db=0)
132-
133115
with docker_run(
134116
image=docker_image,
135117
interactive=True,
118+
network=docker_network,
136119
command=[
137120
"python",
138121
"-m",
139122
"cog.server.redis_queue",
140-
redis_host,
141-
str(redis_port),
123+
"redis",
124+
"6379",
142125
"predict-queue",
143126
"",
144127
"test-worker",
@@ -189,24 +172,20 @@ def test_queue_worker_logging(docker_image, redis_port, request):
189172
]
190173

191174

192-
def test_queue_worker_timeout(docker_image, redis_port, request):
175+
def test_queue_worker_timeout(docker_image, docker_network, redis_client):
193176
project_dir = Path(__file__).parent / "fixtures/timeout-project"
194177
subprocess.run(["cog", "build", "-t", docker_image], check=True, cwd=project_dir)
195178

196-
local_ip = get_local_ip()
197-
redis_host = local_ip
198-
199-
redis_client = redis.Redis(host=redis_host, port=redis_port, db=0)
200-
201179
with docker_run(
202180
image=docker_image,
203181
interactive=True,
182+
network=docker_network,
204183
command=[
205184
"python",
206185
"-m",
207186
"cog.server.redis_queue",
208-
redis_host,
209-
str(redis_port),
187+
"redis",
188+
"6379",
210189
"predict-queue",
211190
"",
212191
"test-worker",
@@ -258,24 +237,20 @@ def test_queue_worker_timeout(docker_image, redis_port, request):
258237
assert response == {"status": "failed", "error": "Prediction timed out"}
259238

260239

261-
def test_queue_worker_yielding_timeout(docker_image, redis_port, request):
240+
def test_queue_worker_yielding_timeout(docker_image, docker_network, redis_client):
262241
project_dir = Path(__file__).parent / "fixtures/yielding-timeout-project"
263242
subprocess.run(["cog", "build", "-t", docker_image], check=True, cwd=project_dir)
264243

265-
local_ip = get_local_ip()
266-
redis_host = local_ip
267-
268-
redis_client = redis.Redis(host=redis_host, port=redis_port, db=0)
269-
270244
with docker_run(
271245
image=docker_image,
272246
interactive=True,
247+
network=docker_network,
273248
command=[
274249
"python",
275250
"-m",
276251
"cog.server.redis_queue",
277-
redis_host,
278-
str(redis_port),
252+
"redis",
253+
"6379",
279254
"predict-queue",
280255
"",
281256
"test-worker",

test-integration/test_integration/util.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def docker_run(
3838
name=None,
3939
detach=False,
4040
interactive=False,
41+
network=None,
42+
net_alias=None,
4143
publish: Optional[List[dict]] = None,
4244
command: Optional[List[str]] = None,
4345
env: Optional[dict] = None,
@@ -46,6 +48,7 @@ def docker_run(
4648
name = random_string(10)
4749

4850
cmd = ["docker", "run", "--name", name]
51+
4952
if publish is not None:
5053
for port_binding in publish:
5154
host_port = port_binding["host"]
@@ -58,6 +61,10 @@ def docker_run(
5861
cmd += ["--detach"]
5962
if interactive:
6063
cmd += ["-i"]
64+
if network is not None:
65+
cmd.extend(["--network", network])
66+
if net_alias is not None:
67+
cmd.extend(["--net-alias", net_alias])
6168
cmd += [image]
6269
if command:
6370
cmd += command
@@ -66,21 +73,3 @@ def docker_run(
6673
yield
6774
finally:
6875
subprocess.Popen(["docker", "rm", "--force", name]).wait()
69-
70-
71-
def get_local_ip():
72-
return socket.gethostbyname(socket.gethostname())
73-
74-
75-
def get_bridge_ip():
76-
"""Return the IP address of the docker bridge network"""
77-
cmd = [
78-
"docker",
79-
"network",
80-
"inspect",
81-
"bridge",
82-
"--format='{{ json (index .IPAM.Config 0).Gateway }}'",
83-
]
84-
out, _ = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
85-
86-
return out.decode().strip().replace('"', "").replace("'", "")

0 commit comments

Comments
 (0)