use nucleus::resources::CgroupState;
use nucleus::StateTransition;
#[test]
fn test_cgroup_state_machine_happy_path() {
let states = [
CgroupState::Nonexistent,
CgroupState::Created,
CgroupState::Configured,
CgroupState::Attached,
CgroupState::Monitoring,
CgroupState::Removed,
];
for i in 0..states.len() - 1 {
assert!(
states[i].can_transition_to(&states[i + 1]),
"Invalid transition from {:?} to {:?}",
states[i],
states[i + 1]
);
}
}
#[test]
fn test_cgroup_property_resource_limits_enforced() {
let state = CgroupState::Configured;
assert!(state.can_transition_to(&CgroupState::Configured)); assert!(state.can_transition_to(&CgroupState::Attached));
assert!(state.can_transition_to(&CgroupState::Removed));
assert!(!state.can_transition_to(&CgroupState::Nonexistent));
assert!(!state.can_transition_to(&CgroupState::Created));
assert!(!state.can_transition_to(&CgroupState::Monitoring)); }
#[test]
fn test_cgroup_property_no_resource_leak() {
let state = CgroupState::Removed;
assert!(state.is_terminal());
assert!(!state.can_transition_to(&CgroupState::Nonexistent));
assert!(!state.can_transition_to(&CgroupState::Created));
assert!(!state.can_transition_to(&CgroupState::Configured));
assert!(!state.can_transition_to(&CgroupState::Attached));
assert!(!state.can_transition_to(&CgroupState::Monitoring));
assert!(state.can_transition_to(&CgroupState::Removed));
}
#[test]
fn test_cgroup_property_cleanup_guaranteed() {
let states = [
CgroupState::Nonexistent,
CgroupState::Created,
CgroupState::Configured,
CgroupState::Attached,
CgroupState::Monitoring,
];
for initial in states {
assert!(
!initial.is_terminal(),
"{:?} should not be terminal",
initial
);
let mut current = initial;
let mut steps = 0;
while !current.is_terminal() && steps < 10 {
current = match current {
CgroupState::Nonexistent => CgroupState::Created,
CgroupState::Created => CgroupState::Configured,
CgroupState::Configured => CgroupState::Attached,
CgroupState::Attached => CgroupState::Monitoring,
CgroupState::Monitoring => CgroupState::Removed,
CgroupState::Removed => break,
};
steps += 1;
}
assert!(
current.is_terminal(),
"Should reach terminal from {:?}",
initial
);
}
}
#[test]
fn test_cgroup_error_paths() {
assert!(CgroupState::Created.can_transition_to(&CgroupState::Removed));
assert!(CgroupState::Configured.can_transition_to(&CgroupState::Removed));
assert!(CgroupState::Attached.can_transition_to(&CgroupState::Removed));
}
#[test]
fn test_cgroup_no_backwards_transitions() {
assert!(!CgroupState::Created.can_transition_to(&CgroupState::Nonexistent));
assert!(!CgroupState::Configured.can_transition_to(&CgroupState::Created));
assert!(!CgroupState::Attached.can_transition_to(&CgroupState::Configured));
assert!(!CgroupState::Monitoring.can_transition_to(&CgroupState::Attached));
assert!(!CgroupState::Removed.can_transition_to(&CgroupState::Monitoring));
}