/
test_update.py
420 lines (294 loc) · 14.5 KB
/
test_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
#
# Copyright 2017-2021 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test ``update`` command."""
import os
import time
from pathlib import Path
import git
from renku.cli import cli
from renku.core.management.repository import DEFAULT_DATA_DIR as DATA_DIR
from renku.core.metadata.gateway.activity_gateway import ActivityGateway
from renku.core.models.workflow.plan import Plan
from tests.utils import format_result_exception, write_and_commit_file
def test_update(runner, client, renku_cli, client_database_injection_manager):
"""Test output is updated when source changes."""
repo = client.repo
source = os.path.join(client.path, "source.txt")
output = os.path.join(client.path, "output.txt")
write_and_commit_file(repo, source, "content")
exit_code, previous_activity = renku_cli("run", "head", "-1", source, stdout=output)
assert 0 == exit_code
write_and_commit_file(repo, source, "changed content")
exit_code, activity = renku_cli("update", "--all")
assert 0 == exit_code
plan = activity.association.plan
assert previous_activity.association.plan.id == plan.id
assert isinstance(plan, Plan)
assert "changed content" == Path(output).read_text()
result = runner.invoke(cli, ["status"])
assert 0 == result.exit_code, format_result_exception(result)
with client_database_injection_manager(client):
activity_gateway = ActivityGateway()
activity_collections = activity_gateway.get_all_activity_collections()
# NOTE: No ActivityCollection is created if update include only one activity
assert [] == activity_collections
def test_update_multiple_steps(runner, client, renku_cli, client_database_injection_manager):
"""Test update in a multi-step workflow."""
repo = client.repo
source = os.path.join(client.path, "source.txt")
intermediate = os.path.join(client.path, "intermediate.txt")
output = os.path.join(client.path, "output.txt")
write_and_commit_file(repo, source, "content")
exit_code, activity1 = renku_cli("run", "cp", source, intermediate)
assert 0 == exit_code
exit_code, activity2 = renku_cli("run", "cp", intermediate, output)
assert 0 == exit_code
write_and_commit_file(repo, source, "changed content")
exit_code, activities = renku_cli("update", "--all")
assert 0 == exit_code
plans = [a.association.plan for a in activities]
assert 2 == len(plans)
assert isinstance(plans[0], Plan)
assert isinstance(plans[1], Plan)
assert {p.id for p in plans} == {activity1.association.plan.id, activity2.association.plan.id}
assert "changed content" == Path(intermediate).read_text()
assert "changed content" == Path(output).read_text()
result = runner.invoke(cli, ["status"])
assert 0 == result.exit_code, format_result_exception(result)
with client_database_injection_manager(client):
activity_gateway = ActivityGateway()
activity_collections = activity_gateway.get_all_activity_collections()
assert 1 == len(activity_collections)
assert {a.id for a in activities} == {a.id for a in activity_collections[0].activities}
def test_update_multiple_steps_with_path(runner, project, renku_cli):
"""Test update in a multi-step workflow when a path is specified."""
repo = git.Repo(project)
source = os.path.join(project, "source.txt")
intermediate = os.path.join(project, "intermediate.txt")
output = os.path.join(project, "output.txt")
write_and_commit_file(repo, source, "content")
exit_code, activity1 = renku_cli("run", "cp", source, intermediate)
assert 0 == exit_code
exit_code, _ = renku_cli("run", "cp", intermediate, output)
assert 0 == exit_code
write_and_commit_file(repo, source, "changed content")
exit_code, activity = renku_cli("update", intermediate)
assert 0 == exit_code
plan = activity.association.plan
assert isinstance(plan, Plan)
assert plan.id == activity1.association.plan.id
assert "changed content" == Path(intermediate).read_text()
assert "content" == Path(output).read_text()
result = runner.invoke(cli, ["status"])
assert 1 == result.exit_code, format_result_exception(result)
assert "output.txt: intermediate.txt" in result.output
assert "source.txt" not in result.output
def test_update_with_directory_paths(project, renku_cli):
"""Test update when a directory path is specified."""
repo = git.Repo(project)
data = os.path.join(project, "data", "dataset", "my-data")
Path(data).mkdir(parents=True, exist_ok=True)
source = os.path.join(project, "source.txt")
output = os.path.join(data, "output.txt")
write_and_commit_file(repo, source, "content")
exit_code, previous_activity = renku_cli("run", "head", "-1", source, stdout=output)
assert 0 == exit_code
write_and_commit_file(repo, source, "changed content")
exit_code, activity = renku_cli("update", data)
assert 0 == exit_code
assert "changed content" == Path(output).read_text()
plan = activity.association.plan
assert previous_activity.association.plan.id == plan.id
def test_multiple_updates(runner, project, renku_cli):
"""Test multiple updates of the same source."""
repo = git.Repo(project)
source = os.path.join(project, "source.txt")
intermediate = os.path.join(project, "intermediate.txt")
output = os.path.join(project, "output.txt")
write_and_commit_file(repo, source, "content")
exit_code, activity1 = renku_cli("run", "cp", source, intermediate)
assert 0 == exit_code
exit_code, activity2 = renku_cli("run", "cp", intermediate, output)
assert 0 == exit_code
write_and_commit_file(repo, source, "changed content")
exit_code, _ = renku_cli("update", "--all")
assert 0 == exit_code
assert "changed content" == Path(intermediate).read_text()
write_and_commit_file(repo, source, "more changed content")
exit_code, activities = renku_cli("update", "--all")
assert 0 == exit_code
plans = [a.association.plan for a in activities]
assert 2 == len(plans)
assert isinstance(plans[0], Plan)
assert isinstance(plans[1], Plan)
assert {p.id for p in plans} == {activity1.association.plan.id, activity2.association.plan.id}
assert "more changed content" == Path(intermediate).read_text()
assert "more changed content" == Path(output).read_text()
result = runner.invoke(cli, ["status"])
assert 0 == result.exit_code, format_result_exception(result)
def test_update_workflow_without_outputs(runner, project, run):
"""Test workflow without outputs."""
repo = git.Repo(project)
source = os.path.join(project, "source.txt")
write_and_commit_file(repo, source, "content")
assert 0 == runner.invoke(cli, ["run", "cat", "--no-output", source]).exit_code
write_and_commit_file(repo, source, "changes")
assert 1 == runner.invoke(cli, ["status"]).exit_code
assert 0 == run(args=["update", "--all"])
result = runner.invoke(cli, ["status"])
# NOTE: Activity is updated or otherwise status would still return 1
assert 0 == result.exit_code, format_result_exception(result)
def test_update_siblings(project, run, no_lfs_warning):
"""Test all generations of an activity are updated together."""
repo = git.Repo(project)
parent = os.path.join(project, "parent.txt")
brother = os.path.join(project, "brother.txt")
sister = os.path.join(project, "sister.txt")
siblings = [Path(brother), Path(sister)]
write_and_commit_file(repo, parent, "content")
assert 0 == run(args=["run", "tee", brother, sister], stdin=parent)
# The output file is copied from the source.
for sibling in siblings:
assert "content" == sibling.read_text()
write_and_commit_file(repo, parent, "changed content")
assert 0 == run(args=["update", brother])
for sibling in siblings:
assert "changed content" == sibling.read_text()
# Siblings kept together even when one is removed.
repo.index.remove([brother], working_tree=True)
repo.index.commit("Brother removed")
assert not os.path.exists(brother)
write_and_commit_file(repo, parent, "more content")
# Update should create the missing sibling
assert 0 == run(args=["update", "--all"])
for sibling in siblings:
assert "more content" == sibling.read_text()
def test_update_siblings_in_output_directory(project, run):
"""Files in output directory are linked or removed after update."""
repo = git.Repo(project)
source = os.path.join(project, "source.txt")
output = Path(os.path.join(project, "output")) # a directory
def write_source():
"""Write source from files."""
write_and_commit_file(repo, source, content="\n".join(" ".join(line) for line in files) + "\n")
def check_files():
"""Check file content."""
assert len(files) == len(list(output.rglob("*")))
for name, content in files:
assert content == (output / name).read_text().strip()
files = [("first", "1"), ("second", "2"), ("third", "3")]
write_source()
script = 'mkdir -p "$0"; ' "cat - | while read -r name content; do " 'echo "$content" > "$0/$name"; done'
assert not os.path.exists(output)
assert 0 == run(args=["run", "sh", "-c", script, "output"], stdin=source)
assert os.path.exists(output)
check_files()
files = [("third", "3"), ("fourth", "4")]
write_source()
assert 0 == run(args=["update", "output"])
check_files()
def test_update_relative_path_for_directory_input(client, run, renku_cli):
"""Test having a directory input generates relative path in CWL."""
write_and_commit_file(client.repo, client.path / DATA_DIR / "file1", "file1")
assert 0 == run(args=["run", "ls", DATA_DIR], stdout="ls.data")
write_and_commit_file(client.repo, client.path / DATA_DIR / "file2", "file2")
exit_code, activity = renku_cli("update", "--all")
assert 0 == exit_code
plan = activity.association.plan
assert 1 == len(plan.inputs)
assert "data" == plan.inputs[0].default_value
def test_update_no_args(runner, project, no_lfs_warning):
"""Test calling update with no args raises ParameterError."""
repo = git.Repo(project)
source = os.path.join(project, "source.txt")
output = os.path.join(project, "output.txt")
write_and_commit_file(repo, source, "content")
assert 0 == runner.invoke(cli, ["run", "cp", source, output]).exit_code
write_and_commit_file(repo, source, "changed content")
before_commit = repo.head.commit
result = runner.invoke(cli, ["update"])
assert 2 == result.exit_code
assert "Either PATHS, --all/-a, or --dry-run/-n should be specified." in result.output
assert before_commit == repo.head.commit
def test_update_with_no_execution(project, runner):
"""Test update when no workflow is executed."""
repo = git.Repo(project)
input = os.path.join(project, "data", "input.txt")
write_and_commit_file(repo, input, "content")
result = runner.invoke(cli, ["update", input], catch_exceptions=False)
assert 1 == result.exit_code
def test_update_overridden_output(project, renku_cli, runner):
"""Test a path where final output is overridden will be updated partially."""
repo = git.Repo(project)
a = os.path.join(project, "a")
b = os.path.join(project, "b")
c = os.path.join(project, "c")
write_and_commit_file(repo, a, "content")
assert 0 == runner.invoke(cli, ["run", "--name", "r1", "cp", a, b]).exit_code
time.sleep(1)
assert 0 == runner.invoke(cli, ["run", "--name", "r2", "cp", b, c]).exit_code
time.sleep(1)
assert 0 == renku_cli("run", "--name", "r3", "wc", a, stdout=c).exit_code
write_and_commit_file(repo, a, "new content")
result = runner.invoke(cli, ["update", "--dry-run"])
assert 0 == result.exit_code
assert "r1" in result.output
assert "r2" not in result.output
assert "r3" in result.output
def test_update_overridden_outputs_partially(project, renku_cli, runner):
"""Test a path where one of the final output is overridden will be updated completely but in proper order."""
repo = git.Repo(project)
a = os.path.join(project, "a")
b = os.path.join(project, "b")
c = os.path.join(project, "c")
d = os.path.join(project, "d")
write_and_commit_file(repo, a, "content")
assert 0 == runner.invoke(cli, ["run", "--name", "r1", "cp", a, b]).exit_code
time.sleep(1)
assert 0 == renku_cli("run", "--name", "r2", "tee", c, d, stdin=b).exit_code
time.sleep(1)
assert 0 == renku_cli("run", "--name", "r3", "wc", a, stdout=c).exit_code
write_and_commit_file(repo, a, "new content")
result = runner.invoke(cli, ["update", "--dry-run"])
assert 0 == result.exit_code
assert "r1" in result.output
assert "r2" in result.output
assert "r3" in result.output
assert result.output.find("r2") < result.output.find("r3")
def test_update_multiple_paths_common_output(project, renku_cli, runner):
"""Test multiple paths that generate the same output will be updated except the last overridden step."""
repo = git.Repo(project)
a = os.path.join(project, "a")
b = os.path.join(project, "b")
c = os.path.join(project, "c")
d = os.path.join(project, "d")
write_and_commit_file(repo, a, "content")
assert 0 == runner.invoke(cli, ["run", "--name", "r1", "cp", a, b]).exit_code
time.sleep(1)
assert 0 == runner.invoke(cli, ["run", "--name", "r2", "cp", b, d]).exit_code
time.sleep(1)
assert 0 == runner.invoke(cli, ["run", "--name", "r3", "cp", a, c]).exit_code
time.sleep(1)
assert 0 == renku_cli("run", "--name", "r4", "wc", c, stdout=d).exit_code
write_and_commit_file(repo, a, "new content")
result = runner.invoke(cli, ["update", "--dry-run"])
assert 0 == result.exit_code
assert "r1" in result.output
assert "r2" not in result.output
assert "r3" in result.output
assert "r4" in result.output