use std::path::Path;
use pretty_assertions::assert_eq;
use rho_protocol::{
methods::{SessionHooksReload, SessionReportRead},
Client, HookStatus, HooksReloadOutcome, SessionHooksReloadParams, SessionHooksReport,
SessionId, SessionReport, SessionReportData, SessionReportParams,
};
use super::sessions::{settled_job, Sandbox};
fn hooks_file(log: &Path, word: &str) -> String {
let script = format!("cat > /dev/null; echo {word} >> '{}'", log.display());
format!(
"version = 1\n\n[[hook]]\nid = \"notify\"\non = \"run_completed\"\ncommand = [\"/bin/sh\", \"-c\", {script:?}]\ntimeout = \"10s\"\n"
)
}
fn listed(report: &SessionHooksReport) -> Vec<(String, HookStatus, String)> {
report
.hooks
.iter()
.map(|hook| {
let script = hook.command.last().map(String::as_str).unwrap_or_default();
let word = script
.split_whitespace()
.skip_while(|token| *token != "echo")
.nth(1)
.unwrap_or_default();
(hook.id.clone(), hook.status, word.to_owned())
})
.collect()
}
async fn hooks_report(client: &Client, session: &SessionId) -> SessionHooksReport {
let report = client
.request::<SessionReportRead>(&SessionReportParams::new(
session.clone(),
SessionReport::Hooks,
))
.await
.expect("the hooks report reads")
.report;
match report {
SessionReportData::Hooks(report) => report,
report => panic!("not a hooks report: {report:?}"),
}
}
async fn reload(client: &Client, session: &SessionId) -> bool {
let outcome = client
.request::<SessionHooksReload>(&SessionHooksReloadParams::new(session.clone()))
.await
.expect("the reload is answered")
.outcome;
match outcome {
HooksReloadOutcome::Reloaded => true,
HooksReloadOutcome::Refused { .. } => false,
outcome => panic!("an outcome this test does not know: {outcome:?}"),
}
}
async fn prompt_and_wait(
sandbox: &Sandbox,
client: &Client,
session: &SessionId,
log: &Path,
runs: usize,
) -> Vec<String> {
sandbox.prompt(client, session).await.unwrap();
settled_job(sandbox, client, session).await;
sandbox
.poll_until("the run_completed hook to run", async || {
let words: Vec<String> = std::fs::read_to_string(log)
.unwrap_or_default()
.lines()
.map(str::to_owned)
.collect();
(words.len() >= runs).then_some(words)
})
.await
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_sessions_hooks_change_only_when_reloaded() {
let sandbox = Sandbox::new();
let home = sandbox.isolated.rho_home();
let file = home.join("hooks.toml");
let log = home.join("notified.log");
std::fs::write(&file, hooks_file(&log, "old")).unwrap();
let client = sandbox.start_daemon("fixed").await;
let session = sandbox.new_session(&client).await;
prompt_and_wait(&sandbox, &client, &session, &log, 1).await;
std::fs::write(&file, hooks_file(&log, "new")).unwrap();
let edited = listed(&hooks_report(&client, &session).await);
std::fs::write(&file, "version = 1\n\n[[hook]]\nid = \"notify\"\n").unwrap();
let invalid = hooks_report(&client, &session).await;
let invalid = (
invalid.invalid.as_ref().map(|file| file.path.clone()),
listed(&invalid),
);
let reloaded_invalid = reload(&client, &session).await;
let ran_unchanged = prompt_and_wait(&sandbox, &client, &session, &log, 2).await;
std::fs::write(&file, hooks_file(&log, "new")).unwrap();
let reloaded_valid = reload(&client, &session).await;
let after_reload = listed(&hooks_report(&client, &session).await);
let ran_reloaded = prompt_and_wait(&sandbox, &client, &session, &log, 3).await;
let hook = |status, word: &str| ("user:notify".to_owned(), status, word.to_owned());
assert_eq!(
(
edited,
invalid,
reloaded_invalid,
ran_unchanged,
reloaded_valid,
after_reload,
ran_reloaded,
),
(
vec![
hook(HookStatus::Pending, "new"),
hook(HookStatus::Stale, "old"),
],
(Some(file.clone()), vec![hook(HookStatus::Active, "old")]),
false,
vec!["old".to_owned(), "old".to_owned()],
true,
vec![hook(HookStatus::Active, "new")],
vec!["old".to_owned(), "old".to_owned(), "new".to_owned()],
),
"log:\n{}",
sandbox.isolated.daemon_log()
);
}