From fcb23060fc3eadef8d0f4a427f5a405fdc47ad27 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Dec 2025 14:15:33 +0100 Subject: [PATCH] integration-cli: rewrite some tests depending on intermediate images These tests used intermediate images (as produced by the classic builder) as part of the test. When using BuildKit, such images are not produced, and will only be in the build-cache. From the tests, it looks like the extra checks were not critical to verify the behavior, so let's simplify them to not depend on this. Signed-off-by: Sebastiaan van Stijn --- integration-cli/docker_cli_build_test.go | 67 ++++++++---------------- 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 749c2cb235..34535bfc53 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -4614,60 +4614,39 @@ func (s *DockerCLIBuildSuite) TestBuildBuildTimeArgDefinitionWithNoEnvInjection( } } +// TestBuildMultiStageArg verifies that build-args are scoped to the FROM +// they're defined in. Test for https://github.com/moby/moby/issues/31892 func (s *DockerCLIBuildSuite) TestBuildMultiStageArg(c *testing.T) { imgName := strings.ToLower(c.Name()) - dockerfile := `FROM busybox - ARG foo=abc - LABEL multifromtest=1 - RUN env > /out - FROM busybox - ARG bar=def - RUN env > /out` + const dockerfile = `FROM busybox +ARG stage_1_arg=AAAA +LABEL multifromtest=1 +RUN env > /out +FROM busybox +ARG stage_2_arg=BBBB +RUN env > /out +` - result := cli.BuildCmd(c, imgName, build.WithDockerfile(dockerfile)) - result.Assert(c, icmd.Success) + cli.BuildCmd(c, imgName, build.WithDockerfile(dockerfile)) - result = cli.DockerCmd(c, "images", "-q", "-f", "label=multifromtest=1") - result.Assert(c, icmd.Success) - - imgs := strings.Split(strings.TrimSpace(result.Stdout()), "\n") - assert.Assert(c, is.Len(imgs, 1), `only one image with "multifromtest" label is expected`) - - parentID := imgs[0] - - result = cli.DockerCmd(c, "run", "--rm", parentID, "cat", "/out") - assert.Assert(c, is.Contains(result.Stdout(), "foo=abc")) - result = cli.DockerCmd(c, "run", "--rm", imgName, "cat", "/out") - assert.Assert(c, !strings.Contains(result.Stdout(), "foo")) - assert.Assert(c, is.Contains(result.Stdout(), "bar=def")) + result := cli.DockerCmd(c, "run", "--rm", imgName, "cat", "/out") + assert.Check(c, !strings.Contains(result.Stdout(), "stage_1_arg"), "build arg leaked to second stage") + assert.Assert(c, is.Contains(result.Stdout(), "stage_2_arg=BBBB"), "build arg not applied to second stage") } func (s *DockerCLIBuildSuite) TestBuildMultiStageGlobalArg(c *testing.T) { imgName := strings.ToLower(c.Name()) - dockerfile := `ARG tag=nosuchtag - FROM busybox:${tag} - LABEL multifromtest2=1 - RUN env > /out - FROM busybox:${tag} - ARG tag - RUN env > /out` + const dockerfile = `ARG tag=nosuchtag +FROM busybox:${tag} +LABEL multifromtest2=1 +RUN env > /out +FROM busybox:${tag} +ARG tag +RUN env > /out` - result := cli.BuildCmd(c, imgName, - build.WithDockerfile(dockerfile), - cli.WithFlags("--build-arg", "tag=latest")) - result.Assert(c, icmd.Success) + cli.BuildCmd(c, imgName, build.WithDockerfile(dockerfile), cli.WithFlags("--build-arg", "tag=latest")) - result = cli.DockerCmd(c, "images", "-q", "-f", "label=multifromtest2=1") - result.Assert(c, icmd.Success) - - imgs := strings.Split(strings.TrimSpace(result.Stdout()), "\n") - assert.Assert(c, is.Len(imgs, 1), `only one image with "multifromtest" label is expected`) - - parentID := imgs[0] - - result = cli.DockerCmd(c, "run", "--rm", parentID, "cat", "/out") - assert.Assert(c, !strings.Contains(result.Stdout(), "tag")) - result = cli.DockerCmd(c, "run", "--rm", imgName, "cat", "/out") + result := cli.DockerCmd(c, "run", "--rm", imgName, "cat", "/out") assert.Assert(c, is.Contains(result.Stdout(), "tag=latest")) }