Skip to content

Commit

Permalink
Enable more tests as components
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Jun 28, 2024
1 parent 7b8c222 commit 679bc21
Show file tree
Hide file tree
Showing 30 changed files with 493 additions and 258 deletions.
11 changes: 7 additions & 4 deletions cli/tests/integration/async_io.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::common::{Test, TestResult};
use crate::{
common::{Test, TestResult},
viceroy_test_sequential,
};
use hyper::{body::HttpBody, Body, Request, Response, StatusCode};
use std::sync::{
atomic::{AtomicUsize, Ordering},
Expand All @@ -13,8 +16,7 @@ use tokio::sync::Barrier;
//
// https://github.com/fastly/Viceroy/issues/207 tracks the broader issue.
#[cfg(target_family = "unix")]
#[tokio::test(flavor = "multi_thread")]
async fn async_io_methods() -> TestResult {
viceroy_test_sequential!(async_io_methods, |is_component| {
let request_count = Arc::new(AtomicUsize::new(0));
let req_count_1 = request_count.clone();
let req_count_2 = request_count.clone();
Expand All @@ -34,6 +36,7 @@ async fn async_io_methods() -> TestResult {
// total and will behave differently depending on which request # it is
// processing.
let test = Test::using_fixture("async_io.wasm")
.adapt_component(is_component)
.async_backend("Simple", "/", None, move |req: Request<Body>| {
assert_eq!(req.headers()["Host"], "simple.org");
let req_count_1 = req_count_1.clone();
Expand Down Expand Up @@ -206,4 +209,4 @@ async fn async_io_methods() -> TestResult {
assert_eq!(resp.headers()["Ready-Index"], "timeout");

Ok(())
}
});
17 changes: 10 additions & 7 deletions cli/tests/integration/body.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
//! Tests related to HTTP request and response bodies.

use {
crate::common::{Test, TestResult},
crate::{
common::{Test, TestResult},
viceroy_test,
},
hyper::{body, StatusCode},
};

#[tokio::test(flavor = "multi_thread")]
async fn bodies_can_be_written_and_appended() -> TestResult {
viceroy_test!(bodies_can_be_written_and_appended, |is_component| {
let resp = Test::using_fixture("write-body.wasm")
.adapt_component(is_component)
.against_empty()
.await?;

Expand All @@ -19,13 +22,13 @@ async fn bodies_can_be_written_and_appended() -> TestResult {
assert_eq!(&body, "Hello, Viceroy!");

Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
async fn bodies_can_be_written_and_read() -> TestResult {
viceroy_test!(bodies_can_be_written_and_read, |is_component| {
let resp = Test::using_fixture("write-and-read-body.wasm")
.adapt_component(is_component)
.against_empty()
.await?;
assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}
});
22 changes: 11 additions & 11 deletions cli/tests/integration/client_certs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::common::{Test, TestResult};
use crate::{
common::{Test, TestResult},
viceroy_test,
};
use base64::engine::{general_purpose, Engine};
use hyper::http::response;
use hyper::server::conn::AddrIncoming;
Expand Down Expand Up @@ -127,10 +130,8 @@ fn build_server_tls_config() -> ServerConfig {
.expect("valid server cert")
}

#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial]
async fn custom_ca_works() -> TestResult {
let test = Test::using_fixture("mutual-tls.wasm");
viceroy_test!(custom_ca_works, |is_component| {
let test = Test::using_fixture("mutual-tls.wasm").adapt_component(is_component);
let server_addr: SocketAddr = "127.0.0.1:0".parse().expect("localhost parses");
let incoming = AddrIncoming::bind(&server_addr).expect("bind");
let bound_port = incoming.local_addr().port();
Expand Down Expand Up @@ -197,17 +198,16 @@ async fn custom_ca_works() -> TestResult {
StatusCode::SERVICE_UNAVAILABLE
);
Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial]
async fn client_certs_work() -> TestResult {
viceroy_test!(client_certs_work, |is_component| {
// Set up the test harness
std::env::set_var(
"SSL_CERT_FILE",
concat!(env!("CARGO_MANIFEST_DIR"), "/../test-fixtures/data/ca.pem"),
);
let test = Test::using_fixture("mutual-tls.wasm");
let test = Test::using_fixture("mutual-tls.wasm").adapt_component(is_component);

let server_addr: SocketAddr = "127.0.0.1:0".parse().expect("localhost parses");
let incoming = AddrIncoming::bind(&server_addr).expect("bind");
let bound_port = incoming.local_addr().port();
Expand Down Expand Up @@ -261,4 +261,4 @@ async fn client_certs_work() -> TestResult {
std::env::remove_var("SSL_CERT_FILE");

Ok(())
}
});
23 changes: 23 additions & 0 deletions cli/tests/integration/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ macro_rules! viceroy_test {
};
}

/// A variation of `viceroy_test` that will produce one test, and run the core-wasm and component
/// cases sequentially. A failure in either one causes the whole test to fail, which makes things a
/// little bit harder to debug, but for tests like logging that manipulate a shared resource, this
/// is the simplest solution.
#[macro_export]
macro_rules! viceroy_test_sequential {
($name:ident, |$is_component:ident| $body:block) => {
mod $name {
use super::*;

async fn test_impl($is_component: bool) -> TestResult {
$body
}

#[tokio::test(flavor = "multi_thread")]
async fn sequential() -> TestResult {
test_impl(false).await?;
test_impl(true).await
}
}
};
}

/// A shorthand for the path to our test fixtures' build artifacts for Rust tests.
///
/// This value can be appended with the name of a fixture's `.wasm` in a test program, using the
Expand Down
24 changes: 13 additions & 11 deletions cli/tests/integration/config_store_lookup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::common::{Test, TestResult};
use crate::{
common::{Test, TestResult},
viceroy_test,
};
use hyper::{body::to_bytes, StatusCode};

#[tokio::test(flavor = "multi_thread")]
async fn json_config_store_lookup_works() -> TestResult {
viceroy_test!(json_config_store_lookup_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "json-config_store-lookup"
description = "json config_store lookup test"
Expand All @@ -13,8 +15,8 @@ async fn json_config_store_lookup_works() -> TestResult {
format = "json"
"#;

// let resp = Test::using_fixture("config_store-lookup.wasm")
let resp = Test::using_fixture("config-store-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;
Expand All @@ -27,10 +29,9 @@ async fn json_config_store_lookup_works() -> TestResult {
.is_empty());

Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
async fn inline_toml_config_store_lookup_works() -> TestResult {
viceroy_test!(inline_toml_config_store_lookup_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "inline-toml-config_store-lookup"
description = "inline toml config_store lookup test"
Expand All @@ -44,6 +45,7 @@ async fn inline_toml_config_store_lookup_works() -> TestResult {
"#;

let resp = Test::using_fixture("config-store-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;
Expand All @@ -56,22 +58,22 @@ async fn inline_toml_config_store_lookup_works() -> TestResult {
.is_empty());

Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
async fn missing_config_store_works() -> TestResult {
viceroy_test!(missing_config_store_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "missing-config_store-config"
description = "missing config_store test"
language = "rust"
"#;

let resp = Test::using_fixture("config-store-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;

assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);

Ok(())
}
});
17 changes: 10 additions & 7 deletions cli/tests/integration/device_detection_lookup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::common::{Test, TestResult};
use crate::{
common::{Test, TestResult},
viceroy_test,
};
use hyper::{body::to_bytes, StatusCode};

#[tokio::test(flavor = "multi_thread")]
async fn json_device_detection_lookup_works() -> TestResult {
viceroy_test!(json_device_detection_lookup_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "json-device-detection-lookup"
description = "json device detection lookup test"
Expand All @@ -15,6 +17,7 @@ async fn json_device_detection_lookup_works() -> TestResult {
"#;

let resp = Test::using_fixture("device-detection-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;
Expand All @@ -27,10 +30,9 @@ async fn json_device_detection_lookup_works() -> TestResult {
.is_empty());

Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
async fn inline_toml_device_detection_lookup_works() -> TestResult {
viceroy_test!(inline_toml_device_detection_lookup_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "inline-toml-device-detection-lookup"
description = "inline toml device detection lookup test"
Expand All @@ -51,6 +53,7 @@ async fn inline_toml_device_detection_lookup_works() -> TestResult {
"#;

let resp = Test::using_fixture("device-detection-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;
Expand All @@ -63,4 +66,4 @@ async fn inline_toml_device_detection_lookup_works() -> TestResult {
.is_empty());

Ok(())
}
});
23 changes: 13 additions & 10 deletions cli/tests/integration/dictionary_lookup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::common::{Test, TestResult};
use crate::{
common::{Test, TestResult},
viceroy_test,
};
use hyper::{body::to_bytes, StatusCode};

#[tokio::test(flavor = "multi_thread")]
async fn json_dictionary_lookup_works() -> TestResult {
viceroy_test!(json_dictionary_lookup_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "json-dictionary-lookup"
description = "json dictionary lookup test"
Expand All @@ -16,6 +18,7 @@ async fn json_dictionary_lookup_works() -> TestResult {
"#;

let resp = Test::using_fixture("dictionary-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;
Expand All @@ -28,10 +31,9 @@ async fn json_dictionary_lookup_works() -> TestResult {
.is_empty());

Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
async fn inline_toml_dictionary_lookup_works() -> TestResult {
viceroy_test!(inline_toml_dictionary_lookup_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "inline-toml-dictionary-lookup"
description = "inline toml dictionary lookup test"
Expand All @@ -47,6 +49,7 @@ async fn inline_toml_dictionary_lookup_works() -> TestResult {
"#;

let resp = Test::using_fixture("dictionary-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;
Expand All @@ -59,22 +62,22 @@ async fn inline_toml_dictionary_lookup_works() -> TestResult {
.is_empty());

Ok(())
}
});

#[tokio::test(flavor = "multi_thread")]
async fn missing_dictionary_works() -> TestResult {
viceroy_test!(missing_dictionary_works, |is_component| {
const FASTLY_TOML: &str = r#"
name = "missing-dictionary-config"
description = "missing dictionary test"
language = "rust"
"#;

let resp = Test::using_fixture("dictionary-lookup.wasm")
.adapt_component(is_component)
.using_fastly_toml(FASTLY_TOML)?
.against_empty()
.await?;

assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);

Ok(())
}
});
11 changes: 7 additions & 4 deletions cli/tests/integration/downstream_req.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use {
crate::common::{Test, TestResult},
crate::{
common::{Test, TestResult},
viceroy_test,
},
hyper::{Request, StatusCode},
};

#[tokio::test(flavor = "multi_thread")]
async fn downstream_request_works() -> TestResult {
viceroy_test!(downstream_request_works, |is_component| {
let req = Request::get("/")
.header("Accept", "text/html")
.header("X-Custom-Test", "abcdef")
.body("Hello, world!")?;
let resp = Test::using_fixture("downstream-req.wasm")
.adapt_component(is_component)
.against(req)
.await?;

assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}
});
11 changes: 7 additions & 4 deletions cli/tests/integration/edge_rate_limiting.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! Tests related to HTTP request and response bodies.

use {
crate::common::{Test, TestResult},
crate::{
common::{Test, TestResult},
viceroy_test,
},
hyper::StatusCode,
};

#[tokio::test(flavor = "multi_thread")]
async fn check_hostcalls_implemented() -> TestResult {
viceroy_test!(check_hostcalls_implemented, |is_component| {
let resp = Test::using_fixture("edge-rate-limiting.wasm")
.adapt_component(is_component)
.against_empty()
.await?;
assert_eq!(resp.status(), StatusCode::OK);
Ok(())
}
});
Loading

0 comments on commit 679bc21

Please sign in to comment.