Skip to content

Commit d043c33

Browse files
committed
tests(vhost-user-block): added initial integration test
Added a simple test that boots a VM with vhost-user-block as root block device. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 0d96db1 commit d043c33

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

tests/framework/defs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
# Default test session root directory path
2424
DEFAULT_TEST_SESSION_ROOT_PATH = "/srv"
2525

26+
# Default test session artifacts path
27+
LOCAL_BUILD_PATH = FC_WORKSPACE_DIR / "build/"
28+
2629
# Absolute path to the test results folder
2730
TEST_RESULTS_DIR = FC_WORKSPACE_DIR / "test_results"
2831

@@ -36,6 +39,6 @@
3639

3740
# fall-back to the local directory
3841
if not IMG_DIR.exists():
39-
IMG_DIR = Path(__file__).joinpath("../../../build/img").resolve()
42+
IMG_DIR = LOCAL_BUILD_PATH / "img"
4043

4144
ARTIFACT_DIR = IMG_DIR / platform.machine()

tests/framework/microvm.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,24 @@ def add_drive(
656656
)
657657
self.disks[drive_id] = path_on_host
658658

659+
def add_vhost_user_block(
660+
self,
661+
drive_id,
662+
socket,
663+
partuuid=None,
664+
is_root_device=False,
665+
cache_type=None,
666+
):
667+
"""Add a vhost-user block device."""
668+
669+
self.api.drive.put(
670+
drive_id=drive_id,
671+
socket=socket,
672+
partuuid=partuuid,
673+
is_root_device=is_root_device,
674+
cache_type=cache_type,
675+
)
676+
659677
def patch_drive(self, drive_id, file):
660678
"""Modify/patch an existing block device."""
661679
self.api.drive.patch(

tests/framework/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
wait_fixed,
3030
)
3131

32-
from framework.defs import MIN_KERNEL_VERSION_FOR_IO_URING
32+
from framework.defs import (
33+
DEFAULT_TEST_SESSION_ROOT_PATH,
34+
LOCAL_BUILD_PATH,
35+
MIN_KERNEL_VERSION_FOR_IO_URING,
36+
)
3337

3438
FLUSH_CMD = 'screen -S {session} -X colon "logfile flush 0^M"'
3539
CommandReturn = namedtuple("CommandReturn", "returncode stdout stderr")
@@ -381,6 +385,16 @@ def __str__(self):
381385
return "\n\n".join(self.failures)
382386

383387

388+
def to_local_dir_path(tmp_dir_path: str) -> str:
389+
"""
390+
Converts path from tmp dir to path on the host.
391+
"""
392+
393+
return tmp_dir_path.replace(
394+
str(DEFAULT_TEST_SESSION_ROOT_PATH), str(LOCAL_BUILD_PATH)
395+
)
396+
397+
384398
def search_output_from_cmd(cmd: str, find_regex: typing.Pattern) -> typing.Match:
385399
"""
386400
Run a shell command and search a given regex object in stdout.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Tests for vhost-user-block device."""
4+
5+
import os
6+
import subprocess
7+
import time
8+
9+
from framework import utils
10+
11+
VHOST_USER_SOCKET = "/vub.socket"
12+
13+
14+
def spawn_vhost_user_backend(vm, host_mem_path):
15+
"""Spawn vhost-user-block backend."""
16+
17+
uid = vm.jailer.uid
18+
gid = vm.jailer.gid
19+
20+
sp = f"{vm.chroot()}{VHOST_USER_SOCKET}"
21+
args = ["vhost-user-blk", "-s", sp, "-b", host_mem_path, "-r"]
22+
proc = subprocess.Popen(args)
23+
24+
time.sleep(1)
25+
if proc is None or proc.poll() is not None:
26+
print("vub is not running")
27+
28+
with utils.chroot(vm.chroot()):
29+
# The backend will create the socket path with root rights.
30+
# Change rights to the jailer's.
31+
os.chown(VHOST_USER_SOCKET, uid, gid)
32+
return proc
33+
34+
35+
def test_vhost_user_block(microvm_factory, guest_kernel, rootfs_ubuntu_22):
36+
"""
37+
This test simply tries to boot a VM with
38+
vhost-user-block as a root device.
39+
"""
40+
41+
vm = microvm_factory.build(guest_kernel, None, monitor_memory=False)
42+
43+
ssh_key = rootfs_ubuntu_22.with_suffix(".id_rsa")
44+
vm.ssh_key = ssh_key
45+
46+
vm.spawn()
47+
48+
# Converting path from tmpfs ("./srv/..") to local
49+
# path on the host ("../build/..")
50+
rootfs_path = utils.to_local_dir_path(str(rootfs_ubuntu_22))
51+
_backend = spawn_vhost_user_backend(vm, rootfs_path)
52+
53+
vm.basic_config()
54+
vm.add_vhost_user_block("1", VHOST_USER_SOCKET, is_root_device=True)
55+
vm.add_net_iface()
56+
vm.start()
57+
58+
# Attempt to connect to the VM.
59+
# Verify if guest can run commands.
60+
exit_code, _, _ = vm.ssh.run("ls")
61+
assert exit_code == 0

0 commit comments

Comments
 (0)