Skip to content

Commit 43d57e0

Browse files
committed
test(podman): cover retry policy deterministically
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent b9c4f2c commit 43d57e0

2 files changed

Lines changed: 107 additions & 80 deletions

File tree

‎crates/openshell-driver-podman/src/driver.rs‎

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ use openshell_core::proto::compute::v1::{
2525
GpuResourceRequirements, MemoryResourceCapabilities, ResourceCapabilities,
2626
};
2727
use std::collections::HashMap;
28+
use std::future::Future;
2829
use std::path::{Path, PathBuf};
2930
use std::sync::Arc;
3031
use std::time::Duration;
3132
use tracing::{Instrument as _, debug, info, warn};
3233

3334
const STOP_COMPLETION_POLL_INTERVAL: Duration = Duration::from_millis(50);
3435
const STOP_COMPLETION_TIMEOUT_HEADROOM: Duration = Duration::from_secs(5);
36+
const MAX_PING_RETRIES: u32 = 5;
37+
const PING_RETRY_DELAY: Duration = Duration::from_secs(2);
3538
const POLICY_DNS_RESOLV_CONF: &[u8] = b"nameserver 127.0.0.53\n";
3639

3740
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -413,12 +416,33 @@ fn resolve_socket_path(
413416
})
414417
}
415418

