Skip to content

Commit

Permalink
Remove hard coded Wasm paths in abitest & hello_world (#578)
Browse files Browse the repository at this point in the history
* Remove hard coded Wasm paths in abitest & hello_world

* Review
  • Loading branch information
blaxill authored Feb 7, 2020
1 parent 418c215 commit 408e06a
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 25 deletions.
4 changes: 1 addition & 3 deletions docs/programming-oak.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ fn test_say_hello() {
simple_logger::init().unwrap();

let configuration = oak_tests::test_configuration(
&[(MODULE_CONFIG_NAME, WASM_PATH)],
build_wasm().expect("failed to build wasm modules"),
LOG_CONFIG_NAME,
MODULE_CONFIG_NAME,
ENTRYPOINT_NAME,
Expand All @@ -471,8 +471,6 @@ fn test_say_hello() {

This has a little bit more boilerplate than testing a method directly:

- The inbound gRPC channel that requests are delivered over has to be explicitly
set up (`oak_tests::grpc_channel_setup_default`)
- After being configured, the runtime executes Nodes in separate threads
(`oak_runtime::configure_and_run`). The `derive(OakExports)` macro (from the
[`oak_derive`](https://project-oak.github.io/oak/sdk/oak_derive/index.html)
Expand Down
1 change: 1 addition & 0 deletions examples/Cargo.lock

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

29 changes: 21 additions & 8 deletions examples/abitest/tests/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,38 @@ const BACKEND_CONFIG_NAME: &str = "backend-config";
const LOG_CONFIG_NAME: &str = "logging-config";
const FRONTEND_ENTRYPOINT_NAME: &str = "frontend_oak_main";

// TODO(#541)
const FRONTEND_WASM: &str = "../../target/wasm32-unknown-unknown/debug/abitest_0_frontend.wasm";
const BACKEND_WASM: &str = "../../target/wasm32-unknown-unknown/debug/abitest_1_backend.wasm";
const FRONTEND_MANIFEST: &str = "../module_0/rust/Cargo.toml";
const BACKEND_MANIFEST: &str = "../module_1/rust/Cargo.toml";

const FRONTEND_WASM_NAME: &str = "abitest_0_frontend.wasm";
const BACKEND_WASM_NAME: &str = "abitest_1_backend.wasm";

fn build_wasm() -> std::io::Result<Vec<(String, Vec<u8>)>> {
Ok(vec![
(
FRONTEND_CONFIG_NAME.to_owned(),
oak_tests::compile_rust_wasm(FRONTEND_MANIFEST, FRONTEND_WASM_NAME)?,
),
(
BACKEND_CONFIG_NAME.to_owned(),
oak_tests::compile_rust_wasm(BACKEND_MANIFEST, BACKEND_WASM_NAME)?,
),
])
}

#[test]
fn test_abi() {
simple_logger::init().unwrap();

let configuration = oak_tests::test_configuration(
&[
(FRONTEND_CONFIG_NAME, FRONTEND_WASM),
(BACKEND_CONFIG_NAME, BACKEND_WASM),
],
build_wasm().expect("failed to build wasm modules"),
LOG_CONFIG_NAME,
FRONTEND_CONFIG_NAME,
FRONTEND_ENTRYPOINT_NAME,
);

let (runtime, entry_channel) = oak_runtime::Runtime::configure_and_run(configuration)
.expect("Unable to configure runtime with test wasm!");
.expect("unable to configure runtime with test wasm");

let result: grpc::Result<ABITestResponse> = oak_tests::grpc_request(
&entry_channel,
Expand Down
13 changes: 10 additions & 3 deletions examples/hello_world/module/rust/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ const MODULE_CONFIG_NAME: &str = "hello_world";
const LOG_CONFIG_NAME: &str = "log";
const ENTRYPOINT_NAME: &str = "oak_main";

// TODO(#541)
const WASM_PATH: &str = "../../../target/wasm32-unknown-unknown/debug/hello_world.wasm";
const MODULE_MANIFEST: &str = "Cargo.toml";
const MODULE_WASM_NAME: &str = "hello_world.wasm";

fn build_wasm() -> std::io::Result<Vec<(String, Vec<u8>)>> {
Ok(vec![(
MODULE_CONFIG_NAME.to_owned(),
oak_tests::compile_rust_wasm(MODULE_MANIFEST, MODULE_WASM_NAME)?,
)])
}

// Test invoking the SayHello Node service method via the Oak runtime.
#[test]
fn test_say_hello() {
simple_logger::init().unwrap();

let configuration = oak_tests::test_configuration(
&[(MODULE_CONFIG_NAME, WASM_PATH)],
build_wasm().expect("failed to build wasm modules"),
LOG_CONFIG_NAME,
MODULE_CONFIG_NAME,
ENTRYPOINT_NAME,
Expand Down
7 changes: 3 additions & 4 deletions scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ readonly SCRIPTS_DIR="$(dirname "$(readlink -f "$0")")"
# shellcheck source=scripts/common
source "$SCRIPTS_DIR/common"

# For each Rust workspace, first build Wasm modules where appropriate, then run tests, then
# run doc tests, then run clippy (turning warnings into errors).
# For each Rust workspace, run tests, then run doc tests, then run clippy
# (turning warnings into errors).
#
# See:
# - https://doc.rust-lang.org/cargo/commands/cargo-test.html
Expand All @@ -17,8 +17,7 @@ cargo test --all-targets --manifest-path=./sdk/rust/Cargo.toml
cargo test --doc --manifest-path=./sdk/rust/Cargo.toml
cargo clippy --all-targets --manifest-path=./sdk/rust/Cargo.toml -- --deny=warnings

cargo build --target wasm32-unknown-unknown --manifest-path=./examples/Cargo.toml
cargo test --all-targets --manifest-path=./examples/Cargo.toml
cargo test --all-targets --manifest-path=./examples/Cargo.toml -- --nocapture
cargo test --doc --manifest-path=./examples/Cargo.toml
cargo clippy --all-targets --manifest-path=./examples/Cargo.toml -- --deny=warnings

Expand Down
1 change: 1 addition & 0 deletions sdk/rust/Cargo.lock

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

1 change: 1 addition & 0 deletions sdk/rust/oak_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ oak_log = "=0.1.0"
oak_runtime = "=0.1.0"
protobuf = "*"
rand = { version = "*" }
tempdir = "*"

[build-dependencies]
protoc-rust = "*"
36 changes: 29 additions & 7 deletions sdk/rust/oak_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,53 @@ use log::info;
use oak_runtime::ChannelEither;
use protobuf::{Message, ProtobufEnum};

use std::process::Command;
use tempdir::TempDir;

use oak_runtime::proto::manager::{
ApplicationConfiguration, LogConfiguration, NodeConfiguration, WebAssemblyConfiguration,
};

// TODO(#544)

/// Uses cargo to compile a Rust manifest to Wasm bytes. Compilation is performed in a temporary
/// directory.
pub fn compile_rust_wasm(cargo_path: &str, module_name: &str) -> std::io::Result<Vec<u8>> {
let temp_dir = TempDir::new("")?;
Command::new("cargo")
.args(&["build", "--target=wasm32-unknown-unknown"])
.args(&[
format!("--manifest-path={}", cargo_path),
format!("--target-dir={}", temp_dir.path().display()),
])
.spawn()?
.wait()?;

let mut path = temp_dir.into_path();
path.push("wasm32-unknown-unknown/debug");
path.push(module_name);

std::fs::read(path)
}

/// Create a simple configuration with collection of Wasm nodes and a logger node.
///
/// - module_name_wasm: Node name and path to associated Wasm file.
/// - module_name_wasm: Node name and Wasm bytes.
/// - logger_name: Node name to use for a logger configuration.
/// - initial_node: Initial node to run on launch.
/// - entrypoint: Entrypoint in the initial node to run on launch.
// TODO(#563)
pub fn test_configuration<P: AsRef<std::path::Path>>(
module_name_wasm: &[(&str, P)],
pub fn test_configuration(
module_name_wasm: Vec<(String, Vec<u8>)>,
logger_name: &str,
initial_node: &str,
entrypoint: &str,
) -> ApplicationConfiguration {
let mut nodes: Vec<NodeConfiguration> = module_name_wasm
.iter()
.map(|(name, module)| {
let wasm = std::fs::read(module).expect("Module missing");
.into_iter()
.map(|(name, wasm)| {
let mut node = NodeConfiguration::new();
node.set_name(name.to_string());
node.set_name(name);
node.set_wasm_config({
let mut w = WebAssemblyConfiguration::new();
w.set_module_bytes(wasm);
Expand Down

0 comments on commit 408e06a

Please sign in to comment.