Skip to content

Commit ad29afb

Browse files
kalyazinroypat
authored andcommitted
test(vhost-user): extract backend spawn helper to utils
Extracted spawn_vhost_user_backend() into util_vhost_user_backend.py as it will be used by multiple test files. Signed-off-by: Nikita Kalyazin <[email protected]>
1 parent 10af1df commit ad29afb

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Utilities for vhost-user-blk backend."""
5+
6+
import os
7+
import subprocess
8+
import time
9+
10+
from framework import utils
11+
12+
VHOST_USER_SOCKET = "/vub.socket"
13+
14+
15+
def spawn_vhost_user_backend(vm, host_mem_path, readonly=False):
16+
"""Spawn vhost-user-blk backend."""
17+
18+
uid = vm.jailer.uid
19+
gid = vm.jailer.gid
20+
21+
socket_path = f"{vm.chroot()}{VHOST_USER_SOCKET}"
22+
args = ["vhost-user-blk", "-s", socket_path, "-b", host_mem_path]
23+
if readonly:
24+
args.append("-r")
25+
proc = subprocess.Popen(args)
26+
27+
# Give the backend time to initialise.
28+
time.sleep(1)
29+
30+
assert proc is not None and proc.poll() is None, "backend is not up"
31+
32+
with utils.chroot(vm.chroot()):
33+
# The backend will create the socket path with root rights.
34+
# Change rights to the jailer's.
35+
os.chown(VHOST_USER_SOCKET, uid, gid)
36+
37+
return proc

tests/integration_tests/functional/test_drive_vhost_user.py

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,11 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""Tests for vhost-user-block device."""
44

5-
import os
6-
import subprocess
7-
import time
8-
95
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
6+
from framework.utils_vhost_user_backend import (
7+
VHOST_USER_SOCKET,
8+
spawn_vhost_user_backend,
9+
)
3310

3411

3512
def test_vhost_user_block(microvm_factory, guest_kernel, rootfs_ubuntu_22):
@@ -48,7 +25,7 @@ def test_vhost_user_block(microvm_factory, guest_kernel, rootfs_ubuntu_22):
4825
# Converting path from tmpfs ("./srv/..") to local
4926
# path on the host ("../build/..")
5027
rootfs_path = utils.to_local_dir_path(str(rootfs_ubuntu_22))
51-
_backend = spawn_vhost_user_backend(vm, rootfs_path)
28+
_backend = spawn_vhost_user_backend(vm, rootfs_path, readonly=True)
5229

5330
vm.basic_config()
5431
vm.add_vhost_user_block("1", VHOST_USER_SOCKET, is_root_device=True)

0 commit comments

Comments
 (0)