-
Notifications
You must be signed in to change notification settings - Fork 190
/
jailer_integ_test.go
290 lines (236 loc) · 8.99 KB
/
jailer_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
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
// 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 (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/firecracker-microvm/firecracker-containerd/firecracker-control"
"github.com/firecracker-microvm/firecracker-containerd/internal"
"github.com/firecracker-microvm/firecracker-containerd/internal/integtest"
"github.com/firecracker-microvm/firecracker-containerd/proto"
"github.com/firecracker-microvm/firecracker-containerd/runtime/cpuset"
"github.com/firecracker-microvm/firecracker-containerd/runtime/firecrackeroci"
)
const (
jailerUID = 300001
jailerGID = 300001
)
func assertEmptyShimDir(tb testing.TB, ns, vmID string) {
_, err := os.Stat(filepath.Join(integtest.ShimBaseDir(), ns+"#"+vmID))
assert.Error(tb, err)
assert.True(tb, os.IsNotExist(err))
shimContents, err := os.ReadDir(integtest.ShimBaseDir())
require.NoError(tb, err)
assert.Len(tb, shimContents, 0)
}
func TestJailer_Isolated(t *testing.T) {
integtest.Prepare(t)
t.Run("Without Jailer", func(t *testing.T) {
testJailer(t, nil)
})
t.Run("With Jailer", func(t *testing.T) {
testJailer(t, &proto.JailerConfig{
UID: jailerUID,
GID: jailerGID,
})
})
t.Run("With Jailer and bind-mount", func(t *testing.T) {
testJailer(t, &proto.JailerConfig{
UID: jailerUID,
GID: jailerGID,
DriveExposePolicy: proto.DriveExposePolicy_BIND,
})
})
}
func TestAttachBlockDevice_Isolated(t *testing.T) {
integtest.Prepare(t)
t.Run("Without Jailer", func(t *testing.T) {
testAttachBlockDevice(t, nil)
})
t.Run("With Jailer", func(t *testing.T) {
testAttachBlockDevice(t, &proto.JailerConfig{
UID: jailerUID,
GID: jailerGID,
})
})
t.Run("With Jailer and bind-mount", func(t *testing.T) {
testAttachBlockDevice(t, &proto.JailerConfig{
UID: jailerUID,
GID: jailerGID,
DriveExposePolicy: proto.DriveExposePolicy_BIND,
})
})
}
func fsSafeTestName(tb testing.TB) string {
return strings.ReplaceAll(tb.Name(), "/", "-")
}
func testJailer(t *testing.T, jailerConfig *proto.JailerConfig) {
client, err := containerd.New(integtest.ContainerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime))
require.NoError(t, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
defer client.Close()
ctx := namespaces.WithNamespace(context.Background(), "default")
image, err := alpineImage(ctx, client, defaultSnapshotterName)
require.NoError(t, err, "failed to get alpine image")
vmID := testNameToVMID(t.Name())
additionalDrive := internal.CreateFSImg(ctx, t, "ext4", internal.FSImgFile{
Subpath: "dir/hello",
Contents: "additional drive\n",
})
request := proto.CreateVMRequest{
VMID: vmID,
JailerConfig: jailerConfig,
DriveMounts: []*proto.FirecrackerDriveMount{
{HostPath: additionalDrive, VMPath: "/mnt", FilesystemType: "ext4"},
},
}
// If the drive files are bind-mounted, the files must be readable from the jailer's user.
if jailerConfig != nil && jailerConfig.DriveExposePolicy == proto.DriveExposePolicy_BIND {
f, err := os.CreateTemp("", fsSafeTestName(t)+"_rootfs")
require.NoError(t, err)
defer f.Close()
dst := f.Name()
// Copy the root drive before chown, since the file is used by other tests.
err = copyFile(integtest.DefaultRuntimeConfig.RootDrive, dst, 0400)
require.NoErrorf(t, err, "failed to copy a rootfs as %q", dst)
err = os.Chown(dst, int(jailerConfig.UID), int(jailerConfig.GID))
require.NoError(t, err, "failed to chown %q", dst)
request.RootDrive = &proto.FirecrackerRootDrive{HostPath: dst}
// The additional drive file is only used by this test.
err = os.Chown(additionalDrive, int(jailerConfig.UID), int(jailerConfig.GID))
require.NoError(t, err, "failed to chown %q", additionalDrive)
}
fcClient, err := integtest.NewFCControlClient(integtest.ContainerdSockPath)
require.NoError(t, err)
resp, err := fcClient.CreateVM(ctx, &request)
require.NoError(t, err)
if jailerConfig != nil {
assert.True(t, cgroupExists(resp.CgroupPath))
}
c, err := client.NewContainer(ctx,
vmID+"-container",
containerd.WithSnapshotter(defaultSnapshotterName),
containerd.WithNewSnapshot(vmID+"-snapshot", image),
containerd.WithNewSpec(
oci.WithProcessArgs(
"/bin/sh", "-c", "echo hello && cat /mnt/in-container/dir/hello",
),
firecrackeroci.WithVMID(vmID),
oci.WithMounts([]specs.Mount{{
Source: "/mnt",
Destination: "/mnt/in-container",
Options: []string{"bind"},
}}),
),
)
require.NoError(t, err)
stdout := startAndWaitTask(ctx, t, c)
require.Equal(t, "hello\nadditional drive\n", stdout)
stat, err := os.Stat(filepath.Join(integtest.ShimBaseDir(), "default#"+vmID))
require.NoError(t, err)
assert.True(t, stat.IsDir())
err = c.Delete(ctx, containerd.WithSnapshotCleanup)
require.NoError(t, err, "failed to delete a container")
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})
require.NoError(t, err)
assertEmptyShimDir(t, "default", vmID)
}
func TestJailerCPUSet_Isolated(t *testing.T) {
integtest.Prepare(t)
b := cpuset.Builder{}
cset := b.AddCPU(0).AddMem(0).Build()
config := &proto.JailerConfig{
CPUs: cset.CPUs(),
Mems: cset.Mems(),
UID: 300000,
GID: 300000,
}
testJailer(t, config)
}
func testAttachBlockDevice(tb testing.TB, jailerConfig *proto.JailerConfig) {
client, err := containerd.New(integtest.ContainerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime))
require.NoError(tb, err, "unable to create client to containerd service at %s, is containerd running?", integtest.ContainerdSockPath)
defer client.Close()
ctx := namespaces.WithNamespace(context.Background(), "default")
image, err := alpineImage(ctx, client, defaultSnapshotterName)
require.NoError(tb, err, "failed to get alpine image")
fcClient, err := integtest.NewFCControlClient(integtest.ContainerdSockPath)
require.NoError(tb, err)
vmID := testNameToVMID(tb.Name())
device, cleanup := internal.CreateBlockDevice(ctx, tb)
defer cleanup()
if jailerConfig != nil {
err := os.Chown(device, int(jailerConfig.UID), int(jailerConfig.GID))
require.NoError(tb, err)
}
request := proto.CreateVMRequest{
VMID: vmID,
JailerConfig: jailerConfig,
DriveMounts: []*proto.FirecrackerDriveMount{
{HostPath: device, VMPath: "/home/driveMount", FilesystemType: "ext4"},
},
}
// If the drive files are bind-mounted, the files must be readable from the jailer's user.
if jailerConfig != nil && jailerConfig.DriveExposePolicy == proto.DriveExposePolicy_BIND {
f, err := os.CreateTemp("", fsSafeTestName(tb)+"_rootfs")
require.NoError(tb, err)
defer f.Close()
dst := f.Name()
// Copy the root drive before chown, since the file is used by other tests.
err = copyFile(integtest.DefaultRuntimeConfig.RootDrive, dst, 0400)
require.NoErrorf(tb, err, "failed to copy a rootfs as %q", dst)
err = os.Chown(dst, int(jailerConfig.UID), int(jailerConfig.GID))
require.NoError(tb, err, "failed to chown %q", dst)
request.RootDrive = &proto.FirecrackerRootDrive{HostPath: dst}
err = os.Chown(device, int(jailerConfig.UID), int(jailerConfig.GID))
require.NoError(tb, err, "failed to chown %q", device)
}
_, err = fcClient.CreateVM(ctx, &request)
require.NoError(tb, err)
// create a container to test bind mount block device into the container
c, err := client.NewContainer(ctx,
vmID+"-container",
containerd.WithSnapshotter(defaultSnapshotterName),
containerd.WithNewSnapshot(vmID+"-snapshot", image),
containerd.WithNewSpec(
oci.WithProcessArgs(
"/bin/sh", "-c", "echo heyhey && cd /mnt/blockDeviceTest",
),
firecrackeroci.WithVMID(vmID),
oci.WithMounts([]specs.Mount{{
Source: "/home/driveMount",
Destination: "/mnt/blockDeviceTest",
Options: []string{"bind"},
}}),
),
)
require.NoError(tb, err)
stdout := startAndWaitTask(ctx, tb, c)
require.Equal(tb, "heyhey\n", stdout)
stat, err := os.Stat(filepath.Join(integtest.ShimBaseDir(), "default#"+vmID))
require.NoError(tb, err)
assert.True(tb, stat.IsDir())
err = c.Delete(ctx, containerd.WithSnapshotCleanup)
require.NoError(tb, err, "failed to delete a container-block-device")
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})
require.NoError(tb, err)
assertEmptyShimDir(tb, "default", vmID)
}