-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refrain from using docker for integration testing
Github actions on Windows do not allow running Linux containers. In order to run the same CI on Windows as Linux, we compile toxic HTTP services at test time.
- Loading branch information
Showing
22 changed files
with
777 additions
and
302 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "failure-server" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
axum = "0.6" | ||
anyhow = "1.0" | ||
noxious-client = "1.0" | ||
rand = "0.8" | ||
serde_json = "1.0" | ||
tempfile = "3.8" | ||
tokio = "1.0" | ||
tower = { version = "0.4", features = ["util"] } | ||
tower-fault = "0.0.5" | ||
tower-http = { version = "0.4", features = ["fs"] } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//! This module sets up 2 HTTP servers. | ||
//! * ToxicStaticHttpServer: serves TUF repo files on port 10101, with occasional random 503s. | ||
//! * ToxicTcpProxy: proxies to the TUF repo on port 10102, with occasional toxic behavior. | ||
use anyhow::Result; | ||
use noxious_client::{StreamDirection, Toxic, ToxicKind}; | ||
use std::path::Path; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
use toxic::{ToxicStaticHttpServer, ToxicTcpProxy}; | ||
|
||
mod toxic; | ||
|
||
const STATIC_HTTP_SERVER_LISTEN: &str = "127.0.0.1:10101"; | ||
const TCP_PROXY_LISTEN: &str = "127.0.0.1:10102"; | ||
const TCP_PROXY_CONFIG_API_LISTEN: &str = "127.0.0.1:8472"; | ||
|
||
pub struct IntegServers { | ||
toxic_tcp_proxy: ToxicTcpProxy, | ||
toxic_static_http_server: ToxicStaticHttpServer, | ||
} | ||
|
||
impl IntegServers { | ||
pub fn new<P: AsRef<Path>>(tuf_reference_repo: P) -> Result<Self> { | ||
let tuf_reference_repo = tuf_reference_repo.as_ref().to_owned(); | ||
|
||
let toxic_tcp_proxy = ToxicTcpProxy::new( | ||
"toxictuf".to_string(), | ||
TCP_PROXY_LISTEN, | ||
STATIC_HTTP_SERVER_LISTEN, | ||
TCP_PROXY_CONFIG_API_LISTEN, | ||
)? | ||
.with_toxic(Toxic { | ||
name: "slowclose".to_string(), | ||
kind: ToxicKind::SlowClose { delay: 500 }, | ||
toxicity: 0.75, | ||
direction: StreamDirection::Downstream, | ||
}) | ||
.with_toxic(Toxic { | ||
name: "timeout".to_string(), | ||
kind: ToxicKind::Timeout { timeout: 100 }, | ||
toxicity: 0.5, | ||
direction: StreamDirection::Downstream, | ||
}); | ||
|
||
let toxic_static_http_server = | ||
ToxicStaticHttpServer::new(STATIC_HTTP_SERVER_LISTEN, tuf_reference_repo)?; | ||
|
||
Ok(Self { | ||
toxic_tcp_proxy, | ||
toxic_static_http_server, | ||
}) | ||
} | ||
|
||
pub async fn run(&mut self) -> Result<()> { | ||
// Make sure we're starting from scratch | ||
self.teardown()?; | ||
|
||
self.toxic_static_http_server.start()?; | ||
self.toxic_tcp_proxy.start().await?; | ||
sleep(Duration::from_secs(1)); // give the servers a chance to start | ||
|
||
println!("**********************************************************************"); | ||
println!("the toxic tuf repo is available at {TCP_PROXY_LISTEN}"); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn teardown(&mut self) -> Result<()> { | ||
self.toxic_tcp_proxy.stop()?; | ||
self.toxic_static_http_server.stop()?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for IntegServers { | ||
fn drop(&mut self) { | ||
self.teardown().ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! A simple filesystem HTTP server that introduces chaos at the HTTP layer. | ||
//! | ||
//! Chaos includes: | ||
//! * Occasional additional request latency | ||
//! * Occasional 503 responses | ||
use super::ToSocketAddrsExt; | ||
use anyhow::{Context, Result}; | ||
use axum::{ | ||
http::{Request, StatusCode}, | ||
middleware::{self, Next}, | ||
response::Response, | ||
Router, | ||
}; | ||
use std::fmt::Debug; | ||
use std::net::{SocketAddr, ToSocketAddrs}; | ||
use std::path::{Path, PathBuf}; | ||
use tower_fault::latency::LatencyLayer; | ||
use tower_http::services::ServeDir; | ||
|
||
const ERR_503_PROBABILITY: f64 = 0.5; | ||
const LATENCY_PROBABILITY: f64 = 0.1; | ||
|
||
/// An HTTP server which serves static files from a directory. | ||
/// | ||
/// The server implementation is "toxic" in that it introduces artificial faults at the HTTP layer. | ||
#[derive(Debug)] | ||
pub(crate) struct ToxicStaticHttpServer { | ||
/// The proxy's listen address. Written to `ProxyConfig`. | ||
listen: SocketAddr, | ||
|
||
/// The path to serve static content from. | ||
serve_dir: PathBuf, | ||
|
||
/// Running server, if any | ||
running_server: Option<tokio::task::JoinHandle<Result<()>>>, | ||
} | ||
|
||
impl ToxicStaticHttpServer { | ||
pub(crate) fn new<T, P>(listen: T, serve_dir: P) -> Result<Self> | ||
where | ||
T: ToSocketAddrs + Debug, | ||
P: AsRef<Path>, | ||
{ | ||
let listen = listen.parse_only_one_address()?; | ||
let serve_dir = serve_dir.as_ref().to_owned(); | ||
let running_server = None; | ||
|
||
Ok(Self { | ||
listen, | ||
serve_dir, | ||
running_server, | ||
}) | ||
} | ||
|
||
/// Starts the HTTP server. | ||
pub(crate) fn start(&mut self) -> Result<()> { | ||
// Stop any existing server | ||
self.stop().ok(); | ||
|
||
// Chance to inject 50 to 200 milliseconds of latency | ||
let latency_layer = LatencyLayer::new(LATENCY_PROBABILITY, 50..200); | ||
// Chance to return an HTTP 503 error | ||
let error_layer = middleware::from_fn(maybe_return_error); | ||
|
||
let app = Router::new() | ||
.nest_service("/", ServeDir::new(&self.serve_dir)) | ||
.layer(error_layer) | ||
.layer(latency_layer); | ||
let server = axum::Server::bind(&self.listen).serve(app.into_make_service()); | ||
|
||
self.running_server = Some(tokio::spawn(async { | ||
server.await.context("Failed to run ToxicStaticHttpServer") | ||
})); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Attempts to kill the running server, if there is one. | ||
/// | ||
/// Succeeds if the server is killed successfully or if it isn't/was never running. | ||
pub(crate) fn stop(&mut self) -> Result<()> { | ||
if let Some(server) = self.running_server.take() { | ||
server.abort(); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Middleware for chaotically returning a 503 error. | ||
async fn maybe_return_error<B>(req: Request<B>, next: Next<B>) -> Result<Response, StatusCode> { | ||
if rand::random::<f64>() < ERR_503_PROBABILITY { | ||
Err(StatusCode::SERVICE_UNAVAILABLE) | ||
} else { | ||
Ok(next.run(req).await) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use anyhow::{Context, Result}; | ||
use std::fmt::Debug; | ||
use std::net::{SocketAddr, ToSocketAddrs}; | ||
|
||
pub(crate) use http_server::ToxicStaticHttpServer; | ||
pub(crate) use tcp_proxy::ToxicTcpProxy; | ||
|
||
mod http_server; | ||
mod tcp_proxy; | ||
|
||
/// Attempts to read exactly one `SocketAddr` from a `ToSocketAddrs`. | ||
/// | ||
/// Returns an error if more than one SocketAddr is present. | ||
trait ToSocketAddrsExt { | ||
fn parse_only_one_address(self) -> Result<SocketAddr>; | ||
} | ||
|
||
impl<T: ToSocketAddrs + Debug> ToSocketAddrsExt for T { | ||
fn parse_only_one_address(self) -> Result<SocketAddr> { | ||
let mut addresses = self | ||
.to_socket_addrs() | ||
.context(format!("Failed to parse {self:?} as socket address"))?; | ||
|
||
let address = addresses | ||
.next() | ||
.context(format!("Did not parse any addresses from {self:?}"))?; | ||
|
||
anyhow::ensure!( | ||
addresses.next().is_none(), | ||
format!("Listen address ({:?}) must parse to one address.", address) | ||
); | ||
|
||
Ok(address) | ||
} | ||
} |
Oops, something went wrong.