[core] Unix upstreams: unix:/path parses strictly into a socket upstream; edge proxies and health-checks it in plain HTTP over the socket, Caddy dials unix/<path>
This commit is contained in:
@@ -240,4 +240,93 @@ mod unix {
|
||||
assert_eq!(r.status, 403, "{}", edge.stderr());
|
||||
assert!(echo.proxied().is_empty());
|
||||
}
|
||||
|
||||
/// A `unix:` upstream (G14 option b: the admin host reaches control on its socket):
|
||||
/// requests and health checks go over the socket, in plain HTTP.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_unix_socket_upstream_is_proxied_and_health_checked() {
|
||||
use http_body_util::Full;
|
||||
use hyper::server::conn::http1;
|
||||
use hyper::service::service_fn;
|
||||
use hyper_util::rt::TokioIo;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
let dir = ianitor_testkit::proc::TempDir::new("unix-upstream");
|
||||
let sock = dir.join("app.sock");
|
||||
let listener = tokio::net::UnixListener::bind(&sock).unwrap();
|
||||
let seen: Arc<Mutex<Vec<(String, String)>>> = Arc::default();
|
||||
let s = seen.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let Ok((stream, _)) = listener.accept().await else {
|
||||
continue;
|
||||
};
|
||||
let s = s.clone();
|
||||
tokio::spawn(async move {
|
||||
let svc = service_fn(move |req: hyper::Request<hyper::body::Incoming>| {
|
||||
let host = req
|
||||
.headers()
|
||||
.get("host")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("")
|
||||
.to_owned();
|
||||
s.lock().unwrap().push((req.uri().path().to_owned(), host));
|
||||
async move {
|
||||
Ok::<_, std::convert::Infallible>(hyper::Response::new(Full::new(
|
||||
bytes::Bytes::from_static(b"via the socket"),
|
||||
)))
|
||||
}
|
||||
});
|
||||
let _ = http1::Builder::new()
|
||||
.serve_connection(TokioIo::new(stream), svc)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
"ui.test",
|
||||
&format!(
|
||||
"upstreams = [\"unix:{}\"]
|
||||
[health_check]
|
||||
path = \"/healthz\"
|
||||
interval_secs = 1
|
||||
",
|
||||
sock.display()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"ui.test",
|
||||
edge.roots(),
|
||||
&Req::get("ui.test", "/x"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
(r.status, r.text().as_str()),
|
||||
(200, "via the socket"),
|
||||
"{}",
|
||||
edge.stderr()
|
||||
);
|
||||
for _ in 0..50 {
|
||||
if seen.lock().unwrap().iter().any(|(p, _)| p == "/healthz") {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
let seen = seen.lock().unwrap().clone();
|
||||
assert!(
|
||||
seen.contains(&("/x".to_owned(), "ui.test".to_owned())),
|
||||
"{seen:?}"
|
||||
);
|
||||
assert!(
|
||||
seen.iter()
|
||||
.any(|(p, h)| p == "/healthz" && h == "localhost"),
|
||||
"{seen:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user