Skip to content

Commit 073f0b3

Browse files
committed
fix(load-task): support tasks with indexed-images
1 parent d40eb17 commit 073f0b3

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

src/taskgraph/docker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from taskgraph.util import docker, json
2121
from taskgraph.util.taskcluster import (
22+
find_task_id,
2223
get_artifact_url,
2324
get_root_url,
2425
get_session,
@@ -306,7 +307,14 @@ def load_task(task_id, remove=True, user=None):
306307
else:
307308
task_cwd = "$TASK_WORKDIR"
308309

309-
image_task_id = image["taskId"]
310+
if image["type"] == "task-image":
311+
image_task_id = image["taskId"]
312+
elif image["type"] == "indexed-image":
313+
image_task_id = find_task_id(image["namespace"])
314+
else:
315+
print(f"Tasks with {image['type']} images are not supported!")
316+
return 1
317+
310318
image_tag = load_image_by_task_id(image_task_id)
311319

312320
# Set some env vars the worker would normally set.

test/test_docker.py

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,19 @@ def test_load_task_invalid_task(run_load_task):
139139
task = {}
140140
assert run_load_task(task)[0] == 1
141141

142-
task["tags"] = {"worker-implementation": "generic-worker"}
142+
task["payload"] = {}
143143
assert run_load_task(task)[0] == 1
144144

145-
task["tags"]["worker-implementation"] = "docker-worker"
146-
task["payload"] = {"command": []}
145+
task["payload"] = {"command": [], "image": {"type": "task-image"}}
147146
assert run_load_task(task)[0] == 1
148147

149148
task["payload"]["command"] = ["echo", "foo"]
150149
assert run_load_task(task)[0] == 1
151150

151+
task["payload"]["image"]["type"] = "foobar"
152+
task["payload"]["command"] = ["run-task", "--", "bash", "-c", "echo foo"]
153+
assert run_load_task(task)[0] == 1
154+
152155

153156
def test_load_task(run_load_task):
154157
image_task_id = "def"
@@ -161,7 +164,7 @@ def test_load_task(run_load_task):
161164
"--",
162165
"echo foo",
163166
],
164-
"image": {"taskId": image_task_id},
167+
"image": {"taskId": image_task_id, "type": "task-image"},
165168
},
166169
"tags": {"worker-implementation": "docker-worker"},
167170
}
@@ -211,9 +214,8 @@ def test_load_task_env_and_remove(run_load_task):
211214
"echo foo",
212215
],
213216
"env": {"FOO": "BAR", "BAZ": 1},
214-
"image": {"taskId": image_task_id},
217+
"image": {"taskId": image_task_id, "type": "task-image"},
215218
},
216-
"tags": {"worker-implementation": "docker-worker"},
217219
}
218220
ret, mocks = run_load_task(task, remove=True)
219221
assert ret == 0
@@ -222,3 +224,64 @@ def test_load_task_env_and_remove(run_load_task):
222224
actual = mocks["subprocess_run"].call_args[0][0]
223225
assert re.match(r"--env-file=/tmp/tmp.*", actual[4])
224226
assert actual[5] == "--rm"
227+
228+
229+
@pytest.mark.parametrize(
230+
"image",
231+
[
232+
pytest.param({"type": "task-image", "taskId": "xyz"}, id="task_image"),
233+
pytest.param(
234+
{"type": "indexed-image", "namespace": "project.some-namespace.latest"},
235+
id="indexed_image",
236+
),
237+
],
238+
)
239+
def test_load_task_with_different_image_types(
240+
mocker,
241+
run_load_task,
242+
image,
243+
):
244+
task_id = "abc"
245+
image_task_id = "xyz"
246+
task = {
247+
"payload": {
248+
"command": [
249+
"/usr/bin/run-task",
250+
"--task-cwd=/builds/worker",
251+
"--",
252+
"echo",
253+
"test",
254+
],
255+
"image": image,
256+
},
257+
"tags": {"worker-implementation": "docker-worker"},
258+
}
259+
260+
mocker.patch.object(docker, "find_task_id", return_value=image_task_id)
261+
262+
ret, mocks = run_load_task(task)
263+
assert ret == 0
264+
265+
mocks["get_task_definition"].assert_called_once_with(task_id)
266+
mocks["load_image_by_task_id"].assert_called_once_with(image_task_id)
267+
268+
269+
def test_load_task_with_unsupported_image_type(capsys, run_load_task):
270+
task = {
271+
"payload": {
272+
"command": [
273+
"/usr/bin/run-task",
274+
"--task-cwd=/builds/worker",
275+
"--",
276+
"echo foo",
277+
],
278+
"image": {"type": "unsupported-type", "path": "/some/path"},
279+
},
280+
"tags": {"worker-implementation": "docker-worker"},
281+
}
282+
283+
ret, mocks = run_load_task(task)
284+
assert ret == 1
285+
286+
out, _ = capsys.readouterr()
287+
assert "Tasks with unsupported-type images are not supported!" in out

0 commit comments

Comments
 (0)