Skip to content

Commit 2d26c6e

Browse files
test(server): assert compute driver resolution matches compiled features
Cover both directions of the compile-time driver gate in one test: a compiled-in driver must resolve to its Builtin variant; a compiled-out driver must fail with a message naming the Cargo feature that re-enables it. An exhaustive match on `ComputeDriverKind` inside the test makes a new variant a compile error until it's given a feature gate. Derive `Hash` on `ComputeDriverKind` so it can key the driver-features map. Signed-off-by: Dimitar Mirchev <[email protected]>
1 parent 9a1e04c commit 2d26c6e

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

‎crates/openshell-core/src/config.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub const CDI_GPU_DEVICE_ALL: &str = "nvidia.com/gpu=all";
4949
pub const DEFAULT_SANDBOX_PIDS_LIMIT: i64 = 2048;
5050

5151
/// Compute backends the gateway can orchestrate sandboxes through.
52-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
52+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
5353
#[serde(rename_all = "snake_case")]
5454
pub enum ComputeDriverKind {
5555
Kubernetes,

‎crates/openshell-server/src/lib.rs‎

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,38 +1514,54 @@ mod tests {
15141514
);
15151515
}
15161516

1517-
/// When a built-in driver was compiled out, resolving it must fail with a
1518-
/// message that names both the driver and the Cargo flag that re-enables
1519-
/// it. Iterates over every compiled-out kind so a single test run under
1520-
/// e.g. `--no-default-features` exercises all four rejection messages;
1521-
/// the default build has every driver and the loop body is skipped.
1517+
/// Exercise both directions of the compile-time driver gate. A driver
1518+
/// compiled *in* must resolve to `ConfiguredComputeDriver::Builtin(kind)`;
1519+
/// a driver compiled *out* must fail with a message that names both the
1520+
/// driver and the Cargo flag that re-enables it.
15221521
#[test]
1523-
fn configured_compute_driver_rejects_compiled_out_builtin() {
1524-
let missing = [
1525-
(
1526-
cfg!(feature = "driver-kubernetes"),
1527-
ComputeDriverKind::Kubernetes,
1528-
),
1529-
(cfg!(feature = "driver-docker"), ComputeDriverKind::Docker),
1530-
(cfg!(feature = "driver-podman"), ComputeDriverKind::Podman),
1531-
(cfg!(feature = "driver-vm"), ComputeDriverKind::Vm),
1522+
fn configured_compute_driver_matches_compiled_features() {
1523+
// Exhaustive match to ensure that the test is updated if a new driver is added.
1524+
fn compiled_in(kind: ComputeDriverKind) -> bool {
1525+
match kind {
1526+
ComputeDriverKind::Kubernetes => cfg!(feature = "driver-kubernetes"),
1527+
ComputeDriverKind::Docker => cfg!(feature = "driver-docker"),
1528+
ComputeDriverKind::Podman => cfg!(feature = "driver-podman"),
1529+
ComputeDriverKind::Vm => cfg!(feature = "driver-vm"),
1530+
}
1531+
}
1532+
1533+
let driver_features: std::collections::HashMap<ComputeDriverKind, bool> = [
1534+
ComputeDriverKind::Kubernetes,
1535+
ComputeDriverKind::Docker,
1536+
ComputeDriverKind::Podman,
1537+
ComputeDriverKind::Vm,
15321538
]
15331539
.into_iter()
1534-
.filter_map(|(present, kind)| (!present).then_some(kind));
1540+
.map(|kind| (kind, compiled_in(kind)))
1541+
.collect();
15351542

1536-
for kind in missing {
1543+
for (&kind, &present) in &driver_features {
15371544
let config = Config::new(None).with_compute_drivers([kind]);
1538-
let err =
1539-
configured_compute_driver(&config, test_driver_startup(&config, None)).unwrap_err();
1540-
let msg = err.to_string();
1541-
assert!(
1542-
msg.contains("not compiled into this gateway"),
1543-
"driver {kind:?}: unexpected error: {msg}"
1544-
);
1545-
assert!(
1546-
msg.contains(&format!("--features driver-{}", kind.as_str())),
1547-
"driver {kind:?}: error must name the Cargo flag that re-enables the driver: {msg}"
1548-
);
1545+
let result = configured_compute_driver(&config, test_driver_startup(&config, None));
1546+
1547+
if present {
1548+
let driver = result.unwrap();
1549+
assert!(
1550+
matches!(driver, ConfiguredComputeDriver::Builtin(k) if k == kind),
1551+
"driver {kind:?}: expected Builtin({kind:?}), got {driver:?}"
1552+
);
1553+
} else {
1554+
let err = result.expect_err("must reject compiled-out driver");
1555+
let msg = err.to_string();
1556+
assert!(
1557+
msg.contains("not compiled into this gateway"),
1558+
"driver {kind:?}: unexpected error: {msg}"
1559+
);
1560+
assert!(
1561+
msg.contains(&format!("--features driver-{}", kind.as_str())),
1562+
"driver {kind:?}: error must name the Cargo flag that re-enables the driver: {msg}"
1563+
);
1564+
}
15491565
}
15501566
}
15511567
}

0 commit comments

Comments
 (0)