Skip to content

Commit e94c818

Browse files
committed
style: remove parameters that never change
Allows us to prune branches that are never executed Add tests for docker mirroring singularity
1 parent 64dc158 commit e94c818

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

cwltool/docker.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Enables Docker software containers via the {dx-,u,}docker runtimes."""
22
from __future__ import absolute_import
33

4+
from distutils import spawn
45
import datetime
56
import os
67
import re
@@ -182,32 +183,17 @@ def get_image(docker_requirement, # type: Dict[Text, Text]
182183

183184
def get_from_requirements(self,
184185
r, # type: Dict[Text, Text]
185-
req, # type: bool
186186
pull_image, # type: bool
187187
force_pull=False, # type: bool
188188
tmp_outdir_prefix=DEFAULT_TMP_PREFIX # type: Text
189189
): # type: (...) -> Optional[Text]
190-
if r:
191-
errmsg = None
192-
try:
193-
subprocess.check_output(["docker", "version"])
194-
except subprocess.CalledProcessError as err:
195-
errmsg = "Cannot communicate with docker daemon: " + Text(err)
196-
except OSError as err:
197-
errmsg = "'docker' executable not found: " + Text(err)
198-
199-
if errmsg is not None:
200-
if req:
201-
raise WorkflowException(errmsg)
202-
else:
203-
return None
190+
if not spawn.find_executable('docker'):
191+
raise WorkflowException("docker executable is not available")
204192

205-
if self.get_image(r, pull_image, force_pull, tmp_outdir_prefix):
206-
return r["dockerImageId"]
207-
if req:
208-
raise WorkflowException(u"Docker image %s not found" % r["dockerImageId"])
209193

210-
return None
194+
if self.get_image(r, pull_image, force_pull, tmp_outdir_prefix):
195+
return r["dockerImageId"]
196+
raise WorkflowException(u"Docker image %s not found" % r["dockerImageId"])
211197

212198
@staticmethod
213199
def append_volume(runtime, source, target, writable=False):

cwltool/job.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ class ContainerCommandLineJob(with_metaclass(ABCMeta, JobBase)):
430430
@abstractmethod
431431
def get_from_requirements(self,
432432
r, # type: Dict[Text, Text]
433-
req, # type: bool
434433
pull_image, # type: bool
435434
force_pull=False, # type: bool
436435
tmp_outdir_prefix=DEFAULT_TMP_PREFIX # type: Text
@@ -565,7 +564,7 @@ def run(self, runtimeContext):
565564
if docker_req is not None and runtimeContext.use_container:
566565
img_id = str(
567566
self.get_from_requirements(
568-
docker_req, True, runtimeContext.pull_image,
567+
docker_req, runtimeContext.pull_image,
569568
getdefault(runtimeContext.force_docker_pull, False),
570569
getdefault(runtimeContext.tmp_outdir_prefix, DEFAULT_TMP_PREFIX)))
571570
if img_id is None:

cwltool/singularity.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def get_image(dockerRequirement, # type: Dict[Text, Text]
124124
return found
125125

126126
def get_from_requirements(self,
127-
r, # type: Optional[Dict[Text, Text]]
128-
req, # type: bool
127+
r, # type: Dict[Text, Text]
129128
pull_image, # type: bool
130129
force_pull=False, # type: bool
131130
tmp_outdir_prefix=None # type: Text
@@ -136,19 +135,12 @@ def get_from_requirements(self,
136135
hello-world-latest.img).
137136
"""
138137

139-
if r is None:
140-
return None
141-
142138
if not bool(spawn.find_executable('singularity')):
143-
if req:
144-
raise WorkflowException('singularity executable is not available')
145-
return None
139+
raise WorkflowException('singularity executable is not available')
146140

147141
if not self.get_image(r, pull_image, force_pull):
148-
if req:
149-
raise WorkflowException(u"Container image {} not "
150-
"found".format(r["dockerImageId"]))
151-
return None
142+
raise WorkflowException(u"Container image {} not "
143+
"found".format(r["dockerImageId"]))
152144

153145
return os.path.abspath(r["dockerImageId"])
154146

tests/test_docker.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
3+
from distutils import spawn
4+
5+
from cwltool.docker import DockerCommandLineJob
6+
from cwltool.main import main
7+
8+
from .util import get_data, get_main_output, needs_docker, needs_singularity
9+
10+
@needs_docker
11+
def test_docker_workflow():
12+
result_code, _, stderr = get_main_output(
13+
['--default-container', 'debian',
14+
get_data("tests/wf/hello-workflow.cwl"), "--usermessage", "hello"])
15+
assert "completed success" in stderr
16+
assert result_code == 0
17+
18+
def test_docker_iwdr():
19+
result_code = main(
20+
['--default-container', 'debian',
21+
get_data("tests/wf/iwdr-entry.cwl"), "--message", "hello"])
22+
docker_installed = bool(spawn.find_executable('docker'))
23+
if docker_installed:
24+
assert result_code == 0
25+
else:
26+
assert result_code != 0
27+
28+
@needs_docker
29+
def test_docker_incorrect_image_pull():
30+
result_code = main(
31+
['--default-container', 'non-existant-weird-image',
32+
get_data("tests/wf/hello-workflow.cwl"), "--usermessage", "hello"])
33+
assert result_code != 0

0 commit comments

Comments
 (0)