Skip to content

Commit 04a448c

Browse files
matthewgrossmanelezar
authored andcommitted
refactor(podman): simplify inspected image metadata and test fixtures
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent 090f490 commit 04a448c

3 files changed

Lines changed: 44 additions & 70 deletions

File tree

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,22 +1305,13 @@ mod tests {
13051305
.expect("image inspect should parse");
13061306

13071307
assert_eq!(image.id, "sha256:immutable");
1308-
assert_eq!(
1309-
image.config.as_ref().map(|config| config.user.as_str()),
1310-
Some("app:staff")
1311-
);
1312-
assert_eq!(
1313-
image
1314-
.config
1315-
.as_ref()
1316-
.map(|config| config.working_dir.as_str()),
1317-
Some("/workspace/project")
1318-
);
1308+
let config = image.config.expect("fixture image config");
1309+
assert_eq!(config.user, "app:staff");
1310+
assert_eq!(config.working_dir, "/workspace/project");
13191311
assert!(
1320-
image
1321-
.config
1312+
config
1313+
.volumes
13221314
.as_ref()
1323-
.and_then(|config| config.volumes.as_ref())
13241315
.is_some_and(|volumes| volumes.contains_key("/workspace/project/cache"))
13251316
);
13261317
handle.await.expect("stub task should finish");

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

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,16 @@ impl ResolvedPodmanImage {
228228
/// - a malformed or reserved working directory;
229229
/// - for a custom working directory, an image volume with an invalid or
230230
/// reserved target, or one covering the resolved workspace.
231-
pub fn from_inspect(inspected: &ImageInspect) -> Result<Self, ComputeDriverError> {
232-
let image_config = inspected.config.as_ref();
233-
let workspace_root = driver_mounts::resolve_oci_workspace_root(
234-
image_config.map_or("", |config| config.working_dir.as_str()),
235-
)
236-
.map_err(ComputeDriverError::Precondition)?;
231+
pub fn from_inspect(inspected: ImageInspect) -> Result<Self, ComputeDriverError> {
232+
let config = inspected.config.unwrap_or_default();
233+
let workspace_root = driver_mounts::resolve_oci_workspace_root(&config.working_dir)
234+
.map_err(ComputeDriverError::Precondition)?;
237235
for control_path in PODMAN_WORKLOAD_CONTROL_PATHS {
238236
driver_mounts::validate_workspace_control_path(&workspace_root, control_path)
239237
.map_err(ComputeDriverError::Precondition)?;
240238
}
241239
if workspace_root != driver_mounts::DEFAULT_WORKSPACE_ROOT
242-
&& let Some(volumes) = image_config.and_then(|config| config.volumes.as_ref())
240+
&& let Some(volumes) = config.volumes.as_ref()
243241
{
244242
for volume in volumes.keys() {
245243
validate_podman_mount_target(volume).map_err(|error| {
@@ -257,31 +255,31 @@ impl ResolvedPodmanImage {
257255
}
258256
}
259257
Ok(Self {
260-
id: inspected.id.clone(),
261-
oci_user: image_config
262-
.map_or("", |config| config.user.as_str())
263-
.to_string(),
264-
environment: image_config.map_or_else(Vec::new, |config| config.env.clone()),
258+
id: inspected.id,
259+
oci_user: config.user,
260+
environment: config.env,
265261
workspace_root,
266262
})
267263
}
268264

269-
/// Image reference without inspected metadata; uses the managed workspace.
270-
#[cfg(test)]
271-
fn unpinned(image: &str) -> Self {
272-
Self {
273-
id: image.to_string(),
274-
oci_user: String::new(),
275-
environment: Vec::new(),
276-
workspace_root: driver_mounts::DEFAULT_WORKSPACE_ROOT.to_string(),
277-
}
278-
}
279-
280265
pub(crate) fn uses_managed_workspace(&self) -> bool {
281266
self.workspace_root == driver_mounts::DEFAULT_WORKSPACE_ROOT
282267
}
283268
}
284269

270+
#[cfg(test)]
271+
fn resolved_image(id: &str, user: &str, working_dir: &str) -> ResolvedPodmanImage {
272+
ResolvedPodmanImage::from_inspect(ImageInspect {
273+
id: id.to_string(),
274+
config: Some(crate::client::ImageConfig {
275+
user: user.to_string(),
276+
working_dir: working_dir.to_string(),
277+
..Default::default()
278+
}),
279+
})
280+
.expect("fixture image metadata should be valid")
281+
}
282+
285283
// ---------------------------------------------------------------------------
286284
// Typed container spec structs for the Podman libpod create API.
287285
// ---------------------------------------------------------------------------
@@ -1142,7 +1140,7 @@ pub fn build_container_spec_with_token_and_gpu_devices(
11421140
gpu_device_ids: Option<&[String]>,
11431141
) -> Result<Value, ComputeDriverError> {
11441142
let image = resolve_image(sandbox, config);
1145-
let resolved_image = ResolvedPodmanImage::unpinned(image);
1143+
let resolved_image = resolved_image(image, "", "");
11461144
build_container_spec_for_image(
11471145
sandbox,
11481146
config,
@@ -1822,22 +1820,6 @@ mod tests {
18221820
static ENV_LOCK: std::sync::LazyLock<std::sync::Mutex<()>> =
18231821
std::sync::LazyLock::new(|| std::sync::Mutex::new(()));
18241822

1825-
fn resolved_image(id: &str, user: &str, working_dir: &str) -> ResolvedPodmanImage {
1826-
ResolvedPodmanImage::from_inspect(&ImageInspect {
1827-
id: id.to_string(),
1828-
config: Some(ImageConfig {
1829-
user: user.to_string(),
1830-
env: vec![
1831-
"LD_PRELOAD=/hostile.so".into(),
1832-
"HTTP_PROXY=http://bypass".into(),
1833-
],
1834-
working_dir: working_dir.to_string(),
1835-
volumes: None,
1836-
}),
1837-
})
1838-
.unwrap()
1839-
}
1840-
18411823
#[test]
18421824
fn isolated_pair_keeps_privileges_network_and_secrets_out_of_workload() {
18431825
let sandbox = DriverSandbox {
@@ -1858,7 +1840,11 @@ mod tests {
18581840
"sha256:image".into(),
18591841
)
18601842
.unwrap();
1861-
let image = resolved_image("sha256:image", "1000:1001", "");
1843+
let mut image = resolved_image("sha256:image", "1000:1001", "");
1844+
image.environment = vec![
1845+
"LD_PRELOAD=/hostile.so".into(),
1846+
"HTTP_PROXY=http://bypass".into(),
1847+
];
18621848
let specs = build_isolation_specs(IsolationSpecInput {
18631849
sandbox: &sandbox,
18641850
config: &config,
@@ -2024,14 +2010,14 @@ mod tests {
20242010
}),
20252011
};
20262012

2027-
assert!(ResolvedPodmanImage::from_inspect(&inspect("/workspace")).is_err());
2028-
let image = ResolvedPodmanImage::from_inspect(&inspect("/workspace/project/cache"))
2013+
assert!(ResolvedPodmanImage::from_inspect(inspect("/workspace")).is_err());
2014+
let image = ResolvedPodmanImage::from_inspect(inspect("/workspace/project/cache"))
20292015
.expect("image volumes nested below the workspace remain valid");
20302016
assert_eq!(image.workspace_root, "/workspace/project");
20312017

20322018
let mut fallback = inspect("/etc/openshell");
20332019
fallback.config.as_mut().unwrap().working_dir = "/sandbox".into();
2034-
ResolvedPodmanImage::from_inspect(&fallback)
2020+
ResolvedPodmanImage::from_inspect(fallback)
20352021
.expect("existing /sandbox images keep their image-volume behavior");
20362022
}
20372023

@@ -3162,19 +3148,16 @@ mod tests {
31623148

31633149
#[test]
31643150
fn resolved_workspace_rejects_covering_driver_mount() {
3165-
use openshell_core::proto::compute::v1::{DriverSandboxSpec, DriverSandboxTemplate};
3166-
31673151
let image = resolved_image("sha256:immutable", "1000:1000", "/workspace/project");
31683152
let mut sandbox = test_sandbox("test-id", "test-name");
3169-
sandbox.spec = Some(DriverSandboxSpec {
3170-
template: Some(DriverSandboxTemplate {
3171-
driver_config: Some(json_struct(serde_json::json!({
3172-
"mounts": [{"type": "tmpfs", "target": "/workspace"}]
3173-
}))),
3174-
..Default::default()
3175-
}),
3176-
..Default::default()
3177-
});
3153+
sandbox
3154+
.spec
3155+
.get_or_insert_default()
3156+
.template
3157+
.get_or_insert_default()
3158+
.driver_config = Some(json_struct(serde_json::json!({
3159+
"mounts": [{"type": "tmpfs", "target": "/workspace"}]
3160+
})));
31783161
let error = build_container_spec_for_image(
31793162
&sandbox,
31803163
&test_config(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ impl PodmanComputeDriver {
10011001
)));
10021002
}
10031003
let resolved_image =
1004-
container::ResolvedPodmanImage::from_inspect(&inspected_image)?;
1004+
container::ResolvedPodmanImage::from_inspect(inspected_image)?;
10051005

10061006
for mount_image in container::podman_driver_image_mount_sources(
10071007
sandbox,

0 commit comments

Comments
 (0)