use std::path::PathBuf;
use super::*;
fn home() -> PathBuf {
PathBuf::from("/Users/dev")
}
#[test]
fn system_layout_targets_the_administrator_daemon_directory() {
let l = layout_from(Scope::System, &home(), 501);
assert_eq!(
l.plist,
PathBuf::from("/Library/LaunchDaemons/com.lablup.all-smi.plist")
);
assert_eq!(l.log, PathBuf::from("/var/log/all-smi/all-smi.log"));
assert_eq!(l.domain, "system");
assert_eq!(l.target, "system/com.lablup.all-smi");
}
#[test]
fn user_layout_targets_the_gui_domain_of_the_invoking_uid() {
let l = layout_from(Scope::User, &home(), 501);
assert_eq!(
l.plist,
PathBuf::from("/Users/dev/Library/LaunchAgents/com.lablup.all-smi.plist")
);
assert_eq!(
l.log,
PathBuf::from("/Users/dev/Library/Logs/all-smi/all-smi.log")
);
assert_eq!(l.domain, "gui/501");
assert_eq!(l.target, "gui/501/com.lablup.all-smi");
}
#[test]
fn the_uid_is_not_hardcoded() {
let l = layout_from(Scope::User, &home(), 1234);
assert_eq!(l.domain, "gui/1234");
assert_eq!(l.target, "gui/1234/com.lablup.all-smi");
}
#[test]
fn the_bootstrap_domain_is_a_prefix_of_the_service_target() {
for scope in [Scope::System, Scope::User] {
let l = layout_from(scope, &home(), 501);
assert_eq!(l.target, format!("{}/{LABEL}", l.domain));
}
}
#[test]
fn both_scopes_share_the_label_and_the_file_name() {
let system = layout_from(Scope::System, &home(), 501);
let user = layout_from(Scope::User, &home(), 501);
assert_eq!(system.plist.file_name(), user.plist.file_name());
assert_eq!(
system.plist.file_name().unwrap().to_str().unwrap(),
PLIST_FILE_NAME
);
assert!(PLIST_FILE_NAME.starts_with(LABEL));
}
#[test]
fn a_user_layout_never_escapes_the_home_directory() {
let l = layout_from(Scope::User, &home(), 501);
assert!(l.plist.starts_with(home()));
assert!(l.log.starts_with(home()));
}
const PRINT_RUNNING: &str = r#"gui/501/com.lablup.all-smi = {
active count = 1
path = /Users/dev/Library/LaunchAgents/com.lablup.all-smi.plist
type = LaunchAgent
state = running
program = /opt/all-smi/bin/all-smi
default environment = {
PATH => /usr/bin:/bin:/usr/sbin:/sbin
}
domain = gui/501 [100019]
asid = 100019
minimum runtime = 10
exit timeout = 30
runs = 1
pid = 4242
immediate reason = speculative
forks = 0
execs = 1
last exit code = (never exited)
endpoints = {
"com.lablup.all-smi.peer" = {
port = 0x1234
active = 1
state = active
}
}
jetsam priority = 4
}
"#;
const PRINT_NOT_RUNNING: &str = r#"gui/501/com.lablup.all-smi = {
active count = 0
path = /Users/dev/Library/LaunchAgents/com.lablup.all-smi.plist
type = LaunchAgent
state = not running
program = /opt/all-smi/bin/all-smi
runs = 3
last exit code = 0
}
"#;
const PRINT_NOT_FOUND: &str = "Bad request.\nCould not find service \
\"com.lablup.all-smi\" in domain for user gui: 501\n";
#[test]
fn a_running_job_yields_its_state_and_pid() {
let info = parse_print_output(PRINT_RUNNING);
assert_eq!(info.state, "running");
assert_eq!(info.pid, Some(4242));
assert!(info.running());
}
#[test]
fn nested_dictionaries_do_not_shadow_the_jobs_own_state() {
assert!(PRINT_RUNNING.contains("state = active"));
assert_eq!(parse_print_output(PRINT_RUNNING).state, "running");
}
#[test]
fn a_loaded_but_stopped_job_is_not_running() {
let info = parse_print_output(PRINT_NOT_RUNNING);
assert_eq!(info.state, "not running");
assert_eq!(info.pid, None);
assert!(!info.running());
}
#[test]
fn neighbouring_keys_are_not_mistaken_for_pid_or_state() {
let info = parse_print_output(PRINT_RUNNING);
assert_eq!(info.pid, Some(4242));
assert_ne!(info.pid, Some(0));
assert_ne!(info.pid, Some(30));
assert_ne!(info.pid, Some(4));
}
#[test]
fn a_zero_pid_is_treated_as_absent() {
let info = parse_print_output("\tstate = not running\n\tpid = 0\n");
assert_eq!(info.pid, None);
}
#[test]
fn garbage_never_panics_and_never_claims_a_running_job() {
for raw in [
"",
PRINT_NOT_FOUND,
"= = =\n",
"state\npid\n",
"pid = abc\n",
] {
let info = parse_print_output(raw);
assert!(!info.running(), "must not claim running for: {raw:?}");
}
}
#[test]
fn only_the_exact_running_state_counts_as_running() {
for state in ["not running", "waiting", "spawn scheduled", ""] {
let info = parse_print_output(&format!("\tstate = {state}\n"));
assert!(!info.running(), "{state:?} must not count as running");
}
assert!(parse_print_output("\tstate = running\n").running());
}
const DISABLED_MODERN: &str = "\tdisabled services = {\n\
\t\t\"com.apple.Siri.agent\" => enabled\n\
\t\t\"com.lablup.all-smi\" => disabled\n\
\t\t\"com.apple.ScriptMenuApp\" => disabled\n\
\t}\n";
const ENABLED_MODERN: &str = "\tdisabled services = {\n\
\t\t\"com.lablup.all-smi\" => enabled\n\
\t}\n";
const DISABLED_LEGACY: &str = "\tdisabled services = {\n\
\t\t\"com.lablup.all-smi\" => true\n\
\t}\n";
#[test]
fn a_persistent_disable_override_is_detected() {
assert_eq!(parse_disabled(DISABLED_MODERN, LABEL), Some(true));
assert_eq!(parse_disabled(ENABLED_MODERN, LABEL), Some(false));
}
#[test]
fn the_legacy_boolean_spelling_is_still_understood() {
assert_eq!(parse_disabled(DISABLED_LEGACY, LABEL), Some(true));
assert_eq!(
parse_disabled("\t\t\"com.lablup.all-smi\" => false\n", LABEL),
Some(false)
);
}
#[test]
fn an_unlisted_label_is_not_an_override() {
assert_eq!(parse_disabled(DISABLED_MODERN, "com.lablup.other"), None);
assert_eq!(parse_disabled("", LABEL), None);
}
#[test]
fn a_label_that_is_a_prefix_of_ours_is_not_confused_for_it() {
let raw = "\t\t\"com.lablup.all-smi-exporter\" => disabled\n";
assert_eq!(parse_disabled(raw, LABEL), None);
}
#[test]
fn a_plist_on_disk_that_is_not_loaded_reads_as_not_loaded() {
assert_eq!(
not_loaded_detail(
true,
"Could not find service \"com.lablup.all-smi\" in domain"
),
"not loaded"
);
assert_eq!(
not_loaded_detail(false, "Could not find service"),
"not installed"
);
}
#[test]
fn an_unexpected_launchctl_failure_is_surfaced_rather_than_hidden() {
let detail = not_loaded_detail(true, "Operation not permitted\nsecond line");
assert!(detail.starts_with("loaded state unknown:"));
assert!(detail.contains("Operation not permitted"));
assert!(!detail.contains("second line"), "one line is enough");
}
#[test]
fn an_empty_failure_still_produces_a_usable_detail() {
assert_eq!(not_loaded_detail(true, ""), "not loaded");
}
#[test]
fn launchctl_invocations_are_described_verbatim_for_diagnostics() {
assert_eq!(
describe(&["bootstrap", "gui/501", "/Users/dev/x.plist"]),
"launchctl bootstrap gui/501 /Users/dev/x.plist"
);
assert_eq!(
describe(&["kickstart", "-k", "system/com.lablup.all-smi"]),
"launchctl kickstart -k system/com.lablup.all-smi"
);
}
#[test]
fn a_non_utf8_plist_path_is_refused_before_it_reaches_launchctl() {
#[cfg(unix)]
{
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
let bad = PathBuf::from(OsString::from_vec(vec![0x2f, 0xff]));
let err = path_arg(&bad).expect_err("launchctl arguments must be UTF-8");
assert!(matches!(err, ServiceError::Conflict(_)));
}
}