Skip to content

Commit

Permalink
docker_image: The image_tags field must not be empty. (pantsbuild…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaos authored Oct 6, 2023
1 parent e155017 commit ebbaa81
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/python/pants/backend/docker/goals/package_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
from pants.engine.fs import CreateDigest, Digest, FileContent
from pants.engine.process import FallibleProcessResult, Process, ProcessExecutionFailure
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import Target, WrappedTarget, WrappedTargetRequest
from pants.engine.target import InvalidFieldException, Target, WrappedTarget, WrappedTargetRequest
from pants.engine.unions import UnionMembership, UnionRule
from pants.option.global_options import GlobalOptions, KeepSandboxes
from pants.util.strutil import bullet_list
from pants.util.strutil import bullet_list, softwrap
from pants.util.value_interpolation import InterpolationContext, InterpolationError

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -414,6 +414,16 @@ async def build_docker_image(
)
)
tags = tuple(tag.full_name for registry in image_refs for tag in registry.tags)
if not tags:
raise InvalidFieldException(
softwrap(
f"""
The `{DockerImageTagsField.alias}` field in target {field_set.address} must not be
empty, unless there is a custom plugin providing additional tags using the
`DockerImageTagsRequest` union type.
"""
)
)

# Mix the upstream image ids into the env to ensure that Pants invalidates this
# image-building process correctly when an upstream image changes, even though the
Expand Down
47 changes: 40 additions & 7 deletions src/python/pants/backend/docker/goals/package_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,18 +1846,40 @@ def test_image_ref_formatting(test: ImageRefTest) -> None:
assert tuple(image_refs) == test.expect_refs


def test_docker_image_tags_from_plugin_hook(rule_runner: RuleRunner) -> None:
rule_runner.write_files({"docker/test/BUILD": 'docker_image(name="plugin")'})
@pytest.mark.parametrize(
"BUILD, plugin_tags, tag_flags",
[
(
'docker_image(name="plugin")',
("1.2.3",),
(
"--tag",
"plugin:latest",
"--tag",
"plugin:1.2.3",
),
),
(
'docker_image(name="plugin", image_tags=[])',
("1.2.3",),
(
"--tag",
"plugin:1.2.3",
),
),
],
)
def test_docker_image_tags_from_plugin_hook(
rule_runner: RuleRunner, BUILD: str, plugin_tags: tuple[str, ...], tag_flags: tuple[str, ...]
) -> None:
rule_runner.write_files({"docker/test/BUILD": BUILD})

def check_docker_proc(process: Process):
assert process.argv == (
"/dummy/docker",
"build",
"--pull=False",
"--tag",
"plugin:latest",
"--tag",
"plugin:1.2.3",
*tag_flags,
"--file",
"docker/test/Dockerfile",
".",
Expand All @@ -1867,10 +1889,21 @@ def check_docker_proc(process: Process):
rule_runner,
Address("docker/test", target_name="plugin"),
process_assertions=check_docker_proc,
plugin_tags=("1.2.3",),
plugin_tags=plugin_tags,
)


def test_docker_image_tags_defined(rule_runner: RuleRunner) -> None:
rule_runner.write_files({"docker/test/BUILD": 'docker_image(name="no-tags", image_tags=[])'})

err = "The `image_tags` field in target docker/test:no-tags must not be empty, unless"
with pytest.raises(InvalidFieldException, match=err):
assert_build(
rule_runner,
Address("docker/test", target_name="no-tags"),
)


def test_docker_info_serialize() -> None:
image_id = "abc123"
# image refs with unique strings (i.e. not actual templates/names etc.), to make sure they're
Expand Down

0 comments on commit ebbaa81

Please sign in to comment.