Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralize running wasi test #220

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/containerd-shim-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ clone3 = "0.2"
libc = { workspace = true }
caps = "0.5"
proc-mounts = "0.3"
tempfile = "3"

[build-dependencies]
ttrpc-codegen = { version = "0.4.2", optional = true }

[dev-dependencies]
tempfile = "3"
pretty_assertions = "1"
signal-hook = "0.3"
env_logger = { workspace = true }
Expand Down
107 changes: 104 additions & 3 deletions crates/containerd-shim-wasm/src/sandbox/testutil.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
//! Testing utilities used across different modules

use super::{Error, Result};
use std::process::{Command, Stdio};
use super::{instance::Wait, EngineGetter, Error, Instance, InstanceConfig, Result};
use anyhow::Result as AnyHowResult;
use chrono::{DateTime, Utc};
use libc::SIGKILL;
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::os::unix::fs::OpenOptionsExt;
use std::{
borrow::Cow,
fs::{create_dir, File, OpenOptions},
path::PathBuf,
process::{Command, Stdio},
sync::mpsc::channel,
time::Duration,
};
use tempfile::TempDir;

fn normalize_test_name(test: &str) -> Result<&str> {
let closure_removed = test.trim_end_matches("::{{closure}}");
Expand Down Expand Up @@ -64,5 +79,91 @@ macro_rules! function {
name
}};
}

pub use function;

#[derive(Serialize, Deserialize)]
struct Options {
root: Option<PathBuf>,
}

pub fn run_wasi_test<I, E>(
dir: &TempDir,
wasmbytes: Cow<[u8]>,
start_fn: Option<&str>,
) -> AnyHowResult<(u32, DateTime<Utc>), Error>
where
I: Instance<E = E> + EngineGetter<E = E>,
E: Sync + Send + Clone,
{
create_dir(dir.path().join("rootfs"))?;
let rootdir = dir.path().join("runwasi");
create_dir(&rootdir)?;
let opts = Options {
root: Some(rootdir),
};
let opts_file = OpenOptions::new()
.read(true)
.create(true)
.truncate(true)
.write(true)
.open(dir.path().join("options.json"))?;
write!(&opts_file, "{}", serde_json::to_string(&opts)?)?;

let wasm_path = dir.path().join("rootfs/hello.wasm");
let mut f = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o755)
.open(wasm_path)?;
f.write_all(&wasmbytes)?;

let stdout = File::create(dir.path().join("stdout"))?;
drop(stdout);

let entrypoint = match start_fn {
Some(s) => "./hello.wasm#".to_string() + s,
None => "./hello.wasm".to_string(),
};
let spec = SpecBuilder::default()
.root(RootBuilder::default().path("rootfs").build()?)
.process(
ProcessBuilder::default()
.cwd("/")
.args(vec![entrypoint])
.build()?,
)
.build()?;

spec.save(dir.path().join("config.json"))?;

let mut cfg = InstanceConfig::new(
I::new_engine()?,
"test_namespace".into(),
"/containerd/address".into(),
);
let cfg = cfg
.set_bundle(dir.path().to_str().unwrap().to_string())
.set_stdout(dir.path().join("stdout").to_str().unwrap().to_string());

let wasi = I::new("test".to_string(), Some(cfg));

wasi.start()?;

let (tx, rx) = channel();
let waiter = Wait::new(tx);
wasi.wait(&waiter).unwrap();

