github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/integration-cli/docker_cli_build_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/pkg/integration/checker"
    10  	"github.com/docker/docker/pkg/ulimit"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
    15  	testRequires(c, cpuCfsQuota)
    16  	name := "testbuildresourceconstraints"
    17  
    18  	ctx, err := fakeContext(`
    19  	FROM hello-world:frozen
    20  	RUN ["/hello"]
    21  	`, map[string]string{})
    22  	c.Assert(err, checker.IsNil)
    23  
    24  	dockerCmdInDir(c, ctx.Dir, "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", "-t", name, ".")
    25  
    26  	out, _ := dockerCmd(c, "ps", "-lq")
    27  	cID := strings.TrimSpace(out)
    28  
    29  	type hostConfig struct {
    30  		Memory     int64
    31  		MemorySwap int64
    32  		CpusetCpus string
    33  		CpusetMems string
    34  		CPUShares  int64
    35  		CPUQuota   int64
    36  		Ulimits    []*ulimit.Ulimit
    37  	}
    38  
    39  	cfg, err := inspectFieldJSON(cID, "HostConfig")
    40  	c.Assert(err, checker.IsNil)
    41  
    42  	var c1 hostConfig
    43  	err = json.Unmarshal([]byte(cfg), &c1)
    44  	c.Assert(err, checker.IsNil, check.Commentf(cfg))
    45  
    46  	c.Assert(c1.Memory, checker.Equals, int64(64*1024*1024), check.Commentf("resource constraints not set properly for Memory"))
    47  	c.Assert(c1.MemorySwap, checker.Equals, int64(-1), check.Commentf("resource constraints not set properly for MemorySwap"))
    48  	c.Assert(c1.CpusetCpus, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetCpus"))
    49  	c.Assert(c1.CpusetMems, checker.Equals, "0", check.Commentf("resource constraints not set properly for CpusetMems"))
    50  	c.Assert(c1.CPUShares, checker.Equals, int64(100), check.Commentf("resource constraints not set properly for CPUShares"))
    51  	c.Assert(c1.CPUQuota, checker.Equals, int64(8000), check.Commentf("resource constraints not set properly for CPUQuota"))
    52  	c.Assert(c1.Ulimits[0].Name, checker.Equals, "nofile", check.Commentf("resource constraints not set properly for Ulimits"))
    53  	c.Assert(c1.Ulimits[0].Hard, checker.Equals, int64(42), check.Commentf("resource constraints not set properly for Ulimits"))
    54  
    55  	// Make sure constraints aren't saved to image
    56  	dockerCmd(c, "run", "--name=test", name)
    57  
    58  	cfg, err = inspectFieldJSON("test", "HostConfig")
    59  	c.Assert(err, checker.IsNil)
    60  
    61  	var c2 hostConfig
    62  	err = json.Unmarshal([]byte(cfg), &c2)
    63  	c.Assert(err, checker.IsNil, check.Commentf(cfg))
    64  
    65  	c.Assert(c2.Memory, check.Not(checker.Equals), int64(64*1024*1024), check.Commentf("resource leaked from build for Memory"))
    66  	c.Assert(c2.MemorySwap, check.Not(checker.Equals), int64(-1), check.Commentf("resource leaked from build for MemorySwap"))
    67  	c.Assert(c2.CpusetCpus, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetCpus"))
    68  	c.Assert(c2.CpusetMems, check.Not(checker.Equals), "0", check.Commentf("resource leaked from build for CpusetMems"))
    69  	c.Assert(c2.CPUShares, check.Not(checker.Equals), int64(100), check.Commentf("resource leaked from build for CPUShares"))
    70  	c.Assert(c2.CPUQuota, check.Not(checker.Equals), int64(8000), check.Commentf("resource leaked from build for CPUQuota"))
    71  	c.Assert(c2.Ulimits, checker.IsNil, check.Commentf("resource leaked from build for Ulimits"))
    72  }