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

Enable WasmEdge roofs support and start writing OCI tests for Wasmedge and Wasmtime #111

Closed
wants to merge 3 commits 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
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
args: --all --verbose
- name: Validate docs
run: ./scripts/validate-docs.sh
- name: Setup rust-wasm target and compile test modules
run: |
rustup target add wasm32-wasi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you put this in the make target it makes, it will just skip if already present and makes it so some one runs that step they don't miss it

make test/wasm-modules
- name: Run tests
run: |
make test
Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ members = [
"crates/oci-tar-builder",
"crates/containerd-shim-wasmedge",
"crates/containerd-shim-wasmtime",
"test/wasi-modules-for-testing",
"test/oci-tests",
]

[workspace.package]
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ test/k8s: test/k8s/cluster
test/k8s/clean: bin/kind
bin/kind delete cluster --name $(KIND_CLUSTER_NAME)

test/wasm-modules:
cargo build -p wasi-modules-for-testing --target wasm32-wasi

.PHONY: bin/k3s
bin/k3s:
mkdir -p bin && \
Expand Down
2 changes: 1 addition & 1 deletion crates/containerd-shim-wasmedge/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl WasmEdgeExecutor {
wasi_module.initialize(
Some(args.iter().map(|s| s as &str).collect()),
Some(envs.iter().map(|s| s as &str).collect()),
None,
Some(vec!["/:/"]), // map the container root filesystem to be available to the WASI module
);
let vm = vm
.register_module_from_file("main", cmd)
Expand Down
31 changes: 31 additions & 0 deletions test/oci-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "oci-tests"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
chrono = { workspace = true }
containerd-shim-wasm = { path = "../../crates/containerd-shim-wasm" }
containerd-shim-wasmedge = { path = "../../crates/containerd-shim-wasmedge" }
containerd-shim-wasmtime = { path = "../../crates/containerd-shim-wasmtime" }
libc = { workspace = true }
oci-spec = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tempfile = "3.0"
wasmedge-sdk = { version = "0.10.1", features = [ "standalone", "static" ] }
wasmtime = { version = "11.0", default-features = false, features = [
'cache',
'wat',
'jitdump',
'parallel-compilation',
'cranelift',
'pooling-allocator',
'vtune',
]}

[dev-dependencies]
tempfile = "3.0"
pretty_assertions = "1"
serial_test = "*"
100 changes: 100 additions & 0 deletions test/oci-tests/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::fs::{create_dir, read_to_string, File, OpenOptions};
use std::io::prelude::*;
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use std::time::Duration;

use libc::SIGKILL;
use oci_spec::runtime::Spec;
use serde::{Deserialize, Serialize};
use tempfile::tempdir;

use containerd_shim_wasm::sandbox::instance::Wait;
use containerd_shim_wasm::sandbox::{EngineGetter, Error, Instance, InstanceConfig};

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

pub static WASM_FILENAME: &str = "./file.wasm";

pub(crate) fn get_external_wasm_module(name: String) -> Result<Vec<u8>, Error> {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let target = Path::new(manifest_dir)
.join("../../target/wasm32-wasi/debug")
.join(name.clone());
std::fs::read(target).map_err(|e| {
Error::Others(format!(
"failed to read requested Wasm module ({}): {}. Perhaps you need to run 'make test/wasm-modules' first.",
name, e
))
})
}

pub(crate) fn run_test_with_spec<I, E>(spec: &Spec, bytes: &[u8]) -> Result<(String, u32), Error>
where
I: Instance<E = E> + EngineGetter<E = E>,
E: Sync + Send + Clone,
{
let dir = tempdir().unwrap();
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").join(WASM_FILENAME);
let mut f = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o755)
.open(wasm_path)?;
f.write_all(bytes)?;

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

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(600)) {
Ok(res) => res,
Err(e) => {
wasi.kill(SIGKILL as u32).unwrap();
return Err(Error::Others(format!(
"error waiting for module to finish: {0}",
e
)));
}
};
wasi.delete()?;
let output = read_to_string(dir.path().join("stdout"))?;
Ok((output, res.0))
}
53 changes: 53 additions & 0 deletions test/oci-tests/tests/devices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::borrow::Cow;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use serial_test::serial;
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use wasmedge_sdk::Vm as WasmEdgeVm;
use wasmtime::Engine as WasmtimeVm;

use containerd_shim_wasm::function;
use containerd_shim_wasm::sandbox::testutil::{has_cap_sys_admin, run_test_with_sudo};
use containerd_shim_wasm::sandbox::Error;

use containerd_shim_wasmedge::instance::Wasi as WasmEdgeWasi;
use containerd_shim_wasmtime::instance::Wasi as WasmtimeWasi;

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

mod common;

#[test]
#[serial]
fn test_has_default_devices() -> Result<(), Error> {
if !has_cap_sys_admin() {
println!("running test with sudo: {}", function!());
return run_test_with_sudo("test_has_default_devices");
}

let wasmbytes = common::get_external_wasm_module("has-default-devices.wasm".to_string())?;

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

let bytes = Cow::from(wasmbytes);

let (output, retval) = common::run_test_with_spec::<WasmtimeWasi, WasmtimeVm>(&spec, &bytes)?;
assert_eq!(retval, 0, "error: {}", output);

let (output, retval) = common::run_test_with_spec::<WasmEdgeWasi, WasmEdgeVm>(&spec, &bytes)?;
assert_eq!(retval, 0, "error: {}", output);

Ok(())
}
Loading