use crate::context::Context;
use crate::duty::autoswitch::Threshold;
use crate::error::{Error, Result};
use crate::permit::Permit;
use crate::schedule::{self, Job, Location};
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Switching {
At(Threshold),
Never,
}
impl Switching {
pub fn at(self) -> Option<Threshold> {
match self {
Switching::At(at) => Some(at),
Switching::Never => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Installed {
Yes {
location: Location,
switching: Option<Switching>,
},
No,
Unsupported,
}
pub(crate) fn status(ctx: &Context) -> Installed {
match schedule::scheduler(ctx) {
None => Installed::Unsupported,
Some(s) if s.installed(ctx, Job::Watch) => Installed::Yes {
location: s.location(ctx, Job::Watch),
switching: switching(&s.options(ctx, Job::Watch)),
},
Some(_) => Installed::No,
}
}
pub(crate) fn installed_program(ctx: &Context) -> Option<PathBuf> {
schedule::scheduler(ctx)?.program(ctx, Job::Watch)
}
pub(crate) fn task(ctx: &Context) -> Option<crate::host::task::Task> {
schedule::task(ctx, Job::Watch)
}
pub(crate) fn install(ctx: &Context, permit: Permit, switching: Switching) -> Result<Location> {
if !schedule::serves(ctx) {
return Err(Error::ScheduleNotDefaultHome {
home: crate::home::dir(ctx),
default: ctx.default_pitboard_home(),
job: Job::Watch,
});
}
let _quiet = crate::switch::exclusive(ctx, permit)?;
let location = schedule::put(
ctx,
permit,
Job::Watch,
&schedule::program(ctx, Job::Watch)?,
&options(switching),
&schedule::environment(ctx, schedule::proxy_variables(ctx)),
)?;
crate::audit::record(ctx, permit, VERB, "install", "ok");
Ok(location)
}
pub(crate) fn uninstall(ctx: &Context, permit: Permit) -> Result<bool> {
let watched = ctx.clone().with_pitboard_home(ctx.default_pitboard_home());
let _quiet = match schedule::scheduler(ctx) {
Some(s) if s.installed(ctx, Job::Watch) => {
Some(crate::switch::exclusive(&watched, permit)?)
}
_ => None,
};
taken_away(ctx, permit)
}
pub(crate) fn taken_away(ctx: &Context, permit: Permit) -> Result<bool> {
if !schedule::remove(ctx, permit, Job::Watch)? {
return Ok(false);
}
crate::audit::record(ctx, permit, VERB, "uninstall", "ok");
Ok(true)
}
const VERB: &str = "watch-service";
fn options(switching: Switching) -> Vec<String> {
match switching {
Switching::At(at) => vec!["--at".to_string(), at.percent().to_string()],
Switching::Never => vec!["--no-switch".to_string()],
}
}
fn switching(options: &[String]) -> Option<Switching> {
match options {
[at, percent] if at == "--at" => percent
.parse()
.ok()
.and_then(Threshold::new)
.map(Switching::At),
[never] if never == "--no-switch" => Some(Switching::Never),
_ => None,
}
}
#[cfg(test)]
#[allow(
clippy::disallowed_methods,
reason = "a test sets up its scratch files as a person or a tool would"
)]
mod tests {
use super::*;
use crate::host::memory::MemoryHost;
use std::sync::Arc;
struct ServiceHome(tempfile::TempDir, Arc<MemoryHost>);
impl ServiceHome {
fn new() -> ServiceHome {
ServiceHome::on(MemoryHost::new())
}
fn scheduling_tasks() -> ServiceHome {
ServiceHome::on(MemoryHost::scheduling_tasks(SID))
}
fn on(host: Arc<MemoryHost>) -> ServiceHome {
let machine = ServiceHome(tempfile::tempdir().expect("a scratch home"), host);
std::fs::create_dir_all(machine.program().parent().expect("its folder")).expect("made");
std::fs::write(machine.program(), "").expect("a program");
std::fs::create_dir_all(machine.ctx(&[]).default_pitboard_home())
.expect("a Pitboard home");
machine
}
fn program(&self) -> PathBuf {
self.0.path().join("bin").join("pitboard")
}
fn ctx(&self, vars: &[(&str, &str)]) -> Context {
let home = self.0.path().to_string_lossy().into_owned();
let env: crate::context::Environment =
[("HOME", home.as_str()), ("USERPROFILE", home.as_str())]
.iter()
.chain(vars)
.copied()
.collect();
Context::for_command_line(&env)
.with_memory_stores(Arc::clone(&self.1))
.with_schedule_program(self.program())
}
fn written(&self, ctx: &Context) -> String {
std::fs::read_to_string(schedule::job_path(ctx, Job::Watch).expect("a scheduler"))
.expect("the job's file")
}
}
const SID: &str = "S-1-5-21-1004-1005-1006-1001";
fn at(percent: u8) -> Switching {
Switching::At(Threshold::new(percent).expect("a share there can be"))
}
#[test]
fn the_service_runs_watch_at_its_share_until_it_is_taken_away() {
for machine in [ServiceHome::new(), ServiceHome::scheduling_tasks()] {
let ctx = machine.ctx(&[]);
assert_eq!(status(&ctx), Installed::No);
let location = install(&ctx, Permit::for_a_test(), at(90)).expect("installed");
assert_eq!(
status(&ctx),
Installed::Yes {
location: location.clone(),
switching: Some(at(90))
}
);
assert_eq!(installed_program(&ctx), Some(machine.program()));
let written = machine.written(&ctx);
assert!(
written.contains("watch --scheduled --at 90")
|| written.contains(
"<string>watch</string>\n <string>--scheduled</string>\n \
<string>--at</string>\n <string>90</string>"
),
"{written}"
);
assert_eq!(
schedule::status(&ctx),
schedule::Installed::No,
"the renewal schedule is a job of its own"
);
install(&ctx, Permit::for_a_test(), at(80)).expect("installed again");
assert_eq!(
status(&ctx),
Installed::Yes {
location,
switching: Some(at(80))
}
);
schedule::install(&ctx, Permit::for_a_test()).expect("renewal too");
assert!(uninstall(&ctx, Permit::for_a_test()).expect("taken away"));
assert_eq!(status(&ctx), Installed::No);
assert!(!uninstall(&ctx, Permit::for_a_test()).expect("nothing there"));
assert!(
matches!(schedule::status(&ctx), schedule::Installed::Yes { .. }),
"renewal stays"
);
let logged: Vec<(String, String)> = crate::audit::read(&ctx, 10)
.into_iter()
.filter(|entry| entry.verb == VERB)
.map(|entry| (entry.subject, entry.outcome))
.collect();
assert_eq!(
logged,
[("install", "ok"), ("install", "ok"), ("uninstall", "ok")]
.map(|(subject, outcome)| (subject.to_string(), outcome.to_string()))
);
}
}
#[test]
fn the_service_is_installed_only_where_pitboard_home_is_the_default_home() {
let machine = ServiceHome::new();
let ctx = machine.ctx(&[]);
let default = ctx.default_pitboard_home();
let elsewhere = machine.0.path().join("elsewhere");
let another = ctx.clone().with_pitboard_home(elsewhere.clone());
let refused = install(&another, Permit::for_a_test(), at(95)).expect_err("refused");
assert_eq!(refused.code(), "schedule_not_default_home");
assert_eq!(
refused.to_string(),
format!(
"the watch service watches only the accounts in {}, not those in {}, which \
PITBOARD_HOME names. Nothing was installed. To install it for {}, unset \
PITBOARD_HOME and install it again.",
default.display(),
elsewhere.display(),
default.display()
)
);
assert_eq!(status(&ctx), Installed::No, "nothing was written");
install(&ctx, Permit::for_a_test(), at(95)).expect("installed");
assert!(uninstall(&another, Permit::for_a_test()).expect("taken away from elsewhere"));
assert_eq!(status(&ctx), Installed::No);
}
#[test]
fn a_run_of_the_service_watches_the_default_home_with_the_proxy_it_was_given() {
let proxied = [("HTTPS_PROXY", "http://alice:s3cret@proxy.example.com:3128")];
for machine in [ServiceHome::new(), ServiceHome::scheduling_tasks()] {
let ctx = machine.ctx(&proxied);
install(&ctx, Permit::for_a_test(), at(95)).expect("installed");
let handed = schedule::scheduler(&ctx)
.expect("a scheduler")
.environment(&ctx, Job::Watch);
assert_eq!(
handed,
[(
"HTTPS_PROXY".to_string(),
"http://alice:s3cret@proxy.example.com:3128".to_string()
)]
);
let tasks = matches!(
schedule::job_path(&ctx, Job::Watch),
Some(path) if path.starts_with(machine.0.path().join("Tasks"))
);
if tasks {
let definition = machine.written(&ctx);
assert!(!definition.contains("alice"), "{definition}");
}
let started = machine
.ctx(&[])
.with_pitboard_home(machine.0.path().join("elsewhere"))
.started_by_the_watch_service();
assert_eq!(started.pitboard_home, ctx.default_pitboard_home());
if tasks {
assert!(
started.proxy.same_way(&ctx.proxy),
"the file's, over its own"
);
}
let by_hand = machine.ctx(&[]);
assert_eq!(
schedule::for_its_run(&by_hand, Job::Watch).pitboard_home,
by_hand.pitboard_home,
"a run by hand watches its own"
);
}
}
#[test]
fn a_service_the_system_will_not_start_leaves_the_one_before() {
for machine in [ServiceHome::new(), ServiceHome::scheduling_tasks()] {
let ctx = machine.ctx(&[]);
machine.1.refuse_next_start();
let refused = install(&ctx, Permit::for_a_test(), at(95)).expect_err("refused");
assert_eq!(refused.code(), "schedule_refused");
assert_eq!(status(&ctx), Installed::No);
let location = install(&ctx, Permit::for_a_test(), at(95)).expect("installed");
machine.1.refuse_next_start();
install(&ctx, Permit::for_a_test(), at(70)).expect_err("refused");
assert_eq!(
status(&ctx),
Installed::Yes {
location,
switching: Some(at(95))
}
);
}
}
#[test]
fn the_services_run_is_ended_only_between_changes() {
for machine in [ServiceHome::new(), ServiceHome::scheduling_tasks()] {
let ctx = machine.ctx(&[]);
install(&ctx, Permit::for_a_test(), at(90)).expect("installed");
for (reinstalling, before) in [(true, 90), (false, 80)] {
let changing =
crate::switch::exclusive(&ctx, Permit::for_a_test()).expect("a change");
let asked = ctx.clone();
let (entered, waits) = std::sync::mpsc::channel();
let waiting = std::thread::spawn(move || {
let told = move || entered.send(()).expect("the test waits");
crate::fault::meanwhile("switch.lock_busy", told, || {
if reinstalling {
install(&asked, Permit::for_a_test(), at(80)).map(|_| true)
} else {
uninstall(&asked, Permit::for_a_test())
}
})
});
waits.recv().expect("it waits for the change");
assert!(matches!(
status(&ctx),
Installed::Yes { switching, .. } if switching == Some(at(before))
));
drop(changing);
assert!(waiting.join().expect("it ends").expect("done"));
}
assert_eq!(status(&ctx), Installed::No);
}
}
#[test]
fn a_machine_with_no_scheduler_says_to_leave_watch_running() {
let machine = ServiceHome::new();
machine.1.without_a_scheduler();
let ctx = machine.ctx(&[]);
assert_eq!(status(&ctx), Installed::Unsupported);
let refused = install(&ctx, Permit::for_a_test(), at(95)).expect_err("nowhere");
assert_eq!(refused.code(), "schedule_unsupported");
assert!(
refused
.to_string()
.contains("leaving `pitboard watch` running"),
"{refused}"
);
assert_eq!(
uninstall(&ctx, Permit::for_a_test())
.expect_err("nowhere")
.code(),
"schedule_unsupported"
);
}
#[test]
fn a_service_that_never_switches_runs_watch_with_no_switch_and_reads_back_so() {
for machine in [ServiceHome::new(), ServiceHome::scheduling_tasks()] {
let ctx = machine.ctx(&[]);
let location =
install(&ctx, Permit::for_a_test(), Switching::Never).expect("installed");
let installed = |switching| Installed::Yes {
location: location.clone(),
switching: Some(switching),
};
assert_eq!(status(&ctx), installed(Switching::Never));
let written = machine.written(&ctx);
assert!(
written.contains("watch --scheduled --no-switch")
|| written.contains(
"<string>watch</string>\n <string>--scheduled</string>\n \
<string>--no-switch</string>"
),
"{written}"
);
assert!(!written.contains("--at"), "{written}");
install(&ctx, Permit::for_a_test(), at(90)).expect("switching");
assert_eq!(
status(&ctx),
installed(at(90)),
"as an earlier Pitboard wrote it"
);
install(&ctx, Permit::for_a_test(), Switching::Never).expect("not switching");
assert_eq!(status(&ctx), installed(Switching::Never));
}
}
#[test]
fn only_what_pitboard_writes_is_read() {
let given = |words: &[&str]| words.iter().map(|o| (*o).to_string()).collect::<Vec<_>>();
assert_eq!(switching(&given(&["--at", "90"])), Some(at(90)));
assert_eq!(switching(&given(&["--no-switch"])), Some(Switching::Never));
for written in [at(90), Switching::Never] {
assert_eq!(switching(&options(written)), Some(written), "{written:?}");
}
for unread in [
&["--at", "100"][..],
&["--at", "ninety"],
&["--at"],
&[],
&["--at", "90", "--once"],
&["--no-switch", "--at", "90"],
&["--at", "90", "--no-switch"],
&["--no-switch", "--once"],
&["--no-switch=true"],
] {
assert_eq!(switching(&given(unread)), None, "{unread:?}");
}
}
}