Skip to content

Commit 8666bab

Browse files
mtjhrcslp
authored andcommitted
tests: extract setup_rootfs() from setup_fs_and_enter()
Allow tests that need custom root configuration (e.g. read-only mounts via krun_add_virtiofs3) to reuse rootfs directory creation and guest-agent copying without being forced into krun_set_root + krun_start_enter. Signed-off-by: Matej Hrica <[email protected]>
1 parent c092cb9 commit 8666bab

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

tests/test_cases/src/common.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::ffi::CString;
55
use std::fs;
66
use std::fs::create_dir;
77
use std::os::unix::ffi::OsStrExt;
8-
use std::path::Path;
8+
use std::path::{Path, PathBuf};
99
use std::ptr::null;
1010

1111
use crate::{krun_call, TestSetup};
@@ -20,6 +20,17 @@ fn copy_guest_agent(dir: &Path) -> anyhow::Result<()> {
2020
Ok(())
2121
}
2222

23+
/// Creates the root filesystem directory and copies the guest agent into it.
24+
/// Returns the path to the root directory. Use this when you need to configure the root
25+
/// filesystem yourself (e.g. via `krun_add_virtiofs3` for read-only mounts) rather than
26+
/// using the default `setup_fs_and_enter`.
27+
pub fn setup_rootfs(test_setup: &TestSetup) -> anyhow::Result<PathBuf> {
28+
let root_dir = test_setup.tmp_dir.join("root");
29+
create_dir(&root_dir).context("Failed to create root directory")?;
30+
copy_guest_agent(&root_dir)?;
31+
Ok(root_dir)
32+
}
33+
2334
/// Common part of most test. This setups an empty root filesystem, copies the guest agent there
2435
/// and runs the guest agent in the VM.
2536
/// Note that some tests might want to use a different root file system (perhaps a qcow image),
@@ -28,11 +39,9 @@ fn copy_guest_agent(dir: &Path) -> anyhow::Result<()> {
2839
///
2940
/// The returned object is used for deleting the temporary files.
3041
pub fn setup_fs_and_enter(ctx: u32, test_setup: TestSetup) -> anyhow::Result<()> {
31-
let root_dir = test_setup.tmp_dir.join("root");
32-
create_dir(&root_dir).context("Failed to create root directory")?;
42+
let root_dir = setup_rootfs(&test_setup)?;
3343

3444
let path_str = CString::new(root_dir.as_os_str().as_bytes()).context("CString::new")?;
35-
copy_guest_agent(&root_dir)?;
3645
unsafe {
3746
krun_call!(krun_set_root(ctx, path_str.as_ptr()))?;
3847
krun_call!(krun_set_workdir(ctx, c"/".as_ptr()))?;

0 commit comments

Comments
 (0)