From c06d903edd7b16588e68680a989b73e27d990871 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 15 Nov 2013 11:09:29 -0800 Subject: [PATCH] bring back tests about LXCConfig --- container_unit_test.go | 89 --------------------------------- lxc_template_unit_test.go | 102 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 89 deletions(-) create mode 100644 lxc_template_unit_test.go diff --git a/container_unit_test.go b/container_unit_test.go index 679f25eee7..679ff57e73 100644 --- a/container_unit_test.go +++ b/container_unit_test.go @@ -147,95 +147,6 @@ func TestParseNetworkOptsUdp(t *testing.T) { } } -// FIXME: test that destroying a container actually removes its root directory - -/* -func TestLXCConfig(t *testing.T) { - // Memory is allocated randomly for testing - rand.Seed(time.Now().UTC().UnixNano()) - memMin := 33554432 - memMax := 536870912 - mem := memMin + rand.Intn(memMax-memMin) - // CPU shares as well - cpuMin := 100 - cpuMax := 10000 - cpu := cpuMin + rand.Intn(cpuMax-cpuMin) - container, _, err := runtime.Create(&Config{ - Image: GetTestImage(runtime).ID, - Cmd: []string{"/bin/true"}, - - Hostname: "foobar", - Memory: int64(mem), - CpuShares: int64(cpu), - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer runtime.Destroy(container) - container.generateLXCConfig() - grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar") - grepFile(t, container.lxcConfigPath(), - fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem)) - grepFile(t, container.lxcConfigPath(), - fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2)) -} - - -func TestCustomLxcConfig(t *testing.T) { - runtime := mkRuntime(t) - defer nuke(runtime) - container, _, err := runtime.Create(&Config{ - Image: GetTestImage(runtime).ID, - Cmd: []string{"/bin/true"}, - - Hostname: "foobar", - }, - "", - ) - if err != nil { - t.Fatal(err) - } - defer runtime.Destroy(container) - container.hostConfig = &HostConfig{LxcConf: []KeyValuePair{ - { - Key: "lxc.utsname", - Value: "docker", - }, - { - Key: "lxc.cgroup.cpuset.cpus", - Value: "0,1", - }, - }} - - container.generateLXCConfig() - grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker") - grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1") -} - - -func grepFile(t *testing.T, path string, pattern string) { - f, err := os.Open(path) - if err != nil { - t.Fatal(err) - } - defer f.Close() - r := bufio.NewReader(f) - var ( - line string - ) - err = nil - for err == nil { - line, err = r.ReadString('\n') - if strings.Contains(line, pattern) == true { - return - } - } - t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path) -} -*/ - func TestGetFullName(t *testing.T) { name, err := getFullName("testing") if err != nil { diff --git a/lxc_template_unit_test.go b/lxc_template_unit_test.go new file mode 100644 index 0000000000..ce5af1d321 --- /dev/null +++ b/lxc_template_unit_test.go @@ -0,0 +1,102 @@ +package docker + +import ( + "bufio" + "fmt" + "io/ioutil" + "math/rand" + "os" + "strings" + "testing" + "time" +) + +func TestLXCConfig(t *testing.T) { + root, err := ioutil.TempDir("", "TestLXCConfig") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(root) + // Memory is allocated randomly for testing + rand.Seed(time.Now().UTC().UnixNano()) + memMin := 33554432 + memMax := 536870912 + mem := memMin + rand.Intn(memMax-memMin) + // CPU shares as well + cpuMin := 100 + cpuMax := 10000 + cpu := cpuMin + rand.Intn(cpuMax-cpuMin) + container := &Container{ + root: root, + Config: &Config{ + Hostname: "foobar", + Memory: int64(mem), + CpuShares: int64(cpu), + NetworkDisabled: true, + }, + hostConfig: &HostConfig{ + Privileged: false, + }, + } + if err := container.generateLXCConfig(); err != nil { + t.Fatal(err) + } + grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar") + grepFile(t, container.lxcConfigPath(), + fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem)) + grepFile(t, container.lxcConfigPath(), + fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2)) +} + +func TestCustomLxcConfig(t *testing.T) { + root, err := ioutil.TempDir("", "TestCustomLxcConfig") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(root) + container := &Container{ + root: root, + Config: &Config{ + Hostname: "foobar", + NetworkDisabled: true, + }, + hostConfig: &HostConfig{ + Privileged: false, + LxcConf: []KeyValuePair{ + { + Key: "lxc.utsname", + Value: "docker", + }, + { + Key: "lxc.cgroup.cpuset.cpus", + Value: "0,1", + }, + }, + }, + } + if err := container.generateLXCConfig(); err != nil { + t.Fatal(err) + } + grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker") + grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1") +} + +func grepFile(t *testing.T, path string, pattern string) { + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer f.Close() + r := bufio.NewReader(f) + var ( + line string + ) + err = nil + for err == nil { + line, err = r.ReadString('\n') + if strings.Contains(line, pattern) == true { + return + } + } + t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path) +}