Repository: moby/moby
File: integration-cli/docker_cli_build_test.go (Go)

Code before:
	assert.Assert(c, is.Contains(result.Combined(), "[Warning]"))
	assert.Assert(c, is.Contains(result.Combined(), "[baz] were not consumed"))
	result = cli.DockerCmd(c, "run", "--rm", imgName, "cat", "/out")
	assert.Assert(c, !strings.Contains(result.Stdout(), "bar"))
	assert.Assert(c, !strings.Contains(result.Stdout(), "baz"))
}

func (s *DockerCLIBuildSuite) TestBuildNoNamedVolume(c *testing.T) {
	imgName := strings.ToLower(c.Name())
	volName := "testname:/foo"

	if testEnv.DaemonInfo.OSType == "windows" {
		volName = "testname:C:\\foo"
	}
	cli.DockerCmd(c, "run", "-v", volName, "busybox", "sh", "-c", "touch /foo/oops")

	// Named volumes are not supported in VOLUME, so the `<volume-name>:<path>`
	// should be used as-is and to be considered a path inside the image.
	dockerFile := `FROM busybox
	VOLUME ` + volName + `
	RUN ls /foo/oops
	`

	cli.Docker(cli.Args("build", "-t", imgName), build.WithDockerfile(dockerFile)).Assert(c, icmd.Expected{
		ExitCode: 1,
		Out:      `ls: /foo/oops: No such file or directory`,
	})
}

func (s *DockerCLIBuildSuite) TestBuildTagEvent(c *testing.T) {
	imgName := strings.ToLower(c.Name())
	since := daemonUnixTime(c)

	dockerFile := `FROM busybox
	RUN echo events
	`
	cli.BuildCmd(c, imgName, build.WithDockerfile(dockerFile))

	until := daemonUnixTime(c)
	out := cli.DockerCmd(c, "events", "--since", since, "--until", until, "--filter", "type=image").Stdout()
	events := strings.Split(strings.TrimSpace(out), "\n")
	actions := eventActionsByIDAndType(c, events, imgName+":latest", "image")
	var foundTag bool
	for _, a := range actions {
		if a == "tag" {
			foundTag = true
			break
		}
	}

	assert.Assert(c, foundTag, "No tag event found:\n%s", out)

Review comment (bug):
Ah, fun; looks like Windows _does_ some validation, but Linux is more relaxed on these;

```console
=== Failed
=== FAIL: integration-cli TestDockerCLIBuildSuite/TestBuildNoNamedVolume (4.40s)
    docker_cli_build_test.go:4688: assertion failed: 
        Command:  D:\a\moby\moby\out\docker.exe build -t testdockerclibuildsuite/testbuildnonamedvolume -
        ExitCode: 1
        Error:    exit status 1
        Stdout:   Sending build context to Docker daemon  2.048kB

        Step 1/3 : FROM busybox
         ---> 894a80b296d9
        Step 2/3 : VOLUME testname:C:\foo
        
        Stderr:   Unrecognised volume spec: invalid volume specification: 'testname:C:foo'
        
        
        Failures:
        Expected stdout to contain "ls: /foo/oops: No such file or directory"
```

Code after:
	assert.Assert(c, is.Contains(result.Combined(), "[Warning]"))
	assert.Assert(c, is.Contains(result.Combined(), "[baz] were not consumed"))
	result = cli.DockerCmd(c, "run", "--rm", imgName, "cat", "/out")
	assert.Assert(c, !strings.Contains(result.Stdout(), "bar"))
	assert.Assert(c, !strings.Contains(result.Stdout(), "baz"))
}

func (s *DockerCLIBuildSuite) TestBuildNoNamedVolume(c *testing.T) {
	imgName := strings.ToLower(c.Name())
	volName := "testname:/foo"
	expError := `ls: /foo/oops: No such file or directory`

	if testEnv.DaemonInfo.OSType == "windows" {
		volName = "testname:C:\\foo"
		expError = `invalid volume specification`
	}
	cli.DockerCmd(c, "run", "-v", volName, "busybox", "sh", "-c", "touch /foo/oops")

	// Named volumes are not supported in VOLUME, so the `<volume-name>:<path>`
	// should be used as-is and to be considered a path inside the image.
	dockerFile := `FROM busybox
	VOLUME ` + volName + `
	RUN ls /foo/oops
	`

	res := cli.Docker(cli.Args("build", "-t", imgName), build.WithDockerfile(dockerFile)).Assert(c, icmd.Expected{
		ExitCode: 1,
	})
	assert.Check(c, is.Contains(res.Combined(), expError))
}

func (s *DockerCLIBuildSuite) TestBuildTagEvent(c *testing.T) {
	imgName := strings.ToLower(c.Name())
	since := daemonUnixTime(c)

	dockerFile := `FROM busybox
	RUN echo events
	`
	cli.BuildCmd(c, imgName, build.WithDockerfile(dockerFile))

	until := daemonUnixTime(c)
	out := cli.DockerCmd(c, "events", "--since", since, "--until", until, "--filter", "type=image").Stdout()
	events := strings.Split(strings.TrimSpace(out), "\n")
	actions := eventActionsByIDAndType(c, events, imgName+":latest", "image")
	var foundTag bool
	for _, a := range actions {
		if a == "tag" {
			foundTag = true
			break
		}
	}