Skip to content

Commit

Permalink
Add dynamic env test
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Jan 11, 2024
1 parent 0e81ea5 commit e0ce58e
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 658 deletions.
3 changes: 2 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ mod integration_tests {
uri: &str,
status: u16,
) -> testing_framework::TestResult<anyhow::Error> {
let r = spin.make_http_request(reqwest::Method::GET, uri)?;
let r =
spin.make_http_request(testing_framework::Request::new(reqwest::Method::GET, uri))?;
if r.status() != status {
return Err(testing_framework::TestError::Failure(anyhow!(
"Expected status {} for {} but got {}",
Expand Down
3 changes: 2 additions & 1 deletion tests/runtime-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ fn copy_manifest<R>(test_dir: &Path, env: &mut TestEnvironment<R>) -> anyhow::Re

fn test(env: &mut TestEnvironment<Spin>) -> TestResult<RuntimeTestFailure> {
let runtime = env.runtime_mut();
let response = runtime.make_http_request(reqwest::Method::GET, "/")?;
let request = testing_framework::Request::new(reqwest::Method::GET, "/");
let response = runtime.make_http_request(request)?;
if response.status() == 200 {
return Ok(());
}
Expand Down
116 changes: 84 additions & 32 deletions tests/spinup_tests.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
use anyhow::Context;
use redis::Commands;
use reqwest::header::HeaderValue;
use std::path::PathBuf;

#[cfg(feature = "e2e-tests")]
mod testcases;

/// Helper macro to assert that a condition is true eventually
macro_rules! assert_eventually {
($e:expr) => {
let mut i = 0;
loop {
if $e {
break;
} else if i > 20 {
assert!($e);
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
i += 1;
}
};
}

#[test]
/// Test that the --key-value flag works as expected
fn key_value_cli_flag() -> anyhow::Result<()> {
Expand All @@ -36,9 +18,12 @@ fn key_value_cli_flag() -> anyhow::Result<()> {
let spin = env.runtime_mut();
assert_spin_request(
spin,
reqwest::Method::GET,
&format!("/test?key={test_key}"),
testing_framework::Request::new(
reqwest::Method::GET,
&format!("/test?key={test_key}"),
),
200,
&[],
&test_value,
)
},
Expand All @@ -58,24 +43,33 @@ fn http_smoke_test() -> anyhow::Result<()> {
let spin = env.runtime_mut();
assert_spin_request(
spin,
reqwest::Method::GET,
"/test/hello",
testing_framework::Request::new(reqwest::Method::GET, "/test/hello"),
200,
&[],
"I'm a teapot",
)?;
assert_spin_request(
spin,
reqwest::Method::GET,
"/test/hello/wildcards/should/be/handled",
testing_framework::Request::new(
reqwest::Method::GET,
"/test/hello/wildcards/should/be/handled",
),
200,
&[],
"I'm a teapot",
)?;
assert_spin_request(spin, reqwest::Method::GET, "/thishsouldfail", 404, "")?;
assert_spin_request(
spin,
reqwest::Method::GET,
"/test/hello/test-placement",
testing_framework::Request::new(reqwest::Method::GET, "/thishsouldfail"),
404,
&[],
"",
)?;
assert_spin_request(
spin,
testing_framework::Request::new(reqwest::Method::GET, "/test/hello/test-placement"),
200,
&[],
"text for test",
)
},
Expand All @@ -85,8 +79,30 @@ fn http_smoke_test() -> anyhow::Result<()> {
}

#[test]
// TODO: it seems that running this test on macOS CI is not possible because the docker services doesn't run.
// Investigate if there is a possible fix for this.
#[cfg(feature = "e2e-tests")]
/// Test that basic redis trigger support works
fn redis_smoke_test() -> anyhow::Result<()> {
/// Helper macro to assert that a condition is true eventually
macro_rules! assert_eventually {
($e:expr) => {
let mut i = 0;
loop {
if $e {
break;
} else if i > 20 {
assert!($e);
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
i += 1;
}
};
}

use anyhow::Context;
use redis::Commands;
run_test(
"redis-smoke-test",
testing_framework::SpinMode::Redis,
Expand Down Expand Up @@ -120,6 +136,30 @@ fn redis_smoke_test() -> anyhow::Result<()> {
Ok(())
}

#[test]
/// Test dynamic environment variables
fn dynamic_env_test() -> anyhow::Result<()> {
run_test(
"dynamic-env-test",
testing_framework::SpinMode::Http,
vec!["--env".to_owned(), "foo=bar".to_owned()],
testing_framework::ServicesConfig::none(),
move |env| {
let spin = env.runtime_mut();
assert_spin_request(
spin,
testing_framework::Request::new(reqwest::Method::GET, "/env"),
200,
&[("env_some_key", "some_value"), ("ENV_foo", "bar")],
"I'm a teapot",
)?;
Ok(())
},
)?;

Ok(())
}

/// Run an e2e test
fn run_test(
test_name: impl Into<String>,
Expand Down Expand Up @@ -147,21 +187,33 @@ fn run_test(
/// Assert that a request to the spin server returns the expected status and body
fn assert_spin_request(
spin: &mut testing_framework::Spin,
method: reqwest::Method,
uri: &str,
request: testing_framework::Request<'_>,
expected_status: u16,
expected_headers: &[(&str, &str)],
expected_body: &str,
) -> testing_framework::TestResult<anyhow::Error> {
let r = spin.make_http_request(method, uri)?;
let uri = request.uri;
let mut r = spin.make_http_request(request)?;
let status = r.status();
let headers = std::mem::take(r.headers_mut());
let body = r.text().unwrap_or_else(|_| String::from("<non-utf8>"));
if status != expected_status {
return Err(testing_framework::TestError::Failure(anyhow::anyhow!(
"Expected status {expected_status} for {uri} but got {status}\nBody:\n{body}",
)));
}
let wrong_headers: std::collections::HashMap<_, _> = expected_headers
.iter()
.copied()
.filter(|(ek, ev)| headers.get(*ek) != Some(&HeaderValue::from_str(ev).unwrap()))
.collect();
if !wrong_headers.is_empty() {
return Err(testing_framework::TestError::Failure(anyhow::anyhow!(
"Expected headers {headers:?} to contain {wrong_headers:?}\nBody:\n{body}"
)));
}
if body != expected_body {
return Err(anyhow::anyhow!("expected {expected_body}, got {body}",).into());
return Err(anyhow::anyhow!("expected {expected_body}, got {body}").into());
}
Ok(())
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test-components/components/Cargo.lock

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

12 changes: 12 additions & 0 deletions tests/test-components/components/headers-dynamic-env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "headers-dynamic-env"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1"
http = "0.2"
spin-sdk = { path = "../../../../sdk/rust" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ name = "headers-dynamic-env-test"
version = "1.0.0"
authors = ["Fermyon Engineering <engineering@fermyon.com>"]
description = "A simple application that returns hello and goodbye."
trigger = {type = "http"}
trigger = { type = "http" }

[[component]]
id = "env"
source = "target/wasm32-wasi/release/headers_dynamic_env_test.wasm"
source = "%{source=headers-dynamic-env}"
environment = { some_key = "some_value" }
[component.build]
command = "cargo build --target wasm32-wasi --release"
[component.trigger]
route = "/env/..."
2 changes: 0 additions & 2 deletions tests/testcases/headers-dynamic-env-test/.cargo/config.toml

This file was deleted.

Loading

0 comments on commit e0ce58e

Please sign in to comment.