2021-08-23 09:14:53 -04:00
|
|
|
//go:build !windows
|
2015-05-29 14:22:21 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-12-28 15:15:34 -05:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2015-05-29 14:22:21 -04:00
|
|
|
"encoding/json"
|
2015-12-30 16:36:48 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-12-28 15:15:34 -05:00
|
|
|
"regexp"
|
2015-05-29 14:22:21 -04:00
|
|
|
"strings"
|
2017-08-21 18:40:56 -04:00
|
|
|
"syscall"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2017-08-21 18:50:40 -04:00
|
|
|
"time"
|
2017-08-21 18:40:56 -04:00
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2017-04-10 08:42:21 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
2019-08-05 10:37:47 -04:00
|
|
|
units "github.com/docker/go-units"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2015-05-29 14:22:21 -04:00
|
|
|
)
|
|
|
|
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIBuildSuite) TestBuildResourceConstraintsAreUsed(c *testing.T) {
|
2017-09-14 13:17:49 -04:00
|
|
|
testRequires(c, cpuCfsQuota)
|
2015-05-29 14:22:21 -04:00
|
|
|
name := "testbuildresourceconstraints"
|
2022-06-16 17:32:10 -04:00
|
|
|
buildLabel := "DockerCLIBuildSuite.TestBuildResourceConstraintsAreUsed"
|
2015-05-29 14:22:21 -04:00
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
ctx := fakecontext.New(c, "", fakecontext.WithDockerfile(`
|
2015-05-29 14:22:21 -04:00
|
|
|
FROM hello-world:frozen
|
|
|
|
RUN ["/hello"]
|
2017-04-10 08:42:21 -04:00
|
|
|
`))
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.Docker(
|
2017-09-14 13:17:49 -04:00
|
|
|
cli.Args("build", "--no-cache", "--rm=false", "--memory=64m", "--memory-swap=-1", "--cpuset-cpus=0", "--cpuset-mems=0", "--cpu-shares=100", "--cpu-quota=8000", "--ulimit", "nofile=42", "--label="+buildLabel, "-t", name, "."),
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.InDir(ctx.Dir),
|
|
|
|
).Assert(c, icmd.Success)
|
2015-05-29 14:22:21 -04:00
|
|
|
|
2017-09-14 13:17:49 -04:00
|
|
|
out := cli.DockerCmd(c, "ps", "-lq", "--filter", "label="+buildLabel).Combined()
|
2015-05-29 14:22:21 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
type hostConfig struct {
|
|
|
|
Memory int64
|
|
|
|
MemorySwap int64
|
|
|
|
CpusetCpus string
|
|
|
|
CpusetMems string
|
2015-07-22 08:59:24 -04:00
|
|
|
CPUShares int64
|
|
|
|
CPUQuota int64
|
2015-12-21 15:10:53 -05:00
|
|
|
Ulimits []*units.Ulimit
|
2015-05-29 14:22:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
cfg := inspectFieldJSON(c, cID, "HostConfig")
|
2015-05-29 14:22:21 -04:00
|
|
|
|
|
|
|
var c1 hostConfig
|
2017-03-27 11:12:48 -04:00
|
|
|
err := json.Unmarshal([]byte(cfg), &c1)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, err == nil, cfg)
|
2015-10-26 09:00:16 -04:00
|
|
|
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, c1.Memory, int64(64*1024*1024), "resource constraints not set properly for Memory")
|
|
|
|
assert.Equal(c, c1.MemorySwap, int64(-1), "resource constraints not set properly for MemorySwap")
|
|
|
|
assert.Equal(c, c1.CpusetCpus, "0", "resource constraints not set properly for CpusetCpus")
|
|
|
|
assert.Equal(c, c1.CpusetMems, "0", "resource constraints not set properly for CpusetMems")
|
|
|
|
assert.Equal(c, c1.CPUShares, int64(100), "resource constraints not set properly for CPUShares")
|
|
|
|
assert.Equal(c, c1.CPUQuota, int64(8000), "resource constraints not set properly for CPUQuota")
|
|
|
|
assert.Equal(c, c1.Ulimits[0].Name, "nofile", "resource constraints not set properly for Ulimits")
|
|
|
|
assert.Equal(c, c1.Ulimits[0].Hard, int64(42), "resource constraints not set properly for Ulimits")
|
2015-05-29 14:22:21 -04:00
|
|
|
|
|
|
|
// Make sure constraints aren't saved to image
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.DockerCmd(c, "run", "--name=test", name)
|
2015-05-29 14:22:21 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
cfg = inspectFieldJSON(c, "test", "HostConfig")
|
2015-07-22 22:26:06 -04:00
|
|
|
|
2015-10-26 09:00:16 -04:00
|
|
|
var c2 hostConfig
|
|
|
|
err = json.Unmarshal([]byte(cfg), &c2)
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, err == nil, cfg)
|
2019-09-09 17:05:55 -04:00
|
|
|
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, c2.Memory != int64(64*1024*1024), "resource leaked from build for Memory")
|
|
|
|
assert.Assert(c, c2.MemorySwap != int64(-1), "resource leaked from build for MemorySwap")
|
|
|
|
assert.Assert(c, c2.CpusetCpus != "0", "resource leaked from build for CpusetCpus")
|
|
|
|
assert.Assert(c, c2.CpusetMems != "0", "resource leaked from build for CpusetMems")
|
|
|
|
assert.Assert(c, c2.CPUShares != int64(100), "resource leaked from build for CPUShares")
|
|
|
|
assert.Assert(c, c2.CPUQuota != int64(8000), "resource leaked from build for CPUQuota")
|
|
|
|
assert.Assert(c, c2.Ulimits == nil, "resource leaked from build for Ulimits")
|
2015-05-29 14:22:21 -04:00
|
|
|
}
|
2015-12-30 16:36:48 -05:00
|
|
|
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIBuildSuite) TestBuildAddChangeOwnership(c *testing.T) {
|
2015-12-30 16:36:48 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
name := "testbuildaddown"
|
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
ctx := func() *fakecontext.Fake {
|
2015-12-30 16:36:48 -05:00
|
|
|
dockerfile := `
|
|
|
|
FROM busybox
|
|
|
|
ADD foo /bar/
|
|
|
|
RUN [ $(stat -c %U:%G "/bar") = 'root:root' ]
|
|
|
|
RUN [ $(stat -c %U:%G "/bar/foo") = 'root:root' ]
|
|
|
|
`
|
2021-08-24 06:10:50 -04:00
|
|
|
tmpDir, err := os.MkdirTemp("", "fake-context")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-12-30 16:36:48 -05:00
|
|
|
testFile, err := os.Create(filepath.Join(tmpDir, "foo"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("failed to create foo file: %v", err)
|
|
|
|
}
|
|
|
|
defer testFile.Close()
|
|
|
|
|
2017-01-05 06:38:34 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{"chown", "daemon:daemon", "foo"},
|
2017-01-05 13:08:24 -05:00
|
|
|
Dir: tmpDir,
|
2017-01-05 06:38:34 -05:00
|
|
|
}).Assert(c, icmd.Success)
|
2015-12-30 16:36:48 -05:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
2015-12-30 16:36:48 -05:00
|
|
|
c.Fatalf("failed to open destination dockerfile: %v", err)
|
|
|
|
}
|
2017-04-10 08:42:21 -04:00
|
|
|
return fakecontext.New(c, tmpDir)
|
2015-12-30 16:36:48 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
defer ctx.Close()
|
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithExternalBuildContext(ctx))
|
2015-12-28 15:15:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that an infinite sleep during a build is killed if the client disconnects.
|
|
|
|
// This test is fairly hairy because there are lots of ways to race.
|
|
|
|
// Strategy:
|
|
|
|
// * Monitor the output of docker events starting from before
|
|
|
|
// * Run a 1-year-long sleep from a docker build.
|
|
|
|
// * When docker events sees container start, close the "docker build" command
|
|
|
|
// * Wait for docker events to emit a dying event.
|
2018-05-15 20:11:25 -04:00
|
|
|
//
|
|
|
|
// TODO(buildkit): this test needs to be rewritten for buildkit.
|
|
|
|
// It has been manually tested positive. Confirmed issue: docker build output parsing.
|
|
|
|
// Potential issue: newEventObserver uses docker events, which is not hooked up to buildkit.
|
2022-06-16 17:32:10 -04:00
|
|
|
func (s *DockerCLIBuildSuite) TestBuildCancellationKillsSleep(c *testing.T) {
|
2018-05-15 20:11:25 -04:00
|
|
|
testRequires(c, DaemonIsLinux, TODOBuildkit)
|
2015-12-28 15:15:34 -05:00
|
|
|
name := "testbuildcancellation"
|
|
|
|
|
|
|
|
observer, err := newEventObserver(c)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-12-28 15:15:34 -05:00
|
|
|
err = observer.Start()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-12-28 15:15:34 -05:00
|
|
|
defer observer.Stop()
|
2015-12-30 16:36:48 -05:00
|
|
|
|
2015-12-28 15:15:34 -05:00
|
|
|
// (Note: one year, will never finish)
|
2017-04-10 08:42:21 -04:00
|
|
|
ctx := fakecontext.New(c, "", fakecontext.WithDockerfile("FROM busybox\nRUN sleep 31536000"))
|
2015-12-28 15:15:34 -05:00
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
buildCmd := exec.Command(dockerBinary, "build", "-t", name, ".")
|
|
|
|
buildCmd.Dir = ctx.Dir
|
|
|
|
|
|
|
|
stdoutBuild, err := buildCmd.StdoutPipe()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-01-17 21:08:31 -05:00
|
|
|
|
2015-12-28 15:15:34 -05:00
|
|
|
if err := buildCmd.Start(); err != nil {
|
|
|
|
c.Fatalf("failed to run build: %s", err)
|
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
// always clean up
|
|
|
|
defer func() {
|
|
|
|
buildCmd.Process.Kill()
|
|
|
|
buildCmd.Wait()
|
|
|
|
}()
|
2015-12-28 15:15:34 -05:00
|
|
|
|
|
|
|
matchCID := regexp.MustCompile("Running in (.+)")
|
|
|
|
scanner := bufio.NewScanner(stdoutBuild)
|
|
|
|
|
|
|
|
outputBuffer := new(bytes.Buffer)
|
|
|
|
var buildID string
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
outputBuffer.WriteString(line)
|
|
|
|
outputBuffer.WriteString("\n")
|
|
|
|
if matches := matchCID.FindStringSubmatch(line); len(matches) > 0 {
|
|
|
|
buildID = matches[1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildID == "" {
|
|
|
|
c.Fatalf("Unable to find build container id in build output:\n%s", outputBuffer.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
testActions := map[string]chan bool{
|
2016-02-18 16:10:29 -05:00
|
|
|
"start": make(chan bool, 1),
|
|
|
|
"die": make(chan bool, 1),
|
2015-12-28 15:15:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
matcher := matchEventLine(buildID, "container", testActions)
|
2016-01-20 17:36:39 -05:00
|
|
|
processor := processEventMatch(testActions)
|
|
|
|
go observer.Match(matcher, processor)
|
2015-12-28 15:15:34 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
observer.CheckEventError(c, buildID, "start", matcher)
|
|
|
|
case <-testActions["start"]:
|
|
|
|
// ignore, done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a kill to the `docker build` command.
|
|
|
|
// Causes the underlying build to be cancelled due to socket close.
|
|
|
|
if err := buildCmd.Process.Kill(); err != nil {
|
|
|
|
c.Fatalf("error killing build command: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the exit status of `docker build`, check it exited because killed.
|
2017-08-21 18:40:56 -04:00
|
|
|
if err := buildCmd.Wait(); err != nil && !isKilled(err) {
|
2015-12-28 15:15:34 -05:00
|
|
|
c.Fatalf("wait failed during build run: %T %s", err, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
observer.CheckEventError(c, buildID, "die", matcher)
|
|
|
|
case <-testActions["die"]:
|
|
|
|
// ignore, done
|
|
|
|
}
|
2015-12-30 16:36:48 -05:00
|
|
|
}
|
2017-08-21 18:40:56 -04:00
|
|
|
|
|
|
|
func isKilled(err error) bool {
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
status, ok := exitErr.Sys().(syscall.WaitStatus)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// status.ExitStatus() is required on Windows because it does not
|
|
|
|
// implement Signal() nor Signaled(). Just check it had a bad exit
|
|
|
|
// status could mean it was killed (and in tests we do kill)
|
|
|
|
return (status.Signaled() && status.Signal() == os.Kill) || status.ExitStatus() != 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|