This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
garden_acceptance_test.go
354 lines (290 loc) · 11.6 KB
/
garden_acceptance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package garden_acceptance_test
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/cloudfoundry-incubator/garden"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("Garden Acceptance Tests", func() {
Describe("a container", func() {
var container garden.Container
It("can be run with an (essentially) empty rootfs (#91423716)", func() {
container := createContainer(gardenClient, garden.ContainerSpec{RootFSPath: "/var/vcap/packages/rootfs/empty"})
buffer := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{User: "root", Path: "/hello"}, recordedProcessIO(buffer))
Ω(err).ShouldNot(HaveOccurred())
Ω(process.Wait()).Should(Equal(0))
Ω(buffer).Should(gbytes.Say("hello"))
})
Context("that's privileged", func() {
BeforeEach(func() {
container = createContainer(gardenClient, garden.ContainerSpec{Privileged: true})
})
It("can set rlimits when running processes", func() {
var nofile uint64 = 1234
output := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", "ulimit -n"},
Limits: garden.ResourceLimits{Nofile: &nofile},
}, recordedProcessIO(output))
Ω(err).ShouldNot(HaveOccurred())
Eventually(output).Should(gbytes.Say("1234"))
Ω(process.Wait()).Should(Equal(0))
})
})
Context("with initial properties", func() {
BeforeEach(func() {
container = createContainer(gardenClient, garden.ContainerSpec{
Properties: garden.Properties{"foo": "bar"},
})
})
It("can CRUD properties", func() {
value, err := container.Property("foo")
Ω(err).ShouldNot(HaveOccurred())
Ω(value).Should(Equal("bar"))
err = container.SetProperty("foo", "baz")
Ω(err).ShouldNot(HaveOccurred())
value, err = container.Property("foo")
Ω(err).ShouldNot(HaveOccurred())
Ω(value).Should(Equal("baz"))
err = container.SetProperty("fiz", "buz")
Ω(err).ShouldNot(HaveOccurred())
err = container.RemoveProperty("foo")
Ω(err).ShouldNot(HaveOccurred())
_, err = container.Property("foo")
Ω(err).Should(MatchError("property does not exist: foo"))
_, err = container.Property("bar")
Ω(err).Should(MatchError("property does not exist: bar"))
properties, err := container.Properties()
Ω(err).ShouldNot(HaveOccurred())
Ω(properties).Should(Equal(garden.Properties{"fiz": "buz"}))
})
It("can filter containers by property", func() {
createContainer(gardenClient, garden.ContainerSpec{
Properties: garden.Properties{"foo": "othercontainer"},
})
createContainer(gardenClient, garden.ContainerSpec{})
containers, err := gardenClient.Containers(map[string]string{"foo": "bar"})
Ω(err).ShouldNot(HaveOccurred())
Ω(containers).Should(ConsistOf(container))
})
})
Context("without initial properties", func() {
BeforeEach(func() {
container = createContainer(gardenClient, garden.ContainerSpec{})
})
It("can set a property (#87599106)", func() {
err := container.SetProperty("foo", "bar")
Ω(err).ShouldNot(HaveOccurred())
value, err := container.Property("foo")
Ω(err).ShouldNot(HaveOccurred())
Ω(value).Should(Equal("bar"))
})
})
Context("that's unprivileged", func() {
BeforeEach(func() {
container = createContainer(gardenClient, garden.ContainerSpec{Privileged: false})
})
PIt("allows containers to be destroyed when wshd isn't running", func() {
info, _ := container.Info()
pidFile, err := os.Open(filepath.Join(info.ContainerPath, "run", "wshd.pid"))
Ω(err).ShouldNot(HaveOccurred())
var pid int
_, err = fmt.Fscanf(pidFile, "%d", &pid)
Ω(err).ShouldNot(HaveOccurred())
_, _, err = runCommand("sudo kill -9 " + strconv.Itoa(pid))
Ω(err).ShouldNot(HaveOccurred())
err = gardenClient.Destroy(container.Handle())
Ω(err).ShouldNot(HaveOccurred())
})
It("can send TERM and KILL signals to processes (#83231270)", func() {
buffer := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `
trap 'echo "TERM received"' TERM
echo trapping
while true; do echo waiting; sleep 1; done
`},
}, recordedProcessIO(buffer))
Ω(err).ShouldNot(HaveOccurred())
Eventually(buffer, "3s").Should(gbytes.Say("trapping"), "Process didn't report trapping")
Ω(process.Signal(garden.SignalTerminate)).Should(Succeed())
Eventually(buffer, "3s").Should(gbytes.Say("TERM received"), "Process did not receive TERM")
Eventually(buffer, "3s").Should(gbytes.Say("waiting"), "Process isn't still running")
Ω(process.Signal(garden.SignalKill)).Should(Succeed())
Ω(process.Wait()).Should(Equal(255))
})
It("avoids a TERM race condition (#89972162)", func() {
for i := 0; i < 50; i++ {
process, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `while true; do echo -n "x"; sleep 1; done`},
}, silentProcessIO)
Ω(err).ShouldNot(HaveOccurred())
Ω(process.Signal(garden.SignalKill)).Should(Succeed())
Ω(process.Wait()).Should(Equal(255))
}
})
It("allows the process to catch SIGCHLD (#85801952)", func() {
buffer := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", `
trap 'echo "SIGCHLD received"' CHLD
$(ls / >/dev/null 2>&1);
while true; do sleep 1; echo waiting; done
`},
}, recordedProcessIO(buffer))
Ω(err).ShouldNot(HaveOccurred())
Eventually(buffer, "2s").Should(gbytes.Say("SIGCHLD received"))
Ω(process.Signal(garden.SignalKill)).Should(Succeed(), "Process being killed")
process.Wait()
})
It("can run processes as root", func() {
buffer := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{User: "root", Path: "whoami"}, recordedProcessIO(buffer))
Ω(err).ShouldNot(HaveOccurred())
Ω(process.Wait()).Should(Equal(0))
Ω(buffer.Contents()).Should(ContainSubstring("root"))
})
It("can run processes with rlimits", func() {
var nofile uint64 = 1234
output := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", "ulimit -n"},
Limits: garden.ResourceLimits{Nofile: &nofile},
}, recordedProcessIO(output))
Ω(err).ShouldNot(HaveOccurred())
Eventually(output).Should(gbytes.Say("1234"))
Ω(process.Wait()).Should(Equal(0))
})
})
})
Describe("iodaemon", func() {
It("supports a timeout when the process fails to link (#77842604)", func() {
iodaemon := "/home/vagrant/go/src/github.com/cloudfoundry-incubator/garden-linux/old/linux_backend/skeleton/bin/iodaemon"
stdout, _, err := runCommand("timeout 3s " + iodaemon + " -timeout 1s spawn /tmp/socketPath bash -c cat <&0; echo $?")
Ω(err).ShouldNot(HaveOccurred())
Ω(stdout).NotTo(ContainSubstring("124"), "124 means `timeout` timed out.")
})
})
It("cleans up after running processes (#89969450)", func() {
container := createContainer(gardenClient, garden.ContainerSpec{})
for i := 0; i < 10; i++ {
_, err := container.Run(lsProcessSpec, silentProcessIO)
Ω(err).ShouldNot(HaveOccurred())
}
info, err := container.Info()
Ω(err).ShouldNot(HaveOccurred())
processesPath := info.ContainerPath + "/processes"
Eventually(func() string {
_, stderr, _ := runCommand("cd " + processesPath + " && ls *.sock")
return stderr
}).Should(ContainSubstring("No such file or directory"))
})
Describe("Destroy", func() {
It("fails when attempting to delete a container twice (#76616270)", func() {
container := createContainer(gardenClient, garden.ContainerSpec{})
var errors = make(chan error)
go func() { errors <- gardenClient.Destroy(container.Handle()) }()
go func() { errors <- gardenClient.Destroy(container.Handle()) }()
results := []error{<-errors, <-errors}
Ω(results).Should(ConsistOf(BeNil(), HaveOccurred()))
})
It("fails when attempting to delete a non-existant container (#86044470)", func() {
err := gardenClient.Destroy("asdf")
Ω(err).Should(MatchError(garden.ContainerNotFoundError{Handle: "asdf"}))
})
PIt("does not leak network namespaces (Bug #91423716)", func() {
container := createContainer(gardenClient, garden.ContainerSpec{})
info, err := container.Info()
Ω(err).ShouldNot(HaveOccurred())
pidFile, err := os.Open(filepath.Join(info.ContainerPath, "run", "wshd.pid"))
Ω(err).ShouldNot(HaveOccurred())
var pid int
_, err = fmt.Fscanf(pidFile, "%d", &pid)
Ω(err).ShouldNot(HaveOccurred())
err = gardenClient.Destroy(container.Handle())
Ω(err).ShouldNot(HaveOccurred())
stdout, _, err := runCommand("ip netns list")
Ω(err).ShouldNot(HaveOccurred())
Ω(stdout).ShouldNot(ContainSubstring(strconv.Itoa(pid)))
})
})
It("supports setting environment variables on the container (Diego: #77303456, Garden: #96893340)", func() {
container := createContainer(gardenClient, garden.ContainerSpec{
Env: []string{
"ROOT_ENV=A",
"OVERWRITTEN_ENV=B",
"HOME=/nowhere",
"PASSWORD=;$*@='\"$(pwd)!!",
},
})
buffer := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{
User: "root",
Path: "sh",
Args: []string{"-c", "printenv"},
Env: []string{
"OVERWRITTEN_ENV=C",
},
}, recordedProcessIO(buffer))
Ω(err).ShouldNot(HaveOccurred())
Ω(process.Wait()).Should(Equal(0))
Ω(buffer.Contents()).Should(ContainSubstring("OVERWRITTEN_ENV=C"))
Ω(buffer.Contents()).ShouldNot(ContainSubstring("OVERWRITTEN_ENV=B"))
Ω(buffer.Contents()).Should(ContainSubstring("HOME=/nowhere"))
Ω(buffer.Contents()).ShouldNot(ContainSubstring("HOME=/home/root"))
Ω(buffer.Contents()).Should(ContainSubstring("ROOT_ENV=A"))
Ω(buffer.Contents()).Should(ContainSubstring("PASSWORD=;$*@='\"$(pwd)!!"))
})
Describe("Bugs around the container lifecycle (#77768828)", func() {
It("supports deleting a container after an errant delete", func() {
handle := fmt.Sprintf("%d", time.Now().UnixNano())
err := gardenClient.Destroy(handle)
Ω(err).Should(HaveOccurred())
_, err = gardenClient.Create(garden.ContainerSpec{Handle: handle})
Ω(err).ShouldNot(HaveOccurred())
_, err = gardenClient.Lookup(handle)
Ω(err).ShouldNot(HaveOccurred())
err = gardenClient.Destroy(handle)
Ω(err).ShouldNot(HaveOccurred(), "Expected no error when attempting to destroy this container")
_, err = gardenClient.Lookup(handle)
Ω(err).Should(HaveOccurred())
})
It("does not allow creating an already existing container", func() {
container, err := gardenClient.Create(garden.ContainerSpec{})
Ω(err).ShouldNot(HaveOccurred())
_, err = gardenClient.Create(garden.ContainerSpec{Handle: container.Handle()})
Ω(err).Should(HaveOccurred(), "Expected an error when creating a Garden container with an existing handle")
})
})
It("mounts /proc read-only", func() {
container := createContainer(gardenClient, garden.ContainerSpec{})
buffer := gbytes.NewBuffer()
process, err := container.Run(garden.ProcessSpec{User: "root", Path: "cat", Args: []string{"/proc/mounts"}}, recordedProcessIO(buffer))
Ω(err).ShouldNot(HaveOccurred())
Ω(process.Wait()).Should(Equal(0))
lines := strings.Split(string(buffer.Contents()), "\n")
for _, line := range lines {
if strings.Contains(line, "proc") {
Ω(line).Should(ContainSubstring("ro"))
Ω(line).ShouldNot(ContainSubstring("rw"))
}
}
})
})