Skip to content

Commit 48538ed

Browse files
committed
refactor(vm): derive GPU device request once
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent b7a3574 commit 48538ed

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,11 @@ impl VmDriver {
622622
)));
623623
}
624624

625-
let spec = sandbox.spec.as_ref();
626-
let is_gpu = spec.is_some_and(|s| s.gpu);
627-
let gpu_device = spec.map_or("", |s| s.gpu_device.as_str());
628-
let gpu_bdf = if is_gpu {
625+
let gpu_device = sandbox
626+
.spec
627+
.as_ref()
628+
.and_then(|spec| requested_gpu_device(spec.gpu, &spec.gpu_device));
629+
let gpu_bdf = if let Some(gpu_device) = gpu_device {
629630
Some(self.assign_gpu_to_record(&sandbox.id, gpu_device).await?)
630631
} else {
631632
None
@@ -2627,6 +2628,10 @@ fn validate_sandbox_id(sandbox_id: &str) -> Result<(), Status> {
26272628
Ok(())
26282629
}
26292630

2631+
fn requested_gpu_device(gpu: bool, gpu_device: &str) -> Option<&str> {
2632+
gpu.then_some(gpu_device)
2633+
}
2634+
26302635
#[allow(clippy::result_large_err)]
26312636
fn validate_gpu_request(gpu: bool, gpu_device: &str, gpu_enabled: bool) -> Result<(), Status> {
26322637
if gpu && !gpu_enabled {
@@ -4556,6 +4561,24 @@ mod tests {
45564561
assert!(err.message().contains("gpu_device requires gpu=true"));
45574562
}
45584563

4564+
#[test]
4565+
fn requested_gpu_device_returns_none_without_gpu_request() {
4566+
assert_eq!(requested_gpu_device(false, ""), None);
4567+
}
4568+
4569+
#[test]
4570+
fn requested_gpu_device_defaults_empty_request_to_inventory_choice() {
4571+
assert_eq!(requested_gpu_device(true, ""), Some(""));
4572+
}
4573+
4574+
#[test]
4575+
fn requested_gpu_device_returns_explicit_device_id() {
4576+
assert_eq!(
4577+
requested_gpu_device(true, "0000:2d:00.0"),
4578+
Some("0000:2d:00.0")
4579+
);
4580+
}
4581+
45594582
#[test]
45604583
fn validate_vm_sandbox_rejects_platform_config() {
45614584
let sandbox = Sandbox {

0 commit comments

Comments
 (0)