|
| 1 | +import grp |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from unittest.mock import call, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from murfey.cli.build_images import run |
| 9 | + |
| 10 | +test_run_params_matrix: tuple[ |
| 11 | + tuple[list[str], list[str], str, str, str, str, str, bool] |
| 12 | +] = ( |
| 13 | + # Images | Tags | Source | Destination | User ID | Group ID | Group Name | Dry Run |
| 14 | + # Default settings |
| 15 | + ([f"test_image_{n}" for n in range(3)], [], "", "", "", "", "", False), |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize("build_params", test_run_params_matrix) |
| 20 | +@patch("murfey.cli.build_images.cleanup") |
| 21 | +@patch("murfey.cli.build_images.push_images") |
| 22 | +@patch("murfey.cli.build_images.tag_image") |
| 23 | +@patch("murfey.cli.build_images.build_image") |
| 24 | +def test_run( |
| 25 | + mock_build, |
| 26 | + mock_tag, |
| 27 | + mock_push, |
| 28 | + mock_clean, |
| 29 | + build_params: tuple[list[str], list[str], str, str, str, str, str, bool], |
| 30 | +): |
| 31 | + """ |
| 32 | + Tests that the function is run with the expected arguments for a given |
| 33 | + combination of flags. |
| 34 | + """ |
| 35 | + |
| 36 | + # Unpack build params |
| 37 | + images, tags, src, dst, uid, gid, gname, dry_run = build_params |
| 38 | + |
| 39 | + # Set defaults of the various flags |
| 40 | + def_tags = ["latest"] |
| 41 | + def_src = "." |
| 42 | + def_dst = "localhost" |
| 43 | + def_uid = os.getuid() |
| 44 | + def_gid = os.getgid() |
| 45 | + def_gname = ( |
| 46 | + grp.getgrgid(os.getgid()).gr_name if hasattr(grp, "getgrgid") else "nogroup" |
| 47 | + ) |
| 48 | + def_dry_run = False |
| 49 | + |
| 50 | + # Set up the command based on what these values are |
| 51 | + build_cmd = [ |
| 52 | + "murfey.build_images", |
| 53 | + " ".join(images), |
| 54 | + ] |
| 55 | + |
| 56 | + # Iterate through flags and add them according to the command |
| 57 | + flags = ( |
| 58 | + # 'images' already include by default |
| 59 | + ("--tags", tags), |
| 60 | + ("--source", src), |
| 61 | + ("--destination", dst), |
| 62 | + ("--user-id", uid), |
| 63 | + ("--group-id", gid), |
| 64 | + ("--group-name", gname), |
| 65 | + ("--dry-run", dry_run), |
| 66 | + ) |
| 67 | + for flag, value in flags: |
| 68 | + if isinstance(value, list) and value: |
| 69 | + build_cmd.extend([flag, *value]) |
| 70 | + if isinstance(value, str) and value: |
| 71 | + build_cmd.extend([flag, value]) |
| 72 | + if isinstance(value, bool) and value: |
| 73 | + build_cmd.append(flag) |
| 74 | + |
| 75 | + # Assign it to the CLI to pass to the function |
| 76 | + sys.argv = build_cmd |
| 77 | + |
| 78 | + # Run the function with the command |
| 79 | + run() |
| 80 | + |
| 81 | + # Check that the functions were called with the correct flags |
| 82 | + assert mock_build.call_count == len(images) |
| 83 | + expected_calls = ( |
| 84 | + call( |
| 85 | + image=image, |
| 86 | + tag=tags[0] if tags else def_tags[0], |
| 87 | + source=src if src else def_src, |
| 88 | + destination=dst if dst else def_dst, |
| 89 | + user_id=uid if uid else def_uid, |
| 90 | + group_id=gid if gid else def_gid, |
| 91 | + groupd_name=gname if gname else def_gname, |
| 92 | + dry_run=dry_run if dry_run else def_dry_run, |
| 93 | + ) |
| 94 | + for image in images |
| 95 | + ) |
| 96 | + mock_build.assert_has_calls(expected_calls, any_order=True) |
0 commit comments