This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
ctx.go
258 lines (223 loc) · 5.8 KB
/
ctx.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
// Copyright 2015 The rkt Authors
//
// 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 testutils
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/coreos/gexpect"
"github.com/rkt/rkt/tests/testutils/logger"
)
// dirDesc structure manages one directory and provides an option for
// rkt invocations
type dirDesc struct {
dir string // directory path
desc string // directory description, mostly for failure cases
prefix string // temporary directory prefix
option string // rkt option for given directory
}
// newDirDesc creates dirDesc instance managing a temporary directory.
func newDirDesc(prefix, desc, option string) *dirDesc {
dir := &dirDesc{
dir: "",
desc: desc,
prefix: prefix,
option: option,
}
dir.reset()
return dir
}
// reset removes the managed directory and recreates it
func (d *dirDesc) reset() {
d.cleanup()
dir, err := ioutil.TempDir("", d.prefix)
if err != nil {
panic(fmt.Sprintf("Failed to create temporary %s directory: %v", d.desc, err))
}
d.dir = dir
}
// cleanup removes the managed directory. After cleanup this instance
// cannot be used for anything, until it is reset.
func (d *dirDesc) cleanup() {
if d.dir == "" {
return
}
if err := os.RemoveAll(d.dir); err != nil && !os.IsNotExist(err) {
panic(fmt.Sprintf("Failed to remove temporary %s directory %q: %s", d.desc, d.dir, err))
}
d.dir = ""
}
// rktOption returns option for rkt invocation
func (d *dirDesc) rktOption() string {
d.ensureValid()
return fmt.Sprintf("--%s=%s", d.option, d.dir)
}
func (d *dirDesc) ensureValid() {
if d.dir == "" {
panic(fmt.Sprintf("A temporary %s directory is not set up", d.desc))
}
}
// TODO(emil): Document exported.
type RktRunCtx struct {
directories []*dirDesc
useDefaults bool
mds *exec.Cmd
children []*gexpect.ExpectSubprocess
}
func NewRktRunCtx() *RktRunCtx {
return &RktRunCtx{
directories: []*dirDesc{
newDirDesc("datadir-", "data", "dir"),
newDirDesc("localdir-", "local configuration", "local-config"),
newDirDesc("systemdir-", "system configuration", "system-config"),
newDirDesc("userdir-", "user configuration", "user-config"),
},
}
}
func (ctx *RktRunCtx) SetupDataDir() error {
return setupDataDir(ctx.DataDir())
}
func (ctx *RktRunCtx) LaunchMDS() error {
ctx.mds = exec.Command(ctx.rktBin(), "metadata-service")
return ctx.mds.Start()
}
func (ctx *RktRunCtx) DataDir() string {
return ctx.dir(0)
}
func (ctx *RktRunCtx) LocalDir() string {
return ctx.dir(1)
}
func (ctx *RktRunCtx) SystemDir() string {
return ctx.dir(2)
}
func (ctx *RktRunCtx) UserDir() string {
return ctx.dir(3)
}
func (ctx *RktRunCtx) dir(idx int) string {
ctx.ensureValid()
if idx < len(ctx.directories) {
return ctx.directories[idx].dir
}
panic("Directory index out of bounds")
}
func (ctx *RktRunCtx) Reset() {
ctx.cleanupChildren()
ctx.RunGC()
for _, d := range ctx.directories {
d.reset()
}
}
func (ctx *RktRunCtx) cleanupChildren() error {
for _, child := range ctx.children {
if child.Cmd == nil ||
child.Cmd.Process == nil ||
child.Cmd.ProcessState == nil {
continue
}
if child.Cmd.ProcessState.Exited() {
logger.Logf("Child %q already exited", child.Cmd.Path)
continue
}
logger.Logf("Shutting down child %q", child.Cmd.Path)
if err := child.Cmd.Process.Kill(); err != nil {
return err
}
if _, err := child.Cmd.Process.Wait(); err != nil {
return err
}
}
return nil
}
func (ctx *RktRunCtx) Cleanup() {
if ctx.mds != nil {
ctx.mds.Process.Kill()
ctx.mds.Wait()
os.Remove("/run/rkt/metadata-svc.sock")
}
if err := ctx.cleanupChildren(); err != nil {
logger.Logf("Error during child cleanup: %v", err)
}
ctx.RunGC()
for _, d := range ctx.directories {
d.cleanup()
}
}
func (ctx *RktRunCtx) RunGC() {
rktArgs := append(ctx.rktOptions(),
"gc",
"--grace-period=0s",
)
if err := exec.Command(ctx.rktBin(), rktArgs...).Run(); err != nil {
panic(fmt.Sprintf("Failed to run gc: %v", err))
}
}
func (ctx *RktRunCtx) Cmd() string {
return fmt.Sprintf("%s %s",
ctx.rktBin(),
strings.Join(ctx.rktOptions(), " "),
)
}
func (ctx *RktRunCtx) ExecCmd(arg ...string) *exec.Cmd {
args := ctx.rktOptions()
args = append(args, arg...)
return exec.Command(ctx.rktBin(), args...)
}
// TODO(jonboulle): clean this up
func (ctx *RktRunCtx) CmdNoConfig() string {
return fmt.Sprintf("%s %s",
ctx.rktBin(),
ctx.directories[0].rktOption(),
)
}
func (ctx *RktRunCtx) rktBin() string {
rkt := GetValueFromEnvOrPanic("RKT")
abs, err := filepath.Abs(rkt)
if err != nil {
abs = rkt
}
return abs
}
func (ctx *RktRunCtx) GetUidGidRktBinOwnerNotRoot() (int, int) {
s, err := os.Stat(ctx.rktBin())
if err != nil {
return GetUnprivilegedUidGid()
}
uid := int(s.Sys().(*syscall.Stat_t).Uid)
gid := int(s.Sys().(*syscall.Stat_t).Gid)
// If owner is root, fallback to user "nobody"
if uid == 0 {
return GetUnprivilegedUidGid()
}
return uid, gid
}
func (ctx *RktRunCtx) rktOptions() []string {
ctx.ensureValid()
opts := make([]string, 0, len(ctx.directories))
for _, d := range ctx.directories {
opts = append(opts, d.rktOption())
}
return opts
}
func (ctx *RktRunCtx) ensureValid() {
for _, d := range ctx.directories {
d.ensureValid()
}
}
func (ctx *RktRunCtx) RegisterChild(child *gexpect.ExpectSubprocess) {
ctx.children = append(ctx.children, child)
}