Skip to content

Commit ce30b0f

Browse files
test(drivers/podman): port stop-is-terminal and ephemeral container cleanup
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent a073e81 commit ce30b0f

3 files changed

Lines changed: 153 additions & 1 deletion

File tree

‎tests/suites/drivers/podman/Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/suites/drivers/podman/Cargo.toml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Each driver suite is its own workspace, with its own lockfile.
55
[package]
66
name = "podman"
7-
description = "Drivers suite: Podman driver behavior, such as user namespaces"
7+
description = "Drivers suite: Podman driver behavior, such as user namespaces and container lifecycle"
88
version = "0.0.0"
99
edition = "2024"
1010
rust-version = "1.94"
@@ -13,8 +13,10 @@ repository = "https://github.com/NVIDIA/OpenShell"
1313
publish = false
1414

1515
[dependencies]
16+
base64 = "0.22"
1617
container-engine-support = { path = "../../../support/container-engine" }
1718
openshell-testkit = { path = "../../../testkit/testkit" }
1819
podman-support = { path = "../../../support/podman" }
20+
serde_json = "1"
1921

2022
[workspace]
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Podman driver sandbox lifecycle: what the driver does with its containers
5+
//! when a sandbox's main process stops or an ephemeral sandbox finishes.
6+
7+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
8+
9+
use base64::Engine as _;
10+
use container_engine_support::Engine;
11+
use testkit::sdk::SandboxPhase;
12+
use testkit::{Context, Error, ResultExt as _};
13+
14+
/// Where the driver mounts the sandbox's bootstrap JWT.
15+
const TOKEN_PATH: &str = "/etc/openshell/auth/sandbox.jwt";
16+
/// How long the gateway may take to report a stopped workload as Error.
17+
const ERROR_TIMEOUT: Duration = Duration::from_secs(30);
18+
/// How long the driver may take to remove an ephemeral sandbox's containers.
19+
const CLEANUP_TIMEOUT: Duration = Duration::from_secs(90);
20+
21+
/// Stopping a sandbox's workload container is terminal: the driver doesn't
22+
/// relaunch the main process and the sandbox ends in Error. Before the stop,
23+
/// the workload can't read its bootstrap JWT, and the supervisor's copy is
24+
/// unexpired.
25+
#[testkit::test]
26+
async fn stopped_workload_container_is_terminal(cx: &mut Context) -> testkit::Result {
27+
cx.expect_compute_driver("podman").await?;
28+
let engine = Engine::from_env()?;
29+
let sandbox = cx
30+
.sandbox("st")
31+
.command(["sleep", "infinity"])
32+
.create_ready()
33+
.await?;
34+
35+
let workload = engine.container_for_role(sandbox.name(), "sandbox").await?;
36+
engine
37+
.output([
38+
"exec",
39+
&workload,
40+
"/bin/sh",
41+
"-c",
42+
&format!("test ! -r {TOKEN_PATH}"),
43+
])
44+
.await
45+
.context("the sandbox workload must not be able to read its bootstrap JWT")?;
46+
47+
let supervisor = engine
48+
.container_for_role(sandbox.name(), "supervisor")
49+
.await?;
50+
let token = engine.copy_from(&supervisor, TOKEN_PATH).await?;
51+
require_unexpired(&String::from_utf8_lossy(&token))?;
52+
53+
engine.output(["stop", &workload]).await?;
54+
engine
55+
.wait_running(&workload, false, Duration::from_mins(1))
56+
.await?;
57+
sandbox
58+
.wait_phase(SandboxPhase::Error, ERROR_TIMEOUT)
59+
.await
60+
.context("a stopped main process makes the sandbox terminal")?;
61+
Ok(())
62+
}
63+
64+
/// A detached `--no-keep` sandbox's containers are removed once its main
65+
/// command exits, whether it succeeds or fails. Conformance checks that the
66+
/// gateway forgets the sandbox; this checks the driver's containers.
67+
#[testkit::test]
68+
async fn ephemeral_sandbox_containers_are_removed_after_main_exits(
69+
cx: &mut Context,
70+
) -> testkit::Result {
71+
cx.expect_compute_driver("podman").await?;
72+
let engine = Engine::from_env()?;
73+
let mut sandboxes = Vec::new();
74+
for (suffix, code) in [("ez", 0), ("ef", 17)] {
75+
let builder = cx.cli_sandbox(suffix);
76+
let release_path = format!("/sandbox/.ephemeral-release-{}", builder.name());
77+
let script =
78+
format!("while [ ! -e '{release_path}' ]; do sleep 0.1; done; sleep 1; exit {code}");
79+
let sandbox = builder
80+
.args(["--no-keep"])
81+
.command(["sh", "-c", &script])
82+
.create()
83+
.await?;
84+
sandboxes.push((sandbox, release_path, code));
85+
}
86+
87+
for (sandbox, release_path, _) in &sandboxes {
88+
sandbox.wait_ready().await?;
89+
testkit::ensure!(
90+
!engine
91+
.containers_for_sandbox_id(sandbox.id())
92+
.await?
93+
.is_empty(),
94+
"no containers found for sandbox {} ({})",
95+
sandbox.name(),
96+
sandbox.id()
97+
);
98+
sandbox
99+
.exec(["touch", release_path.as_str()])
100+
.await?
101+
.success()?;
102+
}
103+
for (sandbox, _, code) in &sandboxes {
104+
testkit::poll_until(
105+
&format!("containers of sandbox {} to be removed", sandbox.name()),
106+
CLEANUP_TIMEOUT,
107+
Duration::from_millis(500),
108+
async || {
109+
let left = engine.containers_for_sandbox_id(sandbox.id()).await?;
110+
Ok(if left.is_empty() {
111+
testkit::Poll::Ready(())
112+
} else {
113+
testkit::Poll::Pending(format!("containers {left:?}"))
114+
})
115+
},
116+
)
117+
.await
118+
.context(format!("ephemeral sandbox exited {code}"))?;
119+
}
120+
Ok(())
121+
}
122+
123+
/// Fail unless `token`'s `exp` claim is in the future. Sandbox tokens use
124+
/// `exp = 0` when no TTL is configured.
125+
fn require_unexpired(token: &str) -> testkit::Result {
126+
let payload = token
127+
.trim()
128+
.split('.')
129+
.nth(1)
130+
.ok_or_else(|| Error::msg("the sandbox JWT has no payload segment"))?;
131+
let payload = base64::engine::general_purpose::URL_SAFE_NO_PAD
132+
.decode(payload)
133+
.map_err(|err| Error::msg(format!("decode the sandbox JWT payload: {err}")))?;
134+
let claims: serde_json::Value =
135+
serde_json::from_slice(&payload).context("parse the sandbox JWT claims")?;
136+
let exp = claims["exp"]
137+
.as_u64()
138+
.ok_or_else(|| Error::msg(format!("the sandbox JWT has no integer exp: {claims}")))?;
139+
let now = SystemTime::now()
140+
.duration_since(UNIX_EPOCH)
141+
.map_err(|err| Error::msg(format!("read the clock: {err}")))?
142+
.as_secs();
143+
testkit::ensure!(
144+
exp == 0 || exp > now,
145+
"the supervisor's bootstrap JWT expired: exp={exp}, now={now}"
146+
);
147+
Ok(())
148+
}

0 commit comments

Comments
 (0)