|
| 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 | +use tokio::net::TcpListener; |
| 12 | + |
| 13 | +async fn start_health_server( |
| 14 | + store: Arc<Store>, |
| 15 | +) -> (std::net::SocketAddr, tokio::task::JoinHandle<()>) { |
| 16 | + let listener = TcpListener::bind("127.0.0.1:0") |
| 17 | + .await |
| 18 | + .expect("bind ephemeral health test listener"); |
| 19 | + let addr = listener |
| 20 | + .local_addr() |
| 21 | + .expect("resolve local address for health test listener"); |
| 22 | + |
| 23 | + let server = tokio::spawn(async move { |
| 24 | + let _ = axum::serve(listener, health_router(store).into_make_service()).await; |
| 25 | + }); |
| 26 | + |
| 27 | + (addr, server) |
| 28 | +} |
| 29 | + |
| 30 | +async fn http_get_json(addr: std::net::SocketAddr, path: &str) -> (StatusCode, Value) { |
| 31 | + let stream = tokio::net::TcpStream::connect(addr) |
| 32 | + .await |
| 33 | + .expect("connect test HTTP client"); |
| 34 | + let (mut sender, conn) = hyper::client::conn::http1::Builder::new() |
| 35 | + .handshake(TokioIo::new(stream)) |
| 36 | + .await |
| 37 | + .expect("handshake HTTP/1 test client"); |
| 38 | + tokio::spawn(async move { |
| 39 | + let _ = conn.await; |
| 40 | + }); |
| 41 | + |
| 42 | + let req = Request::builder() |
| 43 | + .method("GET") |
| 44 | + .uri(format!("http://{addr}{path}")) |
| 45 | + .body(Empty::<Bytes>::new()) |
| 46 | + .expect("build HTTP request"); |
| 47 | + let resp = sender.send_request(req).await.expect("send HTTP request"); |
| 48 | + let status = resp.status(); |
| 49 | + let bytes = resp |
| 50 | + .into_body() |
| 51 | + .collect() |
| 52 | + .await |
| 53 | + .expect("collect response body") |
| 54 | + .to_bytes(); |
| 55 | + let body = if bytes.is_empty() { |
| 56 | + Value::Null |
| 57 | + } else { |
| 58 | + serde_json::from_slice(&bytes).expect("response body must be valid JSON") |
| 59 | + }; |
| 60 | + (status, body) |
| 61 | +} |
| 62 | + |
| 63 | +#[tokio::test] |
| 64 | +async fn readyz_reports_healthy_when_database_is_reachable() { |
| 65 | + let store = Arc::new( |
| 66 | + Store::connect("sqlite::memory:") |
| 67 | + .await |
| 68 | + .expect("connect in-memory sqlite store for health integration test"), |
| 69 | + ); |
| 70 | + let (addr, server) = start_health_server(store.clone()).await; |
| 71 | + |
| 72 | + let (status, body) = http_get_json(addr, "/readyz").await; |
| 73 | + assert_eq!(status, StatusCode::OK); |
| 74 | + assert_eq!(body["status"], "healthy"); |
| 75 | + assert_eq!(body["checks"]["database"]["status"], "healthy"); |
| 76 | + |
| 77 | + server.abort(); |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(feature = "test-support")] |
| 81 | +#[tokio::test] |
| 82 | +async fn readyz_reports_database_health_transition_after_close() { |
| 83 | + let store = Arc::new( |
| 84 | + Store::connect("sqlite::memory:") |
| 85 | + .await |
| 86 | + .expect("connect in-memory sqlite store for health integration test"), |
| 87 | + ); |
| 88 | + let (addr, server) = start_health_server(store.clone()).await; |
| 89 | + |
| 90 | + let (status, body) = http_get_json(addr, "/readyz").await; |
| 91 | + assert_eq!(status, StatusCode::OK); |
| 92 | + assert_eq!(body["status"], "healthy"); |
| 93 | + assert_eq!(body["checks"]["database"]["status"], "healthy"); |
| 94 | + |
| 95 | + store.close().await; |
| 96 | + |
| 97 | + let (status, body) = http_get_json(addr, "/readyz").await; |
| 98 | + assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE); |
| 99 | + assert_eq!(body["status"], "unhealthy"); |
| 100 | + assert_eq!(body["checks"]["database"]["status"], "unhealthy"); |
| 101 | + assert_eq!(body["checks"]["database"]["error"], "database unavailable"); |
| 102 | + |
| 103 | + server.abort(); |
| 104 | +} |
0 commit comments