Skip to content

Commit 88eec19

Browse files
Merge pull request #627 from austinvazquez/integ-test
refactor: integration test common code into internal module
2 parents c900089 + 7875f9c commit 88eec19

File tree

11 files changed

+174
-106
lines changed

11 files changed

+174
-106
lines changed

internal/integtest/config.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 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+
14+
package integtest
15+
16+
import (
17+
"encoding/json"
18+
"os"
19+
20+
"github.com/firecracker-microvm/firecracker-containerd/config"
21+
)
22+
23+
const runtimeConfigPath = "/etc/containerd/firecracker-runtime.json"
24+
25+
// DefaultRuntimeConfig represents a simple firecracker-containerd configuration.
26+
var DefaultRuntimeConfig = config.Config{
27+
FirecrackerBinaryPath: "/usr/local/bin/firecracker",
28+
KernelImagePath: "/var/lib/firecracker-containerd/runtime/default-vmlinux.bin",
29+
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",
30+
RootDrive: "/var/lib/firecracker-containerd/runtime/default-rootfs.img",
31+
LogLevels: []string{"debug"},
32+
ShimBaseDir: ShimBaseDir(),
33+
JailerConfig: config.JailerConfig{
34+
RuncBinaryPath: "/usr/local/bin/runc",
35+
RuncConfigPath: "/etc/containerd/firecracker-runc-config.json",
36+
},
37+
}
38+
39+
func writeRuntimeConfig(options ...func(*config.Config)) error {
40+
config := DefaultRuntimeConfig
41+
for _, option := range options {
42+
option(&config)
43+
}
44+
45+
file, err := os.OpenFile(runtimeConfigPath, os.O_CREATE|os.O_WRONLY, 0600)
46+
if err != nil {
47+
return err
48+
}
49+
defer file.Close()
50+
51+
bytes, err := json.Marshal(config)
52+
if err != nil {
53+
return err
54+
}
55+
56+
_, err = file.Write(bytes)
57+
if err != nil {
58+
return err
59+
}
60+
61+
return nil
62+
}

internal/integtest/prepare.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 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+
14+
package integtest
15+
16+
import (
17+
"testing"
18+
19+
"github.com/firecracker-microvm/firecracker-containerd/config"
20+
"github.com/firecracker-microvm/firecracker-containerd/internal"
21+
)
22+
23+
// Prepare is a common integration test setup function which ensures
24+
// isolation and prepares the runtime configuration for firecracker-containerd
25+
func Prepare(t testing.TB, options ...func(*config.Config)) {
26+
t.Helper()
27+
28+
internal.RequiresIsolation(t)
29+
30+
err := writeRuntimeConfig(options...)
31+
if err != nil {
32+
t.Error(err)
33+
}
34+
}

internal/integtest/shim.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 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+
14+
package integtest
15+
16+
import "os"
17+
18+
const shimBaseDirEnvVar = "SHIM_BASE_DIR"
19+
const defaultShimBaseDir = "/srv/firecracker_containerd_tests"
20+
21+
// ShimBaseDir checks the "SHIM_BASE_DIR" environment variable and returns its value
22+
// if it exists, else returns the default value
23+
func ShimBaseDir() string {
24+
if v := os.Getenv(shimBaseDirEnvVar); v != "" {
25+
return v
26+
}
27+
28+
return defaultShimBaseDir
29+
}

runtime/benchmark_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/containerd/containerd"
2222
"github.com/containerd/containerd/namespaces"
23+
"github.com/firecracker-microvm/firecracker-containerd/internal/integtest"
2324
"github.com/firecracker-microvm/firecracker-containerd/proto"
2425
fccontrol "github.com/firecracker-microvm/firecracker-containerd/proto/service/fccontrol/ttrpc"
2526
"github.com/gofrs/uuid"
@@ -94,13 +95,13 @@ func benchmarkCreateAndStopVM(t *testing.T, vcpuCount uint32, kernelArgs string)
9495
}
9596

