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

make tests fail-stop #59

Merged
merged 6 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
7,376 changes: 3,688 additions & 3,688 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions crates/kv-azure-blob/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use azure_storage::core::prelude::*;
use azure_storage_blobs::prelude::*;
use futures::executor::block_on;
use proc_macro_utils::{Resource, RuntimeResource};
use runtime::resource::{
get, DataT, Linker, Map, Resource, ResourceMap, RuntimeContext, RuntimeResource,
};
use runtime::resource::{get, Ctx, DataT, Linker, Map, Resource, ResourceMap, RuntimeResource};
use std::sync::Arc;
use uuid::Uuid;

Expand Down
4 changes: 1 addition & 3 deletions crates/kv-filesystem/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anyhow::{Context, Result};
use proc_macro_utils::{Resource, RuntimeResource};
use runtime::resource::{
get, DataT, Linker, Map, Resource, ResourceMap, RuntimeContext, RuntimeResource,
};
use runtime::resource::{get, Ctx, DataT, Linker, Map, Resource, ResourceMap, RuntimeResource};
use std::{
fs::{self, File},
io::{Read, Write},
Expand Down
4 changes: 1 addition & 3 deletions crates/lockd-etcd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use anyhow::{Context, Result};
use etcd_client::Client;
use futures::executor::block_on;
use proc_macro_utils::{Resource, RuntimeResource};
use runtime::resource::{
get, DataT, Linker, Map, Resource, ResourceMap, RuntimeContext, RuntimeResource,
};
use runtime::resource::{get, Ctx, DataT, Linker, Map, Resource, ResourceMap, RuntimeResource};
use uuid::Uuid;

mod etcd;
Expand Down
4 changes: 1 addition & 3 deletions crates/mq-azure-servicebus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use anyhow::{Context, Result};
use azure_messaging_servicebus::prelude::*;
use futures::executor::block_on;
use proc_macro_utils::{Resource, RuntimeResource};
use runtime::resource::{
get, DataT, Linker, Map, Resource, ResourceMap, RuntimeContext, RuntimeResource,
};
use runtime::resource::{get, Ctx, DataT, Linker, Map, Resource, ResourceMap, RuntimeResource};

pub use mq::add_to_linker;
use mq::*;
Expand Down
4 changes: 1 addition & 3 deletions crates/mq-filesystem/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use anyhow::{Context, Result};
use mq::*;
use proc_macro_utils::{Resource, RuntimeResource};
use runtime::resource::{
get, DataT, Linker, Map, Resource, ResourceMap, RuntimeContext, RuntimeResource,
};
use runtime::resource::{get, Ctx, DataT, Linker, Map, Resource, ResourceMap, RuntimeResource};
use std::{
fs::{self, File, OpenOptions},
io::{BufRead, BufReader, Read, Write},
Expand Down
2 changes: 1 addition & 1 deletion crates/proc_macro_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn runtime_resource(input: TokenStream) -> TokenStream {
let name = &input.ident;
let expanded = quote! {
impl RuntimeResource for #name {
fn add_to_linker(linker: &mut Linker<RuntimeContext<DataT>>) -> Result<()> {
fn add_to_linker(linker: &mut Linker<Ctx>) -> Result<()> {
crate::add_to_linker(linker, |cx| get::<Self>(cx, SCHEME_NAME.to_string()))
}

Expand Down
4 changes: 1 addition & 3 deletions crates/pubsub-confluent-kafka/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::{env, sync::Arc};
use anyhow::{Context, Result};
use proc_macro_utils::{Resource, RuntimeResource};
use rdkafka::{consumer::BaseConsumer, producer::BaseProducer, ClientConfig};
use runtime::resource::{
get, DataT, Linker, Map, Resource, ResourceMap, RuntimeContext, RuntimeResource,
};
use runtime::resource::{get, Ctx, DataT, Linker, Map, Resource, ResourceMap, RuntimeResource};

use pubsub::*;
use uuid::Uuid;
Expand Down
14 changes: 7 additions & 7 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ pub mod resource;
use std::collections::HashMap;

use anyhow::Result;
use resource::{DataT, ResourceConfig, ResourceMap, RuntimeResource};
use resource::{Ctx, ResourceConfig, ResourceMap, RuntimeResource};
use wasi_cap_std_sync::WasiCtxBuilder;
use wasi_common::WasiCtx;
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
use wasmtime::{Config, Engine, Linker, Module, Store};
use wasmtime_wasi::*;

/// A wasmtime runtime context to be passed to a wasm module.
Expand All @@ -17,8 +17,8 @@ pub struct RuntimeContext<T> {

/// A wasmtime-based runtime builder.
pub struct Builder {
linker: Linker<RuntimeContext<DataT>>,
store: Store<RuntimeContext<DataT>>,
linker: Linker<Ctx>,
store: Store<Ctx>,
engine: Engine,
pub config: Option<Vec<(String, String)>>,
}
Expand Down Expand Up @@ -70,10 +70,10 @@ impl Builder {
}

/// Instantiate the guest module.
pub fn build(mut self, module: &str) -> Result<(Store<RuntimeContext<DataT>>, Instance)> {
pub fn build(mut self, module: &str) -> Result<(Store<Ctx>, Linker<Ctx>)> {
let module = Module::from_file(&self.engine, module)?;
let instance = self.linker.instantiate(&mut self.store, &module)?;
Ok((self.store, instance))
self.linker.module(&mut self.store, "", &module)?;
Ok((self.store, self.linker))
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/runtime/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use crate::RuntimeContext;
pub type DataT = Box<dyn Resource>;
pub type ResourceConfig = String;
pub type ResourceMap = Arc<Mutex<Map>>;
pub type Ctx = RuntimeContext<DataT>;

#[derive(Default)]
pub struct Map(HashMap<String, Box<dyn Resource>>);
Expand Down Expand Up @@ -54,12 +55,12 @@ pub trait Resource: AsAny {

/// A trait for wit-bindgen host resource composed of a resource and a resource table.
pub trait RuntimeResource {
fn add_to_linker(linker: &mut Linker<RuntimeContext<DataT>>) -> Result<()>;
fn add_to_linker(linker: &mut Linker<Ctx>) -> Result<()>;
fn build_data() -> Result<DataT>;
}

/// dynamic dispatch to respective host resource.
pub fn get<T>(cx: &mut RuntimeContext<DataT>, resource_key: String) -> &mut T
pub fn get<T>(cx: &mut Ctx, resource_key: String) -> &mut T
where
T: 'static,
{
Expand Down
9 changes: 5 additions & 4 deletions src/bin/wasi-cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ async fn main() -> Result<()> {
}

builder.link_resource_map(resource_map)?;
let (mut store, instance) = builder.build(&args.module)?;
let (mut store, linker) = builder.build(&args.module)?;

instance
.get_typed_func::<(i32, i32), i32, _>(&mut store, "main")?
.call(&mut store, (0, 0))?;
linker
.get_default(&mut store, "")?
.typed::<(), (), _>(&store)?
.call(&mut store, ())?;
Ok(())
}
4 changes: 4 additions & 0 deletions tests/azblob.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
specversion = "0.1"

[[capability]]
name = "azblobkv"
File renamed without changes.
21 changes: 18 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ mod kv_test {
use crate::common::run;
use anyhow::Result;

const WASI_CLOUD_BINARY: &str = "./target/debug/wasi-cloud";
const WASI_CLOUD_BINARY: &str = "./target/release/wasi-cloud";
const KV_TEST_MODULE: &str = "tests/kv-test/target/wasm32-wasi/debug/kv-test.wasm";

#[test]
fn test_kv_filesystem() -> Result<()> {
let config = "./tests/wc.toml";
run(WASI_CLOUD_BINARY, vec!["-m", KV_TEST_MODULE, "-c", config]);
let file_config = "./tests/file.toml";
run(
WASI_CLOUD_BINARY,
vec!["-m", KV_TEST_MODULE, "-c", file_config],
);
Ok(())
}

#[test]
fn test_kv_azblob() -> Result<()> {
//TODO (mossaka): figure out a way to inject secrets to gh actions
Mossaka marked this conversation as resolved.
Show resolved Hide resolved

// let azblob_config = "./tests/azblob.toml";
// run(
// WASI_CLOUD_BINARY,
// vec!["-m", KV_TEST_MODULE, "-c", azblob_config],
// );
Mossaka marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}
25 changes: 19 additions & 6 deletions tests/kv-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,27 @@ fn main() -> Result<()> {
let value = get(&rd, "key");
assert!(value.is_err());

// test get_kv() will refer to the same underlying resource
// test get_kv() will have a unique allocation in the resource table.
// so two `get_kv()` with different names will return different allocations.
let rd1 = get_kv("random1")?;
let rd2 = get_kv("random2")?;
set(&rd1, "key1", "value1".as_bytes())?;
set(&rd2, "key2", "value2".as_bytes())?;

let value1 = get(&rd1, "key2")?;
let value2 = get(&rd2, "key1")?;
assert_eq!(std::str::from_utf8(&value1)?, "value2");
assert_eq!(std::str::from_utf8(&value2)?, "value1");
assert!(get(&rd1, "key2").is_err());
delete(&rd1, "key1")?;
delete(&rd2, "key2")?;

// test two get_kv() with the same name will return the same allocation.
// but the resource descriptors are not the same.
let rd1 = get_kv("random1")?;
let rd2 = get_kv("random1")?;
assert!(rd1 != rd2);
set(&rd1, "key1", "value1".as_bytes())?;
set(&rd2, "key2", "value2".as_bytes())?;
assert!(get(&rd1, "key2")? == "value2".as_bytes());
delete(&rd1, "key1")?;
delete(&rd2, "key2")?;

// test get empty key
let rd = get_kv("random3")?;
Expand All @@ -38,5 +49,7 @@ fn main() -> Result<()> {
let rd = get_kv("random4")?;
let ret = delete(&rd, "key");
assert!(ret.is_err());

println!("finished running kv-test");
Ok(())
}
}