-
Notifications
You must be signed in to change notification settings - Fork 19
/
runtime_test.go
333 lines (262 loc) · 8.47 KB
/
runtime_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
package lxcri
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/lxc/lxcri/pkg/log"
"github.com/lxc/lxcri/pkg/specki"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
var logLevel = "debug"
var libexecDir = "/usr/local/libexec/lxcri"
var tmpRoot = "."
func init() {
// NOTE keep environment variables in sync with `lxcri` cli
if val, ok := os.LookupEnv("LXCRI_LOG_LEVEL"); ok {
logLevel = val
}
if val, ok := os.LookupEnv("LXCRI_LIBEXEC"); ok {
libexecDir = val
}
if val, ok := os.LookupEnv("HOME"); ok {
tmpRoot = val
}
}
func mkdirTemp() (string, error) {
// /tmp has permissions 1777
// it should never be used as runtime or rootfs parent
return os.MkdirTemp(tmpRoot, "lxcri-test")
}
func removeAll(t *testing.T, filename string) {
err := os.RemoveAll(filename)
require.NoError(t, err)
}
func newRuntime(t *testing.T) *Runtime {
runtimeRoot, err := mkdirTemp()
require.NoError(t, err)
t.Logf("runtime root: %s", runtimeRoot)
err = unix.Chmod(runtimeRoot, 0755)
require.NoError(t, err)
level, err := log.ParseLevel(logLevel)
require.NoError(t, err)
rt := &Runtime{
Log: log.ConsoleLogger(true, level),
Root: runtimeRoot,
LibexecDir: libexecDir,
//MonitorCgroup: "lxcri-monitor.slice",
}
require.NoError(t, rt.Init())
return rt
}
// NOTE a container that was created successfully must always be
// deleted, otherwise the go test runner will hang because it waits
// for the container process to exit.
func newConfig(t *testing.T, cmd string, args ...string) *ContainerConfig {
rootfs, err := mkdirTemp()
require.NoError(t, err)
t.Logf("container rootfs: %s", rootfs)
level, err := log.ParseLevel(logLevel)
require.NoError(t, err)
cmdAbs, err := filepath.Abs(cmd)
require.NoError(t, err)
cmdDest := "/" + filepath.Base(cmdAbs)
spec := specki.NewSpec(rootfs, cmdDest)
id := filepath.Base(rootfs)
cfg := ContainerConfig{
ContainerID: id, Spec: spec,
Log: log.ConsoleLogger(true, level),
LogFile: "/dev/stderr",
LogLevel: logLevel,
}
cfg.Spec.Linux.CgroupsPath = id + ".slice" // use /proc/self/cgroup"
cfg.Spec.Mounts = append(cfg.Spec.Mounts,
specki.BindMount(cmdAbs, cmdDest),
)
return &cfg
}
func TestEmptyNamespaces(t *testing.T) {
t.Parallel()
rt := newRuntime(t)
defer removeAll(t, rt.Root)
cfg := newConfig(t, "lxcri-test")
defer removeAll(t, cfg.Spec.Root.Path)
// Clearing all namespaces should not work,
// since the mount namespace must never be shared with the host.
cfg.Spec.Linux.Namespaces = cfg.Spec.Linux.Namespaces[0:0]
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
c, err := rt.Create(ctx, cfg)
require.Error(t, err)
t.Logf("create error: %s", err)
require.Nil(t, c)
}
func TestSharedPIDNamespace(t *testing.T) {
t.Parallel()
if os.Getuid() != 0 {
t.Skipf("PID namespace sharing is only permitted as root.")
}
rt := newRuntime(t)
defer removeAll(t, rt.Root)
cfg := newConfig(t, "lxcri-test")
defer removeAll(t, cfg.Spec.Root.Path)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
pidns := specs.LinuxNamespace{
Type: specs.PIDNamespace,
Path: fmt.Sprintf("/proc/%d/ns/pid", os.Getpid()),
}
for i, ns := range cfg.Spec.Linux.Namespaces {
if ns.Type == specs.PIDNamespace {
cfg.Spec.Linux.Namespaces[i] = pidns
}
}
c, err := rt.Create(ctx, cfg)
require.NoError(t, err)
require.NotNil(t, c)
err = c.Release()
require.NoError(t, err)
err = rt.Delete(ctx, c.ContainerID, true)
require.NoError(t, err)
}
// TODO test uts namespace (shared with host)
// NOTE works only if cgroup root is writable
// sudo chown -R $(whoami):$(whoami) /sys/fs/cgroup/$(cat /proc/self/cgroup | grep '^0:' | cut -d: -f3)
func TestNonEmptyCgroup(t *testing.T) {
t.Parallel()
rt := newRuntime(t)
defer removeAll(t, rt.Root)
cfg := newConfig(t, "lxcri-test")
defer removeAll(t, cfg.Spec.Root.Path)
if os.Getuid() != 0 {
cfg.Spec.Linux.UIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: uint32(os.Getuid()), Size: 1},
}
cfg.Spec.Linux.GIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: uint32(os.Getgid()), Size: 1},
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
c, err := rt.Create(ctx, cfg)
require.NoError(t, err)
require.NotNil(t, c)
//t.Logf("sleeping for a minute")
//time.Sleep(60*time.Second)
cfg2 := newConfig(t, "lxcri-test")
defer removeAll(t, cfg2.Spec.Root.Path)
cfg2.Spec.Linux.CgroupsPath = cfg.Spec.Linux.CgroupsPath
if os.Getuid() != 0 {
cfg2.Spec.Linux.UIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: uint32(os.Getuid()), Size: 1},
}
cfg2.Spec.Linux.GIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: uint32(os.Getgid()), Size: 1},
}
}
c2, err := rt.Create(ctx, cfg2)
require.Error(t, err)
t.Logf("create error: %s", err)
err = c.Release()
require.NoError(t, err)
err = rt.Delete(ctx, c.ContainerID, true)
require.NoError(t, err)
err = c2.Release()
require.NoError(t, err)
require.NotNil(t, c2)
err = rt.Delete(ctx, c2.ContainerID, true)
require.NoError(t, err)
}
func TestRuntimePrivileged(t *testing.T) {
t.Parallel()
if os.Getuid() != 0 {
t.Skipf("This tests only runs as root")
}
rt := newRuntime(t)
defer removeAll(t, rt.Root)
cfg := newConfig(t, "lxcri-test")
defer removeAll(t, cfg.Spec.Root.Path)
testRuntime(t, rt, cfg)
}
// The following tests require the following setup:
// sudo /bin/sh -c "echo '$(whoami):1000:1' >> /etc/subuid"
// sudo /bin/sh -c "echo '$(whoami):20000:65536' >> /etc/subuid"
// sudo /bin/sh -c "echo '$(whoami):1000:1' >> /etc/subgid"
// sudo /bin/sh -c "echo '$(whoami):20000:65536' >> /etc/subgid"
// sudo chown -R $(whoami):$(whoami) /sys/fs/cgroup/unified$(cat /proc/self/cgroup | grep '^0:' | cut -d: -f3)
// sudo chown -R $(whoami):$(whoami) /sys/fs/cgroup$(cat /proc/self/cgroup | grep '^0:' | cut -d: -f3)
//
func TestRuntimeUnprivileged(t *testing.T) {
t.Parallel()
if os.Getuid() == 0 {
t.Skipf("This test only runs as non-root")
}
rt := newRuntime(t)
defer removeAll(t, rt.Root)
cfg := newConfig(t, "lxcri-test")
defer removeAll(t, cfg.Spec.Root.Path)
// The container UID must have full access to the rootfs.
// MkdirTemp sets directory permissions to 0700.
// If we the container UID (0) / or GID are not mapped to the owner (creator) of the rootfs,
// then the rootfs and runtime directory permissions must be expanded.
err := unix.Chmod(cfg.Spec.Root.Path, 0777)
require.NoError(t, err)
err = unix.Chmod(rt.Root, 0755)
require.NoError(t, err)
cfg.Spec.Linux.UIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: 20000, Size: 65536},
}
cfg.Spec.Linux.GIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: 20000, Size: 65536},
}
testRuntime(t, rt, cfg)
}
func TestRuntimeUnprivileged2(t *testing.T) {
t.Parallel()
rt := newRuntime(t)
defer removeAll(t, rt.Root)
cfg := newConfig(t, "lxcri-test")
defer removeAll(t, cfg.Spec.Root.Path)
if os.Getuid() != 0 {
cfg.Spec.Linux.UIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: uint32(os.Getuid()), Size: 1},
//specs.LinuxIDMapping{ContainerID: 1, HostID: 20000, Size: 65536},
}
cfg.Spec.Linux.GIDMappings = []specs.LinuxIDMapping{
specs.LinuxIDMapping{ContainerID: 0, HostID: uint32(os.Getgid()), Size: 1},
//specs.LinuxIDMapping{ContainerID: 1, HostID: 20000, Size: 65536},
}
}
testRuntime(t, rt, cfg)
}
func testRuntime(t *testing.T, rt *Runtime, cfg *ContainerConfig) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
c, err := rt.Create(ctx, cfg)
require.NoError(t, err)
require.NotNil(t, c)
state, err := c.State()
require.NoError(t, err)
require.Equal(t, specs.StateCreated, state.SpecState.Status)
err = rt.Start(ctx, c)
require.NoError(t, err)
state, err = c.State()
require.NoError(t, err)
require.Equal(t, specs.StateRunning, state.SpecState.Status)
err = rt.Kill(ctx, c, unix.SIGUSR1)
require.NoError(t, err)
state, err = c.State()
require.NoError(t, err)
require.Equal(t, specs.StateRunning, state.SpecState.Status)
err = rt.Delete(ctx, c.ContainerID, true)
require.NoError(t, err)
state, err = c.State()
require.NoError(t, err)
require.Equal(t, specs.StateStopped, state.SpecState.Status)
err = c.Release()
require.NoError(t, err)
}