forked from containers/buildah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_test.go
455 lines (423 loc) · 14 KB
/
run_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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// +build linux
package chroot
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"github.com/containers/buildah/tests/testreport/types"
"github.com/containers/buildah/util"
"github.com/containers/storage/pkg/reexec"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
)
const (
reportCommand = "testreport"
)
func TestMain(m *testing.M) {
if reexec.Init() {
return
}
os.Exit(m.Run())
}
func testMinimal(t *testing.T, modify func(g *generate.Generator, rootDir, bundleDir string), verify func(t *testing.T, report *types.TestReport)) {
g, err := generate.New("linux")
if err != nil {
t.Fatalf("generate.New(%q): %v", "linux", err)
}
tempDir, err := ioutil.TempDir("", "chroot-test")
if err != nil {
t.Fatalf("ioutil.TempDir(%q, %q): %v", "", "chrootTest", err)
}
defer os.RemoveAll(tempDir)
info, err := os.Stat(tempDir)
if err != nil {
t.Fatalf("error checking permissions on %q: %v", tempDir, err)
}
if err = os.Chmod(tempDir, info.Mode()|0111); err != nil {
t.Fatalf("error loosening permissions on %q: %v", tempDir, err)
}
rootDir := filepath.Join(tempDir, "root")
if err := os.Mkdir(rootDir, 0711); err != nil {
t.Fatalf("os.Mkdir(%q): %v", rootDir, err)
}
specPath := filepath.Join("..", "tests", reportCommand, reportCommand)
specBinarySource, err := os.Open(specPath)
if err != nil {
t.Fatalf("open(%q): %v", specPath, err)
}
defer specBinarySource.Close()
specBinary, err := os.OpenFile(filepath.Join(rootDir, reportCommand), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0711)
if err != nil {
t.Fatalf("open(%q): %v", filepath.Join(rootDir, reportCommand), err)
}
if _, err := io.Copy(specBinary, specBinarySource); err != nil {
t.Fatalf("io.Copy error: %v", err)
}
specBinary.Close()
g.SetRootPath(rootDir)
g.SetProcessArgs([]string{"/" + reportCommand})
bundleDir := filepath.Join(tempDir, "bundle")
if err := os.Mkdir(bundleDir, 0700); err != nil {
t.Fatalf("os.Mkdir(%q): %v", bundleDir, err)
}
if modify != nil {
modify(&g, rootDir, bundleDir)
}
uid, gid, err := util.GetHostRootIDs(g.Config)
if err != nil {
t.Fatalf("GetHostRootIDs: %v", err)
}
if err := os.Chown(rootDir, int(uid), int(gid)); err != nil {
t.Fatalf("os.Chown(%q): %v", rootDir, err)
}
output := new(bytes.Buffer)
if err := RunUsingChroot(g.Config, bundleDir, "/", new(bytes.Buffer), output, output); err != nil {
t.Fatalf("run: %v: %s", err, output.String())
}
var report types.TestReport
if json.Unmarshal(output.Bytes(), &report) != nil {
t.Fatalf("decode: %v", err)
}
if verify != nil {
verify(t, &report)
}
}
func TestNoop(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t, nil, nil)
}
func TestMinimalSkeleton(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
},
func(t *testing.T, report *types.TestReport) {
})
}
func TestProcessTerminal(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
for _, terminal := range []bool{false, true} {
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.SetProcessTerminal(terminal)
},
func(t *testing.T, report *types.TestReport) {
if report.Spec.Process.Terminal != terminal {
t.Fatalf("expected terminal = %v, got %v", terminal, report.Spec.Process.Terminal)
}
})
}
}
func TestProcessConsoleSize(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
for _, size := range [][2]uint{{80, 25}, {132, 50}} {
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.SetProcessTerminal(true)
g.SetProcessConsoleSize(size[0], size[1])
},
func(t *testing.T, report *types.TestReport) {
if report.Spec.Process.ConsoleSize.Width != size[0] {
t.Fatalf("expected console width = %v, got %v", size[0], report.Spec.Process.ConsoleSize.Width)
}
if report.Spec.Process.ConsoleSize.Height != size[1] {
t.Fatalf("expected console height = %v, got %v", size[1], report.Spec.Process.ConsoleSize.Height)
}
})
}
}
func TestProcessUser(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
for _, id := range []uint32{0, 1000} {
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.SetProcessUID(id)
g.SetProcessGID(id + 1)
g.AddProcessAdditionalGid(id + 2)
},
func(t *testing.T, report *types.TestReport) {
if report.Spec.Process.User.UID != id {
t.Fatalf("expected UID %v, got %v", id, report.Spec.Process.User.UID)
}
if report.Spec.Process.User.GID != id+1 {
t.Fatalf("expected GID %v, got %v", id+1, report.Spec.Process.User.GID)
}
})
}
}
func TestProcessEnv(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
e := fmt.Sprintf("PARENT_TEST_PID=%d", syscall.Getpid())
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.ClearProcessEnv()
g.AddProcessEnv("PARENT_TEST_PID", fmt.Sprintf("%d", syscall.Getpid()))
},
func(t *testing.T, report *types.TestReport) {
for _, ev := range report.Spec.Process.Env {
if ev == e {
return
}
}
t.Fatalf("expected environment variable %q", e)
})
}
func TestProcessCwd(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
if err := os.Mkdir(filepath.Join(rootDir, "/no-such-directory"), 0700); err != nil {
t.Fatalf("mkdir(%q): %v", filepath.Join(rootDir, "/no-such-directory"), err)
}
g.SetProcessCwd("/no-such-directory")
},
func(t *testing.T, report *types.TestReport) {
if report.Spec.Process.Cwd != "/no-such-directory" {
t.Fatalf("expected %q, got %q", "/no-such-directory", report.Spec.Process.Cwd)
}
})
}
func TestProcessCapabilities(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.ClearProcessCapabilities()
},
func(t *testing.T, report *types.TestReport) {
if len(report.Spec.Process.Capabilities.Permitted) != 0 {
t.Fatalf("expected no permitted capabilities, got %#v", report.Spec.Process.Capabilities.Permitted)
}
})
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.ClearProcessCapabilities()
if err := g.AddProcessCapabilityEffective("CAP_IPC_LOCK"); err != nil {
t.Fatalf("%v", err)
}
if err := g.AddProcessCapabilityPermitted("CAP_IPC_LOCK"); err != nil {
t.Fatalf("%v", err)
}
if err := g.AddProcessCapabilityInheritable("CAP_IPC_LOCK"); err != nil {
t.Fatalf("%v", err)
}
if err := g.AddProcessCapabilityBounding("CAP_IPC_LOCK"); err != nil {
t.Fatalf("%v", err)
}
if err := g.AddProcessCapabilityAmbient("CAP_IPC_LOCK"); err != nil {
t.Fatalf("%v", err)
}
},
func(t *testing.T, report *types.TestReport) {
if len(report.Spec.Process.Capabilities.Permitted) != 1 {
t.Fatalf("expected one permitted capability, got %#v", report.Spec.Process.Capabilities.Permitted)
}
if report.Spec.Process.Capabilities.Permitted[0] != "CAP_IPC_LOCK" {
t.Fatalf("expected one capability CAP_IPC_LOCK, got %#v", report.Spec.Process.Capabilities.Permitted)
}
})
}
func TestProcessRlimits(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
for _, limit := range []int64{100 * 1024 * 1024 * 1024, 200 * 1024 * 1024 * 1024, syscall.RLIM_INFINITY} {
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.ClearProcessRlimits()
if limit != syscall.RLIM_INFINITY {
g.AddProcessRlimits("rlimit_as", uint64(limit), uint64(limit))
}
},
func(t *testing.T, report *types.TestReport) {
var rlim *specs.POSIXRlimit
for i := range report.Spec.Process.Rlimits {
if strings.ToUpper(report.Spec.Process.Rlimits[i].Type) == "RLIMIT_AS" {
rlim = &report.Spec.Process.Rlimits[i]
}
}
if limit == syscall.RLIM_INFINITY && !(rlim == nil || (int64(rlim.Soft) == syscall.RLIM_INFINITY && int64(rlim.Hard) == syscall.RLIM_INFINITY)) {
t.Fatalf("wasn't supposed to set limit on number of open files: %#v", rlim)
}
if limit != syscall.RLIM_INFINITY && rlim == nil {
t.Fatalf("was supposed to set limit on number of open files")
}
if rlim != nil {
if int64(rlim.Soft) != limit {
t.Fatalf("soft limit was set to %d, not %d", rlim.Soft, limit)
}
if int64(rlim.Hard) != limit {
t.Fatalf("hard limit was set to %d, not %d", rlim.Hard, limit)
}
}
})
}
}
func TestProcessNoNewPrivileges(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
for _, nope := range []bool{false, true} {
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.SetProcessNoNewPrivileges(nope)
},
func(t *testing.T, report *types.TestReport) {
if report.Spec.Process.NoNewPrivileges != nope {
t.Fatalf("expected no-new-prives to be %v, got %v", nope, report.Spec.Process.NoNewPrivileges)
}
})
}
}
func TestProcessOOMScoreAdj(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
for _, adj := range []int{0, 1, 2, 3} {
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.SetProcessOOMScoreAdj(adj)
},
func(t *testing.T, report *types.TestReport) {
adjusted := 0
if report.Spec.Process.OOMScoreAdj != nil {
adjusted = *report.Spec.Process.OOMScoreAdj
}
if adjusted != adj {
t.Fatalf("expected oom-score-adj to be %v, got %v", adj, adjusted)
}
})
}
}
func TestHostname(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
hostname := fmt.Sprintf("host%d", syscall.Getpid())
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.SetHostname(hostname)
},
func(t *testing.T, report *types.TestReport) {
if report.Spec.Hostname != hostname {
t.Fatalf("expected %q, got %q", hostname, report.Spec.Hostname)
}
})
}
func TestMounts(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.AddMount(specs.Mount{
Source: "tmpfs",
Destination: "/was-not-there-before",
Type: "tmpfs",
Options: []string{"ro,size=0"},
})
},
func(t *testing.T, report *types.TestReport) {
found := false
for _, mount := range report.Spec.Mounts {
if mount.Destination == "/was-not-there-before" && mount.Type == "tmpfs" {
found = true
}
}
if !found {
t.Fatal("added mount not found")
}
})
}
func TestLinuxIDMapping(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.ClearLinuxUIDMappings()
g.ClearLinuxGIDMappings()
g.AddLinuxUIDMapping(uint32(syscall.Getuid()), 0, 1)
g.AddLinuxGIDMapping(uint32(syscall.Getgid()), 0, 1)
},
func(t *testing.T, report *types.TestReport) {
if len(report.Spec.Linux.UIDMappings) != 1 {
t.Fatalf("expected 1 uid mapping, got %q", len(report.Spec.Linux.UIDMappings))
}
if report.Spec.Linux.UIDMappings[0].HostID != uint32(syscall.Getuid()) {
t.Fatalf("expected host uid mapping to be %d, got %d", syscall.Getuid(), report.Spec.Linux.UIDMappings[0].HostID)
}
if report.Spec.Linux.UIDMappings[0].ContainerID != 0 {
t.Fatalf("expected container uid mapping to be 0, got %d", report.Spec.Linux.UIDMappings[0].ContainerID)
}
if report.Spec.Linux.UIDMappings[0].Size != 1 {
t.Fatalf("expected container uid map size to be 1, got %d", report.Spec.Linux.UIDMappings[0].Size)
}
if report.Spec.Linux.GIDMappings[0].HostID != uint32(syscall.Getgid()) {
t.Fatalf("expected host uid mapping to be %d, got %d", syscall.Getgid(), report.Spec.Linux.GIDMappings[0].HostID)
}
if report.Spec.Linux.GIDMappings[0].ContainerID != 0 {
t.Fatalf("expected container gid mapping to be 0, got %d", report.Spec.Linux.GIDMappings[0].ContainerID)
}
if report.Spec.Linux.GIDMappings[0].Size != 1 {
t.Fatalf("expected container gid map size to be 1, got %d", report.Spec.Linux.GIDMappings[0].Size)
}
})
}
func TestLinuxIDMappingShift(t *testing.T) {
if syscall.Getuid() != 0 {
t.Skip("tests need to be run as root")
}
testMinimal(t,
func(g *generate.Generator, rootDir, bundleDir string) {
g.ClearLinuxUIDMappings()
g.ClearLinuxGIDMappings()
g.AddLinuxUIDMapping(uint32(syscall.Getuid())+1, 0, 1)
g.AddLinuxGIDMapping(uint32(syscall.Getgid())+1, 0, 1)
},
func(t *testing.T, report *types.TestReport) {
if len(report.Spec.Linux.UIDMappings) != 1 {
t.Fatalf("expected 1 uid mapping, got %q", len(report.Spec.Linux.UIDMappings))
}
if report.Spec.Linux.UIDMappings[0].HostID != uint32(syscall.Getuid()+1) {
t.Fatalf("expected host uid mapping to be %d, got %d", syscall.Getuid()+1, report.Spec.Linux.UIDMappings[0].HostID)
}
if report.Spec.Linux.UIDMappings[0].ContainerID != 0 {
t.Fatalf("expected container uid mapping to be 0, got %d", report.Spec.Linux.UIDMappings[0].ContainerID)
}
if report.Spec.Linux.UIDMappings[0].Size != 1 {
t.Fatalf("expected container uid map size to be 1, got %d", report.Spec.Linux.UIDMappings[0].Size)
}
if report.Spec.Linux.GIDMappings[0].HostID != uint32(syscall.Getgid()+1) {
t.Fatalf("expected host uid mapping to be %d, got %d", syscall.Getgid()+1, report.Spec.Linux.GIDMappings[0].HostID)
}
if report.Spec.Linux.GIDMappings[0].ContainerID != 0 {
t.Fatalf("expected container gid mapping to be 0, got %d", report.Spec.Linux.GIDMappings[0].ContainerID)
}
if report.Spec.Linux.GIDMappings[0].Size != 1 {
t.Fatalf("expected container gid map size to be 1, got %d", report.Spec.Linux.GIDMappings[0].Size)
}
})
}