From 1859c536c3b22ebf73781b83a5642d38af991d7b Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 22:33:46 +0100 Subject: [PATCH] refactor(probe): keep the tests out of the file they test --- crates/piqueld-probe/src/lib.rs | 127 +----------------------------- crates/piqueld-probe/src/tests.rs | 124 +++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 126 deletions(-) create mode 100644 crates/piqueld-probe/src/tests.rs diff --git a/crates/piqueld-probe/src/lib.rs b/crates/piqueld-probe/src/lib.rs index e73ae4cae..a9da8d036 100644 --- a/crates/piqueld-probe/src/lib.rs +++ b/crates/piqueld-probe/src/lib.rs @@ -247,129 +247,4 @@ impl HttpProbe { } #[cfg(test)] -mod tests { - use super::*; - use std::{net::TcpListener, thread}; - - fn arguments(values: &[&str]) -> Vec { - values.iter().map(ToString::to_string).collect() - } - - /// Answers one connection on a free loopback port with `response`. - fn serve(response: &'static [u8]) -> u16 { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let port = listener.local_addr().unwrap().port(); - thread::spawn(move || { - let (mut stream, _) = listener.accept().unwrap(); - // Read the whole request: closing with unread data resets the - // connection, which may discard the response. - let mut request = Vec::new(); - let mut buffer = [0; 256]; - while !request.ends_with(b"\r\n\r\n") { - let read = stream.read(&mut buffer).unwrap(); - assert_ne!(read, 0, "the request ended early"); - request.extend_from_slice(&buffer[..read]); - } - // Split writes exercise reading the status line in pieces. - for chunk in response.chunks(5) { - let _ = stream.write_all(chunk); - } - }); - port - } - - fn probe(port: u16) -> HttpProbe { - HttpProbe { - port, - path: "/health/ready".into(), - timeout_seconds: 1, - } - } - - #[test] - fn command_round_trips_through_parse() { - let probe = probe(8080); - let command = probe.command(); - assert_eq!( - command, - arguments(&[ - CONTAINER_PATH, - "http", - "--port", - "8080", - "--path", - "/health/ready", - "--timeout", - "1" - ]) - ); - assert_eq!(HttpProbe::parse(command.into_iter().skip(1)), Ok(probe)); - } - - #[test] - fn parse_rejects_malformed_arguments() { - for (values, expected) in [ - (&["tcp"][..], ArgumentError::Usage), - ( - &["http", "--port", "1", "--port", "2"], - ArgumentError::Unexpected("--port".into()), - ), - ( - &["http", "--port"], - ArgumentError::Unexpected("--port".into()), - ), - ( - &["http", "--port", "1", "--path", "/"], - ArgumentError::Missing("--timeout"), - ), - ( - &["http", "--port", "0", "--path", "/", "--timeout", "1"], - ArgumentError::Invalid { - flag: "--port", - value: "0".into(), - }, - ), - ( - &["http", "--port", "1", "--path", "/a b", "--timeout", "1"], - ArgumentError::Invalid { - flag: "--path", - value: "/a b".into(), - }, - ), - ] { - assert_eq!(HttpProbe::parse(arguments(values)), Err(expected)); - } - } - - #[test] - fn success_and_redirect_statuses_are_healthy() { - let port = serve(b"HTTP/1.1 204 No Content\r\n\r\n"); - assert_eq!(probe(port).check().unwrap(), 204); - let port = serve(b"HTTP/1.0 302 Found\r\nLocation: /\r\n\r\n"); - assert_eq!(probe(port).check().unwrap(), 302); - let port = serve(b"HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 200 OK\r\n\r\n"); - assert_eq!(probe(port).check().unwrap(), 200); - } - - #[test] - fn failures_are_unhealthy() { - let port = serve(b"HTTP/1.1 503 Service Unavailable\r\n\r\n"); - assert!(matches!(probe(port).check(), Err(ProbeError::Status(503)))); - let port = serve(b"SSH-2.0-OpenSSH\r\n"); - assert!(matches!(probe(port).check(), Err(ProbeError::Malformed(_)))); - // A head cut off before its blank line is not a response. - let port = serve(b"HTTP/1.1 200 OK\r\n"); - assert!(matches!(probe(port).check(), Err(ProbeError::Malformed(_)))); - - // A listener that never answers runs into the timeout. - let silent = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); - let port = silent.local_addr().unwrap().port(); - assert!(matches!(probe(port).check(), Err(ProbeError::Timeout(1)))); - - drop(silent); - assert!(matches!( - probe(port).check(), - Err(ProbeError::Connect { .. }) - )); - } -} +mod tests; diff --git a/crates/piqueld-probe/src/tests.rs b/crates/piqueld-probe/src/tests.rs new file mode 100644 index 000000000..7a40faac0 --- /dev/null +++ b/crates/piqueld-probe/src/tests.rs @@ -0,0 +1,124 @@ +use super::*; +use std::{net::TcpListener, thread}; + +fn arguments(values: &[&str]) -> Vec { + values.iter().map(ToString::to_string).collect() +} + +/// Answers one connection on a free loopback port with `response`. +fn serve(response: &'static [u8]) -> u16 { + let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); + let port = listener.local_addr().unwrap().port(); + thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + // Read the whole request: closing with unread data resets the + // connection, which may discard the response. + let mut request = Vec::new(); + let mut buffer = [0; 256]; + while !request.ends_with(b"\r\n\r\n") { + let read = stream.read(&mut buffer).unwrap(); + assert_ne!(read, 0, "the request ended early"); + request.extend_from_slice(&buffer[..read]); + } + // Split writes exercise reading the status line in pieces. + for chunk in response.chunks(5) { + let _ = stream.write_all(chunk); + } + }); + port +} + +fn probe(port: u16) -> HttpProbe { + HttpProbe { + port, + path: "/health/ready".into(), + timeout_seconds: 1, + } +} + +#[test] +fn command_round_trips_through_parse() { + let probe = probe(8080); + let command = probe.command(); + assert_eq!( + command, + arguments(&[ + CONTAINER_PATH, + "http", + "--port", + "8080", + "--path", + "/health/ready", + "--timeout", + "1" + ]) + ); + assert_eq!(HttpProbe::parse(command.into_iter().skip(1)), Ok(probe)); +} + +#[test] +fn parse_rejects_malformed_arguments() { + for (values, expected) in [ + (&["tcp"][..], ArgumentError::Usage), + ( + &["http", "--port", "1", "--port", "2"], + ArgumentError::Unexpected("--port".into()), + ), + ( + &["http", "--port"], + ArgumentError::Unexpected("--port".into()), + ), + ( + &["http", "--port", "1", "--path", "/"], + ArgumentError::Missing("--timeout"), + ), + ( + &["http", "--port", "0", "--path", "/", "--timeout", "1"], + ArgumentError::Invalid { + flag: "--port", + value: "0".into(), + }, + ), + ( + &["http", "--port", "1", "--path", "/a b", "--timeout", "1"], + ArgumentError::Invalid { + flag: "--path", + value: "/a b".into(), + }, + ), + ] { + assert_eq!(HttpProbe::parse(arguments(values)), Err(expected)); + } +} + +#[test] +fn success_and_redirect_statuses_are_healthy() { + let port = serve(b"HTTP/1.1 204 No Content\r\n\r\n"); + assert_eq!(probe(port).check().unwrap(), 204); + let port = serve(b"HTTP/1.0 302 Found\r\nLocation: /\r\n\r\n"); + assert_eq!(probe(port).check().unwrap(), 302); + let port = serve(b"HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 200 OK\r\n\r\n"); + assert_eq!(probe(port).check().unwrap(), 200); +} + +#[test] +fn failures_are_unhealthy() { + let port = serve(b"HTTP/1.1 503 Service Unavailable\r\n\r\n"); + assert!(matches!(probe(port).check(), Err(ProbeError::Status(503)))); + let port = serve(b"SSH-2.0-OpenSSH\r\n"); + assert!(matches!(probe(port).check(), Err(ProbeError::Malformed(_)))); + // A head cut off before its blank line is not a response. + let port = serve(b"HTTP/1.1 200 OK\r\n"); + assert!(matches!(probe(port).check(), Err(ProbeError::Malformed(_)))); + + // A listener that never answers runs into the timeout. + let silent = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); + let port = silent.local_addr().unwrap().port(); + assert!(matches!(probe(port).check(), Err(ProbeError::Timeout(1)))); + + drop(silent); + assert!(matches!( + probe(port).check(), + Err(ProbeError::Connect { .. }) + )); +}