419+
async fn ping_with_retry<F, Fut>(mut ping: F) -> Result<(), PodmanApiError>
420+
where
421+
F: FnMut() -> Fut,
422+
Fut: Future<Output = Result<(), PodmanApiError>>,
423+
{
424+
let mut retries = 0;
425+
loop {
426+
match ping().await {
427+
Ok(()) => return Ok(()),
428+
Err(error) if retries < MAX_PING_RETRIES => {
429+
retries += 1;
430+
warn!(
431+
attempt = retries,
432+
max_retries = MAX_PING_RETRIES,
433+
error = %error,
434+
"Podman socket not ready, retrying"
435+
);
436+
tokio::time::sleep(PING_RETRY_DELAY).await;
437+
}
438+
Err(error) => return Err(error),
439+
}
440+
}
441+
}
442+
416443
impl PodmanComputeDriver {
417444
/// Create a new driver, verifying the Podman socket is reachable.
418445
pub async fn new(mut config: PodmanComputeConfig) -> Result<Self, PodmanApiError> {
419-
const MAX_PING_RETRIES: u32 = 5;
420-
const PING_RETRY_DELAY: Duration = Duration::from_secs(2);
421-
422446
let socket_path = resolve_socket_path(config.socket_path.clone(), detect_socket)?;
423447
config.socket_path = Some(socket_path.clone());
424448

@@ -449,23 +473,7 @@ impl PodmanComputeDriver {
449473
// unavailability (e.g. podman.socket restarting after a package
450474
// upgrade). The systemd unit uses Wants=podman.socket (not Requires),
451475
// so the gateway may start while the socket is briefly re-activating.
452-
let mut attempts = 0;
453-
loop {
454-
match client.ping().await {
455-
Ok(()) => break,
456-
Err(e) if attempts < MAX_PING_RETRIES => {
457-
attempts += 1;
458-
warn!(
459-
attempt = attempts,
460-
max_retries = MAX_PING_RETRIES,
461-
error = %e,
462-
"Podman socket not ready, retrying"
463-
);
464-
tokio::time::sleep(PING_RETRY_DELAY).await;
465-
}
466-
Err(e) => return Err(e),
467-
}
468-
}
476+
ping_with_retry(|| client.ping()).await?;
469477

470478
// Verify cgroups v2, detect rootless mode, and log system info.
471479
let rootless = match client.system_info().await {
@@ -2048,7 +2056,7 @@ mod tests {
20482056
use openshell_core::proto::compute::v1::{
20492057
DriverSandboxSpec, DriverSandboxTemplate, ResourceRequirements,
20502058
};
2051-
use std::collections::HashMap;
2059+
use std::collections::{HashMap, VecDeque};
20522060
use std::fs;
20532061
use std::path::{Path, PathBuf};
20542062

@@ -2109,6 +2117,63 @@ mod tests {
21092117
assert!(err.to_string().contains("no responsive Podman API socket"));
21102118
}
21112119

2120+
#[tokio::test(start_paused = true)]
2121+
async fn ping_retries_transient_failures() {
2122+
let mut outcomes = VecDeque::from([
2123+
Err(PodmanApiError::Connection("first".to_string())),
2124+
Err(PodmanApiError::Connection("second".to_string())),
2125+
Ok(()),
2126+
]);
2127+
let started = tokio::time::Instant::now();
2128+
2129+
ping_with_retry(|| std::future::ready(outcomes.pop_front().expect("ping outcome")))
2130+
.await
2131+
.expect("a later successful ping should stop retries");
2132+
2133+
assert!(outcomes.is_empty());
2134+
assert_eq!(started.elapsed(), PING_RETRY_DELAY * 2);
2135+
}
2136+
2137+
#[tokio::test(start_paused = true)]
2138+
async fn ping_failure_is_bounded_by_retry_policy() {
2139+
let mut attempts = 0;
2140+
let started = tokio::time::Instant::now();
2141+
2142+
let error = ping_with_retry(|| {
2143+
attempts += 1;
2144+
std::future::ready(Err(PodmanApiError::Connection(format!(
2145+
"attempt {attempts}"
2146+
))))
2147+
})
2148+
.await
2149+
.expect_err("persistent connection failures should be returned");
2150+
2151+
assert_eq!(attempts, MAX_PING_RETRIES + 1);
2152+
assert_eq!(started.elapsed(), PING_RETRY_DELAY * MAX_PING_RETRIES);
2153+
assert_eq!(
2154+
error.to_string(),
2155+
format!("connection error: attempt {}", MAX_PING_RETRIES + 1)
2156+
);
2157+
}
2158+
2159+
#[tokio::test]
2160+
async fn missing_socket_connection_error_names_configured_path() {
2161+
let tempdir = tempfile::tempdir().expect("create isolated socket directory");
2162+
let missing_socket = tempdir.path().join("missing-podman.sock");
2163+
2164+
let error = PodmanClient::new(missing_socket.clone())
2165+
.ping()
2166+
.await
2167+
.expect_err("a missing socket should fail to connect");
2168+
2169+
assert!(matches!(error, PodmanApiError::Connection(_)));
2170+
assert!(
2171+
error
2172+
.to_string()
2173+
.contains(&missing_socket.display().to_string())
2174+
);
2175+
}
2176+
21122177
fn cdi_devices_config(device_ids: &[&str]) -> prost_types::Struct {
21132178
prost_types::Struct {
21142179
fields: std::iter::once((
Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,51 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Podman driver daemon-unavailable integration tests.
4+
//! Standalone driver smoke test for the Podman-unavailable diagnostic path.
55
//!
6-
//! These tests verify that `openshell-driver-podman` fails fast with an
7-
//! actionable error when it cannot reach a Podman API socket, instead of
8-
//! hanging or silently serving gRPC against a dead connection.
9-
//!
10-
//! They do NOT require a running Podman daemon or gateway — they point
11-
//! `--podman-socket` at a path that is guaranteed not to exist to simulate
12-
//! the daemon being unavailable. As a plain Cargo integration test in this
13-
//! crate, this runs via the normal `cargo test -p openshell-driver-podman`
14-
//! lane with no special CI wiring: Cargo provides `CARGO_BIN_EXE_<name>` for
15-
//! this crate's own `[[bin]]` target automatically.
6+
//! Retry timing and connection failures are covered deterministically by unit
7+
//! tests. This test retains only the executable boundary: argument wiring,
8+
//! process exit status, and the rendered error shown to operators.
169
1710
use std::path::PathBuf;
18-
use std::time::{Duration, Instant};
11+
use std::process::Stdio;
12+
use std::time::Duration;
1913

20-
/// Run `openshell-driver-podman` pointed at a Podman socket that does not
21-
/// exist, and wait for it to exit.
22-
///
23-
/// The driver retries a handful of times before giving up (to tolerate the
24-
/// socket briefly re-activating), so this can take several seconds.
25-
async fn run_with_unreachable_podman_socket() -> (String, i32, Duration, PathBuf) {
14+
#[tokio::test]
15+
async fn missing_podman_socket_exits_with_actionable_diagnostic() {
2616
let tmpdir = tempfile::tempdir().expect("create isolated socket dir");
2717
// Use a short relative path so miette cannot insert a line-wrap gutter
2818
// inside it on platforms with long temporary-directory paths.
2919
let missing_socket = PathBuf::from("missing-podman.sock");
3020

31-
let start = Instant::now();
3221
let mut cmd = tokio::process::Command::new(env!("CARGO_BIN_EXE_openshell-driver-podman"));
3322
cmd.arg("--podman-socket")
3423
.arg(&missing_socket)
3524
.current_dir(tmpdir.path())
3625
.kill_on_drop(true)
37-
.stdout(std::process::Stdio::piped())
38-
.stderr(std::process::Stdio::piped());
26+
.stdout(Stdio::piped())
27+
.stderr(Stdio::piped());
3928

40-
let output = tokio::time::timeout(Duration::from_mins(1), cmd.output())
29+
let output = tokio::time::timeout(Duration::from_secs(30), cmd.output())
4130
.await
42-
.expect("openshell-driver-podman should exit instead of hanging")
31+
.expect("driver should stop after its bounded retry window")
4332
.expect("spawn openshell-driver-podman");
44-
let elapsed = start.elapsed();
45-
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
46-
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
47-
let combined = format!("{stdout}{stderr}");
48-
let code = output.status.code().unwrap_or(-1);
49-
(combined, code, elapsed, missing_socket)
50-
}
51-
52-
/// `openshell-driver-podman` should exit non-zero, not hang, when its
53-
/// configured Podman socket does not exist.
54-
#[tokio::test]
55-
async fn driver_exits_when_podman_socket_unreachable() {
56-
let (output, code, elapsed, _) = run_with_unreachable_podman_socket().await;
57-
58-
assert_ne!(
59-
code, 0,
60-
"driver should exit non-zero when Podman is unreachable, output:\n{output}"
61-
);
6233

6334
assert!(
64-
elapsed < Duration::from_secs(30),
65-
"driver should give up retrying and exit within its bounded retry \
66-
window (took {}s), output:\n{output}",
67-
elapsed.as_secs()
35+
!output.status.success(),
36+
"driver should exit non-zero when Podman is unreachable"
6837
);
69-
}
70-
71-
/// The error surfaced when the Podman socket is unreachable should name the
72-
/// configured socket path and describe a connection failure, not a generic
73-
/// panic or timeout with no actionable detail.
74-
#[tokio::test]
75-
async fn driver_error_names_unreachable_socket() {
76-
let (output, code, _, missing_socket) = run_with_unreachable_podman_socket().await;
77-
78-
assert_ne!(code, 0);
7938

39+
let stdout = String::from_utf8_lossy(&output.stdout);
40+
let stderr = String::from_utf8_lossy(&output.stderr);
41+
let combined = format!("{stdout}{stderr}");
8042
assert!(
81-
output.contains("connection error"),
82-
"driver error should describe a connection failure:\n{output}"
43+
combined.contains("connection error"),
44+
"driver error should describe a connection failure:\n{combined}"
8345
);
8446
assert!(
85-
output.contains(missing_socket.to_str().expect("socket path is utf-8")),
86-
"driver error should name the unreachable socket path {}:\n{output}",
47+
combined.contains(missing_socket.to_str().expect("socket path is utf-8")),
48+
"driver error should name the unreachable socket path {}:\n{combined}",
8749
missing_socket.display()
8850
);
8951
}

0 commit comments

Comments
 (0)