19
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20
20
# MA 02111-1307 USA
21
21
#
22
-
23
- from asyncio .log import logger
24
22
import os
25
23
import shutil
26
24
import tempfile
27
25
import unittest
28
-
29
26
import configobj
30
27
import docker
31
28
29
+ from asyncio .log import logger
30
+
32
31
from dlg import droputils , utils , prepareUser
33
32
from dlg .apps .dockerapp import DockerApp
34
33
from dlg .data .drops .ngas import NgasDROP
42
41
except :
43
42
pass
44
43
44
+ class CustomContainer :
45
+ """
46
+ Used to create custom docker images
47
+ """
48
+ client = docker .from_env ()
49
+
50
+ def create_container (self , fstr , tag ):
51
+ """
52
+ Create a customer docker image through a temporary Dockerfile.
53
+ :return: image tag
54
+ """
55
+ from pathlib import Path
56
+
57
+ docker_dir = tempfile .TemporaryDirectory ()
58
+ dockerfile = Path (docker_dir .name ) / "Dockerfile"
59
+ with dockerfile .open ("w" ) as fp :
60
+ fp .write (fstr )
61
+ image , _ = self .client .images .build (path = str (dockerfile .parent ),
62
+ tag = tag )
63
+ return image .tags .pop ()
64
+
65
+
66
+ def remove_container (self , image : str ):
67
+ """
68
+ Remove the custom docker image
69
+
70
+ :param image:
71
+ :return:
72
+ """
73
+ return self .client .images .remove (image )
45
74
46
75
@unittest .skipIf (docker_unavailable , "Docker daemon not available" )
47
76
class DockerTests (unittest .TestCase ):
@@ -79,7 +108,7 @@ def test_simpleCopy(self):
79
108
"""
80
109
81
110
a = FileDROP ("a" , "a" )
82
- b = DockerApp ("b" , "b" , image = "ubuntu:14 .04" , command = "cp {a} {c}" )
111
+ b = DockerApp ("b" , "b" , image = "ubuntu:22 .04" , command = "cp {a} {c}" )
83
112
c = FileDROP ("c" , "c" )
84
113
85
114
b .addInput (a )
@@ -112,15 +141,21 @@ def test_clientServer(self):
112
141
treated as a publisher of D. This way D waits for both applications to
113
142
finish before proceeding.
114
143
"""
115
-
144
+ dockerfile = ("FROM ubuntu:22.04\n "
145
+ "RUN apt update && apt install -y netcat\n " )
146
+ container_manager = CustomContainer ()
116
147
a = FileDROP ("a" , "a" )
117
148
b = DockerApp (
118
149
"b" ,
119
150
"b" ,
120
- image = "ubuntu:14 .04" ,
121
- command = "cat {a} > /dev/tcp/%containerIp[c ]%/8000" ,
151
+ image = "ubuntu:22 .04" ,
152
+ command = "cat {a} > /dev/tcp/%containerIp[{c} ]%/8000" ,
122
153
)
123
- c = DockerApp ("c" , "c" , image = "ubuntu:14.04" , command = "nc -l 8000 > {d}" )
154
+
155
+ image = container_manager .create_container (
156
+ dockerfile ,tag = "netcat_test_container" )
157
+ c = DockerApp ("c" , "c" , image = image , command = "nc -l "
158
+ "8000 > {d}" )
124
159
d = FileDROP ("d" , "d" )
125
160
126
161
b .addInput (a )
@@ -132,7 +167,7 @@ def test_clientServer(self):
132
167
b .handleInterest (c )
133
168
134
169
data = os .urandom (10 )
135
- with DROPWaiterCtx (self , d , 10 ):
170
+ with DROPWaiterCtx (self , d , 5 ):
136
171
a .write (data )
137
172
a .setCompleted ()
138
173
@@ -146,7 +181,7 @@ def test_quotedCommands(self):
146
181
"""
147
182
148
183
def assertMsgIsCorrect (msg , command ):
149
- a = DockerApp ("a" , "a" , image = "ubuntu:14 .04" , command = command )
184
+ a = DockerApp ("a" , "a" , image = "ubuntu:22 .04" , command = command )
150
185
b = FileDROP ("b" , "b" )
151
186
a .addOutput (b )
152
187
with DROPWaiterCtx (self , b , 100 ):
@@ -181,7 +216,7 @@ def _ngas_and_fs_io(self, command):
181
216
"HelloWorld_out.txt" , "HelloWorld_out.txt"
182
217
) # not a filesystem-related DROP, we can reference its URL in the command-line
183
218
a .ngasSrv = "ngas.icrar.org"
184
- b = DockerApp ("b" , "b" , image = "ubuntu:14 .04" , command = command )
219
+ b = DockerApp ("b" , "b" , image = "ubuntu:22 .04" , command = command )
185
220
c = FileDROP ("c" , "c" )
186
221
b .addInput (a )
187
222
b .addOutput (c )
@@ -201,7 +236,7 @@ def test_additional_bindings(self):
201
236
a = DockerApp (
202
237
"a" ,
203
238
"a" ,
204
- image = "ubuntu:14 .04" ,
239
+ image = "ubuntu:22 .04" ,
205
240
command = "cp /opt/file %s" % (tempDir ,),
206
241
additionalBindings = [tempDir , "%s:/opt/file" % (tempFile ,)],
207
242
)
@@ -225,7 +260,7 @@ def _test_working_dir(self, ensureUserAndSwitch):
225
260
"a" ,
226
261
"a" ,
227
262
workingDir = "/mydir" ,
228
- image = "ubuntu:14 .04" ,
263
+ image = "ubuntu:22 .04" ,
229
264
command = "pwd > {b} && sleep 0.05" ,
230
265
ensureUserAndSwitch = ensureUserAndSwitch ,
231
266
)
0 commit comments