Skip to content

Commit

Permalink
tests: Add WasiRunner file system mounting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theduke committed Sep 15, 2024
1 parent 07d884b commit 31fc3d1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,92 @@ mod tests {
assert_send::<WasiRunner>();
assert_sync::<WasiRunner>();
}

#[tokio::test]
async fn test_volume_mount_without_webcs() {
use std::sync::Arc;

let root_fs = virtual_fs::RootFileSystemBuilder::new().build();

let tokrt = tokio::runtime::Handle::current();

let hostdir = virtual_fs::host_fs::FileSystem::new(tokrt.clone(), "/").unwrap();
let hostdir_dyn: Arc<dyn virtual_fs::FileSystem + Send + Sync> = Arc::new(hostdir);

root_fs
.mount("/host".into(), &hostdir_dyn, "/".into())
.unwrap();

let envb = crate::runners::wasi::WasiRunner::new();

let annotations = webc::metadata::annotations::Wasi::new("test");

let tm = Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::new(
tokrt.clone(),
));
let rt = crate::PluggableRuntime::new(tm);

let envb = envb
.prepare_webc_env("test", &annotations, None, Arc::new(rt), Some(root_fs))
.unwrap();

let init = envb.build_init().unwrap();

let fs = &init.state.fs.root_fs;

fs.read_dir(&std::path::Path::new("/host")).unwrap();
}

#[tokio::test]
async fn test_volume_mount_with_webcs() {
use std::sync::Arc;

let root_fs = virtual_fs::RootFileSystemBuilder::new().build();

let tokrt = tokio::runtime::Handle::current();

let hostdir = virtual_fs::host_fs::FileSystem::new(tokrt.clone(), "/").unwrap();
let hostdir_dyn: Arc<dyn virtual_fs::FileSystem + Send + Sync> = Arc::new(hostdir);

root_fs
.mount("/host".into(), &hostdir_dyn, "/".into())
.unwrap();

let envb = crate::runners::wasi::WasiRunner::new();

let annotations = webc::metadata::annotations::Wasi::new("test");

let tm = Arc::new(crate::runtime::task_manager::tokio::TokioTaskManager::new(
tokrt.clone(),
));
let mut rt = crate::PluggableRuntime::new(tm);
rt.set_package_loader(crate::runtime::package_loader::BuiltinPackageLoader::new());

let webc_path = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("../../tests/integration/cli/tests/webc/[email protected]");
let webc_data = std::fs::read(webc_path).unwrap();
let container = webc::Container::from_bytes(webc_data).unwrap();

let binpkg = crate::bin_factory::BinaryPackage::from_webc(&container, &rt)
.await
.unwrap();

let mut envb = envb
.prepare_webc_env(
"test",
&annotations,
Some(&binpkg),
Arc::new(rt),
Some(root_fs),
)
.unwrap();

envb = envb.preopen_dir("/host").unwrap();

let init = envb.build_init().unwrap();

let fs = &init.state.fs.root_fs;

fs.read_dir(&std::path::Path::new("/host")).unwrap();
fs.read_dir(&std::path::Path::new("/settings")).unwrap();
}
}
Binary file not shown.

0 comments on commit 31fc3d1

Please sign in to comment.