Skip to content

Commit ecbde29

Browse files
committed
LIU-458: Upgrade test_docker.py images to 22.04
- Doing this alone causes errors because nc is not available on base ubuntu:22.04 images. - Need to install netstat (nc) onto image - Easiest way to do this is using a dockerfile with the docker-py API.
1 parent 9caf08d commit ecbde29

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

daliuge-engine/dlg/apps/dockerapp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def handleInterest(self, drop):
391391
# The only interest we currently have is the containerIp of other
392392
# DockerApps, and only if our command actually uses this IP
393393
if isinstance(drop, DockerApp):
394-
if "%containerIp[{0}]%".format(drop.uid) in self._command:
394+
if f"%containerIp[{{{drop.uid}}}]%" in self._command:
395395
self._waiters.append(ContainerIpWaiter(drop))
396396
logger.debug("%r: Added ContainerIpWaiter for %r", self, drop)
397397

@@ -538,7 +538,7 @@ def run(self):
538538
# started, and replace their IP placeholders by the real IPs
539539
for waiter in self._waiters:
540540
uid, ip = waiter.waitForIp()
541-
cmd = cmd.replace("%containerIp[{0}]%".format(uid), ip)
541+
cmd = cmd.replace(f"%containerIp[{{{uid}}}]%", ip)
542542
logger.debug("Command after IP replacement is: %s", cmd)
543543

544544
# Wrap everything inside bash

daliuge-engine/test/apps/test_docker.py

+48-13
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2020
# MA 02111-1307 USA
2121
#
22-
23-
from asyncio.log import logger
2422
import os
2523
import shutil
2624
import tempfile
2725
import unittest
28-
2926
import configobj
3027
import docker
3128

29+
from asyncio.log import logger
30+
3231
from dlg import droputils, utils, prepareUser
3332
from dlg.apps.dockerapp import DockerApp
3433
from dlg.data.drops.ngas import NgasDROP
@@ -42,6 +41,36 @@
4241
except:
4342
pass
4443

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)
4574

4675
@unittest.skipIf(docker_unavailable, "Docker daemon not available")
4776
class DockerTests(unittest.TestCase):
@@ -79,7 +108,7 @@ def test_simpleCopy(self):
79108
"""
80109

81110
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}")
83112
c = FileDROP("c", "c")
84113

85114
b.addInput(a)
@@ -112,15 +141,21 @@ def test_clientServer(self):
112141
treated as a publisher of D. This way D waits for both applications to
113142
finish before proceeding.
114143
"""
115-
144+
dockerfile = ("FROM ubuntu:22.04\n"
145+
"RUN apt update && apt install -y netcat\n")
146+
container_manager = CustomContainer()
116147
a = FileDROP("a", "a")
117148
b = DockerApp(
118149
"b",
119150
"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",
122153
)
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}")
124159
d = FileDROP("d", "d")
125160

126161
b.addInput(a)
@@ -132,7 +167,7 @@ def test_clientServer(self):
132167
b.handleInterest(c)
133168

134169
data = os.urandom(10)
135-
with DROPWaiterCtx(self, d, 10):
170+
with DROPWaiterCtx(self, d, 5):
136171
a.write(data)
137172
a.setCompleted()
138173

@@ -146,7 +181,7 @@ def test_quotedCommands(self):
146181
"""
147182

148183
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)
150185
b = FileDROP("b", "b")
151186
a.addOutput(b)
152187
with DROPWaiterCtx(self, b, 100):
@@ -181,7 +216,7 @@ def _ngas_and_fs_io(self, command):
181216
"HelloWorld_out.txt", "HelloWorld_out.txt"
182217
) # not a filesystem-related DROP, we can reference its URL in the command-line
183218
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)
185220
c = FileDROP("c", "c")
186221
b.addInput(a)
187222
b.addOutput(c)
@@ -201,7 +236,7 @@ def test_additional_bindings(self):
201236
a = DockerApp(
202237
"a",
203238
"a",
204-
image="ubuntu:14.04",
239+
image="ubuntu:22.04",
205240
command="cp /opt/file %s" % (tempDir,),
206241
additionalBindings=[tempDir, "%s:/opt/file" % (tempFile,)],
207242
)
@@ -225,7 +260,7 @@ def _test_working_dir(self, ensureUserAndSwitch):
225260
"a",
226261
"a",
227262
workingDir="/mydir",
228-
image="ubuntu:14.04",
263+
image="ubuntu:22.04",
229264
command="pwd > {b} && sleep 0.05",
230265
ensureUserAndSwitch=ensureUserAndSwitch,
231266
)

0 commit comments

Comments
 (0)