Browse code

bring back tests about LXCConfig

Victor Vieux authored on 2013/11/16 04:09:29
Showing 2 changed files
... ...
@@ -147,95 +147,6 @@ func TestParseNetworkOptsUdp(t *testing.T) {
147 147
 	}
148 148
 }
149 149
 
150
-// FIXME: test that destroying a container actually removes its root directory
151
-
152
-/*
153
-func TestLXCConfig(t *testing.T) {
154
-	// Memory is allocated randomly for testing
155
-	rand.Seed(time.Now().UTC().UnixNano())
156
-	memMin := 33554432
157
-	memMax := 536870912
158
-	mem := memMin + rand.Intn(memMax-memMin)
159
-	// CPU shares as well
160
-	cpuMin := 100
161
-	cpuMax := 10000
162
-	cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
163
-	container, _, err := runtime.Create(&Config{
164
-		Image: GetTestImage(runtime).ID,
165
-		Cmd:   []string{"/bin/true"},
166
-
167
-		Hostname:  "foobar",
168
-		Memory:    int64(mem),
169
-		CpuShares: int64(cpu),
170
-	},
171
-		"",
172
-	)
173
-	if err != nil {
174
-		t.Fatal(err)
175
-	}
176
-	defer runtime.Destroy(container)
177
-	container.generateLXCConfig()
178
-	grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
179
-	grepFile(t, container.lxcConfigPath(),
180
-		fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
181
-	grepFile(t, container.lxcConfigPath(),
182
-		fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
183
-}
184
-
185
-
186
-func TestCustomLxcConfig(t *testing.T) {
187
-	runtime := mkRuntime(t)
188
-	defer nuke(runtime)
189
-	container, _, err := runtime.Create(&Config{
190
-		Image: GetTestImage(runtime).ID,
191
-		Cmd:   []string{"/bin/true"},
192
-
193
-		Hostname: "foobar",
194
-	},
195
-		"",
196
-	)
197
-	if err != nil {
198
-		t.Fatal(err)
199
-	}
200
-	defer runtime.Destroy(container)
201
-	container.hostConfig = &HostConfig{LxcConf: []KeyValuePair{
202
-		{
203
-			Key:   "lxc.utsname",
204
-			Value: "docker",
205
-		},
206
-		{
207
-			Key:   "lxc.cgroup.cpuset.cpus",
208
-			Value: "0,1",
209
-		},
210
-	}}
211
-
212
-	container.generateLXCConfig()
213
-	grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
214
-	grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
215
-}
216
-
217
-
218
-func grepFile(t *testing.T, path string, pattern string) {
219
-	f, err := os.Open(path)
220
-	if err != nil {
221
-		t.Fatal(err)
222
-	}
223
-	defer f.Close()
224
-	r := bufio.NewReader(f)
225
-	var (
226
-		line string
227
-	)
228
-	err = nil
229
-	for err == nil {
230
-		line, err = r.ReadString('\n')
231
-		if strings.Contains(line, pattern) == true {
232
-			return
233
-		}
234
-	}
235
-	t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
236
-}
237
-*/
238
-
239 150
 func TestGetFullName(t *testing.T) {
240 151
 	name, err := getFullName("testing")
241 152
 	if err != nil {
242 153
new file mode 100644
... ...
@@ -0,0 +1,102 @@
0
+package docker
1
+
2
+import (
3
+	"bufio"
4
+	"fmt"
5
+	"io/ioutil"
6
+	"math/rand"
7
+	"os"
8
+	"strings"
9
+	"testing"
10
+	"time"
11
+)
12
+
13
+func TestLXCConfig(t *testing.T) {
14
+	root, err := ioutil.TempDir("", "TestLXCConfig")
15
+	if err != nil {
16
+		t.Fatal(err)
17
+	}
18
+	defer os.RemoveAll(root)
19
+	// Memory is allocated randomly for testing
20
+	rand.Seed(time.Now().UTC().UnixNano())
21
+	memMin := 33554432
22
+	memMax := 536870912
23
+	mem := memMin + rand.Intn(memMax-memMin)
24
+	// CPU shares as well
25
+	cpuMin := 100
26
+	cpuMax := 10000
27
+	cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
28
+	container := &Container{
29
+		root: root,
30
+		Config: &Config{
31
+			Hostname:        "foobar",
32
+			Memory:          int64(mem),
33
+			CpuShares:       int64(cpu),
34
+			NetworkDisabled: true,
35
+		},
36
+		hostConfig: &HostConfig{
37
+			Privileged: false,
38
+		},
39
+	}
40
+	if err := container.generateLXCConfig(); err != nil {
41
+		t.Fatal(err)
42
+	}
43
+	grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
44
+	grepFile(t, container.lxcConfigPath(),
45
+		fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
46
+	grepFile(t, container.lxcConfigPath(),
47
+		fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
48
+}
49
+
50
+func TestCustomLxcConfig(t *testing.T) {
51
+	root, err := ioutil.TempDir("", "TestCustomLxcConfig")
52
+	if err != nil {
53
+		t.Fatal(err)
54
+	}
55
+	defer os.RemoveAll(root)
56
+	container := &Container{
57
+		root: root,
58
+		Config: &Config{
59
+			Hostname:        "foobar",
60
+			NetworkDisabled: true,
61
+		},
62
+		hostConfig: &HostConfig{
63
+			Privileged: false,
64
+			LxcConf: []KeyValuePair{
65
+				{
66
+					Key:   "lxc.utsname",
67
+					Value: "docker",
68
+				},
69
+				{
70
+					Key:   "lxc.cgroup.cpuset.cpus",
71
+					Value: "0,1",
72
+				},
73
+			},
74
+		},
75
+	}
76
+	if err := container.generateLXCConfig(); err != nil {
77
+		t.Fatal(err)
78
+	}
79
+	grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
80
+	grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
81
+}
82
+
83
+func grepFile(t *testing.T, path string, pattern string) {
84
+	f, err := os.Open(path)
85
+	if err != nil {
86
+		t.Fatal(err)
87
+	}
88
+	defer f.Close()
89
+	r := bufio.NewReader(f)
90
+	var (
91
+		line string
92
+	)
93
+	err = nil
94
+	for err == nil {
95
+		line, err = r.ReadString('\n')
96
+		if strings.Contains(line, pattern) == true {
97
+			return
98
+		}
99
+	}
100
+	t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
101
+}