Skip to content

Commit a835811

Browse files
authored
Merge pull request spinframework#1657 from fermyon/test-local-redis-sdk
Make a test that exercises the canary Rust SDK's redis support
2 parents 364d3e8 + 5f97e2b commit a835811

File tree

13 files changed

+128
-33
lines changed

13 files changed

+128
-33
lines changed

build.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ fn run<S: Into<String> + AsRef<std::ffi::OsStr>>(
130130
cmd.stdout(process::Stdio::piped());
131131
cmd.stderr(process::Stdio::piped());
132132

133-
if let Some(dir) = dir {
134-
cmd.current_dir(dir.into());
133+
let dir = dir.map(Into::into);
134+
if let Some(dir) = &dir {
135+
cmd.current_dir(dir);
135136
};
136137

137138
if let Some(env) = env {
@@ -141,20 +142,20 @@ fn run<S: Into<String> + AsRef<std::ffi::OsStr>>(
141142
};
142143

143144
cmd.arg("-c");
144-
cmd.arg(
145-
args.into_iter()
146-
.map(Into::into)
147-
.collect::<Vec<String>>()
148-
.join(" "),
149-
);
145+
let c = args
146+
.into_iter()
147+
.map(Into::into)
148+
.collect::<Vec<String>>()
149+
.join(" ");
150+
cmd.arg(&c);
150151

151152
let output = cmd.output().unwrap();
152-
let code = output.status.code().unwrap();
153-
if code != 0 {
154-
println!("{:#?}", std::str::from_utf8(&output.stderr).unwrap());
155-
println!("{:#?}", std::str::from_utf8(&output.stdout).unwrap());
156-
// just fail
157-
assert_eq!(0, code);
153+
let exit = output.status;
154+
if !exit.success() {
155+
println!("{}", std::str::from_utf8(&output.stderr).unwrap());
156+
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
157+
let dir = dir.unwrap_or_else(current_dir);
158+
panic!("while running the build script, the command '{c}' failed to run in '{dir}'")
158159
}
159160

160161
output
@@ -167,3 +168,9 @@ fn get_os_process() -> String {
167168
String::from("bash")
168169
}
169170
}
171+
172+
fn current_dir() -> String {
173+
std::env::current_dir()
174+
.map(|d| d.display().to_string())
175+
.unwrap_or_else(|_| String::from("<CURRENT DIR>"))
176+
}

crates/e2e-testing/src/testcase.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ impl TestCase {
153153
}
154154

155155
// run spin build
156-
let build_output = controller.build_app(&appname).context("building app")?;
156+
let build_output = controller
157+
.build_app(&appname)
158+
.context("failed building app")?;
157159
if bail_on_run_failure {
158160
utils::assert_success(&build_output);
159161
}

tests/spinup_tests.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ mod spinup_tests {
8989
}
9090

9191
#[tokio::test]
92-
async fn simple_spin_rust_works() {
93-
testcases::simple_spin_rust_works(CONTROLLER).await
92+
async fn head_rust_sdk_http() {
93+
testcases::head_rust_sdk_http(CONTROLLER).await
94+
}
95+
96+
#[tokio::test]
97+
async fn head_rust_sdk_redis() {
98+
testcases::head_rust_sdk_redis(CONTROLLER).await
9499
}
95100

96101
#[tokio::test]

tests/testcases/simple-spin-rust-test/Cargo.toml tests/testcases/head-rust-sdk-http/Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "simple-spin-rust-test"
2+
name = "head-rust-sdk-http"
33
version = "0.1.0"
44
edition = "2021"
55

@@ -17,7 +17,3 @@ http = "0.2"
1717
spin-sdk = { path = "../../../sdk/rust" }
1818

1919
[workspace]
20-
21-
# Metadata about this component.
22-
[package.metadata.component]
23-
name = "spinhelloworld"

tests/testcases/simple-spin-rust-test/spin.toml tests/testcases/head-rust-sdk-http/spin.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spin_version = "1"
22
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
33
description = "A simple application that returns hello and goodbye."
4-
name = "simple-spin-rust-test"
4+
name = "head-rust-sdk-http"
55
trigger = {type = "http", base = "/test"}
66
version = "1.0.0"
77

@@ -10,7 +10,7 @@ object = { default = "teapot" }
1010

1111
[[component]]
1212
id = "hello"
13-
source = "target/wasm32-wasi/release/simple_spin_rust_test.wasm"
13+
source = "target/wasm32-wasi/release/head_rust_sdk_http.wasm"
1414
files = [ { source = "assets", destination = "/" } ]
1515
[component.trigger]
1616
route = "/hello/..."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target = "wasm32-wasi"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "head-rust-sdk-redis"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = [ "cdylib" ]
8+
9+
[dependencies]
10+
anyhow = "1"
11+
bytes = "1"
12+
http = "0.2"
13+
spin-sdk = { path = "../../../sdk/rust"}
14+
15+
[workspace]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spin_version = "1"
2+
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
3+
description = "A simple redis application that exercises the Rust SDK in the current branch"
4+
name = "head-rust-sdk-redis"
5+
trigger = {type = "redis", address = "redis://redis:6379"}
6+
version = "1.0.0"
7+
8+
[[component]]
9+
id = "hello"
10+
source = "target/wasm32-wasi/release/head_rust_sdk_redis.wasm"
11+
[component.trigger]
12+
channel = "my-channel"
13+
[component.build]
14+
command = "cargo build --target wasm32-wasi --release"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use spin_sdk::redis_component;
2+
3+
#[redis_component]
4+
fn on_message(message: bytes::Bytes) -> anyhow::Result<()> {
5+
println!(
6+
"Got message: '{}'",
7+
std::str::from_utf8(&*message).unwrap_or("<MESSAGE NOT UTF8>")
8+
);
9+
Ok(())
10+
}

tests/testcases/mod.rs

+53-9
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ pub async fn assets_routing_works(controller: &dyn Controller) {
665665
tc.run(controller).await.unwrap()
666666
}
667667

668-
pub async fn simple_spin_rust_works(controller: &dyn Controller) {
668+
/// Test an http app using the current branch's version of the Rust SDK
669+
pub async fn head_rust_sdk_http(controller: &dyn Controller) {
669670
async fn checks(
670671
metadata: AppMetadata,
671672
_: Option<Pin<Box<dyn AsyncBufRead>>>,
@@ -719,8 +720,42 @@ pub async fn simple_spin_rust_works(controller: &dyn Controller) {
719720
}
720721

721722
let tc = TestCaseBuilder::default()
722-
.name("simple-spin-rust-test".to_string())
723-
.appname(Some("simple-spin-rust-test".to_string()))
723+
.name("head-rust-sdk-http".to_string())
724+
.appname(Some("head-rust-sdk-http".to_string()))
725+
.assertions(
726+
|metadata: AppMetadata,
727+
stdout_stream: Option<Pin<Box<dyn AsyncBufRead>>>,
728+
stderr_stream: Option<Pin<Box<dyn AsyncBufRead>>>| {
729+
Box::pin(checks(metadata, stdout_stream, stderr_stream))
730+
},
731+
)
732+
.build()
733+
.unwrap();
734+
735+
tc.run(controller).await.unwrap()
736+
}
737+
738+
/// Test a redis app using the current branch's version of the Rust SDK
739+
pub async fn head_rust_sdk_redis(controller: &dyn Controller) {
740+
async fn checks(
741+
_: AppMetadata,
742+
_: Option<Pin<Box<dyn AsyncBufRead>>>,
743+
stderr_stream: Option<Pin<Box<dyn AsyncBufRead>>>,
744+
) -> Result<()> {
745+
wait_for_spin().await;
746+
let stderr = get_output_stream(stderr_stream).await?;
747+
anyhow::ensure!(
748+
stderr.is_empty(),
749+
"expected stderr to be empty, but it was not: {}",
750+
stderr.join("\n")
751+
);
752+
Ok(())
753+
}
754+
755+
let tc = TestCaseBuilder::default()
756+
.name("head-rust-sdk-redis".to_string())
757+
.appname(Some("head-rust-sdk-redis".to_string()))
758+
.trigger_type("redis".to_string())
724759
.assertions(
725760
|metadata: AppMetadata,
726761
stdout_stream: Option<Pin<Box<dyn AsyncBufRead>>>,
@@ -877,8 +912,7 @@ pub async fn redis_go_works(controller: &dyn Controller) {
877912
_: Option<Pin<Box<dyn AsyncBufRead>>>,
878913
stderr_stream: Option<Pin<Box<dyn AsyncBufRead>>>,
879914
) -> Result<()> {
880-
//TODO: wait for spin up to be ready dynamically
881-
sleep(Duration::from_secs(10)).await;
915+
wait_for_spin().await;
882916

883917
let output = utils::run(
884918
&[
@@ -894,7 +928,7 @@ pub async fn redis_go_works(controller: &dyn Controller) {
894928
)?;
895929
utils::assert_success(&output);
896930

897-
let stderr = utils::get_output_stream(stderr_stream, Duration::from_secs(5)).await?;
931+
let stderr = get_output_stream(stderr_stream).await?;
898932
let expected_logs = vec!["Payload::::", "msg-from-go-channel"];
899933

900934
assert!(expected_logs
@@ -934,8 +968,7 @@ pub async fn redis_rust_works(controller: &dyn Controller) {
934968
_: Option<Pin<Box<dyn AsyncBufRead>>>,
935969
stderr_stream: Option<Pin<Box<dyn AsyncBufRead>>>,
936970
) -> Result<()> {
937-
//TODO: wait for spin up to be ready dynamically
938-
sleep(Duration::from_secs(20)).await;
971+
wait_for_spin().await;
939972

940973
utils::run(
941974
&[
@@ -950,7 +983,7 @@ pub async fn redis_rust_works(controller: &dyn Controller) {
950983
None,
951984
)?;
952985

953-
let stderr = utils::get_output_stream(stderr_stream, Duration::from_secs(5)).await?;
986+
let stderr = get_output_stream(stderr_stream).await?;
954987

955988
let expected_logs = vec!["msg-from-rust-channel"];
956989

@@ -1207,3 +1240,14 @@ pub async fn error_messages(controller: &dyn Controller) {
12071240

12081241
tc.try_run(controller).await.unwrap();
12091242
}
1243+
1244+
async fn get_output_stream(
1245+
stream: Option<Pin<Box<dyn AsyncBufRead>>>,
1246+
) -> anyhow::Result<Vec<String>> {
1247+
utils::get_output_stream(stream, Duration::from_secs(5)).await
1248+
}
1249+
1250+
async fn wait_for_spin() {
1251+
//TODO: wait for spin up to be ready dynamically
1252+
sleep(Duration::from_secs(10)).await;
1253+
}

0 commit comments

Comments
 (0)