This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent_test.go
500 lines (381 loc) · 11.9 KB
/
agent_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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"syscall"
"testing"
"google.golang.org/grpc"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/opencontainers/runc/libcontainer"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
const (
testExecID = "testExecID"
testContainerID = "testContainerID"
)
func skipUnlessRoot(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("Test disabled as requires root user")
}
}
func TestClosePostStartFDsAllNil(t *testing.T) {
p := &process{}
p.closePostStartFDs()
}
func TestClosePostStartFDsAllInitialized(t *testing.T) {
rStdin, wStdin, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer wStdin.Close()
rStdout, wStdout, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer rStdout.Close()
rStderr, wStderr, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer rStderr.Close()
rConsoleSocket, wConsoleSocket, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer wConsoleSocket.Close()
rConsoleSock, wConsoleSock, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer wConsoleSock.Close()
p := &process{
process: libcontainer.Process{
Stdin: rStdin,
Stdout: wStdout,
Stderr: wStderr,
ConsoleSocket: rConsoleSocket,
},
consoleSock: rConsoleSock,
}
p.closePostStartFDs()
}
func TestClosePostExitFDsAllNil(t *testing.T) {
p := &process{}
p.closePostExitFDs()
}
func TestClosePostExitFDsAllInitialized(t *testing.T) {
rTermMaster, wTermMaster, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer wTermMaster.Close()
rStdin, wStdin, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer rStdin.Close()
rStdout, wStdout, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer wStdout.Close()
rStderr, wStderr, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer wStderr.Close()
p := &process{
termMaster: rTermMaster,
stdin: wStdin,
stdout: rStdout,
stderr: rStderr,
}
p.closePostExitFDs()
}
func TestSetProcess(t *testing.T) {
c := &container{
processes: make(map[string]*process),
}
p := &process{
id: testExecID,
}
c.setProcess(p)
proc, exist := c.processes[testExecID]
assert.True(t, exist, "Process entry should exist")
assert.True(t, reflect.DeepEqual(p, proc),
"Process structures should be identical: got %+v, expecting %+v",
proc, p)
}
func TestDeleteProcess(t *testing.T) {
c := &container{
processes: make(map[string]*process),
}
p := &process{
id: testExecID,
}
c.processes[testExecID] = p
c.deleteProcess(testExecID)
_, exist := c.processes[testExecID]
assert.False(t, exist, "Process entry should not exist")
}
func TestGetProcessEntryExist(t *testing.T) {
c := &container{
processes: make(map[string]*process),
}
p := &process{
id: testExecID,
}
c.processes[testExecID] = p
proc, err := c.getProcess(testExecID)
assert.Nil(t, err, "%v", err)
assert.True(t, reflect.DeepEqual(p, proc),
"Process structures should be identical: got %+v, expecting %+v",
proc, p)
}
func TestGetProcessNoEntry(t *testing.T) {
c := &container{
processes: make(map[string]*process),
}
_, err := c.getProcess(testExecID)
assert.Error(t, err, "Should fail because no entry has been created")
}
func TestSetSandboxStorage(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
s.storages = make(map[string]*sandboxStorage)
storagePath := "/tmp/testEphe/"
// Add a new sandbox storage
newStorage := s.setSandboxStorage(storagePath)
// Check the reference counter
refCount := s.storages[storagePath].refCount
assert.Equal(t, 1, refCount, "Invalid refcount, got %d expected 1", refCount)
assert.True(t, newStorage, "expected value was true")
// Use the existing sandbox storage
newStorage = s.setSandboxStorage(storagePath)
assert.False(t, newStorage, "expected value was false")
// Since we are using existing storage, the reference counter
// should read 2 by now
refCount = s.storages[storagePath].refCount
assert.Equal(t, 2, refCount, "Invalid refcount, got %d expected 2", refCount)
}
func TestRemoveSandboxStorage(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
s.storages = make(map[string]*sandboxStorage)
err := s.removeSandboxStorage("/tmp/testEphePath/")
assert.Error(t, err, "Should fail because sandbox storage doesn't exist")
}
func TestUnsetAndRemoveSandboxStorage(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
s.storages = make(map[string]*sandboxStorage)
err := s.unsetAndRemoveSandboxStorage("/tmp/testEphePath/")
assert.Error(t, err, "Should fail because sandbox storage doesn't exist")
}
func TestUnSetSandboxStorage(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
s.storages = make(map[string]*sandboxStorage)
storagePath := "/tmp/testEphe/"
// Add a new sandbox storage
s.setSandboxStorage(storagePath)
// Use the existing sandbox storage
s.setSandboxStorage(storagePath)
removeSandboxStorage, _ := s.unSetSandboxStorage(storagePath)
assert.False(t, removeSandboxStorage, "Expected value was false")
// Reference counter should decrement to 1
refCount := s.storages[storagePath].refCount
assert.Equal(t, 1, refCount, "Invalid refcount, got %d expected 1", refCount)
removeSandboxStorage, _ = s.unSetSandboxStorage(storagePath)
assert.True(t, removeSandboxStorage, "Expected value was true")
// Since no container is using this sandbox storage anymore
// there should not be any reference in sandbox struct
// for the given storage
_, ok := s.storages[storagePath]
assert.False(t, ok, "expected value was false")
// If no container is using the sandbox storage, the reference
// counter for it should not exist
_, err := s.unSetSandboxStorage(storagePath)
assert.Error(t, err, "Should fail because sandbox storage doesn't exist")
_, err = s.unSetSandboxStorage("/tmp/nosbs/")
assert.Error(t, err, "Should fail because sandbox storage doesn't exist")
}
func TestGetContainerEntryExist(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
c := &container{
id: testContainerID,
}
s.containers[testContainerID] = c
cont, err := s.getContainer(testContainerID)
assert.Nil(t, err, "%v", err)
assert.True(t, reflect.DeepEqual(c, cont),
"Container structures should be identical: got %+v, expecting %+v",
cont, c)
}
func TestGetContainerNoEntry(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
_, err := s.getContainer(testContainerID)
assert.Error(t, err, "Should fail because no entry has been created")
}
func TestSetContainer(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
c := &container{
id: testContainerID,
}
s.setContainer(testContainerID, c)
cont, exist := s.containers[testContainerID]
assert.True(t, exist, "Container entry should exist")
assert.True(t, reflect.DeepEqual(c, cont),
"Container structures should be identical: got %+v, expecting %+v",
cont, c)
}
func TestDeleteContainer(t *testing.T) {
s := &sandbox{
containers: make(map[string]*container),
}
c := &container{
id: testContainerID,
}
s.containers[testContainerID] = c
s.deleteContainer(testContainerID)
_, exist := s.containers[testContainerID]
assert.False(t, exist, "Process entry should not exist")
}
func TestGetProcessFromSandbox(t *testing.T) {
s := &sandbox{
running: true,
containers: make(map[string]*container),
}
c := &container{
id: testContainerID,
processes: make(map[string]*process),
}
p := &process{
id: testExecID,
}
c.processes[testExecID] = p
s.containers[testContainerID] = c
proc, _, err := s.getProcess(testContainerID, testExecID)
assert.Nil(t, err, "%v", err)
assert.True(t, reflect.DeepEqual(p, proc),
"Process structures should be identical: got %+v, expecting %+v",
proc, p)
}
func TestStartStopGRPCServer(t *testing.T) {
_, out, err := os.Pipe()
assert.Nil(t, err, "%v", err)
s := &sandbox{
containers: make(map[string]*container),
channel: &serialChannel{serialConn: out},
}
s.startGRPC()
assert.NotNil(t, s.server, "failed starting grpc server")
s.stopGRPC()
assert.Nil(t, s.server, "failed stopping grpc server")
}
func TestSettingGrpcTracer(t *testing.T) {
_, out, err := os.Pipe()
assert.Nil(t, err, "%v", err)
s := &sandbox{
containers: make(map[string]*container),
channel: &serialChannel{serialConn: out},
enableGrpcTrace: true,
}
s.startGRPC()
assert.NotNil(t, s.server, "failed starting grpc server")
s.stopGRPC()
assert.Nil(t, s.server, "failed stopping grpc server")
}
func TestGrpcTracer(t *testing.T) {
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return &pb.HealthCheckResponse{}, nil
}
_, err := grpcTracer(context.Background(), &pb.CheckRequest{}, &grpc.UnaryServerInfo{}, handler)
assert.Nil(t, err, "failed to trace grpc request: %v", err)
}
func TestMountToRootfs(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("mount need cap_sys_admin")
}
cgprocDir, err := ioutil.TempDir("", "proc-cgroup")
assert.Nil(t, err, "%v", err)
defer os.RemoveAll(cgprocDir)
mounts := []initMount{
{"proc", "proc", filepath.Join(cgprocDir, "proc"), []string{"nosuid", "nodev", "noexec"}},
{"sysfs", "sysfs", filepath.Join(cgprocDir, "sysfs"), []string{"nosuid", "nodev", "noexec"}},
{"devtmpfs", "dev", filepath.Join(cgprocDir, "dev"), []string{"nosuid"}},
{"tmpfs", "tmpfs", filepath.Join(cgprocDir, "tmpfs"), []string{"nosuid", "nodev"}},
{"devpts", "devpts", filepath.Join(cgprocDir, "devpts"), []string{"nosuid", "noexec"}},
}
for _, m := range mounts {
err = mountToRootfs(m)
assert.Nil(t, err, "%v", err)
}
for _, m := range mounts {
err = syscall.Unmount(m.dest, 0)
assert.Nil(t, err, "%v", err)
}
}
func TestGetCgroupMountsFailed(t *testing.T) {
cgprocDir, err := ioutil.TempDir("", "proc-cgroup")
assert.Nil(t, err, "%v", err)
defer os.RemoveAll(cgprocDir)
_, err = getCgroupMounts(filepath.Join(cgprocDir, "cgroups"))
assert.NotNil(t, err, "proc/cgroups is not exist: but got nil")
}
func TestGetCgroupMountsSuccessful(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip("mount need cap_sys_admin")
}
cgprocDir, err := ioutil.TempDir("", "proc-cgroup")
assert.Nil(t, err, "%v", err)
defer os.RemoveAll(cgprocDir)
testMounts := initMount{"proc", "proc", cgprocDir, []string{"nosuid", "nodev", "noexec"}}
err = mountToRootfs(testMounts)
assert.Nil(t, err, "%v", err)
defer os.RemoveAll(cgprocDir)
_, err = getCgroupMounts(filepath.Join(cgprocDir, "cgroups"))
assert.Nil(t, err, "%v", err)
err = syscall.Unmount(cgprocDir, 0)
assert.Nil(t, err, "%v", err)
}
func TestAddGuestHooks(t *testing.T) {
assert := assert.New(t)
hookPath, err := ioutil.TempDir("", "hooks")
assert.NoError(err)
defer os.RemoveAll(hookPath)
poststopPath := path.Join(hookPath, "poststop")
err = os.Mkdir(poststopPath, 0750)
assert.NoError(err)
dirPath := path.Join(poststopPath, "directory")
err = os.Mkdir(dirPath, 0750)
assert.NoError(err)
normalPath := path.Join(poststopPath, "normalfile")
f, err := os.OpenFile(normalPath, os.O_RDONLY|os.O_CREATE, 0640)
assert.NoError(err)
f.Close()
symlinkPath := path.Join(poststopPath, "symlink")
err = os.Link(normalPath, symlinkPath)
assert.NoError(err)
s := &sandbox{
guestHooks: &specs.Hooks{},
guestHooksPresent: false,
}
s.scanGuestHooks(hookPath)
assert.False(s.guestHooksPresent)
spec := &specs.Spec{}
s.addGuestHooks(spec)
assert.True(len(spec.Hooks.Poststop) == 0)
execPath := path.Join(poststopPath, "executable")
f, err = os.OpenFile(execPath, os.O_RDONLY|os.O_CREATE, 0750)
assert.NoError(err)
f.Close()
s.scanGuestHooks(hookPath)
assert.True(s.guestHooksPresent)
s.addGuestHooks(spec)
assert.True(len(spec.Hooks.Poststop) == 1)
assert.True(strings.Contains(spec.Hooks.Poststop[0].Path, "executable"))
}