Skip to content

Commit 7dbee2d

Browse files
authored
Merge pull request #298 from kzys/test-limits
Test that the size of a root drive is capped
2 parents 386eba8 + 1415d87 commit 7dbee2d

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

runtime/integ_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
package main
1414

1515
import (
16+
"bytes"
17+
"context"
1618
"encoding/json"
1719
"os"
1820
"strings"
1921
"testing"
2022

23+
"github.com/containerd/containerd"
24+
"github.com/containerd/containerd/cio"
2125
"github.com/firecracker-microvm/firecracker-containerd/internal"
26+
"github.com/pkg/errors"
2227
)
2328

2429
const runtimeConfigPath = "/etc/containerd/firecracker-runtime.json"
@@ -90,3 +95,49 @@ var testNameToVMIDReplacer = strings.NewReplacer("/", "_")
9095
func testNameToVMID(s string) string {
9196
return testNameToVMIDReplacer.Replace(s)
9297
}
98+
99+
type commandResult struct {
100+
stdout string
101+
stderr string
102+
exitCode uint32
103+
}
104+
105+
func runTask(ctx context.Context, c containerd.Container) (*commandResult, error) {
106+
var stdout bytes.Buffer
107+
var stderr bytes.Buffer
108+
109+
task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr)))
110+
if err != nil {
111+
return nil, err
112+
}
113+
114+
exitCh, err := task.Wait(ctx)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
err = task.Start(ctx)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
select {
125+
case exitStatus := <-exitCh:
126+
if err := exitStatus.Error(); err != nil {
127+
return nil, err
128+
}
129+
130+
_, err := task.Delete(ctx)
131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
return &commandResult{
136+
stdout: stdout.String(),
137+
stderr: stderr.String(),
138+
exitCode: exitStatus.ExitCode(),
139+
}, nil
140+
case <-ctx.Done():
141+
return nil, errors.New("context cancelled")
142+
}
143+
}

runtime/limits_integ_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
package main
14+
15+
import (
16+
"context"
17+
"testing"
18+
19+
"github.com/containerd/containerd"
20+
"github.com/containerd/containerd/namespaces"
21+
"github.com/containerd/containerd/oci"
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func TestDiskLimit_Isolated(t *testing.T) {
27+
prepareIntegTest(t)
28+
29+
assert := assert.New(t)
30+
require := require.New(t)
31+
32+
ctx := namespaces.WithNamespace(context.Background(), "default")
33+
34+
client, err := containerd.New(containerdSockPath, containerd.WithDefaultRuntime(firecrackerRuntime))
35+
require.NoError(err, "unable to create client to containerd service at %s, is containerd running?", containerdSockPath)
36+
defer client.Close()
37+
38+
image, err := alpineImage(ctx, client, defaultSnapshotterName())
39+
require.NoError(err, "failed to get alpine image")
40+
41+
// Right now, both naive snapshotter and devmapper snapshotter are configured to have 1024MB image size.
42+
// The former is hard-coded since the snapshotter is not for production. The latter is configured in tools/docker/entrypoint.sh.
43+
sh := containerd.WithNewSpec(
44+
oci.WithProcessArgs("dd", "if=/dev/zero", "of=/tmp/fill", "bs=1M", "count=2000"),
45+
oci.WithDefaultPathEnv,
46+
)
47+
48+
container, err := client.NewContainer(ctx,
49+
"container",
50+
containerd.WithSnapshotter(defaultSnapshotterName()),
51+
containerd.WithNewSnapshot("snapshot", image),
52+
sh,
53+
)
54+
defer func() {
55+
err = container.Delete(ctx, containerd.WithSnapshotCleanup)
56+
require.NoError(err, "failed to delete a container")
57+
}()
58+
59+
result, err := runTask(ctx, container)
60+
require.NoError(err, "failed to create a container")
61+
62+
assert.Equal(uint32(1), result.exitCode, "writing 2GB must fail")
63+
assert.Equal(`952+0 records in
64+
951+0 records out
65+
`, result.stderr, "but it must be able to write ~1024MB")
66+
}

tools/docker/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ EOF
2626
[plugins]
2727
[plugins.devmapper]
2828
pool_name = "fcci--vg-${FICD_DM_POOL}"
29-
base_image_size = "128MB"
29+
base_image_size = "1024MB"
3030
EOF
3131
;;
3232
*)

0 commit comments

Comments
 (0)