forked from kata-containers/shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_test.go
156 lines (128 loc) · 3.38 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
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"context"
"fmt"
"net"
"os"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/kata-containers/agent/protocols/mockserver"
)
const mockSockAddr = "/tmp/agentserver.sock"
const unixMockAddr = "unix:///" + mockSockAddr
const badMockAddr = "vsock://" + mockSockAddr
type testAgent struct {
t *testing.T
server *grpc.Server
waitCh chan error
ctx context.Context
client *shimAgent
}
func startMockServer(t *testing.T) (*grpc.Server, chan error) {
os.Remove(mockSockAddr)
l, err := net.Listen("unix", mockSockAddr)
assert.Nil(t, err, "Listen on %s failed: %s", mockSockAddr, err)
mock := mockserver.NewMockServer()
stopWait := make(chan error, 1)
go func() {
mock.Serve(l)
stopWait <- nil
}()
return mock, stopWait
}
func testSetup(t *testing.T) *testAgent {
mock, waitCh := startMockServer(t)
agent, err := newShimAgent(mockSockAddr)
if !assert.Nil(t, err, "Failed to create new agent client: %s", err) {
t.FailNow()
}
ctx := context.Background()
_, err = agent.CreateSandbox(ctx, &pb.CreateSandboxRequest{
Hostname: "testSandbox",
Dns: []string{},
Storages: []*pb.Storage{},
SandboxPidns: true,
})
if !assert.Nil(t, err, "Failed to create sandbox: %s", err) {
agent.Close()
t.FailNow()
}
return &testAgent{
t: t,
server: mock,
waitCh: waitCh,
ctx: ctx,
client: agent,
}
}
func testTearDown(t *testAgent) {
t.client.Close()
t.server.Stop()
<-t.waitCh
}
func newTestSpec() *pb.Spec {
return &pb.Spec{
Version: "testGrpcVersion",
Process: &pb.Process{
Terminal: true,
ConsoleSize: &pb.Box{10, 10},
User: pb.User{UID: 0, GID: 0, Username: "root:root"},
Capabilities: &pb.LinuxCapabilities{},
Rlimits: []pb.POSIXRlimit{},
},
Root: &pb.Root{Path: "rootpath", Readonly: true},
Hostname: "testGuest",
}
}
func (t *testAgent) addContainer(containerID, execID string) error {
_, err := t.client.CreateContainer(t.ctx, &pb.CreateContainerRequest{
ContainerId: containerID,
ExecId: execID,
StringUser: &pb.StringUser{Uid: "root", Gid: "root"},
OCI: newTestSpec(),
})
if err != nil {
return fmt.Errorf("failed to create new container: %s", err)
}
_, err = t.client.StartContainer(t.ctx, &pb.StartContainerRequest{ContainerId: containerID})
if err != nil {
return fmt.Errorf("failed to create new container: %s", err)
}
return nil
}
func TestNewShimAgent(t *testing.T) {
mock, waitCh := startMockServer(t)
cliFunc := func(sock string, success bool) {
agent, err := newShimAgent(sock)
if success {
assert.Nil(t, err, "Failed to create new agent client: %s", err)
} else if !success {
assert.NotNil(t, err, "Unexpected success with sock address: %s", sock)
}
if err == nil {
agent.Close()
}
}
cliFunc(mockSockAddr, true)
cliFunc(unixMockAddr, true)
cliFunc(badMockAddr, false)
// wait mock server to stop
mock.Stop()
<-waitCh
}
func TestAddContainer(t *testing.T) {
agent := testSetup(t)
defer testTearDown(agent)
contID := "testContainer"
execID := "testExec"
err := agent.addContainer(contID, execID)
assert.Nil(t, err, "%s", err)
err = agent.addContainer(contID, execID)
assert.NotNil(t, err, "unexpected success when adding duplicated container")
}