9697
func TestPerf_CreateVM(t *testing.T) {
97-
prepareIntegTest(t)
98+
integtest.Prepare(t)
9899

99100
t.Run("vcpu=1", func(subtest *testing.T) {
100-
benchmarkCreateAndStopVM(subtest, 1, defaultRuntimeConfig.KernelArgs)
101+
benchmarkCreateAndStopVM(subtest, 1, integtest.DefaultRuntimeConfig.KernelArgs)
101102
})
102103
t.Run("vcpu=5", func(subtest *testing.T) {
103-
benchmarkCreateAndStopVM(subtest, 1, defaultRuntimeConfig.KernelArgs)
104+
benchmarkCreateAndStopVM(subtest, 1, integtest.DefaultRuntimeConfig.KernelArgs)
104105
})
105106
t.Run("firecracker-demo", func(subtest *testing.T) {
106107
benchmarkCreateAndStopVM(

runtime/cni_integ_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ import (
3434

3535
"github.com/firecracker-microvm/firecracker-containerd/config"
3636
"github.com/firecracker-microvm/firecracker-containerd/internal"
37+
"github.com/firecracker-microvm/firecracker-containerd/internal/integtest"
3738
"github.com/firecracker-microvm/firecracker-containerd/proto"
3839
"github.com/firecracker-microvm/firecracker-containerd/runtime/firecrackeroci"
3940
)
4041

4142
func TestCNISupport_Isolated(t *testing.T) {
42-
prepareIntegTest(t)
43+
integtest.Prepare(t)
4344
testTimeout := 120 * time.Second
4445
ctx, cancel := context.WithTimeout(namespaces.WithNamespace(context.Background(), defaultNamespace), testTimeout)
4546
defer cancel()
@@ -147,7 +148,7 @@ func TestCNISupport_Isolated(t *testing.T) {
147148
}
148149

149150
func TestAutomaticCNISupport_Isolated(t *testing.T) {
150-
prepareIntegTest(t, withDefaultNetwork())
151+
integtest.Prepare(t, withDefaultNetwork())
151152

152153
testTimeout := 120 * time.Second
153154
ctx, cancel := context.WithTimeout(namespaces.WithNamespace(context.Background(), defaultNamespace), testTimeout)
@@ -213,7 +214,7 @@ func TestAutomaticCNISupport_Isolated(t *testing.T) {
213214
}
214215

215216
func TestCNIPlugin_Performance(t *testing.T) {
216-
prepareIntegTest(t)
217+
integtest.Prepare(t)
217218

218219
numVMs := perfTestVMCount(t)
219220
runtimeDuration := perfTestRuntime(t)

runtime/integ_test.go

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,95 +15,31 @@ package main
1515
import (
1616
"bytes"
1717
"context"
18-
"encoding/json"
19-
"os"
2018
"strings"
21-
"testing"
2219

2320
"github.com/containerd/containerd"
2421
"github.com/containerd/containerd/cio"
2522
"github.com/pkg/errors"
2623

27-
"github.com/firecracker-microvm/firecracker-containerd/config"
2824
"github.com/firecracker-microvm/firecracker-containerd/firecracker-control/client"
2925
"github.com/firecracker-microvm/firecracker-containerd/internal"
26+
"github.com/firecracker-microvm/firecracker-containerd/internal/integtest"
3027
)
3128

32-
const runtimeConfigPath = "/etc/containerd/firecracker-runtime.json"
33-
const shimBaseDirEnvVar = "SHIM_BASE_DIR"
34-
const defaultShimBaseDir = "/srv/firecracker_containerd_tests"
35-
36-
var defaultRuntimeConfig = config.Config{
37-
FirecrackerBinaryPath: "/usr/local/bin/firecracker",
38-
KernelImagePath: "/var/lib/firecracker-containerd/runtime/default-vmlinux.bin",
39-
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",
40-
RootDrive: "/var/lib/firecracker-containerd/runtime/default-rootfs.img",
41-
LogLevels: []string{"debug"},
42-
ShimBaseDir: shimBaseDir(),
43-
JailerConfig: config.JailerConfig{
44-
RuncBinaryPath: "/usr/local/bin/runc",
45-
RuncConfigPath: "/etc/containerd/firecracker-runc-config.json",
46-
},
47-
}
48-
4929
func init() {
5030
flag, err := internal.SupportCPUTemplate()
5131
if err != nil {
5232
panic(err)
5333
}
5434

5535
if flag {
56-
defaultRuntimeConfig.CPUTemplate = "T2"
36+
integtest.DefaultRuntimeConfig.CPUTemplate = "T2"
5737
}
5838
}
5939

60-
func shimBaseDir() string {
61-
if v := os.Getenv(shimBaseDirEnvVar); v != "" {
62-
return v
63-
}
64-
65-
return defaultShimBaseDir
66-
}
67-
6840
// devmapper is the only snapshotter we can use with Firecracker
6941
const defaultSnapshotterName = "devmapper"
7042

71-
func prepareIntegTest(t testing.TB, options ...func(*config.Config)) {
72-
t.Helper()
73-
74-
internal.RequiresIsolation(t)
75-
76-
err := writeRuntimeConfig(options...)
77-
if err != nil {
78-
t.Error(err)
79-
}
80-
}
81-
82-
func writeRuntimeConfig(options ...func(*config.Config)) error {
83-
config := defaultRuntimeConfig
84-
for _, option := range options {
85-
option(&config)
86-
}
87-
88-
file, err := os.OpenFile(runtimeConfigPath, os.O_CREATE|os.O_WRONLY, 0600)
89-
if err != nil {
90-
return err
91-
}
92-
defer file.Close()
93-
94-
bytes, err := json.Marshal(config)
95-
if err != nil {
96-
return err
97-
}
98-
99-
_, err = file.Write(bytes)
100-
if err != nil {
101-
return err
102-
}
103-
104-
return nil
105-
}
106-
10743
var testNameToVMIDReplacer = strings.NewReplacer("/", "-", "_", "-")
10844

10945
func testNameToVMID(s string) string {

runtime/jailer_integ_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
_ "github.com/firecracker-microvm/firecracker-containerd/firecracker-control"
3232
"github.com/firecracker-microvm/firecracker-containerd/internal"
33+
"github.com/firecracker-microvm/firecracker-containerd/internal/integtest"
3334
"github.com/firecracker-microvm/firecracker-containerd/proto"
3435
"github.com/firecracker-microvm/firecracker-containerd/runtime/cpuset"
3536
"github.com/firecracker-microvm/firecracker-containerd/runtime/firecrackeroci"
@@ -41,7 +42,7 @@ const (
4142
)
4243

4344
func TestJailer_Isolated(t *testing.T) {
44-
prepareIntegTest(t)
45+
integtest.Prepare(t)
4546
t.Run("Without Jailer", func(t *testing.T) {
4647
t.Parallel()
4748
testJailer(t, nil)
@@ -64,7 +65,7 @@ func TestJailer_Isolated(t *testing.T) {
6465
}
6566

6667
func TestAttachBlockDevice_Isolated(t *testing.T) {
67-
prepareIntegTest(t)
68+
integtest.Prepare(t)
6869
t.Run("Without Jailer", func(t *testing.T) {
6970
t.Parallel()
7071
testAttachBlockDevice(t, nil)
@@ -126,7 +127,7 @@ func testJailer(t *testing.T, jailerConfig *proto.JailerConfig) {
126127
dst := f.Name()
127128

128129
// Copy the root drive before chown, since the file is used by other tests.
129-
err = copyFile(defaultRuntimeConfig.RootDrive, dst, 0400)
130+
err = copyFile(integtest.DefaultRuntimeConfig.RootDrive, dst, 0400)
130131
require.NoErrorf(err, "failed to copy a rootfs as %q", dst)
131132

132133
err = os.Chown(dst, int(jailerConfig.UID), int(jailerConfig.GID))
@@ -166,7 +167,7 @@ func testJailer(t *testing.T, jailerConfig *proto.JailerConfig) {
166167
stdout := startAndWaitTask(ctx, t, c)
167168
require.Equal("hello\nadditional drive\n", stdout)
168169

169-
stat, err := os.Stat(filepath.Join(shimBaseDir(), "default#"+vmID))
170+
stat, err := os.Stat(filepath.Join(integtest.ShimBaseDir(), "default#"+vmID))
170171
require.NoError(err)
171172
assert.True(t, stat.IsDir())
172173

@@ -176,17 +177,17 @@ func testJailer(t *testing.T, jailerConfig *proto.JailerConfig) {
176177
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})
177178
require.NoError(err)
178179

179-
_, err = os.Stat(filepath.Join(shimBaseDir(), "default#"+vmID))
180+
_, err = os.Stat(filepath.Join(integtest.ShimBaseDir(), "default#"+vmID))
180181
assert.Error(t, err)
181182
assert.True(t, os.IsNotExist(err))
182183

183-
shimContents, err := ioutil.ReadDir(shimBaseDir())
184+
shimContents, err := ioutil.ReadDir(integtest.ShimBaseDir())
184185
require.NoError(err)
185186
assert.Len(t, shimContents, 0)
186187
}
187188

188189
func TestJailerCPUSet_Isolated(t *testing.T) {
189-
prepareIntegTest(t)
190+
integtest.Prepare(t)
190191

191192
b := cpuset.Builder{}
192193
cset := b.AddCPU(0).AddMem(0).Build()
@@ -240,7 +241,7 @@ func testAttachBlockDevice(t *testing.T, jailerConfig *proto.JailerConfig) {
240241
dst := f.Name()
241242

242243
// Copy the root drive before chown, since the file is used by other tests.
243-
err = copyFile(defaultRuntimeConfig.RootDrive, dst, 0400)
244+
err = copyFile(integtest.DefaultRuntimeConfig.RootDrive, dst, 0400)
244245
require.NoErrorf(err, "failed to copy a rootfs as %q", dst)
245246

246247
err = os.Chown(dst, int(jailerConfig.UID), int(jailerConfig.GID))
@@ -277,7 +278,7 @@ func testAttachBlockDevice(t *testing.T, jailerConfig *proto.JailerConfig) {
277278
stdout := startAndWaitTask(ctx, t, c)
278279
require.Equal("heyhey\n", stdout)
279280

280-
stat, err := os.Stat(filepath.Join(shimBaseDir(), "default#"+vmID))
281+
stat, err := os.Stat(filepath.Join(integtest.ShimBaseDir(), "default#"+vmID))
281282
require.NoError(err)
282283
assert.True(t, stat.IsDir())
283284

@@ -287,11 +288,11 @@ func testAttachBlockDevice(t *testing.T, jailerConfig *proto.JailerConfig) {
287288
_, err = fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})
288289
require.NoError(err)
289290

290-
_, err = os.Stat(filepath.Join(shimBaseDir(), "default#"+vmID))
291+
_, err = os.Stat(filepath.Join(integtest.ShimBaseDir(), "default#"+vmID))
291292
assert.Error(t, err)
292293
assert.True(t, os.IsNotExist(err))
293294

294-
shimContents, err := ioutil.ReadDir(shimBaseDir())
295+
shimContents, err := ioutil.ReadDir(integtest.ShimBaseDir())
295296
require.NoError(err)
296297
assert.Len(t, shimContents, 0)
297298
}

runtime/jailer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/stretchr/testify/require"
2727

2828
"github.com/firecracker-microvm/firecracker-containerd/config"
29+
"github.com/firecracker-microvm/firecracker-containerd/internal/integtest"
2930
"github.com/firecracker-microvm/firecracker-containerd/internal/vm"
3031
"github.com/firecracker-microvm/firecracker-containerd/proto"
3132
)
@@ -99,7 +100,7 @@ func TestJailer_invalidUIDGID(t *testing.T) {
99100
}
100101

101102
func TestNewJailer_Isolated(t *testing.T) {
102-
prepareIntegTest(t)
103+
integtest.Prepare(t)
103104

104105
ociBundlePath := t.TempDir()
105106
b, err := exec.Command(

0 commit comments

Comments
 (0)