Skip to content

Commit b3f99f3

Browse files
authored
Merge pull request firecracker-microvm#530 from kzys/test-device-file
Run TestAttachBlockDevice_Isolated's subtests in parallel
2 parents b93e875 + f371c83 commit b3f99f3

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

internal/fsutil.go

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import (
2121
"os/exec"
2222
"path/filepath"
2323
"strings"
24-
"syscall"
2524
"testing"
2625

2726
"github.com/pkg/errors"
2827
"github.com/stretchr/testify/require"
2928
)
3029

30+
const mib = 1024 * 1024
31+
3132
// CreateFSImg will create a file containing a filesystem image of the provided type containing
3233
// the provided files. It returns the path at which the image file can be found.
3334
func CreateFSImg(ctx context.Context, t *testing.T, fsType string, testFiles ...FSImgFile) string {
@@ -73,46 +74,37 @@ func createTestExtImg(ctx context.Context, t *testing.T, extName string, testFil
7374
return imgFile.Name()
7475
}
7576

76-
// BlockDeviceFile represents a block device
77-
type BlockDeviceFile struct {
78-
Subpath string
79-
UID int
80-
GID int
81-
Dev int // major number
82-
}
83-
8477
// CreateBlockDevice creates a block device, or block special file for testing
85-
func CreateBlockDevice(ctx context.Context, t *testing.T, fsType string, testFile BlockDeviceFile) string {
78+
func CreateBlockDevice(ctx context.Context, t *testing.T) (string, func()) {
8679
t.Helper()
8780

88-
switch fsType {
89-
case "ext4":
90-
return createTestBlockDevice(ctx, t, fsType, testFile)
91-
default:
92-
require.FailNowf(t, "unsupported fs type %q", fsType)
93-
return ""
94-
}
95-
}
81+
f, err := ioutil.TempFile("", "")
82+
require.NoError(t, err)
9683

97-
func createTestBlockDevice(ctx context.Context, t *testing.T, extName string, testFile BlockDeviceFile) string {
98-
t.Helper()
84+
err = f.Truncate(32 * mib)
85+
require.NoError(t, err)
9986

100-
deviceFile := filepath.Join("/tmp/blockExample", testFile.Subpath)
101-
err := os.MkdirAll(filepath.Dir(deviceFile), 0750)
102-
require.NoError(t, err, "failed to mkdir for the block device")
87+
out, err := exec.CommandContext(ctx, "mkfs.ext4", "-v", f.Name()).CombinedOutput()
88+
require.NoErrorf(t, err, "failed to create ext img, command out:%s \n", string(out))
10389

104-
err = syscall.Mknod(deviceFile, syscall.S_IFBLK, testFile.Dev)
105-
require.NoError(t, err, "failed to create a new block device")
90+
err = f.Close()
91+
require.NoError(t, err)
10692

107-
err = os.Chmod(deviceFile, 0600)
108-
require.NoError(t, err, "failed to change file mode for the new created block device")
93+
out, err = exec.CommandContext(ctx, "losetup", "--show", "--find", f.Name()).CombinedOutput()
94+
require.NoError(t, err)
10995

110-
err = os.Chown(deviceFile, testFile.UID, testFile.GID)
111-
require.NoError(t, err, "failed to grant permission to the new created block device")
96+
device := strings.TrimRight(string(out), "\n")
11297

113-
output, err := exec.CommandContext(ctx, "mkfs."+extName, "-v", deviceFile).CombinedOutput()
114-
require.NoErrorf(t, err, "failed to create ext img, command output:%s \n", string(output))
115-
return deviceFile
98+
err = os.Chmod(device, 0600)
99+
require.NoError(t, err, "failed to change file mode for the new created block device")
100+
101+
return device, func() {
102+
out, err := exec.CommandContext(ctx, "losetup", "--detach", device).CombinedOutput()
103+
if len(out) > 0 {
104+
t.Logf("losetup --detach: %s", out)
105+
}
106+
require.NoError(t, err)
107+
}
116108
}
117109

118110
// MountInfo holds data parsed from a line of /proc/mounts

runtime/jailer_integ_test.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,18 @@ func TestJailer_Isolated(t *testing.T) {
6666
func TestAttachBlockDevice_Isolated(t *testing.T) {
6767
prepareIntegTest(t)
6868
t.Run("Without Jailer", func(t *testing.T) {
69+
t.Parallel()
6970
testAttachBlockDevice(t, nil)
7071
})
7172
t.Run("With Jailer", func(t *testing.T) {
73+
t.Parallel()
7274
testAttachBlockDevice(t, &proto.JailerConfig{
7375
UID: jailerUID,
7476
GID: jailerGID,
7577
})
7678
})
7779
t.Run("With Jailer and bind-mount", func(t *testing.T) {
80+
t.Parallel()
7881
testAttachBlockDevice(t, &proto.JailerConfig{
7982
UID: jailerUID,
8083
GID: jailerGID,
@@ -212,28 +215,19 @@ func testAttachBlockDevice(t *testing.T, jailerConfig *proto.JailerConfig) {
212215

213216
vmID := testNameToVMID(t.Name())
214217

215-
// default BlockDeviceFile setup, will change UID & GID if a jailerConfig is passed
216-
deviceFile := internal.BlockDeviceFile{
217-
Subpath: "dir/block1",
218-
UID: 0,
219-
GID: 0,
220-
Dev: 259, // 259 is the major number for blkext which is one type of block devices
221-
}
218+
device, cleanup := internal.CreateBlockDevice(ctx, t)
219+
defer cleanup()
222220

223221
if jailerConfig != nil {
224-
// cover "With Jailer" & "With Jailer and bind-mount" test cases
225-
deviceFile.UID = int(jailerConfig.UID)
226-
deviceFile.GID = int(jailerConfig.GID)
222+
err := os.Chown(device, int(jailerConfig.UID), int(jailerConfig.GID))
223+
require.NoError(err)
227224
}
228-
additionalBlockDevice := internal.CreateBlockDevice(ctx, t, "ext4", deviceFile)
229-
blockExampleDir := filepath.Dir(additionalBlockDevice)
230-
defer os.RemoveAll(filepath.Dir(blockExampleDir)) // clean up
231225

232226
request := proto.CreateVMRequest{
233227
VMID: vmID,
234228
JailerConfig: jailerConfig,
235229
DriveMounts: []*proto.FirecrackerDriveMount{
236-
{HostPath: additionalBlockDevice, VMPath: "/home/driveMount", FilesystemType: "ext4"},
230+
{HostPath: device, VMPath: "/home/driveMount", FilesystemType: "ext4"},
237231
},
238232
}
239233

@@ -254,9 +248,8 @@ func testAttachBlockDevice(t *testing.T, jailerConfig *proto.JailerConfig) {
254248

255249
request.RootDrive = &proto.FirecrackerRootDrive{HostPath: dst}
256250

257-
// The additional drive file is only used by this test.
258-
err = os.Chown(additionalBlockDevice, int(jailerConfig.UID), int(jailerConfig.GID))
259-
require.NoError(err, "failed to chown %q", additionalBlockDevice)
251+
err = os.Chown(device, int(jailerConfig.UID), int(jailerConfig.GID))
252+
require.NoError(err, "failed to chown %q", device)
260253
}
261254

262255
_, err = fcClient.CreateVM(ctx, &request)

0 commit comments

Comments
 (0)