Skip to content

Commit

Permalink
Fix docker GetSpec to include image, labels, and env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
tallclair committed Apr 15, 2016
1 parent dc6415a commit 9790a0d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fi
sudo -v || exit 1

echo ">> starting cAdvisor locally"
sudo ./cadvisor &
sudo ./cadvisor --docker_env_metadata_whitelist=TEST_VAR &

readonly TIMEOUT=120 # Timeout to wait for cAdvisor, in seconds.
START=$(date +%s)
Expand Down
8 changes: 7 additions & 1 deletion container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,13 @@ func (self *dockerContainerHandler) needNet() bool {

func (self *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) {
hasFilesystem := !self.ignoreMetrics.Has(container.DiskUsageMetrics)
return common.GetSpec(self.cgroupPaths, self.machineInfoFactory, self.needNet(), hasFilesystem)
spec, err := common.GetSpec(self.cgroupPaths, self.machineInfoFactory, self.needNet(), hasFilesystem)

spec.Labels = self.labels
spec.Envs = self.envs
spec.Image = self.image

return spec, err
}

func (self *dockerContainerHandler) getFsStats(stats *info.ContainerStats) error {
Expand Down
2 changes: 1 addition & 1 deletion integration/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func PushAndRunTests(host, testDir string) error {
portStr := strconv.Itoa(*port)
errChan := make(chan error)
go func() {
err = RunSshCommand("ssh", host, "--", fmt.Sprintf("sudo %s --port %s --logtostderr &> %s/log.txt", path.Join(testDir, cadvisorBinary), portStr, testDir))
err = RunSshCommand("ssh", host, "--", fmt.Sprintf("sudo %s --port %s --logtostderr --docker_env_metadata_whitelist=TEST_VAR &> %s/log.txt", path.Join(testDir, cadvisorBinary), portStr, testDir))
if err != nil {
errChan <- fmt.Errorf("error running cAdvisor: %v", err)
}
Expand Down
26 changes: 19 additions & 7 deletions integration/tests/api/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,27 @@ func TestDockerContainerSpec(t *testing.T) {
fm := framework.New(t)
defer fm.Cleanup()

cpuShares := uint64(2048)
cpuMask := "0"
memoryLimit := uint64(1 << 30) // 1GB
var (
cpuShares = uint64(2048)
cpuMask = "0"
memoryLimit = uint64(1 << 30) // 1GB
image = "kubernetes/pause"
env = map[string]string{"test_var": "FOO"}
labels = map[string]string{"bar": "baz"}
)

cpusetArg := "--cpuset"
if getDockerMinorVersion(fm) >= 10 {
cpusetArg = "--cpuset-cpus"
}
containerId := fm.Docker().Run(framework.DockerRunArgs{
Image: "kubernetes/pause",
Image: image,
Args: []string{
"--cpu-shares", strconv.FormatUint(cpuShares, 10),
cpusetArg, cpuMask,
"--memory", strconv.FormatUint(memoryLimit, 10),
"--env", "TEST_VAR=FOO",
"--label", "bar=baz",
},
})

Expand All @@ -213,12 +221,16 @@ func TestDockerContainerSpec(t *testing.T) {
assert := assert.New(t)

assert.True(containerInfo.Spec.HasCpu, "CPU should be isolated")
assert.Equal(containerInfo.Spec.Cpu.Limit, cpuShares, "Container should have %d shares, has %d", cpuShares, containerInfo.Spec.Cpu.Limit)
assert.Equal(containerInfo.Spec.Cpu.Mask, cpuMask, "Cpu mask should be %q, but is %q", cpuMask, containerInfo.Spec.Cpu.Mask)
assert.Equal(cpuShares, containerInfo.Spec.Cpu.Limit, "Container should have %d shares, has %d", cpuShares, containerInfo.Spec.Cpu.Limit)
assert.Equal(cpuMask, containerInfo.Spec.Cpu.Mask, "Cpu mask should be %q, but is %q", cpuMask, containerInfo.Spec.Cpu.Mask)
assert.True(containerInfo.Spec.HasMemory, "Memory should be isolated")
assert.Equal(containerInfo.Spec.Memory.Limit, memoryLimit, "Container should have memory limit of %d, has %d", memoryLimit, containerInfo.Spec.Memory.Limit)
assert.Equal(memoryLimit, containerInfo.Spec.Memory.Limit, "Container should have memory limit of %d, has %d", memoryLimit, containerInfo.Spec.Memory.Limit)
assert.True(containerInfo.Spec.HasNetwork, "Network should be isolated")
assert.True(containerInfo.Spec.HasDiskIo, "Blkio should be isolated")

assert.Equal(image, containerInfo.Spec.Image, "Spec should include container image")
assert.Equal(env, containerInfo.Spec.Envs, "Spec should include environment variables")
assert.Equal(labels, containerInfo.Spec.Labels, "Spec should include labels")
}

// Check the CPU ContainerStats.
Expand Down

0 comments on commit 9790a0d

Please sign in to comment.