Skip to content

Commit ccf0b6b

Browse files
committed
Run redis integration tests in Docker network
The queue_controller server has been replaced by a separate container that can run inside the network. Closes replicate#262 Closes replicate#263 Signed-off-by: Ben Firshman <[email protected]>
1 parent 4b4bcf4 commit ccf0b6b

File tree

7 files changed

+207
-255
lines changed

7 files changed

+207
-255
lines changed
Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,76 @@
1+
from pathlib import Path
12
import subprocess
23
import pytest
34

4-
from .util import random_string, find_free_port, docker_run, get_local_ip, wait_for_port
5+
import redis
6+
7+
from .util import random_string, find_free_port, docker_run, wait_for_port
58

69

710
@pytest.fixture
8-
def redis_port():
9-
container_name = "cog-test-redis-" + random_string(10)
11+
def docker_network():
12+
name = "cog-test-" + random_string(10)
13+
subprocess.run(["docker", "network", "create", name])
14+
yield name
15+
subprocess.run(["docker", "network", "rm", name])
16+
17+
18+
@pytest.fixture
19+
def redis_port(docker_network):
20+
"""Start a redis server inside the Docker network.
21+
Inside the network, it is available at redis:6379
22+
Outside the network, it is available at localhost:redis_port
23+
"""
1024
port = find_free_port()
1125
with docker_run(
1226
"redis",
13-
name=container_name,
27+
net_alias="redis",
28+
network=docker_network,
1429
publish=[{"host": port, "container": 6379}],
1530
detach=True,
1631
):
17-
wait_for_port(get_local_ip(), port)
32+
wait_for_port("localhost", port)
1833
yield port
1934

2035

36+
@pytest.fixture
37+
def redis_client(redis_port):
38+
yield redis.Redis("localhost", redis_port)
39+
40+
2141
@pytest.fixture
2242
def docker_image():
2343
image = "cog-test-" + random_string(10)
2444
yield image
2545
subprocess.run(["docker", "rmi", "-f", image], check=False)
46+
47+
48+
@pytest.fixture
49+
def upload_server_image():
50+
"""
51+
Build the upload server once for the test run. The image doesn't change.
52+
"""
53+
subprocess.run(
54+
["docker", "build", "-t", "cog-test-upload-server", "."],
55+
cwd=Path(__file__).parent.parent / "upload_server",
56+
check=True,
57+
)
58+
return "cog-test-upload-server"
59+
60+
61+
@pytest.fixture
62+
def upload_server(docker_network, upload_server_image, tmpdir_factory):
63+
"""
64+
Run a server that can be used to upload and download files from.
65+
66+
It is accessible at http://upload-server:5000 inside the network. The thing returned is the path for uploads.
67+
"""
68+
tmpdir = tmpdir_factory.mktemp("uploads")
69+
with docker_run(
70+
upload_server_image,
71+
net_alias="upload-server",
72+
network=docker_network,
73+
volumes=["-v", f"{tmpdir}:/uploads"],
74+
detach=True,
75+
):
76+
yield tmpdir

test-integration/test_integration/fixtures/file-project/predict.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ def setup(self):
1212
def predict(self, text, path):
1313
with open(path) as f:
1414
output = self.foo + text + f.read()
15-
tmp = tempfile.NamedTemporaryFile(suffix=".txt")
16-
tmp.close()
17-
tmp_path = Path(tmp.name)
18-
with tmp_path.open("w") as f:
19-
f.write(output)
20-
return tmp_path
15+
tmpdir = Path(tempfile.mkdtemp())
16+
with open(tmpdir / "output.txt", "w") as fh:
17+
fh.write(output)
18+
return tmpdir / "output.txt"

0 commit comments

Comments
 (0)