|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use bytes::Bytes; |
| 5 | +use http_body_util::{BodyExt, Empty}; |
| 6 | +use hyper::{Request, StatusCode}; |
| 7 | +use hyper_util::rt::TokioIo; |
| 8 | +use openshell_server::{Store, health_router}; |
| 9 | +use serde_json::Value; |
| 10 | +use std::sync::Arc; |
| 11 | +#[cfg(feature = "test-support")] |
| 12 | +use std::time::Duration; |
| 13 | +use tokio::net::TcpListener; |
| 14 | + |
| 15 | +async fn start_health_server( |
| 16 | + store: Arc<Store>, |
| 17 | +) -> (std::net::SocketAddr, tokio::task::JoinHandle<()>) { |
| 18 | + let listener = TcpListener::bind("127.0.0.1:0") |
| 19 | + .await |
| 20 | + .expect("bind ephemeral health test listener"); |
| 21 | + let addr = listener |
| 22 | + .local_addr() |
| 23 | + .expect("resolve local address for health test listener"); |
| 24 | + |
| 25 | + let router = health_router(store).await; |
| 26 | + let server = tokio::spawn(async move { |
| 27 | + let _ = axum::serve(listener, router.into_make_service()).await; |
| 28 | + }); |
| 29 | + |
| 30 | + (addr, server) |
| 31 | +} |
| 32 | + |
| 33 | +async fn http_get_json(addr: std::net::SocketAddr, path: &str) -> (StatusCode, Value) { |
| 34 | + let stream = tokio::net::TcpStream::connect(addr) |
| 35 | + .await |
| 36 | + .expect("connect test HTTP client"); |
| 37 | + let (mut sender, conn) = hyper::client::conn::http1::Builder::new() |
| 38 | + .handshake(TokioIo::new(stream)) |
| 39 | + .await |
| 40 | + .expect("handshake HTTP/1 test client"); |
| 41 | + tokio::spawn(async move { |
| 42 | + let _ = conn.await; |
| 43 | + }); |
| 44 | + |
| 45 | + let req = Request::builder() |
| 46 | + .method("GET") |
| 47 | + .uri(format!("http://{addr}{path}")) |
| 48 | + .body(Empty::<Bytes>::new()) |
| 49 | + .expect("build HTTP request"); |
| 50 | + let resp = sender.send_request(req).await.expect("send HTTP request"); |
| 51 | + let status = resp.status(); |
| 52 | + let bytes = resp |
| 53 | + .into_body() |
| 54 | + .collect() |
| 55 | + .await |
| 56 | + .expect("collect response body") |
| 57 | + .to_bytes(); |
| 58 | + let body = if bytes.is_empty() { |
| 59 | + Value::Null |
| 60 | + } else { |
| 61 | + serde_json::from_slice(&bytes).expect("response body must be valid JSON") |
| 62 | + }; |
| 63 | + (status, body) |
| 64 | +} |
| 65 | + |
| 66 | +#[tokio::test] |
| 67 | +async fn readyz_reports_healthy_when_database_is_reachable() { |
| 68 | + let store = Arc::new( |
| 69 | + Store::connect("sqlite::memory:") |
| 70 | + .await |
| 71 | + .expect("connect in-memory sqlite store for health integration test"), |
| 72 | + ); |
| 73 | + let (addr, server) = start_health_server(store.clone()).await; |
| 74 | + |
| 75 | + let (status, body) = http_get_json(addr, "/readyz").await; |
| 76 | + assert_eq!(status, StatusCode::OK); |
| 77 | + assert_eq!(body["status"], "healthy"); |
| 78 | + assert_eq!(body["checks"]["database"]["status"], "healthy"); |
| 79 | + |
| 80 | + server.abort(); |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(feature = "test-support")] |
| 84 | +#[tokio::test] |
| 85 | +async fn readyz_reports_database_health_transition_after_close() { |
| 86 | + let store = Arc::new( |
| 87 | + Store::connect("sqlite::memory:") |
| 88 | + .await |
| 89 | + .expect("connect in-memory sqlite store for health integration test"), |
| 90 | + ); |
| 91 | + let (addr, server) = start_health_server(store.clone()).await; |
| 92 | + |
| 93 | + let (status, body) = http_get_json(addr, "/readyz").await; |
| 94 | + assert_eq!(status, StatusCode::OK); |
| 95 | + assert_eq!(body["status"], "healthy"); |
| 96 | + assert_eq!(body["checks"]["database"]["status"], "healthy"); |
| 97 | + |
| 98 | + store.close().await; |
| 99 | + |
| 100 | + // The handler reads the cached state published by the background |
| 101 | + // readiness monitor, so the transition to Unhealthy can only show up |
| 102 | + // after the monitor's next tick. With the default 5s interval the |
| 103 | + // outage surfaces within ~5s; poll with a generous deadline so the |
| 104 | + // assertion never races the polling cycle. |
| 105 | + let (status, body) = wait_for_unhealthy(addr, Duration::from_secs(10)) |
| 106 | + .await |
| 107 | + .expect("/readyz did not transition to 503 after store.close() within 10s"); |
| 108 | + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); |
| 109 | + assert_eq!(body["status"], "unhealthy"); |
| 110 | + assert_eq!(body["checks"]["database"]["status"], "unhealthy"); |
| 111 | + assert_eq!(body["checks"]["database"]["error"], "database unavailable"); |
| 112 | + |
| 113 | + server.abort(); |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(feature = "test-support")] |
| 117 | +async fn wait_for_unhealthy( |
| 118 | + addr: std::net::SocketAddr, |
| 119 | + timeout: Duration, |
| 120 | +) -> Option<(StatusCode, Value)> { |
| 121 | + let deadline = tokio::time::Instant::now() + timeout; |
| 122 | + loop { |
| 123 | + let observation = http_get_json(addr, "/readyz").await; |
| 124 | + if observation.0 == StatusCode::SERVICE_UNAVAILABLE { |
| 125 | + return Some(observation); |
| 126 | + } |
| 127 | + if tokio::time::Instant::now() >= deadline { |
| 128 | + return None; |
| 129 | + } |
| 130 | + tokio::time::sleep(Duration::from_millis(250)).await; |
| 131 | + } |
| 132 | +} |
0 commit comments