Skip to content

Commit db8b161

Browse files
fix: updated to review and added tests
1 parent 7e2c1bd commit db8b161

File tree

4 files changed

+96
-16
lines changed

4 files changed

+96
-16
lines changed

src/taskgraph/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
Required("trust-domain"): str,
2929
Optional(
3030
"docker-image-kind",
31-
description="What kind of docker image to use for the task.",
32-
): [str],
31+
default="docker-image",
32+
description="Name of the docker image kind (default: docker-image)",
33+
): str,
3334
Required("task-priority"): optionally_keyed_by(
3435
"project",
3536
"level",

src/taskgraph/util/docker.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,33 +206,31 @@ def stream_context_tar(topsrcdir, context_dir, out_file, args=None):
206206

207207

208208
@functools.lru_cache(maxsize=None)
209-
def image_paths(graph_config=None):
209+
def image_paths(graph_config):
210210
"""Return a map of image name to paths containing their Dockerfile."""
211-
if graph_config:
212-
config = load_yaml(
213-
graph_config.docker_dir,
214-
"kinds",
215-
graph_config["docker_image_kind"],
216-
"kind.yml",
217-
)
218-
else:
219-
config = load_yaml("taskcluster", "kinds", "docker-image", "kind.yml")
211+
212+
config = load_yaml(
213+
graph_config.root_dir,
214+
"kinds",
215+
graph_config["docker-image-kind"],
216+
"kind.yml",
217+
)
220218

221219
return {
222-
k: os.path.join(IMAGE_DIR, v.get("definition", k))
220+
k: os.path.join(graph_config.docker_dir, v.get("definition", k))
223221
for k, v in config["tasks"].items()
224222
}
225223

226224

227-
def image_path(name, graph_config=None):
225+
def image_path(name, graph_config):
228226
paths = image_paths(graph_config)
229227
if name in paths:
230228
return paths[name]
231-
return os.path.join(IMAGE_DIR, name)
229+
return os.path.join(graph_config.docker_dir, name)
232230

233231

234232
@functools.lru_cache(maxsize=None)
235-
def parse_volumes(image, graph_config=None):
233+
def parse_volumes(image, graph_config):
236234
"""Parse VOLUME entries from a Dockerfile for an image."""
237235
volumes = set()
238236

taskcluster/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ index:
1414

1515
task-priority: low
1616

17+
docker-image-kind: docker-image
18+
1719
taskgraph:
1820
register: self_taskgraph:register
1921
decision-parameters: 'self_taskgraph.custom_parameters:decision_parameters'

test/test_util_docker.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import taskcluster_urls as liburls
1616

17+
from taskgraph.config import GraphConfig
1718
from taskgraph.util import docker
1819

1920
from .mockedopen import MockedOpen
@@ -269,3 +270,81 @@ def test_stream_context_tar(self):
269270
)
270271
finally:
271272
shutil.rmtree(tmp)
273+
274+
def test_image_paths_with_custom_kind(self):
275+
"""Test image_paths function with graph_config parameter."""
276+
temp_dir = tempfile.mkdtemp()
277+
try:
278+
# Create the kinds directory structure
279+
kinds_dir = os.path.join(temp_dir, "kinds", "docker-test-image")
280+
os.makedirs(kinds_dir)
281+
282+
# Create the kind.yml file with task definitions
283+
kind_yml_path = os.path.join(kinds_dir, "kind.yml")
284+
with open(kind_yml_path, "w") as f:
285+
f.write("tasks:\n")
286+
f.write(" test-image:\n")
287+
f.write(" definition: test-image\n")
288+
f.write(" another-image:\n")
289+
f.write(" definition: custom-path\n")
290+
291+
# Create graph config pointing to our test directory
292+
temp_graph_config = GraphConfig(
293+
{
294+
"trust-domain": "test-domain",
295+
"docker-image-kind": "docker-test-image",
296+
},
297+
temp_dir,
298+
)
299+
300+
paths = docker.image_paths(temp_graph_config)
301+
302+
expected_docker_dir = os.path.join(temp_graph_config.root_dir, "docker")
303+
self.assertEqual(
304+
paths["test-image"], os.path.join(expected_docker_dir, "test-image")
305+
)
306+
self.assertEqual(
307+
paths["another-image"], os.path.join(expected_docker_dir, "custom-path")
308+
)
309+
finally:
310+
shutil.rmtree(temp_dir)
311+
312+
def test_parse_volumes_with_graph_config(self):
313+
"""Test parse_volumes function with graph_config parameter."""
314+
temp_dir = tempfile.mkdtemp()
315+
try:
316+
kinds_dir = os.path.join(temp_dir, "kinds", "docker-test-image")
317+
os.makedirs(kinds_dir)
318+
319+
kind_yml_path = os.path.join(kinds_dir, "kind.yml")
320+
with open(kind_yml_path, "w") as f:
321+
f.write("tasks:\n")
322+
f.write(" test-image:\n")
323+
f.write(" definition: test-image\n")
324+
325+
docker_dir = os.path.join(temp_dir, "docker")
326+
os.makedirs(docker_dir)
327+
328+
image_dir = os.path.join(docker_dir, "test-image")
329+
os.makedirs(image_dir)
330+
331+
dockerfile_path = os.path.join(image_dir, "Dockerfile")
332+
with open(dockerfile_path, "wb") as fh:
333+
fh.write(b"VOLUME /foo/bar \n")
334+
fh.write(b"VOLUME /hello /world \n")
335+
336+
test_graph_config = GraphConfig(
337+
{
338+
"trust-domain": "test-domain",
339+
"docker-image-kind": "docker-test-image",
340+
},
341+
temp_dir,
342+
)
343+
344+
volumes = docker.parse_volumes("test-image", test_graph_config)
345+
346+
expected_volumes = {"/foo/bar", "/hello", "/world"}
347+
self.assertEqual(volumes, expected_volumes)
348+
349+
finally:
350+
shutil.rmtree(temp_dir)

0 commit comments

Comments
 (0)