Skip to content

Commit a8369be

Browse files
committed
Use custom dind for talking to insecure registry
1 parent 69e67a2 commit a8369be

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

tests/registry/test_registry.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
import socket
13
from pathlib import Path
24
import subprocess
35
import pytest
@@ -9,17 +11,62 @@
911

1012
HERE = Path(__file__).parent
1113

12-
@pytest.fixture
13-
def registry():
14+
@pytest.fixture(scope="session")
15+
def dind(registry, host_ip):
16+
port = get_free_port()
17+
dind_image = "docker:dind"
18+
subprocess.check_call(["docker", "pull", dind_image])
19+
# This is insecure, because I'm disabling all kinds of authentication on the docker daemon.
20+
# FIXME: Use TLS verification.
21+
# but also docker this is your own fucking fault for making technical choices that force dockerhub
22+
# to be the primary registry, so your registry handling sucks and forces these kinds of difficulties.
23+
cmd = [
24+
"docker", "run", "-e", 'DOCKER_TLS_CERTDIR=',
25+
"--privileged", "-p", f"{port}:2376", dind_image,
26+
"--host", "0.0.0.0:2376",
27+
"--insecure-registry", registry,
28+
"--tls=false"
29+
]
30+
proc = subprocess.Popen(cmd)
31+
time.sleep(5)
32+
33+
try:
34+
yield f"tcp://{host_ip}:{port}"
35+
finally:
36+
proc.terminate()
37+
proc.wait()
38+
39+
@pytest.fixture(scope="session")
40+
def host_ip():
41+
# Get the IP of the current machine, as we need to use the same IP
42+
# for all our docker commands, *and* the dind we run needs to reach it
43+
# in the same way.
44+
# Thanks to https://stackoverflow.com/a/28950776
45+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
46+
s.settimeout(0)
47+
try:
48+
# doesn't even have to be reachable
49+
s.connect(('10.254.254.254', 1))
50+
host_ip = s.getsockname()[0]
51+
finally:
52+
s.close()
53+
54+
return host_ip
55+
56+
@pytest.fixture(scope="session")
57+
def registry(host_ip):
1458
port = get_free_port()
1559
# Explicitly pull the image first so it runs on time
1660
registry_image = "registry:3.0.0-rc.3"
1761
subprocess.check_call(["docker", "pull", registry_image])
62+
63+
1864
cmd = [
19-
"docker", "run", "-p", f"{port}:5000", registry_image
65+
"docker", "run", "--rm",
66+
"-p", f"{port}:5000", registry_image
2067
]
2168
proc = subprocess.Popen(cmd)
22-
health_url = f'http://localhost:{port}/v2'
69+
health_url = f'http://{host_ip}:{port}/v2'
2370
# Wait for the registry to actually come up
2471
for i in range(10):
2572
try:
@@ -34,19 +81,20 @@ def registry():
3481
raise TimeoutError("Test registry did not come up in time")
3582

3683
try:
37-
yield f"localhost:{port}"
84+
yield f"{host_ip}:{port}"
3885
finally:
3986
proc.terminate()
4087
proc.wait()
4188

4289

43-
def test_registry(registry):
90+
def test_registry(registry, dind):
4491
image_name = f"{registry}/{secrets.token_hex(8)}:latest"
4592
r2d = make_r2d([
4693
"--image", image_name,
4794
"--push", "--no-run", str(HERE)
4895
])
4996

97+
os.environ["DOCKER_HOST"] = dind
5098
r2d.start()
5199

52100
proc = subprocess.run(["docker", "manifest", "inspect", "--insecure", image_name])

0 commit comments

Comments
 (0)