1
+ import os
2
+ import socket
1
3
from pathlib import Path
2
4
import subprocess
3
5
import pytest
9
11
10
12
HERE = Path (__file__ ).parent
11
13
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 ):
14
58
port = get_free_port ()
15
59
# Explicitly pull the image first so it runs on time
16
60
registry_image = "registry:3.0.0-rc.3"
17
61
subprocess .check_call (["docker" , "pull" , registry_image ])
62
+
63
+
18
64
cmd = [
19
- "docker" , "run" , "-p" , f"{ port } :5000" , registry_image
65
+ "docker" , "run" , "--rm" ,
66
+ "-p" , f"{ port } :5000" , registry_image
20
67
]
21
68
proc = subprocess .Popen (cmd )
22
- health_url = f'http://localhost :{ port } /v2'
69
+ health_url = f'http://{ host_ip } :{ port } /v2'
23
70
# Wait for the registry to actually come up
24
71
for i in range (10 ):
25
72
try :
@@ -34,19 +81,20 @@ def registry():
34
81
raise TimeoutError ("Test registry did not come up in time" )
35
82
36
83
try :
37
- yield f"localhost :{ port } "
84
+ yield f"{ host_ip } :{ port } "
38
85
finally :
39
86
proc .terminate ()
40
87
proc .wait ()
41
88
42
89
43
- def test_registry (registry ):
90
+ def test_registry (registry , dind ):
44
91
image_name = f"{ registry } /{ secrets .token_hex (8 )} :latest"
45
92
r2d = make_r2d ([
46
93
"--image" , image_name ,
47
94
"--push" , "--no-run" , str (HERE )
48
95
])
49
96
97
+ os .environ ["DOCKER_HOST" ] = dind
50
98
r2d .start ()
51
99
52
100
proc = subprocess .run (["docker" , "manifest" , "inspect" , "--insecure" , image_name ])
0 commit comments