/
e2e_matrix_test.go
393 lines (335 loc) · 8.52 KB
/
e2e_matrix_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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//go:build windows && functional
// +build windows,functional
package runhcs
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"testing"
"github.com/Microsoft/go-winio/vhd"
"github.com/Microsoft/hcsshim/osversion"
runhcs "github.com/Microsoft/hcsshim/pkg/go-runhcs"
"github.com/Microsoft/hcsshim/test/internal/layers"
"github.com/Microsoft/hcsshim/test/internal/require"
runc "github.com/containerd/go-runc"
"github.com/opencontainers/runtime-tools/generate"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// These support matrix of runhcs.exe for end to end activations is quite
// complex. These tests attempt to codify a simple start test on each support
// host/guest/isolation type so that we can have at least minimal confidence
// when changing code that activations across all platforms still work.
//
// Host OS | Container OS | Isolation
//
// RS1 | RS1 | V1 - Argon, Xenon
//
// RS3 | RS1 | V1 - Xenon
// | RS3 | V1 - Argon, Xenon
//
// RS4 | RS1, RS3 | V1 - Xenon
// | RS4 | V1 - Argon, Xenon
//
// RS5 | RS1, RS3, RS4 | V2 - UVM + Argon
// | RS5 | V2 - Argon, UVM + Argon, UVM + Argon (s) (POD's)
// | LCOW | V2 - UVM + Linux Container, UVM + Linux Container (s) (POD's)
var _ = (runc.IO)(&testIO{})
type testIO struct {
g *errgroup.Group
or, ow *os.File
outBuff *bytes.Buffer
er, ew *os.File
errBuff *bytes.Buffer
}
func newTestIO(t *testing.T) *testIO {
var err error
tio := &testIO{
outBuff: &bytes.Buffer{},
errBuff: &bytes.Buffer{},
}
defer func() {
if err != nil {
tio.Close()
}
}()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create stdout pipes: %v", err)
}
tio.or, tio.ow = r, w
r, w, err = os.Pipe()
if err != nil {
t.Fatalf("failed to create stderr pipes: %v", err)
}
tio.er, tio.ew = r, w
g, _ := errgroup.WithContext(context.TODO())
tio.g = g
tio.g.Go(func() error {
_, err := io.Copy(tio.outBuff, tio.Stdout())
return err
})
tio.g.Go(func() error {
_, err := io.Copy(tio.errBuff, tio.Stderr())
return err
})
return tio
}
func (t *testIO) Stdin() io.WriteCloser {
return nil
}
func (t *testIO) Stdout() io.ReadCloser {
return t.or
}
func (t *testIO) Stderr() io.ReadCloser {
return t.er
}
func (t *testIO) Set(cmd *exec.Cmd) {
cmd.Stdout = t.ow
cmd.Stderr = t.ew
}
func (t *testIO) Close() error {
var err error
for _, v := range []*os.File{
t.ow, t.ew,
t.or, t.er,
} {
if cerr := v.Close(); err == nil {
err = cerr
}
}
return err
}
func (t *testIO) CloseAfterStart() error {
t.ow.Close()
t.ew.Close()
return nil
}
func (t *testIO) Wait() error {
return t.g.Wait()
}
func getWindowsImageNameByVersion(t *testing.T, bv int) string {
switch bv {
case osversion.RS1:
return "mcr.microsoft.com/windows/nanoserver:sac2016"
case osversion.RS3:
return "mcr.microsoft.com/windows/nanoserver:1709"
case osversion.RS4:
return "mcr.microsoft.com/windows/nanoserver:1803"
case osversion.RS5:
// testImage = "mcr.microsoft.com/windows/nanoserver:1809"
return "mcr.microsoft.com/windows/nanoserver/insider:10.0.17763.55"
default:
t.Fatalf("unsupported build (%d) for Windows containers", bv)
}
// Won't hit because of t.Fatal
return ""
}
func readPidFile(path string) (int, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return -1, errors.Wrap(err, "failed to read pidfile")
}
p, err := strconv.Atoi(string(data))
if err != nil {
return -1, errors.Wrap(err, "pidfile failed to parse pid")
}
return p, nil
}
func testWindows(t *testing.T, version int, isolated bool) {
var err error
// Make the bundle
bundle := t.TempDir()
defer func() {
if err == nil {
os.RemoveAll(bundle)
} else {
t.Errorf("additional logs at bundle path: %v", bundle)
}
}()
scratch := t.TempDir()
defer func() {
vhd.DetachVhd(filepath.Join(scratch, "sandbox.vhdx"))
os.RemoveAll(scratch)
}()
// Generate the Spec
g, err := generate.New("windows")
if err != nil {
t.Errorf("failed to generate Windows config with error: %v", err)
return
}
g.SetProcessArgs([]string{"cmd", "/c", "echo Hello World!"})
if isolated {
g.SetWindowsHypervUntilityVMPath("")
}
g.Config.Windows.Network = nil
// Get the LayerFolders
imageName := getWindowsImageNameByVersion(t, version)
layers := layers.LayerFolders(t, imageName)
for _, layer := range layers {
g.AddWindowsLayerFolders(layer)
}
g.AddWindowsLayerFolders(scratch)
cf, err := os.Create(filepath.Join(bundle, "config.json"))
if err != nil {
t.Errorf("failed to create config.json with error: %v", err)
return
}
err = json.NewEncoder(cf).Encode(g.Config)
if err != nil {
cf.Close()
t.Errorf("failed to encode config.json with error: %v", err)
return
}
cf.Close()
// Create the Argon, Xenon, or UVM
ctx := context.TODO()
rhcs := runhcs.Runhcs{
Debug: true,
}
tio := newTestIO(t)
defer func() {
if err != nil {
t.Errorf("additional info stdout: '%v', stderr: '%v'", tio.outBuff.String(), tio.errBuff.String())
}
}()
defer func() {
tio.Close()
}()
copts := &runhcs.CreateOpts{
IO: tio,
PidFile: filepath.Join(bundle, "pid-file.txt"),
ShimLog: filepath.Join(bundle, "shim-log.txt"),
}
if isolated {
copts.VMLog = filepath.Join(bundle, "vm-log.txt")
}
err = rhcs.Create(ctx, t.Name(), bundle, copts)
if err != nil {
t.Errorf("failed to create container with error: %v", err)
return
}
defer func() {
rhcs.Delete(ctx, t.Name(), &runhcs.DeleteOpts{Force: true})
}()
// Find the shim/vmshim process and begin exit wait
pid, err := readPidFile(copts.PidFile)
if err != nil {
t.Errorf("failed to read pidfile with error: %v", err)
return
}
p, err := os.FindProcess(pid)
if err != nil {
t.Errorf("failed to find container process by pid: %d, with error: %v", pid, err)
return
}
// Start the container
err = rhcs.Start(ctx, t.Name())
if err != nil {
t.Errorf("failed to start container with error: %v", err)
return
}
defer func() {
if err != nil {
rhcs.Kill(ctx, t.Name(), "CtrlC")
}
}()
// Wait for process exit, verify the exited state
var exitStatus int
_, eerr := p.Wait()
if eerr != nil {
if exitErr, ok := eerr.(*exec.ExitError); ok {
if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
exitStatus = ws.ExitStatus()
}
}
}
if exitStatus != 0 {
err = eerr
t.Errorf("container process failed with exit status: %d", exitStatus)
return
}
// Wait for the relay to exit
tio.Wait()
outString := tio.outBuff.String()
if outString != "Hello World!\r\n" {
t.Errorf("stdout expected: 'Hello World!', got: '%v'", outString)
}
errString := tio.errBuff.String()
if errString != "" {
t.Errorf("stderr expected: '', got: '%v'", errString)
}
}
func testWindowsPod(t *testing.T, version int, isolated bool) {
t.Skip("not implemented")
}
func testLCOW(t *testing.T) {
t.Skip("not implemented")
}
func testLCOWPod(t *testing.T) {
t.Skip("not implemented")
}
func Test_RS1_Argon(t *testing.T) {
require.ExactBuild(t, osversion.RS1)
testWindows(t, osversion.RS1, false)
}
func Test_RS1_Xenon(t *testing.T) {
require.ExactBuild(t, osversion.RS1)
testWindows(t, osversion.RS1, true)
}
func Test_RS3_Argon(t *testing.T) {
require.ExactBuild(t, osversion.RS3)
testWindows(t, osversion.RS3, false)
}
func Test_RS3_Xenon(t *testing.T) {
require.ExactBuild(t, osversion.RS3)
guests := []int{osversion.RS1, osversion.RS3}
for _, g := range guests {
testWindows(t, g, true)
}
}
func Test_RS4_Argon(t *testing.T) {
require.ExactBuild(t, osversion.RS4)
testWindows(t, osversion.RS4, false)
}
func Test_RS4_Xenon(t *testing.T) {
require.ExactBuild(t, osversion.RS4)
guests := []int{osversion.RS1, osversion.RS3, osversion.RS4}
for _, g := range guests {
testWindows(t, g, true)
}
}
func Test_RS5_Argon(t *testing.T) {
require.ExactBuild(t, osversion.RS5)
testWindows(t, osversion.RS5, false)
}
func Test_RS5_ArgonPods(t *testing.T) {
require.ExactBuild(t, osversion.RS5)
testWindowsPod(t, osversion.RS5, false)
}
func Test_RS5_UVMAndContainer(t *testing.T) {
require.ExactBuild(t, osversion.RS5)
guests := []int{osversion.RS1, osversion.RS3, osversion.RS4, osversion.RS5}
for _, g := range guests {
testWindows(t, g, true)
}
}
func Test_RS5_UVMPods(t *testing.T) {
require.ExactBuild(t, osversion.RS5)
testWindowsPod(t, osversion.RS5, true)
}
func Test_RS5_LCOW(t *testing.T) {
require.ExactBuild(t, osversion.RS5)
testLCOW(t)
}
func Test_RS5_LCOW_UVMPods(t *testing.T) {
require.ExactBuild(t, osversion.RS5)
testLCOWPod(t)
}