Skip to content

Commit

Permalink
Merge pull request #2223 from fermyon/e2e-testing-framework
Browse files Browse the repository at this point in the history
E2E testing using testing framework
  • Loading branch information
rylev authored Jan 12, 2024
2 parents b7d6576 + 2efcbdd commit 7a6b81b
Show file tree
Hide file tree
Showing 37 changed files with 557 additions and 2,800 deletions.
22 changes: 19 additions & 3 deletions Cargo.lock

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

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ e2e-testing = { path = "crates/e2e-testing" }
http-body-util = { workspace = true }
testing-framework = { path = "tests/testing-framework" }
hyper-util = { version = "0.1.2", features = ["tokio"] }
redis = "0.24"
runtime-tests = { path = "tests/runtime-tests" }
test-components = { path = "tests/test-components" }
test-codegen-macro = { path = "crates/test-codegen-macro" }
Expand Down Expand Up @@ -133,8 +134,12 @@ tracing = { version = "0.1", features = ["log"] }

# TODO: update to final 17.0.0 release once it's available
wasi-common-preview1 = { git = "https://github.com/bytecodealliance/wasmtime", branch = "release-17.0.0", package = "wasi-common" }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", branch = "release-17.0.0", features = ["component-model"] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", branch = "release-17.0.0", features = ["tokio"] }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", branch = "release-17.0.0", features = [
"component-model",
] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", branch = "release-17.0.0", features = [
"tokio",
] }
wasmtime-wasi-http = { git = "https://github.com/bytecodealliance/wasmtime", branch = "release-17.0.0" }

spin-componentize = { git = "https://github.com/fermyon/spin-componentize", rev = "191789170abde10cd55590466c0660dd6c7d472a" }
Expand Down
18 changes: 13 additions & 5 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ mod integration_tests {
"{}/{}",
RUST_HTTP_INTEGRATION_TEST, DEFAULT_MANIFEST_LOCATION
),
|spin| {
|env| {
let spin = env.runtime_mut();
assert_spin_status(spin, "/test/hello", 200)?;
assert_spin_status(spin, "/test/hello/wildcards/should/be/handled", 200)?;
assert_spin_status(spin, "/thisshouldfail", 404)?;
Expand All @@ -57,7 +58,8 @@ mod integration_tests {
fn test_duplicate_rust_local() -> Result<()> {
integration_test(
format!("{}/{}", RUST_HTTP_INTEGRATION_TEST, "double-trouble.toml"),
|spin| {
|env| {
let spin = env.runtime_mut();
assert_spin_status(spin, "/route1", 200)?;
assert_spin_status(spin, "/route2", 200)?;
assert_spin_status(spin, "/thisshouldfail", 404)?;
Expand Down Expand Up @@ -243,12 +245,15 @@ mod integration_tests {

fn integration_test(
manifest_path: impl Into<PathBuf>,
test: impl FnOnce(&mut testing_framework::Spin) -> testing_framework::TestResult<anyhow::Error>
test: impl FnOnce(
&mut testing_framework::TestEnvironment<testing_framework::Spin>,
) -> testing_framework::TestResult<anyhow::Error>
+ 'static,
) -> anyhow::Result<()> {
let manifest_path = manifest_path.into();
let spin = testing_framework::TestEnvironmentConfig::spin(
spin_binary().into(),
[],
move |env| {
// Copy manifest
let mut template = testing_framework::ManifestTemplate::from_file(manifest_path)?;
Expand All @@ -261,17 +266,20 @@ mod integration_tests {
Ok(())
},
testing_framework::ServicesConfig::none(),
testing_framework::SpinMode::Http,
);
let mut env = testing_framework::TestEnvironment::up(spin)?;
Ok(env.test(test)?)
test(&mut env)?;
Ok(())
}

fn assert_spin_status(
spin: &mut testing_framework::Spin,
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
16 changes: 12 additions & 4 deletions tests/runtime-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ impl RuntimeTest<Spin> {
Ok(())
};
let services_config = services_config(&config)?;
let env_config = TestEnvironmentConfig::spin(spin_binary, preboot, services_config);
let env_config = TestEnvironmentConfig::spin(
spin_binary,
[],
preboot,
services_config,
testing_framework::SpinMode::Http,
);
let env = TestEnvironment::up(env_config)?;
Ok(Self {
test_path: config.test_path,
Expand All @@ -84,7 +90,7 @@ impl RuntimeTest<Spin> {
}
};
}
let response = self.env.test(test);
let response = test(&mut self.env);
let error_file = self.test_path.join("error.txt");
match response {
Ok(()) if !error_file.exists() => log::info!("Test passed!"),
Expand Down Expand Up @@ -173,8 +179,10 @@ fn copy_manifest<R>(test_dir: &Path, env: &mut TestEnvironment<R>) -> anyhow::Re
Ok(())
}

fn test(runtime: &mut Spin) -> TestResult<RuntimeTestFailure> {
let response = runtime.make_http_request(reqwest::Method::GET, "/")?;
fn test(env: &mut TestEnvironment<Spin>) -> TestResult<RuntimeTestFailure> {
let runtime = env.runtime_mut();
let request = testing_framework::Request::new(reqwest::Method::GET, "/");
let response = runtime.make_http_request(request)?;
if response.status() == 200 {
return Ok(());
}
Expand Down
Loading

0 comments on commit 7a6b81b

Please sign in to comment.