comparison env/lib/python3.9/site-packages/cwltool/tests/test_tmpdir.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Test that all temporary directories respect the --tmpdir-prefix and --tmp-outdir-prefix options."""
2 import subprocess
3 import sys
4 from pathlib import Path
5 from typing import List, cast
6
7 from _pytest.monkeypatch import MonkeyPatch
8 from ruamel.yaml.comments import CommentedMap
9 from schema_salad.avro import schema
10 from schema_salad.sourceline import cmap
11
12 from cwltool.builder import Builder
13 from cwltool.command_line_tool import CommandLineTool
14 from cwltool.context import LoadingContext, RuntimeContext
15 from cwltool.docker import DockerCommandLineJob
16 from cwltool.job import JobBase
17 from cwltool.pathmapper import MapperEnt, PathMapper
18 from cwltool.stdfsaccess import StdFsAccess
19 from cwltool.update import INTERNAL_VERSION
20 from cwltool.utils import create_tmp_dir
21
22 from .util import get_data, needs_docker
23
24
25 def test_docker_commandLineTool_job_tmpdir_prefix(tmp_path: Path) -> None:
26 """Test that docker enabled CommandLineTool respects temp directory directives."""
27 loading_context = LoadingContext(
28 {
29 "metadata": {
30 "cwlVersion": INTERNAL_VERSION,
31 "http://commonwl.org/cwltool#original_cwlVersion": INTERNAL_VERSION,
32 }
33 }
34 )
35 clt = CommandLineTool(
36 cast(
37 CommentedMap,
38 cmap(
39 {
40 "cwlVersion": INTERNAL_VERSION,
41 "class": "CommandLineTool",
42 "inputs": [],
43 "outputs": [],
44 "requirements": [
45 {"class": "DockerRequirement", "dockerPull": "debian:stable"}
46 ],
47 }
48 ),
49 ),
50 loading_context,
51 )
52 tmpdir_prefix = str(tmp_path / "1")
53 tmp_outdir_prefix = str(tmp_path / "2")
54 runtime_context = RuntimeContext(
55 {
56 "tmpdir_prefix": tmpdir_prefix,
57 "tmp_outdir_prefix": tmp_outdir_prefix,
58 }
59 )
60 job = next(clt.job({}, None, runtime_context))
61 assert isinstance(job, JobBase)
62 assert job.stagedir and job.stagedir.startswith(tmpdir_prefix)
63 assert job.tmpdir and job.tmpdir.startswith(tmpdir_prefix)
64 assert job.outdir and job.outdir.startswith(tmp_outdir_prefix)
65
66
67 def test_commandLineTool_job_tmpdir_prefix(tmp_path: Path) -> None:
68 """Test that non-docker enabled CommandLineTool respects temp directory directives."""
69 loading_context = LoadingContext(
70 {
71 "metadata": {
72 "cwlVersion": INTERNAL_VERSION,
73 "http://commonwl.org/cwltool#original_cwlVersion": INTERNAL_VERSION,
74 }
75 }
76 )
77 clt = CommandLineTool(
78 cast(
79 CommentedMap,
80 cmap(
81 {
82 "cwlVersion": INTERNAL_VERSION,
83 "class": "CommandLineTool",
84 "inputs": [],
85 "outputs": [],
86 "requirements": [],
87 }
88 ),
89 ),
90 loading_context,
91 )
92 tmpdir_prefix = str(tmp_path / "1")
93 tmp_outdir_prefix = str(tmp_path / "2")
94 runtime_context = RuntimeContext(
95 {
96 "tmpdir_prefix": tmpdir_prefix,
97 "tmp_outdir_prefix": tmp_outdir_prefix,
98 }
99 )
100 job = next(clt.job({}, None, runtime_context))
101 assert isinstance(job, JobBase)
102 assert job.stagedir and job.stagedir.startswith(tmpdir_prefix)
103 assert job.tmpdir and job.tmpdir.startswith(tmpdir_prefix)
104 assert job.outdir and job.outdir.startswith(tmp_outdir_prefix)
105
106
107 @needs_docker
108 def test_dockerfile_tmpdir_prefix(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
109 """Test that DockerCommandLineJob.get_image respects temp directory directives."""
110 monkeypatch.setattr(
111 target=subprocess, name="check_call", value=lambda *args, **kwargs: True
112 )
113 tmp_outdir_prefix = tmp_path / "1"
114 assert DockerCommandLineJob.get_image(
115 {
116 "class": "DockerRequirement",
117 "dockerFile": "FROM debian:stable",
118 "dockerImageId": sys._getframe().f_code.co_name,
119 },
120 pull_image=True,
121 force_pull=True,
122 tmp_outdir_prefix=str(tmp_outdir_prefix),
123 )
124 children = sorted(tmp_outdir_prefix.parent.glob("*"))
125 assert len(children) == 1
126 subdir = tmp_path / children[0]
127 assert len(sorted(subdir.glob("*"))) == 1
128 assert (subdir / "Dockerfile").exists()
129
130
131 def test_docker_tmpdir_prefix(tmp_path: Path) -> None:
132 """Test that DockerCommandLineJob respects temp directory directives."""
133 (tmp_path / "3").mkdir()
134 tmpdir_prefix = str(tmp_path / "3" / "ttmp")
135 runtime_context = RuntimeContext(
136 {"tmpdir_prefix": tmpdir_prefix, "user_space_docker_cmd": None}
137 )
138 builder = Builder(
139 {},
140 [],
141 [],
142 {},
143 schema.Names(),
144 [],
145 [],
146 {},
147 None,
148 None,
149 StdFsAccess,
150 StdFsAccess(""),
151 None,
152 0.1,
153 False,
154 False,
155 False,
156 "",
157 runtime_context.get_outdir(),
158 runtime_context.get_tmpdir(),
159 runtime_context.get_stagedir(),
160 INTERNAL_VERSION,
161 )
162 job = DockerCommandLineJob(builder, {}, PathMapper, [], [], "")
163 runtime: List[str] = []
164
165 volume_writable_file = MapperEnt(
166 resolved=get_data("tests/2.fastq"), target="foo", type=None, staged=None
167 )
168 (tmp_path / "1").mkdir()
169 job.add_writable_file_volume(
170 runtime, volume_writable_file, None, str(tmp_path / "1" / "writable_file")
171 )
172 children = sorted((tmp_path / "1").glob("*"))
173 assert len(children) == 1
174 subdir = tmp_path / children[0]
175 assert subdir.name.startswith("writable_file")
176 assert len(sorted(subdir.glob("*"))) == 1
177 assert (subdir / "2.fastq").exists()
178
179 resolved_writable_dir = tmp_path / "data_orig"
180 resolved_writable_dir.mkdir(parents=True)
181 volume_dir = MapperEnt(
182 resolved=str(resolved_writable_dir), target="bar", type=None, staged=None
183 )
184 (tmp_path / "2").mkdir()
185 job.add_writable_directory_volume(
186 runtime, volume_dir, None, str(tmp_path / "2" / "dir")
187 )
188 children = sorted((tmp_path / "2").glob("*"))
189 assert len(children) == 1
190 subdir = tmp_path / "2" / children[0]
191 assert subdir.name.startswith("dir")
192 assert len(sorted(subdir.glob("*"))) == 1
193 assert (subdir / "data_orig").exists()
194
195 cidfile = job.create_runtime({}, runtime_context)[1]
196 assert cidfile and cidfile.startswith(tmpdir_prefix)
197
198 volume_file = MapperEnt(resolved="Hoopla!", target="baz", type=None, staged=None)
199 (tmp_path / "4").mkdir()
200 job.create_file_and_add_volume(
201 runtime, volume_file, None, None, str(tmp_path / "4" / "file")
202 )
203 children = sorted((tmp_path / "4").glob("*"))
204 assert len(children) == 1
205 subdir = tmp_path / "4" / children[0]
206 assert subdir.name.startswith("file")
207 assert len(sorted(subdir.glob("*"))) == 1
208 assert (subdir / "baz").exists()
209
210
211 def test_runtimeContext_respects_tmpdir_prefix(tmp_path: Path) -> None:
212 """Test that RuntimeContext helper methods respects tmpdir_prefix."""
213 tmpdir_prefix = str(tmp_path / "foo")
214 runtime_context = RuntimeContext({"tmpdir_prefix": tmpdir_prefix})
215 assert runtime_context.get_tmpdir().startswith(tmpdir_prefix)
216 assert runtime_context.get_stagedir().startswith(tmpdir_prefix)
217 assert runtime_context.create_tmpdir().startswith(tmpdir_prefix)
218 assert create_tmp_dir(tmpdir_prefix).startswith(tmpdir_prefix)
219
220
221 def test_runtimeContext_respects_tmp_outdir_prefix(tmp_path: Path) -> None:
222 """Test that RuntimeContext helper methods respects tmp_outdir_prefix."""
223 tmpdir_prefix = str(tmp_path / "foo")
224 runtime_context = RuntimeContext({"tmp_outdir_prefix": tmpdir_prefix})
225 assert runtime_context.get_outdir().startswith(tmpdir_prefix)
226 assert runtime_context.create_outdir().startswith(tmpdir_prefix)