Skip to content

Commit 6660f2a

Browse files
committed
Added unit test for the 'build_image' function
1 parent 298f9ce commit 6660f2a

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

tests/cli/test_build_images.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from murfey.cli.build_images import run
8+
from murfey.cli.build_images import build_image, run
99

1010
images = [f"test_image_{n}" for n in range(3)]
1111

@@ -19,6 +19,62 @@
1919
def_dry_run = False
2020

2121

22+
build_image_params_matrix: tuple[
23+
tuple[list[str], list[str], str, str, int, int, str, bool], ...
24+
] = (
25+
# Images | Tags | Source | Destination | User ID | Group ID | Group Name | Dry Run
26+
# Populated flags
27+
(
28+
images,
29+
["latest", "dev", "1.1.1"],
30+
"",
31+
"docker.io",
32+
12345,
33+
34567,
34+
"my-group",
35+
False,
36+
),
37+
(
38+
images,
39+
["latest", "dev", "1.1.1"],
40+
"",
41+
"docker.io",
42+
12345,
43+
34567,
44+
"my-group",
45+
True,
46+
),
47+
)
48+
49+
50+
@pytest.mark.parametrize("build_params", build_image_params_matrix)
51+
@patch("murfey.cli.build_images.Path.exists")
52+
@patch("murfey.cli.build_images.run_subprocess")
53+
def test_build_images(mock_subprocess, mock_exists, build_params):
54+
55+
# Unpack build params
56+
images, tags, src, dst, uid, gid, gname, dry_run = build_params
57+
58+
# Set the return values for 'Path().exists()' and 'run_subprocess'
59+
mock_exists.return_value = True
60+
mock_subprocess.return_value = 0
61+
62+
# Run the command
63+
built_image = build_image(
64+
image=images[0],
65+
tag=tags[0],
66+
source=src,
67+
destination=dst,
68+
user_id=uid,
69+
group_id=gid,
70+
group_name=gname,
71+
dry_run=dry_run,
72+
)
73+
74+
# Check that the image path generated is correct
75+
assert built_image == f"{dst}/{images[0]:{tags[0]}}"
76+
77+
2278
test_run_params_matrix: tuple[
2379
tuple[list[str], list[str], str, str, str, str, str, bool], ...
2480
] = (

0 commit comments

Comments
 (0)