let res = match rx.recv_timeout(Duration::from_secs(10)) {
Ok(res) => Ok(res),
Err(e) => {
wasi.kill(SIGKILL as u32).unwrap();
return Err(Error::Others(format!(
"error waiting for module to finish: {0}",
e
)));
}
};
wasi.delete()?;
res
}
90 changes: 6 additions & 84 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,13 @@ impl EngineGetter for Wasi {

#[cfg(test)]
mod wasitest {
use std::borrow::Cow;
use std::fs::{create_dir, read_to_string, File, OpenOptions};
use std::os::unix::prelude::OpenOptionsExt;
use std::sync::mpsc::channel;
use std::time::Duration;

use super::*;
use containerd_shim_wasm::function;
use containerd_shim_wasm::sandbox::exec::has_cap_sys_admin;
use containerd_shim_wasm::sandbox::testutil::run_test_with_sudo;
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use tempfile::{tempdir, TempDir};

use containerd_shim_wasm::sandbox::testutil::{run_test_with_sudo, run_wasi_test};
use serial_test::serial;

use super::*;
use std::fs::read_to_string;
use tempfile::tempdir;

use wasmedge_sdk::{
config::{CommonConfigOptions, ConfigBuilder},
Expand Down Expand Up @@ -332,76 +324,6 @@ mod wasitest {
"#
.as_bytes();

fn run_wasi_test(dir: &TempDir, wasmbytes: Cow<[u8]>) -> Result<(u32, DateTime<Utc>), Error> {
create_dir(dir.path().join("rootfs"))?;
let rootdir = dir.path().join("runwasi");
create_dir(&rootdir)?;
let opts = Options {
root: Some(rootdir),
};
let opts_file = OpenOptions::new()
.read(true)
.create(true)
.truncate(true)
.write(true)
.open(dir.path().join("options.json"))?;
write!(&opts_file, "{}", serde_json::to_string(&opts)?)?;

let wasm_path = dir.path().join("rootfs/hello.wasm");
let mut f = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o755)
.open(wasm_path)?;
f.write_all(&wasmbytes)?;

let stdout = File::create(dir.path().join("stdout"))?;
drop(stdout);

let spec = SpecBuilder::default()
.root(RootBuilder::default().path("rootfs").build()?)
.process(
ProcessBuilder::default()
.cwd("/")
.args(vec!["./hello.wasm".to_string()])
.build()?,
)
.build()?;

spec.save(dir.path().join("config.json"))?;

let mut cfg = InstanceConfig::new(
Wasi::new_engine()?,
"test_namespace".into(),
"/containerd/address".into(),
);
let cfg = cfg
.set_bundle(dir.path().to_str().unwrap().to_string())
.set_stdout(dir.path().join("stdout").to_str().unwrap().to_string());

let wasi = Wasi::new("test".to_string(), Some(cfg));

wasi.start()?;

let (tx, rx) = channel();
let waiter = Wait::new(tx);
wasi.wait(&waiter).unwrap();

let res = match rx.recv_timeout(Duration::from_secs(10)) {
Ok(res) => Ok(res),
Err(e) => {
wasi.kill(SIGKILL as u32).unwrap();
return Err(Error::Others(format!(
"error waiting for module to finish: {0}",
e
)));
}
};
wasi.delete()?;
res
}

#[test]
#[serial]
fn test_delete_after_create() {
Expand Down Expand Up @@ -432,7 +354,7 @@ mod wasitest {
let path = dir.path();
let wasmbytes = wat2wasm(WASI_HELLO_WAT).unwrap();

let res = run_wasi_test(&dir, wasmbytes)?;
let res = run_wasi_test::<Wasi, Vm>(&dir, wasmbytes, None)?;

assert_eq!(res.0, 0);

Expand All @@ -454,7 +376,7 @@ mod wasitest {
let dir = tempdir()?;
let wasmbytes = wat2wasm(WASI_RETURN_ERROR).unwrap();

let res = run_wasi_test(&dir, wasmbytes)?;
let res = run_wasi_test::<Wasi, Vm>(&dir, wasmbytes, None)?;

// Expect error code from the run.
assert_eq!(res.0, 137);
Expand Down
1 change: 1 addition & 0 deletions crates/containerd-shim-wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tempfile = "3.7"
libc = { workspace = true }
pretty_assertions = "1"
env_logger = "0.10"
serial_test = "*"

[[bin]]
name = "containerd-shim-wasmtime-v1"
Expand Down
Loading
Loading