2026-10-09 15:58:29 +02:00
//! The `mind-control` command driven end to end through `mind_body::cli::run`
//! against a temporary root, and the binary once as a process for its exit
//! status. Nothing touches `/etc`.
use chrono ::{ TimeZone , Utc } ;
use mind_body ::cli ::{ operator_name , run } ;
2026-11-01 19:11:21 +01:00
use mind_body ::launch ::StopScope ;
2026-10-09 15:58:29 +02:00
fn go ( root : & std ::path ::Path , args : & [ & str ] , set_by : & str , secs : i64 ) -> ( anyhow ::Result < ( ) > , String ) {
2026-10-12 03:48:49 +02:00
let ( result , out , _ ) = go_stopping ( root , args , set_by , secs , | _ | Ok ( ( ) ) ) ;
( result , out )
}
2026-11-01 19:11:21 +01:00
/// Runs one command with a stopper that records every call (the instance of a
/// Mind's stop, None for the swarm's) and answers with `stop`. `stop` also
/// sees the call before it returns.
2026-10-12 03:48:49 +02:00
fn go_stopping (
root : & std ::path ::Path ,
args : & [ & str ] ,
set_by : & str ,
secs : i64 ,
2026-11-01 19:11:21 +01:00
mut stop : impl FnMut ( Option < & str > ) -> anyhow ::Result < ( ) > ,
) -> ( anyhow ::Result < ( ) > , String , Vec < Option < String > > ) {
2026-10-09 15:58:29 +02:00
let args : Vec < String > = args . iter ( ) . map ( | a | a . to_string ( ) ) . collect ( ) ;
let mut out = Vec ::new ( ) ;
2026-10-12 03:48:49 +02:00
let mut calls = Vec ::new ( ) ;
2026-10-09 15:58:29 +02:00
let now = Utc . timestamp_opt ( 1_800_000_000 + secs , 0 ) . unwrap ( ) ;
2026-11-01 19:11:21 +01:00
let result = run ( & args , root , set_by , now , & mut out , & mut | scope | {
let instance = match scope {
StopScope ::Mind ( instance ) = > Some ( instance . 0. as_str ( ) ) ,
StopScope ::Swarm = > None ,
} ;
calls . push ( instance . map ( str ::to_string ) ) ;
2026-10-12 03:48:49 +02:00
stop ( instance )
} ) ;
( result , String ::from_utf8 ( out ) . unwrap ( ) , calls )
2026-10-09 15:58:29 +02:00
}
#[ test ]
fn show_reports_every_rung_from_an_absent_store_to_released ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let ( r , out ) = go ( root . path ( ) , & [ " --instance " , " eureka " , " show " ] , " op " , 0 ) ;
r . unwrap ( ) ;
2026-10-30 02:49:35 +01:00
assert! ( out . contains ( " brake: absent " ) & & out . contains ( " BrakeAbsent " ) , " {out} " ) ;
2026-10-09 15:58:29 +02:00
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " alice " , 1 ) . 0. unwrap ( ) ;
let ( _ , out ) = go ( root . path ( ) , & [ " --instance " , " eureka " , " show " ] , " op " , 2 ) ;
2026-10-30 02:49:35 +01:00
assert! ( out . contains ( " brake: released " ) & & out . contains ( " set_by alice " ) & & out . contains ( " BrakeAbsent " ) , " the swarm is still unreleased: {out} " ) ;
2026-10-09 15:58:29 +02:00
2026-10-30 02:49:35 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " release " ] , " bob " , 3 ) . 0. unwrap ( ) ;
2026-10-09 15:58:29 +02:00
let ( _ , out ) = go ( root . path ( ) , & [ " --instance " , " eureka " , " show " ] , " op " , 4 ) ;
2026-10-31 05:34:07 +01:00
assert! ( out . contains ( " Released " ) , " {out} " ) ;
2026-10-09 15:58:29 +02:00
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " alice " , 5 ) . 0. unwrap ( ) ;
let ( _ , out ) = go ( root . path ( ) , & [ " --instance " , " eureka " , " show " ] , " op " , 6 ) ;
assert! ( out . contains ( " brake: held " ) & & out . contains ( " BrakeHeld " ) , " {out} " ) ;
}
#[ test ]
fn instances_do_not_share_a_store ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " --instance " , " one " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
let ( _ , out ) = go ( root . path ( ) , & [ " --instance " , " two " , " show " ] , " op " , 1 ) ;
assert! ( out . contains ( " brake: absent " ) , " {out} " ) ;
assert! ( root . path ( ) . join ( " one " ) . join ( " control.cc " ) . exists ( ) ) ;
}
#[ test ]
fn malformed_commands_are_refused_and_write_nothing ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let bad : & [ & [ & str ] ] = & [
& [ ] ,
& [ " show " ] ,
& [ " --instance " , " eureka " ] ,
& [ " --instance " , " eureka " , " brake " ] ,
& [ " --instance " , " eureka " , " brake " , " toggle " ] ,
& [ " --instance " , " ../evil " , " brake " , " release " ] ,
2026-10-09 16:05:51 +02:00
& [ " --instances " , " eureka " , " brake " , " release " ] ,
& [ " eureka " , " eureka " , " brake " , " release " ] ,
2026-10-09 15:58:29 +02:00
] ;
for args in bad {
assert! ( go ( root . path ( ) , args , " op " , 0 ) . 0. is_err ( ) , " {args:?} " ) ;
}
assert_eq! ( std ::fs ::read_dir ( root . path ( ) ) . unwrap ( ) . count ( ) , 0 , " a refused command wrote something " ) ;
}
#[ test ]
fn the_operator_is_sudo_user_then_user_then_unknown ( ) {
let s = | v : & str | Some ( v . to_string ( ) ) ;
assert_eq! ( operator_name ( s ( " alice " ) , s ( " root " ) ) , " alice " ) ;
assert_eq! ( operator_name ( None , s ( " bob " ) ) , " bob " ) ;
assert_eq! ( operator_name ( s ( " " ) , s ( " bob " ) ) , " bob " ) ;
assert_eq! ( operator_name ( None , s ( " " ) ) , " unknown " ) ;
assert_eq! ( operator_name ( None , None ) , " unknown " ) ;
}
2026-10-09 16:05:51 +02:00
#[ test ]
fn the_binary_exits_nonzero_with_the_usage_when_refused ( ) {
// A refusal before any path is derived, so this never reaches /etc.
let output = std ::process ::Command ::new ( env! ( " CARGO_BIN_EXE_mind-control " ) ) . output ( ) . unwrap ( ) ;
assert_eq! ( output . status . code ( ) , Some ( 2 ) ) ;
assert! ( String ::from_utf8_lossy ( & output . stderr ) . contains ( " usage: mind-control " ) ) ;
assert! ( output . stdout . is_empty ( ) ) ;
}
2026-10-09 16:53:27 +02:00
2026-10-12 07:33:20 +02:00
#[ test ]
2026-10-30 02:49:35 +01:00
fn the_binary_exits_2_for_check_while_held_and_0_after_release_with_a_swarm ( ) {
2026-10-12 07:33:20 +02:00
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let run = | args : & [ & str ] | {
std ::process ::Command ::new ( env! ( " CARGO_BIN_EXE_mind-control " ) )
. arg ( " --root " )
. arg ( root . path ( ) )
. args ( [ " --instance " , " eureka " ] )
. args ( args )
. output ( )
. unwrap ( )
} ;
// Absent store: refused, and the gate wrote nothing.
let output = run ( & [ " check " ] ) ;
assert_eq! ( output . status . code ( ) , Some ( 2 ) ) ;
assert! ( String ::from_utf8_lossy ( & output . stderr ) . contains ( " held: BrakeAbsent " ) ) ;
assert_eq! ( std ::fs ::read_dir ( root . path ( ) ) . unwrap ( ) . count ( ) , 0 ) ;
2026-10-30 02:49:35 +01:00
// Released with no swarm is still refused; with a released swarm it opens.
2026-10-12 07:33:20 +02:00
assert! ( run ( & [ " brake " , " release " ] ) . status . success ( ) ) ;
assert_eq! ( run ( & [ " check " ] ) . status . code ( ) , Some ( 2 ) ) ;
2026-10-30 02:49:35 +01:00
let swarm = | args : & [ & str ] | {
std ::process ::Command ::new ( env! ( " CARGO_BIN_EXE_mind-control " ) ) . arg ( " --root " ) . arg ( root . path ( ) ) . arg ( " swarm " ) . args ( args ) . output ( ) . unwrap ( )
} ;
assert! ( swarm ( & [ " brake " , " release " ] ) . status . success ( ) ) ;
2026-10-12 07:33:20 +02:00
let open = run ( & [ " check " ] ) ;
assert_eq! ( open . status . code ( ) , Some ( 0 ) ) ;
assert! ( String ::from_utf8_lossy ( & open . stdout ) . contains ( " Released " ) ) ;
// Holding closes it again. The brake is written through the library: the
// binary's `brake hold` would also ask systemd to stop units.
mind_body ::control ::ControlWriter ::open ( root . path ( ) . join ( " eureka " ) . join ( " control.cc " ) )
. unwrap ( )
. set_brake ( false , Utc . timestamp_opt ( 1_800_000_009 , 0 ) . unwrap ( ) , " op " )
. unwrap ( ) ;
let closed = run ( & [ " check " ] ) ;
assert_eq! ( closed . status . code ( ) , Some ( 2 ) ) ;
assert! ( String ::from_utf8_lossy ( & closed . stderr ) . contains ( " held: BrakeHeld " ) ) ;
}
2026-10-09 16:53:27 +02:00
#[ test ]
fn a_leading_root_flag_replaces_the_default_root ( ) {
let default = tempfile ::tempdir ( ) . unwrap ( ) ;
let chosen = tempfile ::tempdir ( ) . unwrap ( ) ;
let chosen_arg = chosen . path ( ) . to_str ( ) . unwrap ( ) ;
let ( result , _ ) = go ( default . path ( ) , & [ " --root " , chosen_arg , " --instance " , " eureka " , " brake " , " release " ] , " op " , 0 ) ;
result . unwrap ( ) ;
assert! ( chosen . path ( ) . join ( " eureka " ) . join ( " control.cc " ) . exists ( ) , " the write went to the chosen root " ) ;
assert_eq! ( std ::fs ::read_dir ( default . path ( ) ) . unwrap ( ) . count ( ) , 0 , " the default root was not touched " ) ;
// Without the flag the default applies.
go ( default . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 1 ) . 0. unwrap ( ) ;
assert! ( default . path ( ) . join ( " eureka " ) . join ( " control.cc " ) . exists ( ) ) ;
}
#[ test ]
fn a_root_flag_without_a_directory_or_an_instance_is_refused_and_writes_nothing ( ) {
let default = tempfile ::tempdir ( ) . unwrap ( ) ;
for args in [ & [ " --root " ] [ .. ] , & [ " --root " , " /nonexistent-root-for-test " ] [ .. ] ] {
assert! ( go ( default . path ( ) , args , " op " , 0 ) . 0. is_err ( ) , " {args:?} " ) ;
}
assert_eq! ( std ::fs ::read_dir ( default . path ( ) ) . unwrap ( ) . count ( ) , 0 ) ;
assert! ( ! std ::path ::Path ::new ( " /nonexistent-root-for-test " ) . exists ( ) ) ;
}
2026-10-12 03:48:49 +02:00
2026-10-12 07:39:55 +02:00
fn listing ( dir : & std ::path ::Path ) -> Vec < String > {
let mut names : Vec < String > = std ::fs ::read_dir ( dir ) . unwrap ( ) . map ( | e | e . unwrap ( ) . file_name ( ) . to_string_lossy ( ) . into_owned ( ) ) . collect ( ) ;
names . sort ( ) ;
names
}
2026-10-12 07:33:20 +02:00
fn check_out ( root : & std ::path ::Path ) -> ( anyhow ::Result < ( ) > , String ) {
go ( root , & [ " --instance " , " eureka " , " check " ] , " op " , 0 )
}
#[ test ]
fn check_refuses_every_reading_but_released ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let held = | root : & std ::path ::Path , reason : & str | {
let ( result , out ) = check_out ( root ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
assert! ( error . contains ( reason ) , " {reason}: {error} " ) ;
assert! ( out . contains ( reason ) , " {reason}: {out} " ) ;
} ;
held ( root . path ( ) , " BrakeAbsent " ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " op " , 1 ) . 0. unwrap ( ) ;
held ( root . path ( ) , " BrakeHeld " ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 2 ) . 0. unwrap ( ) ;
2026-10-30 02:49:35 +01:00
held ( root . path ( ) , " BrakeAbsent " ) ;
2026-10-31 05:58:44 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " hold " ] , " op " , 3 ) . 0. unwrap ( ) ;
held ( root . path ( ) , " BrakeHeld " ) ;
go ( root . path ( ) , & [ " swarm " , " brake " , " release " ] , " op " , 4 ) . 0. unwrap ( ) ;
check_out ( root . path ( ) ) . 0. unwrap ( ) ;
2026-10-12 07:33:20 +02:00
// An undecodable store is a held reading too.
let broken = tempfile ::tempdir ( ) . unwrap ( ) ;
std ::fs ::create_dir ( broken . path ( ) . join ( " eureka " ) ) . unwrap ( ) ;
std ::fs ::write ( broken . path ( ) . join ( " eureka " ) . join ( " control.cc " ) , b " not a store " ) . unwrap ( ) ;
held ( broken . path ( ) , " Undecodable " ) ;
}
#[ test ]
2026-10-30 02:49:35 +01:00
fn check_opens_on_release_with_a_swarm_and_is_per_instance ( ) {
2026-10-12 07:33:20 +02:00
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
2026-10-30 02:49:35 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " release " ] , " op " , 1 ) . 0. unwrap ( ) ;
2026-10-12 07:33:20 +02:00
let ( result , out ) = check_out ( root . path ( ) ) ;
result . unwrap ( ) ;
assert! ( out . contains ( " Released " ) , " {out} " ) ;
// Another instance's store is a different file: still absent, still refused.
let ( other , _ ) = go ( root . path ( ) , & [ " --instance " , " other " , " check " ] , " op " , 2 ) ;
assert! ( format! ( " {:#} " , other . unwrap_err ( ) ) . contains ( " BrakeAbsent " ) ) ;
}
2026-10-22 17:37:17 +02:00
#[ test ]
fn check_holds_for_the_named_instance_with_a_released_prefix_neighbour ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " op " , 0 ) . 0. unwrap ( ) ;
2026-10-30 02:49:35 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
2026-10-31 05:58:44 +01:00
// Neighbours that share the held instance's text as a prefix, or only an edge of it, are released.
2026-10-22 17:37:17 +02:00
for ( i , neighbour ) in [ " eureka-two " , " eureka2 " , " xeureka " ] . into_iter ( ) . enumerate ( ) {
let at = 1 + 2 * i as i64 ;
go ( root . path ( ) , & [ " --instance " , neighbour , " brake " , " release " ] , " op " , at ) . 0. unwrap ( ) ;
let ( open , _ ) = go ( root . path ( ) , & [ " --instance " , neighbour , " check " ] , " op " , 10 ) ;
open . unwrap_or_else ( | error | panic! ( " {neighbour} is released and must open: {error:#} " ) ) ;
}
let ( result , out ) = check_out ( root . path ( ) ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
assert! ( error . contains ( " BrakeHeld " ) & & out . contains ( " BrakeHeld " ) , " {error} / {out} " ) ;
}
2026-10-12 07:33:20 +02:00
#[ test ]
fn check_writes_nothing_and_creates_no_directory ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
assert! ( check_out ( root . path ( ) ) . 0. is_err ( ) ) ;
assert_eq! ( std ::fs ::read_dir ( root . path ( ) ) . unwrap ( ) . count ( ) , 0 ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
let file = root . path ( ) . join ( " eureka " ) . join ( " control.cc " ) ;
let before = ( std ::fs ::read ( & file ) . unwrap ( ) , std ::fs ::metadata ( & file ) . unwrap ( ) . modified ( ) . unwrap ( ) ) ;
2026-10-12 07:39:55 +02:00
let names_before = listing ( & root . path ( ) . join ( " eureka " ) ) ;
2026-10-12 07:33:20 +02:00
let _ = check_out ( root . path ( ) ) ;
let _ = check_out ( root . path ( ) ) ;
assert_eq! ( ( std ::fs ::read ( & file ) . unwrap ( ) , std ::fs ::metadata ( & file ) . unwrap ( ) . modified ( ) . unwrap ( ) ) , before ) ;
2026-10-12 07:39:55 +02:00
assert_eq! ( listing ( & root . path ( ) . join ( " eureka " ) ) , names_before ) ;
2026-10-12 07:33:20 +02:00
}
#[ test ]
fn check_takes_no_extra_word_and_needs_an_instance ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
for args in [ & [ " --instance " , " eureka " , " check " , " now " ] [ .. ] , & [ " --instance " , " eureka " , " check " , " show " ] [ .. ] , & [ " check " ] [ .. ] ] {
let ( result , out ) = go ( root . path ( ) , args , " op " , 1 ) ;
assert! ( format! ( " {:#} " , result . unwrap_err ( ) ) . contains ( " usage: mind-control " ) , " {args:?} " ) ;
assert! ( out . is_empty ( ) , " {args:?}: {out} " ) ;
}
}
2026-10-12 03:48:49 +02:00
fn shown ( root : & std ::path ::Path ) -> String {
go ( root , & [ " --instance " , " eureka " , " show " ] , " op " , 0 ) . 1
}
#[ test ]
fn brake_hold_stops_once_with_the_instance_and_the_store_already_held ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
let mut seen_at_stop = String ::new ( ) ;
let ( result , out , calls ) = go_stopping ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " alice " , 1 , | _ | {
seen_at_stop = shown ( root . path ( ) ) ;
Ok ( ( ) )
} ) ;
result . unwrap ( ) ;
2026-11-01 19:11:21 +01:00
assert_eq! ( calls , [ Some ( " eureka " . to_string ( ) ) ] ) ;
2026-10-12 03:48:49 +02:00
assert! ( seen_at_stop . contains ( " BrakeHeld " ) , " the write comes before the stop: {seen_at_stop} " ) ;
2026-11-11 11:58:12 +00:00
assert! ( out . lines ( ) . any ( | line | line = = " units: stopped mind-self@eureka.service mind-persona@eureka.service mind-stream@eureka.service " ) , " {out} " ) ;
2026-10-12 03:48:49 +02:00
}
#[ test ]
fn only_brake_hold_calls_the_stopper ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let commands : & [ & [ & str ] ] = & [
& [ " --instance " , " eureka " , " brake " , " release " ] ,
& [ " --instance " , " eureka " , " show " ] ,
& [ " --instance " , " eureka " , " brake " ] ,
& [ " --instance " , " eureka " , " brake " , " toggle " ] ,
& [ " --instance " , " ../evil " , " brake " , " release " ] ,
& [ " --instance " , " eureka " , " brake " , " hold " , " now " ] ,
& [ ] ,
] ;
for args in commands {
let ( _ , _ , calls ) = go_stopping ( root . path ( ) , args , " op " , 0 , | _ | Ok ( ( ) ) ) ;
assert! ( calls . is_empty ( ) , " {args:?} called the stopper " ) ;
}
let ( _ , _ , calls ) = go_stopping ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " op " , 1 , | _ | Ok ( ( ) ) ) ;
assert_eq! ( calls . len ( ) , 1 ) ;
}
#[ test ]
fn a_failing_stopper_fails_the_hold_and_leaves_the_brake_held ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
let ( result , out , calls ) = go_stopping ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " op " , 1 , | _ | {
Err ( anyhow ::anyhow! ( " systemctl stop failed with exit status 5 " ) )
} ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
assert_eq! ( calls . len ( ) , 1 ) ;
2026-10-22 17:37:08 +02:00
assert! ( error . contains ( " the brake is held " ) & & error . contains ( " exit status 5 " ) , " {error} " ) ;
assert! ( ! error . contains ( " were not stopped " ) , " {error} " ) ;
2026-10-12 03:48:49 +02:00
assert! ( ! out . contains ( " stopped " ) , " a failed stop printed success: {out} " ) ;
assert! ( shown ( root . path ( ) ) . contains ( " BrakeHeld " ) ) ;
}
#[ test ]
fn a_failing_write_still_stops_and_the_hold_fails ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
std ::fs ::create_dir_all ( root . path ( ) . join ( " eureka " ) . join ( " control.cc " ) ) . unwrap ( ) ;
let ( result , out , calls ) = go_stopping ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " op " , 1 , | _ | Ok ( ( ) ) ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
2026-11-01 19:11:21 +01:00
assert_eq! ( calls , [ Some ( " eureka " . to_string ( ) ) ] , " stopping does not wait on the write " ) ;
2026-10-12 03:48:49 +02:00
assert! ( error . contains ( " brake was not written " ) , " {error} " ) ;
assert! ( ! out . contains ( " stopped " ) , " {out} " ) ;
let ( result , _ , calls ) = go_stopping ( root . path ( ) , & [ " --instance " , " eureka " , " brake " , " hold " ] , " op " , 2 , | _ | Err ( anyhow ::anyhow! ( " stop failed " ) ) ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
assert_eq! ( calls . len ( ) , 1 ) ;
2026-10-22 17:37:08 +02:00
assert! ( error . contains ( " brake was not written " ) & & error . contains ( " the stop failed " ) & & ! error . contains ( " were not stopped " ) , " {error} " ) ;
2026-10-12 03:48:49 +02:00
}
2026-10-12 06:43:48 +02:00
/// Runs the real binary's `brake hold` with a `systemctl` of ours first on
2026-10-22 17:37:08 +02:00
/// PATH, so the stopper main.rs wires is the one under test. The fake appends
/// its arguments to a record, one per line; `stop` exits with `exit`, and
/// `is-active` answers inactive for the Self unit and active for the Persona
/// unit, as systemd does when it stopped one and failed the other. The root is
/// returned so the store can be read afterwards.
2026-10-12 06:43:48 +02:00
#[ cfg(unix) ]
2026-10-22 17:37:08 +02:00
fn hold_through_the_binary ( exit : i32 ) -> ( std ::process ::Output , Vec < String > , tempfile ::TempDir ) {
2026-10-12 06:43:48 +02:00
use std ::os ::unix ::fs ::PermissionsExt ;
let bin = tempfile ::tempdir ( ) . unwrap ( ) ;
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let record = bin . path ( ) . join ( " recorded " ) ;
let script = bin . path ( ) . join ( " systemctl " ) ;
2026-10-22 17:37:08 +02:00
let body = format! (
" #!/bin/sh \n printf '%s \n ' \" $@ \" >> '{}' \n case \" $1 \" in \n is-active) case \" $2 \" in mind-self@eureka.service) echo inactive; exit 3;; *) echo active; exit 0;; esac;; \n *) exit {exit};; \n esac \n " ,
record . display ( )
) ;
std ::fs ::write ( & script , body ) . unwrap ( ) ;
2026-10-12 06:43:48 +02:00
std ::fs ::set_permissions ( & script , std ::fs ::Permissions ::from_mode ( 0o755 ) ) . unwrap ( ) ;
let output = std ::process ::Command ::new ( env! ( " CARGO_BIN_EXE_mind-control " ) )
. env ( " PATH " , bin . path ( ) )
. args ( [ " --root " , root . path ( ) . to_str ( ) . unwrap ( ) , " --instance " , " eureka " , " brake " , " hold " ] )
. output ( )
. unwrap ( ) ;
let recorded = std ::fs ::read_to_string ( & record ) . map ( | text | text . lines ( ) . map ( str ::to_string ) . collect ( ) ) . unwrap_or_default ( ) ;
2026-10-22 17:37:08 +02:00
( output , recorded , root )
2026-10-12 06:43:48 +02:00
}
#[ cfg(unix) ]
#[ test ]
fn the_binary_hold_runs_systemctl_stop_on_exactly_the_units_it_starts ( ) {
2026-10-22 17:37:08 +02:00
let ( output , recorded , _root ) = hold_through_the_binary ( 0 ) ;
2026-10-12 06:43:48 +02:00
assert_eq! ( output . status . code ( ) , Some ( 0 ) , " {} " , String ::from_utf8_lossy ( & output . stderr ) ) ;
2026-11-11 11:37:34 +00:00
assert_eq! ( recorded , [ " stop " , " -- " , " mind-self@eureka.service " , " mind-persona@eureka.service " , " mind-stream@eureka.service " ] , " a stop that succeeds asks no state " ) ;
assert! ( String ::from_utf8_lossy ( & output . stdout ) . contains ( " units: stopped mind-self@eureka.service mind-persona@eureka.service mind-stream@eureka.service " ) ) ;
2026-10-12 06:43:48 +02:00
}
#[ cfg(unix) ]
#[ test ]
2026-10-22 17:37:08 +02:00
fn a_partial_stop_names_each_units_state_and_keeps_the_brake_held_with_exit_2 ( ) {
let ( output , recorded , root ) = hold_through_the_binary ( 5 ) ;
2026-10-12 06:43:48 +02:00
assert_eq! ( output . status . code ( ) , Some ( 2 ) ) ;
2026-10-22 17:37:08 +02:00
assert_eq! (
recorded ,
2026-11-11 11:37:34 +00:00
[ " stop " , " -- " , " mind-self@eureka.service " , " mind-persona@eureka.service " , " mind-stream@eureka.service " , " is-active " , " mind-self@eureka.service " , " is-active " , " mind-persona@eureka.service " , " is-active " , " mind-stream@eureka.service " ]
2026-10-22 17:37:08 +02:00
) ;
2026-10-12 06:43:48 +02:00
let stderr = String ::from_utf8_lossy ( & output . stderr ) ;
2026-10-22 17:37:08 +02:00
assert! ( stderr . contains ( " exit status 5 " ) & & stderr . contains ( " the brake is held " ) , " {stderr} " ) ;
2026-11-11 11:37:34 +00:00
assert! ( stderr . contains ( " mind-self@eureka.service is inactive " ) & & stderr . contains ( " mind-persona@eureka.service is active " ) & & stderr . contains ( " mind-stream@eureka.service is active " ) , " {stderr} " ) ;
2026-10-22 17:37:08 +02:00
assert! ( ! stderr . contains ( " were not stopped " ) , " {stderr} " ) ;
2026-10-12 06:43:48 +02:00
assert! ( ! String ::from_utf8_lossy ( & output . stdout ) . contains ( " stopped " ) ) ;
2026-10-22 17:37:08 +02:00
assert! ( shown ( root . path ( ) ) . contains ( " BrakeHeld " ) ) ;
2026-10-12 06:43:48 +02:00
}
2026-10-22 23:26:31 +02:00
// ---- the arguments every organ binary shares ----
mod shared_arguments {
use mind_body ::cli ::instance_and_endpoint ;
const USAGE : & str = " usage: test-organ --instance <slug> " ;
fn parse ( args : & [ & str ] , env : Option < & str > ) -> anyhow ::Result < ( eureka_pipeline ::Slug , std ::net ::SocketAddr ) > {
let args : Vec < String > = args . iter ( ) . map ( | a | a . to_string ( ) ) . collect ( ) ;
instance_and_endpoint ( & args , env . map ( str ::to_string ) , USAGE )
}
#[ test ]
fn the_flag_names_the_endpoint_and_wins_over_the_environment_which_is_the_fallback ( ) {
let ( instance , address ) = parse ( & [ " --instance " , " yggdrasil " , " --endpoint " , " rudp://10.77.0.1:17872 " ] , Some ( " rudp://127.0.0.1:1 " ) ) . unwrap ( ) ;
assert_eq! ( ( instance . 0. as_str ( ) , address . to_string ( ) . as_str ( ) ) , ( " yggdrasil " , " 10.77.0.1:17872 " ) ) ;
let ( _ , address ) = parse ( & [ " --endpoint " , " rudp://10.77.0.1:17872 " , " --instance " , " yggdrasil " ] , None ) . unwrap ( ) ;
assert_eq! ( address . to_string ( ) , " 10.77.0.1:17872 " , " flag order does not matter " ) ;
let ( _ , address ) = parse ( & [ " --instance " , " yggdrasil " ] , Some ( " rudp://127.0.0.1:9 " ) ) . unwrap ( ) ;
assert_eq! ( address . to_string ( ) , " 127.0.0.1:9 " ) ;
}
#[ test ]
fn every_refusal_is_fixed_text_that_echoes_no_value ( ) {
let canary = " canary-value-91e7 " ;
let cases : Vec < ( Vec < & str > , Option < & str > , & str ) > = vec! [
( vec! [ ] , Some ( " rudp://127.0.0.1:9 " ) , USAGE ) ,
( vec! [ " --instance " ] , None , USAGE ) ,
( vec! [ " --instance " , " a " , " --instance " , canary ] , Some ( " rudp://127.0.0.1:9 " ) , USAGE ) ,
( vec! [ " --instance " , " a " , " --endpoint " , " rudp://127.0.0.1:9 " , " --endpoint " , canary ] , None , USAGE ) ,
( vec! [ " --instance " , " a " , canary ] , Some ( " rudp://127.0.0.1:9 " ) , USAGE ) ,
( vec! [ " --endpoint " , " rudp://127.0.0.1:9 " ] , None , USAGE ) ,
( vec! [ " --instance " , " Canary/../x " ] , Some ( " rudp://127.0.0.1:9 " ) , " the instance is not a slug " ) ,
( vec! [ " --instance " , " a " ] , None , " no daemon endpoint: --endpoint or HUGINN_ENDPOINT " ) ,
( vec! [ " --instance " , " a " , " --endpoint " , canary ] , None , " the endpoint is not rudp://<ip>:<port> " ) ,
( vec! [ " --instance " , " a " , " --endpoint " , " http://127.0.0.1:9 " ] , None , " the endpoint is not rudp://<ip>:<port> " ) ,
( vec! [ " --instance " , " a " , " --endpoint " , " rudp://not-an-address " ] , None , " the endpoint is not rudp://<ip>:<port> " ) ,
( vec! [ " --instance " , " a " ] , Some ( canary ) , " the endpoint is not rudp://<ip>:<port> " ) ,
] ;
for ( args , env , message ) in cases {
let error = parse ( & args , env ) . unwrap_err ( ) ;
assert_eq! ( format! ( " {error:#} " ) , message , " {args:?} {env:?} " ) ;
}
}
}
2026-10-29 20:28:57 +01:00
fn swarm_show ( root : & std ::path ::Path ) -> String {
let ( result , out ) = go ( root , & [ " swarm " , " show " ] , " op " , 0 ) ;
result . unwrap ( ) ;
out
}
#[ test ]
fn swarm_verbs_round_trip ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let shown = swarm_show ( root . path ( ) ) ;
2026-10-31 05:34:07 +01:00
assert! ( shown . contains ( " brake: absent " ) & & shown . contains ( " BrakeAbsent " ) , " {shown} " ) ;
2026-10-29 20:28:57 +01:00
2026-10-31 10:05:52 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " release " ] , " alice " , 5 ) . 0. unwrap ( ) ;
2026-10-29 20:28:57 +01:00
let shown = swarm_show ( root . path ( ) ) ;
assert! ( shown . contains ( " brake: released " ) & & shown . contains ( " set_by alice " ) , " {shown} " ) ;
2026-10-31 05:34:07 +01:00
assert! ( shown . contains ( " Released " ) , " {shown} " ) ;
assert! ( ! shown . contains ( " dial " ) & & ! shown . contains ( " share " ) , " the swarm store holds a brake and nothing else: {shown} " ) ;
2026-10-29 20:28:57 +01:00
2026-10-31 05:34:07 +01:00
// The dollar-metering verbs are gone: they are refused, not read as something else.
for args in [ & [ " swarm " , " dial " , " set " , " --heat " , " 0 " , " --base-rate " , " 4 " , " --base-run " , " 2 " , " --k " , " 3 " ] [ .. ] , & [ " swarm " , " share " , " set " , " weksa " , " 2.5 " ] ] {
assert! ( go ( root . path ( ) , args , " alice " , 6 ) . 0. is_err ( ) , " {args:?} " ) ;
}
2026-10-29 20:28:57 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " hold " ] , " alice " , 7 ) . 0. unwrap ( ) ;
assert! ( swarm_show ( root . path ( ) ) . contains ( " BrakeHeld " ) ) ;
}
#[ test ]
2026-11-01 19:11:21 +01:00
fn swarm_verbs_write_only_the_swarm_file ( ) {
2026-10-29 20:28:57 +01:00
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
2026-11-01 19:11:21 +01:00
go ( root . path ( ) , & [ " swarm " , " brake " , " hold " ] , " op " , 0 ) . 0. unwrap ( ) ;
2026-10-29 20:28:57 +01:00
let names = listing ( root . path ( ) ) ;
assert! ( names . iter ( ) . all ( | name | name . starts_with ( " swarm.cc " ) ) , " {names:?} " ) ;
}
2026-11-01 19:11:21 +01:00
#[ test ]
fn swarm_brake_hold_stops_once_with_the_swarm_scope_and_the_swarm_store_already_held ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
go ( root . path ( ) , & [ " swarm " , " brake " , " release " ] , " op " , 0 ) . 0. unwrap ( ) ;
let mut seen_at_stop = String ::new ( ) ;
let ( result , out , calls ) = go_stopping ( root . path ( ) , & [ " swarm " , " brake " , " hold " ] , " alice " , 1 , | _ | {
seen_at_stop = swarm_show ( root . path ( ) ) ;
Ok ( ( ) )
} ) ;
result . unwrap ( ) ;
assert_eq! ( calls , [ None ] , " one stop, for the swarm " ) ;
assert! ( seen_at_stop . contains ( " BrakeHeld " ) , " the write comes before the stop: {seen_at_stop} " ) ;
2026-11-11 11:58:12 +00:00
assert! ( out . lines ( ) . any ( | line | line = = " units: stopped every mind-self@*.service mind-persona@*.service mind-stream@*.service " ) , " the line names exactly what stop_args stopped: {out} " ) ;
2026-11-01 19:11:21 +01:00
}
#[ test ]
fn a_failing_swarm_stopper_fails_the_hold_and_leaves_it_held ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
let ( result , out , calls ) =
go_stopping ( root . path ( ) , & [ " swarm " , " brake " , " hold " ] , " op " , 1 , | _ | Err ( anyhow ::anyhow! ( " systemctl stop failed with exit status 5 " ) ) ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
assert_eq! ( calls , [ None ] ) ;
assert! ( error . contains ( " the brake is held " ) & & error . contains ( " exit status 5 " ) , " {error} " ) ;
assert! ( ! out . contains ( " stopped " ) , " a failed stop printed success: {out} " ) ;
assert! ( swarm_show ( root . path ( ) ) . contains ( " BrakeHeld " ) ) ;
}
#[ test ]
fn a_failing_swarm_write_still_stops_and_the_hold_fails ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
std ::fs ::create_dir_all ( root . path ( ) . join ( " swarm.cc " ) ) . unwrap ( ) ;
let ( result , out , calls ) = go_stopping ( root . path ( ) , & [ " swarm " , " brake " , " hold " ] , " op " , 1 , | _ | Ok ( ( ) ) ) ;
let error = format! ( " {:#} " , result . unwrap_err ( ) ) ;
assert_eq! ( calls , [ None ] , " stopping does not wait on the write " ) ;
assert! ( error . contains ( " brake was not written " ) , " {error} " ) ;
assert! ( ! out . contains ( " stopped " ) , " {out} " ) ;
}
#[ test ]
fn swarm_show_and_release_never_call_the_stopper ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
for args in [ & [ " swarm " , " show " ] [ .. ] , & [ " swarm " , " brake " , " release " ] , & [ " swarm " , " brake " , " release " , " now " ] , & [ " swarm " , " brake " ] ] {
let ( _ , _ , calls ) = go_stopping ( root . path ( ) , args , " op " , 0 , | _ | Ok ( ( ) ) ) ;
assert! ( calls . is_empty ( ) , " {args:?} called the stopper " ) ;
}
}
2026-10-29 20:28:57 +01:00
#[ test ]
fn swarm_commands_refuse_bad_input_and_write_nothing ( ) {
let root = tempfile ::tempdir ( ) . unwrap ( ) ;
for args in [
& [ " swarm " ] [ .. ] ,
& [ " swarm " , " brake " , " release " , " now " ] ,
] {
assert! ( go ( root . path ( ) , args , " op " , 0 ) . 0. is_err ( ) , " {args:?} was accepted " ) ;
}
assert! ( listing ( root . path ( ) ) . is_empty ( ) , " a refused command wrote something " ) ;
}