forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
321 lines (249 loc) · 6.54 KB
/
container.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
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package tests
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Container represents a kata container
type Container struct {
// Bundle contains the container information
// if nil then try to run the container without --bundle option
Bundle *Bundle
// Console pty slave path
// if nil then try to run the container without --console option
Console *string
// PidFile where process id is written
// if nil then try to run the container without --pid-file option
PidFile *string
// LogFile where debug information is written
// if nil then try to run the container without --log option
LogFile *string
// Detach allows to run the process detached from the shell
Detach bool
// ID of the container
// if nil then try to run the container without container ID
ID *string
}
// Process describes a process to be executed on a running container.
type Process struct {
ContainerID *string
Console *string
Tty *string
Detach bool
Workload []string
}
// NewContainer returns a new Container
func NewContainer(workload []string, detach bool) (*Container, error) {
b, err := NewBundle(workload)
if err != nil {
return nil, err
}
console := ""
pidFile := filepath.Join(b.Path, "pid")
logFile := filepath.Join(b.Path, "log")
id := RandID(20)
return &Container{
Bundle: b,
Console: &console,
PidFile: &pidFile,
LogFile: &logFile,
Detach: detach,
ID: &id,
}, nil
}
// Run the container
// calls to run command returning its stdout, stderr and exit code
func (c *Container) Run() (string, string, int) {
args := []string{}
if c.LogFile != nil {
args = append(args, fmt.Sprintf("--log=%s", *c.LogFile))
}
args = append(args, "run")
if c.Bundle != nil {
args = append(args, fmt.Sprintf("--bundle=%s", c.Bundle.Path))
}
if c.Console != nil {
args = append(args, fmt.Sprintf("--console=%s", *c.Console))
}
if c.PidFile != nil {
args = append(args, fmt.Sprintf("--pid-file=%s", *c.PidFile))
}
if c.Detach {
args = append(args, "--detach")
}
if c.ID != nil {
args = append(args, *c.ID)
}
cmd := NewCommand(Runtime, args...)
return cmd.Run()
}
// Delete the container
// calls to delete command returning its stdout, stderr and exit code
func (c *Container) Delete(force bool) (string, string, int) {
args := []string{"delete"}
if force {
args = append(args, "--force")
}
if c.ID != nil {
args = append(args, *c.ID)
}
cmd := NewCommand(Runtime, args...)
return cmd.Run()
}
// Kill the container
// calls to kill command returning its stdout, stderr and exit code
func (c *Container) Kill(all bool, signal interface{}) (string, string, int) {
args := []string{"kill"}
if all {
args = append(args, "--all")
}
if c.ID != nil {
args = append(args, *c.ID)
}
switch t := signal.(type) {
case syscall.Signal:
args = append(args, strconv.Itoa(int(t)))
case string:
args = append(args, t)
}
cmd := NewCommand(Runtime, args...)
return cmd.Run()
}
// Exec the container
// calls into exec command returning its stdout, stderr and exit code
func (c *Container) Exec(process Process) (string, string, int) {
args := []string{}
if c.LogFile != nil {
args = append(args, fmt.Sprintf("--log=%s", *c.LogFile))
}
args = append(args, "exec")
if process.Console != nil {
args = append(args, fmt.Sprintf("--console=%s", *process.Console))
}
if process.Tty != nil {
args = append(args, fmt.Sprintf("--tty=%s", *process.Tty))
}
if process.Detach {
args = append(args, "--detach")
}
if process.ContainerID != nil {
args = append(args, *process.ContainerID)
}
args = append(args, process.Workload...)
cmd := NewCommand(Runtime, args...)
return cmd.Run()
}
// State returns the state of the container
// calls into state command returning its stdout, stderr and exit code
func (c *Container) State() (string, string, int) {
args := []string{}
args = append(args, "state")
if c.ID != nil {
args = append(args, *c.ID)
}
cmd := NewCommand(Runtime, args...)
return cmd.Run()
}
// List the containers
// calls to list command returning its stdout, stderr and exit code
func (c *Container) List(format string, quiet bool, all bool) (string, string, int) {
args := []string{"list"}
if format != "" {
args = append(args, fmt.Sprintf("--format=%s", format))
}
if quiet {
args = append(args, "--quiet")
}
if all {
args = append(args, "--all")
}
cmd := NewCommand(Runtime, args...)
return cmd.Run()
}
// SetWorkload sets a workload for the container
func (c *Container) SetWorkload(workload []string) error {
c.Bundle.Config.Process.Args = workload
return c.Bundle.Save()
}
// RemoveOption removes a specific option
// container will run without the specific option
func (c *Container) RemoveOption(option string) error {
switch option {
case "--bundle", "-b":
defer c.Bundle.Remove()
c.Bundle = nil
case "--console":
c.Console = nil
case "--pid-file":
c.PidFile = nil
default:
return fmt.Errorf("undefined option '%s'", option)
}
return nil
}
// Teardown deletes the container if it is running,
// ensures the container is not running and removes any
// file created by the container
func (c *Container) Teardown() error {
var cid string
if c.ID != nil {
cid = *c.ID
}
// if container exist then delete it
if c.Exist() {
_, stderr, exitCode := c.Delete(true)
if exitCode != 0 {
return fmt.Errorf("failed to delete container %s %s", cid, stderr)
}
// if container still exist then fail
if c.Exist() {
return fmt.Errorf("unable to delete container %s", cid)
}
}
if c.Bundle != nil {
return c.Bundle.Remove()
}
return nil
}
// Exist returns true if any of next cases is true:
// - list command shows the container
// - the process id specified in the pid file is running (cc-shim)
// - the VM is running (qemu)
// - the proxy is running
// - the shim is running
// else false is returned
func (c *Container) Exist() bool {
return c.isListed() || c.isWorkloadRunning() ||
HypervisorRunning(*c.ID) || ProxyRunning(*c.ID) ||
ShimRunning(*c.ID)
}
func (c *Container) isListed() bool {
if c.ID == nil {
return false
}
stdout, _, ret := c.List("", true, false)
if ret != 0 {
return false
}
return strings.Contains(stdout, *c.ID)
}
func (c *Container) isWorkloadRunning() bool {
if c.PidFile == nil {
return false
}
content, err := ioutil.ReadFile(*c.PidFile)
if err != nil {
return false
}
if _, err := os.Stat(fmt.Sprintf("/proc/%s/stat", string(content))); os.IsNotExist(err) {
return false
}
return true
}