github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/integration-cli/docker_cli_build_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func (s *DockerSuite) TestBuildResourceConstraintsAreUsed(c *check.C) {
    14  	testRequires(c, CpuCfsQuota)
    15  	name := "testbuildresourceconstraints"
    16  
    17  	ctx, err := fakeContext(`
    18  	FROM hello-world:frozen
    19  	RUN ["/hello"]
    20  	`, map[string]string{})
    21  	if err != nil {
    22  		c.Fatal(err)
    23  	}
    24  
    25  	cmd := exec.Command(dockerBinary, "build", "--no-cache", "--rm=false", "--memory=64m", "--memory-swap=-1", "--cpuset-cpus=0", "--cpuset-mems=0", "--cpu-shares=100", "--cpu-quota=8000", "-t", name, ".")
    26  	cmd.Dir = ctx.Dir
    27  
    28  	out, _, err := runCommandWithOutput(cmd)
    29  	if err != nil {
    30  		c.Fatal(err, out)
    31  	}
    32  	out, _ = dockerCmd(c, "ps", "-lq")
    33  
    34  	cID := strings.TrimSpace(out)
    35  
    36  	type hostConfig struct {
    37  		Memory     int64
    38  		MemorySwap int64
    39  		CpusetCpus string
    40  		CpusetMems string
    41  		CpuShares  int64
    42  		CpuQuota   int64
    43  	}
    44  
    45  	cfg, err := inspectFieldJSON(cID, "HostConfig")
    46  	if err != nil {
    47  		c.Fatal(err)
    48  	}
    49  
    50  	var c1 hostConfig
    51  	if err := json.Unmarshal([]byte(cfg), &c1); err != nil {
    52  		c.Fatal(err, cfg)
    53  	}
    54  	if c1.Memory != 67108864 || c1.MemorySwap != -1 || c1.CpusetCpus != "0" || c1.CpusetMems != "0" || c1.CpuShares != 100 || c1.CpuQuota != 8000 {
    55  		c.Fatalf("resource constraints not set properly:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
    56  			c1.Memory, c1.MemorySwap, c1.CpusetCpus, c1.CpusetMems, c1.CpuShares, c1.CpuQuota)
    57  	}
    58  
    59  	// Make sure constraints aren't saved to image
    60  	_, _ = dockerCmd(c, "run", "--name=test", name)
    61  
    62  	cfg, err = inspectFieldJSON("test", "HostConfig")
    63  	if err != nil {
    64  		c.Fatal(err)
    65  	}
    66  	var c2 hostConfig
    67  	if err := json.Unmarshal([]byte(cfg), &c2); err != nil {
    68  		c.Fatal(err, cfg)
    69  	}
    70  	if c2.Memory == 67108864 || c2.MemorySwap == -1 || c2.CpusetCpus == "0" || c2.CpusetMems == "0" || c2.CpuShares == 100 || c2.CpuQuota == 8000 {
    71  		c.Fatalf("resource constraints leaked from build:\nMemory: %d, MemSwap: %d, CpusetCpus: %s, CpusetMems: %s, CpuShares: %d, CpuQuota: %d",
    72  			c2.Memory, c2.MemorySwap, c2.CpusetCpus, c2.CpusetMems, c2.CpuShares, c2.CpuQuota)
    73  	}
    74  
    75  }