forked from firecracker-microvm/firecracker-containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteg_test.go
161 lines (132 loc) · 3.91 KB
/
integ_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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 main
import (
"bytes"
"context"
"encoding/json"
"os"
"strings"
"testing"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/pkg/errors"
"github.com/firecracker-microvm/firecracker-containerd/config"
"github.com/firecracker-microvm/firecracker-containerd/firecracker-control/client"
"github.com/firecracker-microvm/firecracker-containerd/internal"
)
const runtimeConfigPath = "/etc/containerd/firecracker-runtime.json"
const shimBaseDirEnvVar = "SHIM_BASE_DIR"
const defaultShimBaseDir = "/srv/firecracker_containerd_tests"
var defaultRuntimeConfig = config.Config{
FirecrackerBinaryPath: "/usr/local/bin/firecracker",
KernelImagePath: "/var/lib/firecracker-containerd/runtime/default-vmlinux.bin",
KernelArgs: "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules systemd.unified_cgroup_hierarchy=0 systemd.journald.forward_to_console systemd.log_color=false systemd.unit=firecracker.target init=/sbin/overlay-init",
RootDrive: "/var/lib/firecracker-containerd/runtime/default-rootfs.img",
LogLevels: []string{"debug"},
ShimBaseDir: shimBaseDir(),
JailerConfig: config.JailerConfig{
RuncBinaryPath: "/usr/local/bin/runc",
RuncConfigPath: "/etc/containerd/firecracker-runc-config.json",
},
}
func init() {
flag, err := internal.SupportCPUTemplate()
if err != nil {
panic(err)
}
if flag {
defaultRuntimeConfig.CPUTemplate = "T2"
}
}
func shimBaseDir() string {
if v := os.Getenv(shimBaseDirEnvVar); v != "" {
return v
}
return defaultShimBaseDir
}
// devmapper is the only snapshotter we can use with Firecracker
const defaultSnapshotterName = "devmapper"
func prepareIntegTest(t testing.TB, options ...func(*config.Config)) {
t.Helper()
internal.RequiresIsolation(t)
err := writeRuntimeConfig(options...)
if err != nil {
t.Error(err)
}
}
func writeRuntimeConfig(options ...func(*config.Config)) error {
config := defaultRuntimeConfig
for _, option := range options {
option(&config)
}
file, err := os.OpenFile(runtimeConfigPath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
bytes, err := json.Marshal(config)
if err != nil {
return err
}
_, err = file.Write(bytes)
if err != nil {
return err
}
return nil
}
var testNameToVMIDReplacer = strings.NewReplacer("/", "-", "_", "-")
func testNameToVMID(s string) string {
return testNameToVMIDReplacer.Replace(s)
}
type commandResult struct {
stdout string
stderr string
exitCode uint32
}
func runTask(ctx context.Context, c containerd.Container) (*commandResult, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr)))
if err != nil {
return nil, err
}
exitCh, err := task.Wait(ctx)
if err != nil {
return nil, err
}
err = task.Start(ctx)
if err != nil {
return nil, err
}
select {
case exitStatus := <-exitCh:
if err := exitStatus.Error(); err != nil {
return nil, err
}
_, err := task.Delete(ctx)
if err != nil {
return nil, err
}
return &commandResult{
stdout: stdout.String(),
stderr: stderr.String(),
exitCode: exitStatus.ExitCode(),
}, nil
case <-ctx.Done():
return nil, errors.New("context cancelled")
}
}
func newFCControlClient(socket string) (*client.Client, error) {
return client.New(socket + ".ttrpc")
}