Skip to content

Commit 9da8f6f

Browse files
committed
test(podman): exercise production endpoint selection
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 71ddc1f commit 9da8f6f

2 files changed

Lines changed: 67 additions & 37 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub struct PodmanComputeConfig {
3636
pub image_pull_policy: ImagePullPolicy,
3737
/// Gateway gRPC endpoint the sandbox connects back to.
3838
///
39-
/// When empty, the driver auto-detects the endpoint using
40-
/// `gateway_port` and `host.containers.internal`.
39+
/// When empty, the driver selects loopback on Linux or
40+
/// `host.containers.internal` with Podman Machine, using `gateway_port`.
4141
pub grpc_endpoint: String,
4242
/// Port the gateway server is actually listening on.
4343
///

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

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,49 @@ use tracing::{Instrument as _, debug, info, warn};
3333
const STOP_COMPLETION_POLL_INTERVAL: Duration = Duration::from_millis(50);
3434
const STOP_COMPLETION_TIMEOUT_HEADROOM: Duration = Duration::from_secs(5);
3535

36+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37+
enum PodmanEndpointEnvironment {
38+
LinuxHost,
39+
PodmanMachine,
40+
}
41+
42+
impl PodmanEndpointEnvironment {
43+
const fn current() -> Self {
44+
if cfg!(target_os = "linux") {
45+
Self::LinuxHost
46+
} else {
47+
Self::PodmanMachine
48+
}
49+
}
50+
51+
const fn gateway_host(self) -> &'static str {
52+
match self {
53+
Self::LinuxHost => "127.0.0.1",
54+
Self::PodmanMachine => "host.containers.internal",
55+
}
56+
}
57+
}
58+
59+
fn select_grpc_endpoint(
60+
config: &PodmanComputeConfig,
61+
environment: PodmanEndpointEnvironment,
62+
) -> String {
63+
if !config.grpc_endpoint.is_empty() {
64+
return config.grpc_endpoint.clone();
65+
}
66+
67+
let scheme = if config.tls_enabled() {
68+
"https"
69+
} else {
70+
"http"
71+
};
72+
format!(
73+
"{scheme}://{}:{}",
74+
environment.gateway_host(),
75+
config.gateway_port
76+
)
77+
}
78+
3679
fn decode_launch_authentication(
3780
encoded: &[u8],
3881
) -> Result<openshell_core::jwt::SandboxLaunchAuthentication, ComputeDriverError> {
@@ -443,18 +486,9 @@ impl PodmanComputeDriver {
443486
// The supervisor shares the Podman host network. Linux supervisors can
444487
// therefore use gateway loopback directly; Podman Machine retains its
445488
// standard desktop-host alias.
446-
if config.grpc_endpoint.is_empty() {
447-
let scheme = if config.tls_enabled() {
448-
"https"
449-
} else {
450-
"http"
451-
};
452-
let host = if cfg!(target_os = "linux") {
453-
"127.0.0.1"
454-
} else {
455-
"host.containers.internal"
456-
};
457-
config.grpc_endpoint = format!("{scheme}://{host}:{}", config.gateway_port);
489+
let endpoint_was_selected = config.grpc_endpoint.is_empty();
490+
config.grpc_endpoint = select_grpc_endpoint(&config, PodmanEndpointEnvironment::current());
491+
if endpoint_was_selected {
458492
info!(
459493
grpc_endpoint = %config.grpc_endpoint,
460494
tls = config.tls_enabled(),
@@ -2359,37 +2393,34 @@ mod tests {
23592393

23602394
// ── grpc_endpoint auto-detection ───────────────────────────────────
23612395
//
2362-
// PodmanComputeDriver::new() fills grpc_endpoint when it is empty.
2363-
// The scheme (http vs https) depends on whether TLS client certs are
2364-
// configured. These tests simulate the auto-detection logic.
2396+
// PodmanComputeDriver::new() fills grpc_endpoint through
2397+
// select_grpc_endpoint() when it is empty.
23652398

23662399
#[test]
2367-
fn grpc_endpoint_http_without_tls() {
2368-
let mut cfg = PodmanComputeConfig {
2400+
fn grpc_endpoint_uses_loopback_on_linux() {
2401+
let cfg = PodmanComputeConfig {
23692402
gateway_port: 8081,
23702403
..PodmanComputeConfig::default()
23712404
};
2372-
if cfg.grpc_endpoint.is_empty() {
2373-
let scheme = if cfg.tls_enabled() { "https" } else { "http" };
2374-
cfg.grpc_endpoint = format!("{scheme}://host.containers.internal:{}", cfg.gateway_port);
2375-
}
2376-
assert_eq!(cfg.grpc_endpoint, "http://host.containers.internal:8081");
2405+
assert_eq!(
2406+
select_grpc_endpoint(&cfg, PodmanEndpointEnvironment::LinuxHost),
2407+
"http://127.0.0.1:8081"
2408+
);
23772409
}
23782410

23792411
#[test]
2380-
fn grpc_endpoint_https_with_tls() {
2381-
let mut cfg = PodmanComputeConfig {
2412+
fn grpc_endpoint_uses_host_alias_on_podman_machine() {
2413+
let cfg = PodmanComputeConfig {
23822414
gateway_port: 8080,
23832415
guest_tls_ca: Some(PathBuf::from("/tls/ca.crt")),
23842416
guest_tls_cert: Some(PathBuf::from("/tls/tls.crt")),
23852417
guest_tls_key: Some(PathBuf::from("/tls/tls.key")),
23862418
..PodmanComputeConfig::default()
23872419
};
2388-
if cfg.grpc_endpoint.is_empty() {
2389-
let scheme = if cfg.tls_enabled() { "https" } else { "http" };
2390-
cfg.grpc_endpoint = format!("{scheme}://host.containers.internal:{}", cfg.gateway_port);
2391-
}
2392-
assert_eq!(cfg.grpc_endpoint, "https://host.containers.internal:8080");
2420+
assert_eq!(
2421+
select_grpc_endpoint(&cfg, PodmanEndpointEnvironment::PodmanMachine),
2422+
"https://host.containers.internal:8080"
2423+
);
23932424
}
23942425

23952426
#[test]
@@ -2417,16 +2448,15 @@ mod tests {
24172448

24182449
#[test]
24192450
fn explicit_grpc_endpoint_takes_precedence() {
2420-
let mut cfg = PodmanComputeConfig {
2451+
let cfg = PodmanComputeConfig {
24212452
grpc_endpoint: "https://gateway.internal:9000".to_string(),
24222453
gateway_port: 8081,
24232454
..PodmanComputeConfig::default()
24242455
};
2425-
if cfg.grpc_endpoint.is_empty() {
2426-
let scheme = if cfg.tls_enabled() { "https" } else { "http" };
2427-
cfg.grpc_endpoint = format!("{scheme}://host.containers.internal:{}", cfg.gateway_port);
2428-
}
2429-
assert_eq!(cfg.grpc_endpoint, "https://gateway.internal:9000");
2456+
assert_eq!(
2457+
select_grpc_endpoint(&cfg, PodmanEndpointEnvironment::LinuxHost),
2458+
"https://gateway.internal:9000"
2459+
);
24302460
}
24312461

24322462
#[test]

0 commit comments

Comments
 (0)