use crate::mxc_common::models::ContainmentBackend;
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ProbeCoverage {
Detected(&'static [&'static str]),
NotDetected(&'static str),
}
#[derive(Debug)]
pub(crate) struct BackendRegistration {
pub(crate) backend: ContainmentBackend,
pub(crate) experimental: bool,
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) probe_coverage: ProbeCoverage,
}
static BACKENDS: [BackendRegistration; 10] = [
BackendRegistration {
backend: ContainmentBackend::ProcessContainer,
experimental: false,
probe_coverage: ProbeCoverage::Detected(&["windows"]),
},
BackendRegistration {
backend: ContainmentBackend::Wslc,
experimental: false,
probe_coverage: ProbeCoverage::Detected(&["windows"]),
},
BackendRegistration {
backend: ContainmentBackend::Lxc,
experimental: false,
probe_coverage: ProbeCoverage::Detected(&["linux"]),
},
BackendRegistration {
backend: ContainmentBackend::Vm,
experimental: false,
probe_coverage: ProbeCoverage::NotDetected(
"abstract VM intent; resolves to a concrete backend per host, and that backend is \
detected in its own right",
),
},
BackendRegistration {
backend: ContainmentBackend::MicroVm,
experimental: true,
probe_coverage: ProbeCoverage::NotDetected(
"no hypervisor-presence detector yet; see the follow-up items in \
docs/development/plans/backend-support-probe-api.md",
),
},
BackendRegistration {
backend: ContainmentBackend::Hyperlight,
experimental: true,
probe_coverage: ProbeCoverage::Detected(&["windows", "linux"]),
},
BackendRegistration {
backend: ContainmentBackend::WindowsSandbox,
experimental: true,
probe_coverage: ProbeCoverage::Detected(&["windows"]),
},
BackendRegistration {
backend: ContainmentBackend::IsolationSession,
experimental: false,
probe_coverage: ProbeCoverage::Detected(&["windows"]),
},
BackendRegistration {
backend: ContainmentBackend::Seatbelt,
experimental: false,
probe_coverage: ProbeCoverage::Detected(&["macos"]),
},
BackendRegistration {
backend: ContainmentBackend::Bubblewrap,
experimental: false,
probe_coverage: ProbeCoverage::Detected(&["linux"]),
},
];
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn all() -> &'static [BackendRegistration] {
&BACKENDS
}
#[cfg(test)]
pub(crate) const CANONICAL_BACKEND_NAMES: &[(ContainmentBackend, &str)] = &[
(ContainmentBackend::ProcessContainer, "processcontainer"),
(ContainmentBackend::Wslc, "wslc"),
(ContainmentBackend::Lxc, "lxc"),
(ContainmentBackend::Vm, "vm"),
(ContainmentBackend::MicroVm, "microvm"),
(ContainmentBackend::Hyperlight, "hyperlight"),
(ContainmentBackend::WindowsSandbox, "windows_sandbox"),
(ContainmentBackend::IsolationSession, "isolation_session"),
(ContainmentBackend::Seatbelt, "seatbelt"),
(ContainmentBackend::Bubblewrap, "bubblewrap"),
];
#[cfg(test)]
pub(crate) fn all_containment_names() -> Vec<String> {
all()
.iter()
.map(|entry| entry.backend.wire_name().to_string())
.collect()
}
#[cfg(test)]
pub(crate) fn detected_on_current_target() -> Vec<&'static str> {
all()
.iter()
.filter(|entry| match &entry.probe_coverage {
ProbeCoverage::Detected(targets) => targets.contains(&std::env::consts::OS),
ProbeCoverage::NotDetected(_) => false,
})
.map(|entry| entry.backend.wire_name())
.collect()
}
pub(crate) fn registration(backend: &ContainmentBackend) -> &'static BackendRegistration {
let index = match backend {
ContainmentBackend::ProcessContainer => 0,
ContainmentBackend::Wslc => 1,
ContainmentBackend::Lxc => 2,
ContainmentBackend::Vm => 3,
ContainmentBackend::MicroVm => 4,
ContainmentBackend::Hyperlight => 5,
ContainmentBackend::WindowsSandbox => 6,
ContainmentBackend::IsolationSession => 7,
ContainmentBackend::Seatbelt => 8,
ContainmentBackend::Bubblewrap => 9,
};
&BACKENDS[index]
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeSet;
#[test]
fn registrations_have_unique_backend_keys_and_matching_lookups() {
let mut names = BTreeSet::new();
for entry in &BACKENDS {
assert!(names.insert(entry.backend.wire_name()));
assert!(std::ptr::eq(registration(&entry.backend), entry));
}
assert_eq!(names.len(), 10);
}
#[test]
fn registration_preserves_the_experimental_backend_set() {
let experimental: BTreeSet<_> = BACKENDS
.iter()
.filter(|entry| entry.experimental)
.map(|entry| entry.backend.wire_name())
.collect();
assert_eq!(
experimental,
BTreeSet::from(["microvm", "hyperlight", "windows_sandbox"])
);
}
#[test]
fn canonical_names_match_the_backend_wire_names() {
for (backend, expected) in CANONICAL_BACKEND_NAMES {
assert_eq!(backend.wire_name(), *expected, "{backend:?}");
}
}
#[test]
fn the_canonical_name_table_covers_every_registered_backend() {
let registered: BTreeSet<_> = BACKENDS
.iter()
.map(|entry| entry.backend.wire_name())
.collect();
let canonical: BTreeSet<_> = CANONICAL_BACKEND_NAMES
.iter()
.map(|(_, name)| *name)
.collect();
assert_eq!(registered, canonical);
}
#[test]
fn undetected_backends_explain_why() {
for entry in all() {
if let ProbeCoverage::NotDetected(reason) = &entry.probe_coverage {
assert!(
!reason.trim().is_empty(),
"{:?} must say why it has no host detector",
entry.backend
);
}
}
}
#[test]
fn detected_backends_name_supported_targets() {
for entry in all() {
if let ProbeCoverage::Detected(targets) = &entry.probe_coverage {
assert!(
!targets.is_empty(),
"{:?} is registered as Detected but names no target",
entry.backend
);
for target in *targets {
assert!(
["windows", "linux", "macos"].contains(target),
"{:?} names the unknown target {target:?}",
entry.backend
);
}
}
}
}
}