forked from clearcontainers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
257 lines (205 loc) · 5.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
/// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tests
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Container represents a clear 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
// Debug enables debug output
Debug bool
// LogFile where debug information is written
// if nil then try to run the container without --log option
LogFile *string
// ID of the container
// if nil then try to run the container without container ID
ID *string
}
// NewContainer returns a new Container
func NewContainer(workload []string) (*Container, error) {
b, err := NewBundle(workload)
if err != nil {
return nil, err
}
console := "/dev/ptmx"
pidFile := filepath.Join(b.Path, "pid")
logFile := filepath.Join(b.Path, "log")
id := RandID(20)
return &Container{
Bundle: b,
Console: &console,
PidFile: &pidFile,
Debug: true,
LogFile: &logFile,
ID: &id,
}, nil
}
// Run the container
// calls to run command returning its stdout, stderr and exit code
func (c *Container) Run() (bytes.Buffer, bytes.Buffer, int) {
args := []string{}
if c.Debug {
args = append(args, "--debug")
}
if c.LogFile != nil {
args = append(args, "--log", *c.LogFile)
}
args = append(args, "run")
if c.Bundle != nil {
args = append(args, "--bundle", c.Bundle.Path)
}
if c.Console != nil {
args = append(args, "--console", *c.Console)
}
if c.PidFile != nil {
args = append(args, "--pid-file", *c.PidFile)
}
if c.ID != nil {
args = append(args, *c.ID)
}
cmd := NewCommand(Runtime, args...)
ret := cmd.Run()
return cmd.Stdout, cmd.Stderr, ret
}
// Delete the container
// calls to delete command returning its stdout, stderr and exit code
func (c *Container) Delete(force bool) (bytes.Buffer, bytes.Buffer, int) {
args := []string{"delete"}
if force {
args = append(args, "--force")
}
if c.ID != nil {
args = append(args, *c.ID)
}
cmd := NewCommand(Runtime, args...)
ret := cmd.Run()
return cmd.Stdout, cmd.Stderr, ret
}
// Kill the container
// calls to kill command returning its stdout, stderr and exit code
func (c *Container) Kill(all bool, signal interface{}) (bytes.Buffer, bytes.Buffer, 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...)
ret := cmd.Run()
return cmd.Stdout, cmd.Stderr, ret
}
// List the containers
// calls to list command returning its stdout, stderr and exit code
func (c *Container) List(format string, quiet bool, all bool) (bytes.Buffer, bytes.Buffer, int) {
args := []string{"list"}
if format != "" {
args = append(args, "--format", format)
}
if quiet {
args = append(args, "--quiet")
}
if all {
args = append(args, "--all")
}
cmd := NewCommand(Runtime, args...)
ret := cmd.Run()
return cmd.Stdout, cmd.Stderr, ret
}
// 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
}
// Cleanup removes files and directories created by the container
// returns an error if a file or directory can not be removed
func (c *Container) Cleanup() error {
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)
// else false is returned
func (c *Container) Exist() bool {
return c.isListed() || c.isWorkloadRunning() || c.isVMRunning()
}
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.String(), *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
}
func (c *Container) isVMRunning() bool {
// FIXME: find a way to check if the VM is still running
return false
}