Skip to content

Commit

Permalink
add unit test for io redirection
Browse files Browse the repository at this point in the history
Signed-off-by: David Justice <[email protected]>
  • Loading branch information
devigned committed Mar 8, 2023
1 parent 7bf15d0 commit 6deb755
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 6 deletions.
12 changes: 7 additions & 5 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ flate2 = "1"
tar = "0.4"

[dev-dependencies]
tempdir = { workspace = true}
tempdir = { workspace = true }
tempfile = { workspace = true }
rand = { worspace = true }

[features]
default = ["keyvalue", "distributed-locking", "messaging", "runtime-configs", "sql", "http-server", "http-client"]
Expand Down Expand Up @@ -83,6 +85,7 @@ tokio = { version = "1", features = ["full"] }
tracing = { version = "0.1", features = ["log"] }
toml = "0.5"
tempdir = "0.3"
tempfile = "3.4"
log = { version = "0.4", default-features = false }
env_logger = "0.9"
as-any = "0.3"
Expand Down
50 changes: 50 additions & 0 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,53 @@ fn maybe_add_named_capability_to_store(

Ok(())
}

#[cfg(test)]
mod unittest {
use std::path::{Path, PathBuf};
use std::fs::File;
use tempfile::tempdir;
use tokio::fs;
use rand::Rng;
use slight_runtime::IORedirects;
use crate::commands::run::{handle_run, RunArgs};

#[tokio::test]
async fn test_handle_run_with_io() -> anyhow::Result<()> {
let module = "./src/commands/test/io-test.wasm";
assert!(Path::new(module).exists());
let slightfile = "./src/commands/test/slightfile.toml";
assert!(Path::new(slightfile).exists());

let tmp_dir = tempdir()?;
let stdin_path = tmp_dir.path().join("stdin");
let stdout_path = tmp_dir.path().join("stdout");
let stderr_path = tmp_dir.path().join("stderr");

let canary: String = rand::thread_rng()
.gen_ascii_chars()
.take(7)
.map(char::from)
.collect();
fs::write(&stdin_path, &canary).await?;
let _ = File::create(&stdout_path)?;
let _ = File::create(&stderr_path)?;

let args = RunArgs{
module: PathBuf::from(module),
slightfile: PathBuf::from(slightfile),
io_redirects: Some(IORedirects{
stdin_path: PathBuf::from(&stdin_path),
stdout_path: PathBuf::from(&stdout_path),
stderr_path: PathBuf::from(&stderr_path),
})
};

let _ = handle_run(args).await?;
let stdout_output = fs::read_to_string(&stdout_path).await?;
assert_eq!(stdout_output, canary);
let stderr_output = fs::read_to_string(&stderr_path).await?;
assert_eq!(stderr_output, format!("error: {canary}"));
Ok(())
}
}
Binary file added src/commands/test/io-test.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions src/commands/test/slightfile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
specversion = "0.2"

[[capability]]
resource = "configs.envvars"
name = "env-configs"
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tokio = { workspace = true }
anyhow = { workspace = true }
mosquitto-rs = { version = "0.4.0", features = ["vendored-openssl", "vendored-mosquitto"] }
tempdir = { workspace = true }
rand = { workspace = true }

[target.'cfg(unix)'.dev-dependencies]
signal-child = "1"
2 changes: 2 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const WIT_DIRECTORY: &str = "wit/*";
const KEYVALUE_TEST_PATH: &str = "./keyvalue-test";
const HTTP_TEST_PATH: &str = "./http-test";
const CONFIGS_TEST_PATH: &str = "./configs-test";
const IO_TEST_PATH: &str = "./io-test";

fn main() {
println!("cargo:rerun-if-changed=build.rs");
Expand All @@ -28,6 +29,7 @@ fn main() {
std::process::exit(1);
}
// Build test wasm modules
cargo_wasi_build(IO_TEST_PATH);
cargo_wasi_build(KEYVALUE_TEST_PATH);
cargo_wasi_build(HTTP_TEST_PATH);
cargo_wasi_build(CONFIGS_TEST_PATH);
Expand Down
16 changes: 16 additions & 0 deletions tests/io-test/Cargo.lock

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

14 changes: 14 additions & 0 deletions tests/io-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "io-test"
version = "0.1.0"
edition = "2021"
authors = [ "DeisLabs Engineering Team" ]

[[bin]]
name = "io-test"
test = false

[dependencies]
anyhow = "1"

[workspace]
1 change: 1 addition & 0 deletions tests/io-test/slightfile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
specversion = "0.2"
11 changes: 11 additions & 0 deletions tests/io-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::io;
use std::io::Write;
use anyhow::Result;

fn main() -> Result<()> {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer)?;
io::stdout().write_all(buffer.as_bytes())?;
io::stderr().write_all(format!("error: {buffer}").as_bytes())?;
Ok(())
}

0 comments on commit 6deb755

Please sign in to comment.