forked from opencontainers/runtime-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.go
More file actions
186 lines (161 loc) · 3.97 KB
/
test.go
File metadata and controls
186 lines (161 loc) · 3.97 KB
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
package util
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"github.com/mndrix/tap-go"
"github.com/mrunalp/fileutils"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/satori/go.uuid"
)
var (
// RuntimeCommand is the default runtime command.
RuntimeCommand = "runc"
)
// PreFunc initializes the test environment after preparing the bundle
// but before creating the container.
type PreFunc func(string) error
// AfterFunc validate container's outside environment after created
type AfterFunc func(config *rspec.Spec, state *rspec.State) error
func init() {
runtimeInEnv := os.Getenv("RUNTIME")
if runtimeInEnv != "" {
RuntimeCommand = runtimeInEnv
}
}
// Fatal prints a warning to stderr and exits.
func Fatal(err error) {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
// Skip skips a full TAP suite.
func Skip(message string, diagnostic interface{}) {
t := tap.New()
t.Header(1)
t.Skip(1, message)
if diagnostic != nil {
t.YAML(diagnostic)
}
}
// PrepareBundle creates a test bundle in a temporary directory.
func PrepareBundle() (string, error) {
bundleDir, err := ioutil.TempDir("", "ocitest")
if err != nil {
return "", err
}
// Untar the root fs
untarCmd := exec.Command("tar", "-xf", fmt.Sprintf("rootfs-%s.tar.gz", runtime.GOARCH), "-C", bundleDir)
output, err := untarCmd.CombinedOutput()
if err != nil {
os.Stderr.Write(output)
os.RemoveAll(bundleDir)
return "", err
}
return bundleDir, nil
}
// GetDefaultGenerator creates a default configuration generator.
func GetDefaultGenerator() *generate.Generator {
g := generate.New()
g.SetRootPath(".")
g.SetProcessArgs([]string{"/runtimetest", "--path=/"})
return &g
}
// RuntimeInsideValidate runs runtimetest inside a container.
func RuntimeInsideValidate(g *generate.Generator, f PreFunc) (err error) {
bundleDir, err := PrepareBundle()
if err != nil {
return err
}
if f != nil {
if err := f(bundleDir); err != nil {
return err
}
}
r, err := NewRuntime(RuntimeCommand, bundleDir)
if err != nil {
os.RemoveAll(bundleDir)
return err
}
defer r.Clean(true, true)
err = r.SetConfig(g)
if err != nil {
return err
}
err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
if err != nil {
return err
}
r.SetID(uuid.NewV4().String())
stderr, err := r.Create()
if err != nil {
os.Stderr.WriteString("failed to create the container\n")
os.Stderr.Write(stderr)
return err
}
// FIXME: why do we need this? Without a sleep here, I get:
// failed to start the container
// container "..." does not exist
time.Sleep(1 * time.Second)
stderr, err = r.Start()
if err != nil {
os.Stderr.WriteString("failed to start the container\n")
os.Stderr.Write(stderr)
return err
}
// FIXME: wait until the container exits and collect its exit code.
time.Sleep(1 * time.Second)
stdout, stderr, err := r.ReadStandardStreams()
if err != nil {
if len(stderr) == 0 {
stderr = stdout
}
os.Stderr.WriteString("failed to read standard streams\n")
os.Stderr.Write(stderr)
return err
}
os.Stdout.Write(stdout)
return nil
}
// RuntimeOutsideValidate validate runtime outside a container.
func RuntimeOutsideValidate(g *generate.Generator, f AfterFunc) error {
bundleDir, err := PrepareBundle()
if err != nil {
return err
}
r, err := NewRuntime(RuntimeCommand, bundleDir)
if err != nil {
os.RemoveAll(bundleDir)
return err
}
defer r.Clean(true, true)
err = r.SetConfig(g)
if err != nil {
return err
}
err = fileutils.CopyFile("runtimetest", filepath.Join(r.BundleDir, "runtimetest"))
if err != nil {
return err
}
r.SetID(uuid.NewV4().String())
stderr, err := r.Create()
if err != nil {
os.Stderr.WriteString("failed to create the container\n")
os.Stderr.Write(stderr)
return err
}
if f != nil {
state, err := r.State()
if err != nil {
return err
}
if err := f(g.Spec(), &state); err != nil {
return err
}
}
return nil
}