1 // Copyright 2020-2021 Ian Jackson and contributors to Otter
2 // SPDX-License-Identifier: AGPL-3.0-or-later
3 // There is NO WARRANTY.
5 //! Otter game system (part thereeof)
7 //! <https://www.chiark.greenend.org.uk/~ianmdlvl/otter/docs/README.html>
9 //! This crate is intended for use only by other parts of Otter.
11 // ==================== namespace preparation ====================
15 pub use otter::crates::*;
21 pub use otter::prelude::*;
23 pub use std::cell::{RefCell, RefMut};
25 pub use num_traits::NumCast;
26 pub use serde_json::json;
27 pub use structopt::StructOpt;
30 pub type MgmtChannel = ClientMgmtChannel;
32 pub type JsV = serde_json::Value;
33 pub type MC = MgmtCommand;
36 use otter::prelude::define_index_type;
37 define_index_type!{ pub struct PIA = usize; }
38 define_index_type!{ pub struct PIB = usize; }
42 // -------------------- private crates ----------
44 use otter_support::config::DAEMON_STARTUP_REPORT;
46 // ==================== public constants ====================
48 pub const TABLE: &str = "server::dummy";
49 pub const CONFIG: &str = "server-config.toml";
51 pub const URL: &str = "http://localhost:8000";
53 #[derive(Copy,Clone,Debug,Eq,PartialEq,Ord,PartialOrd)]
54 #[derive(FromPrimitive,EnumIter,IntoStaticStr,EnumProperty)]
55 #[strum(serialize_all = "snake_case")]
57 #[strum(props(Token="kmqAKPwK4TfReFjMor8MJhdRPBcwIBpe"))] Alice,
58 #[strum(props(Token="ccg9kzoTh758QrVE1xMY7BQWB36dNJTx"))] Bob,
61 // ==================== principal public structs ====================
63 #[derive(Debug,Clone)]
66 #[structopt(long="--as-if")]
67 pub as_if: Option<String>,
69 #[structopt(long="--no-bwrap")]
72 #[structopt(long="--tmp-dir", default_value="tmp")]
75 #[structopt(long="--pause", default_value="0ms")]
76 pub pause: humantime::Duration,
79 pub tests: WantedTestsOpt,
81 #[structopt(long="--test")]
82 test_name: Option<String>,
86 pub struct SetupCore {
88 pub mgmt_conn: RefCell<MgmtChannelForGame>,
89 pub server_child: Child,
90 pub wanted_tests: TrackWantedTests,
91 pub cln: cleanup_notify::Handle,
92 pub initial_pieces_cache: Option<Vec<MgmtGamePieceInfo>>,
95 #[derive(Clone,Debug)]
99 pub start_dir: String,
103 pub struct Instance(pub InstanceName);
105 // ==================== Facilities for tests ====================
107 impl AsRef<Opts> for Opts { fn as_ref(&self) -> &Opts { self } }
111 impl<'e, E:Into<Box<dyn Error + 'e>>> From<E> for Explode {
112 fn from(e: E) -> Explode {
113 let mut m = "exploding on error".to_string();
114 let e: Box<dyn Error> = e.into();
115 let mut e: Option<&dyn Error> = Some(&*e);
116 while let Some(te) = e {
117 m += &format!(": {}", &te);
123 impl From<Explode> for anyhow::Error {
124 fn from(e: Explode) -> AE { match e { } }
126 #[ext(pub, name=ResultExplodeExt)]
127 impl<T> Result<T,Explode> {
128 fn y(self) -> T { match self { Ok(y) => y, Err(n) => match n { } } }
129 fn did(self, msg: &'static str) -> anyhow::Result<T> {
130 ResultGenDidExt::<_,AE>::did(Ok(self.y()), msg)
135 impl<E:Error> From<Explode> for E {
136 fn from(e: Explode) -> E { match e { } }
141 fn set<K: Into<String>>(&mut self, k: K, v: &JsV) {
142 self.as_object_mut().unwrap().insert(k.into(), v.clone());
145 fn extend<I,K,V>(&mut self, i: I)
146 where I: IntoIterator<Item=(K, V)>,
150 let i = i.into_iter().map(|(k,v)| (k.into(), v.borrow().clone()));
151 self.as_object_mut().unwrap().extend(i);
155 fn tree_walk<F,E>(&self, #[allow(unused_mut,unused_variables)] mut f: F)
156 where F: FnMut(&[String], &JsV) -> Result<(),E>
159 fn recurse<F,E>(kl: &mut Vec<String>, v: &JsV, f: &mut F)
160 where F: FnMut(&[String], &JsV) -> Result<(),E> {
162 if let Some(o) = v.as_object() {
164 kl.push(k.to_owned());
165 let y = recurse(kl, v, f);
169 } else if let Some(a) = v.as_array() {
170 for (k,v) in a.iter().enumerate() {
171 kl.push(k.to_string());
172 let y = recurse(kl, v, f);
180 recurse(&mut kl, self, &mut f)?
184 // -------------------- Substition --------------------
186 pub trait Substitutor {
187 fn get(&self, kw: &str) -> Option<String>;
189 fn also<L: Into<Subst>>(&self, xl: L) -> ExtendedSubst<Self, Subst>
190 where Self: Clone + Sized {
191 ExtendedSubst(self.clone(), xl.into())
195 fn subst(&self, s: &str) -> String {
196 let re = Regex::new(r"@(\w+)@").expect("bad re!");
197 let mut errs = vec![];
198 let out = re.replace_all(s, |caps: ®ex::Captures| {
199 let kw = caps.get(1).expect("$1 missing!").as_str();
200 if kw == "" { return "".to_owned() }
201 let v = self.get(kw);
203 errs.push(kw.to_owned());
207 if ! errs.is_empty() {
208 throw!(anyhow!("bad substitution(s) {:?} in {:?}",
215 fn ss(&self, s: &str) -> Vec<String> {
219 .filter(|s| !s.is_empty())
225 fn gss(&self, s: &str) -> Vec<String> {
226 self.ss(&format!("-g @table@ {}", s))?
230 #[derive(Clone,Debug)]
231 pub struct Subst(HashMap<String,String>);
233 impl Substitutor for Subst {
234 fn get(&self, kw: &str) -> Option<String> {
235 self.0.get(kw).map(String::clone)
242 L: IntoIterator<Item=&'i (T, U)>>
245 fn from(l: L) -> Subst {
246 let map = l.into_iter()
247 .map(|(k,v)| (k.as_ref().to_owned(), v.as_ref().to_owned())).collect();
252 #[derive(Clone,Debug)]
253 pub struct ExtendedSubst<B: Substitutor, X: Substitutor>(B, X);
255 impl<B:Substitutor, X:Substitutor> Substitutor for ExtendedSubst<B, X> {
256 fn get(&self, kw: &str) -> Option<String> {
257 self.1.get(kw).or_else(|| self.0.get(kw))
261 impl Substitutor for DirSubst {
262 fn get(&self, kw: &str) -> Option<String> {
264 "url" => URL.to_owned(),
265 "src" => self.src.clone(),
266 "build" => self.start_dir.clone(),
267 "abstmp" => self.abstmp.clone(),
268 "target" => format!("{}/target", &self.start_dir),
269 "specs" => self.specs_dir(),
270 "table" => TABLE.to_owned(),
271 "command_socket" => "command.socket".to_owned(),
272 "examples" => format!("{}/examples", &self.src),
278 // ---------- requested/available test tracking ----------
280 #[derive(Clone,Debug)]
282 pub struct WantedTestsOpt {
287 pub struct TrackWantedTests {
288 wanted: WantedTestsOpt,
289 found: BTreeSet<String>,
292 impl WantedTestsOpt {
293 pub fn track(&self) -> TrackWantedTests {
294 TrackWantedTests { wanted: self.clone(), found: default() }
298 impl TrackWantedTests {
299 pub fn wantp(&mut self, tname: &str) -> bool {
300 self.found.insert(tname.to_owned());
302 self.wanted.tests.is_empty() ||
303 self.wanted.tests.iter().any(|s| s==tname);
308 impl Drop for TrackWantedTests {
310 let missing_tests = self.wanted.tests.iter().cloned()
311 .filter(|s| !self.found.contains(s))
312 .collect::<Vec<_>>();
314 if !missing_tests.is_empty() && !self.found.is_empty() {
315 for f in &self.found {
316 eprintln!("fyi: test that exists: {}", f);
318 for m in &missing_tests {
319 eprintln!("warning: unknown test requested: {}", m);
326 macro_rules! usual_wanted_tests {
327 ($ctx:ty, $su:ident) => {
329 fn wanted_tests(&mut self) -> &mut TrackWantedTests {
330 &mut self.su.wanted_tests
338 ($c:expr, $tname:expr, $s:stmt) => {
339 if $c.wanted_tests().wantp($tname) {
340 debug!("==================== {} starting ====================", $tname);
342 info!("==================== {} completed ====================", $tname);
344 trace!("= = = {} skipped = = =", $tname);
349 // -------------------- Extra anyhow result handling --------------------
351 pub trait PropagateDid {
352 fn propagate_did<T>(self, msg: &'static str) -> anyhow::Result<T>;
355 #[ext(pub, name=ResultGenDidExt)]
356 impl<T,E> Result<T,E> where Result<T,E>: anyhow::Context<T,E> {
357 fn did(self, msg: &'static str) -> anyhow::Result<T>
360 Ok(y) => { info!("did {}.", msg); Ok(y) }
361 n@ Err(_) => n.context(msg),
367 impl<T,E> Result<T,E> {
368 fn just_warn(self) -> Option<T>
381 // -------------------- cleanup_notify (signaling) --------------------
383 pub mod cleanup_notify {
384 use super::crates::*;
385 use otter_support::crates::*;
387 pub use super::Void; // TODO remove the need for this
390 use fehler::{throw, throws};
392 use nix::{unistd::*, fcntl::OFlag};
393 use nix::sys::signal::*;
394 use nix::Error as NE;
396 use std::os::unix::io::RawFd;
397 use std::panic::catch_unwind;
398 use std::process::Command;
401 pub struct Handle(RawFd);
404 fn mkpipe() -> (RawFd,RawFd) {
405 pipe2(OFlag::O_CLOEXEC).map_err(nix2io)?
409 fn read_await(fd: RawFd) {
411 let mut buf = [0u8; 1];
412 match nix::unistd::read(fd, &mut buf) {
414 Ok(_) => throw!(io::Error::from_raw_os_error(libc::EINVAL)),
415 Err(NE::EINTR) => continue,
416 _ => throw!(io::Error::last_os_error()),
421 fn nix2io(_n: nix::Error) -> io::Error {
422 io::Error::last_os_error()
427 pub fn new() -> Self {
428 let (reading_end, _writing_end) = mkpipe()
429 .context("create cleanup notify pipe")?;
430 // we leak the writing end, keeping it open only in this process
435 pub fn arm_hook(&self, cmd: &mut Command) { unsafe {
436 use std::os::unix::process::CommandExt;
438 let notify_writing_end = self.0;
439 let all_signals = nix::sys::signal::SigSet::all();
441 cmd.pre_exec(move || -> Result<(), io::Error> {
442 let semidaemon = nix::unistd::getpid();
443 let (reading_end, writing_end) = mkpipe()?;
445 match fork().map_err(nix2io)? {
446 ForkResult::Child => {
447 let _ = catch_unwind(move || -> Void {
449 SigmaskHow::SIG_BLOCK,
454 let _ = close(writing_end);
455 let _ = nix::unistd::dup2(2, 1);
458 if fd == notify_writing_end { continue }
460 if fd > writing_end && matches!(r, Err(NE::EBADF)) {
464 let _ = read_await(notify_writing_end);
465 let _ = kill(semidaemon, SIGTERM);
466 let _ = kill(semidaemon, SIGCONT);
469 let _ = raise(SIGABRT);
472 ForkResult::Parent{..} => {
474 close(writing_end).map_err(nix2io)?;
475 read_await(reading_end)?;
485 // -------------------- generalised daemon startup --------------------
488 pub fn fork_something_which_prints(mut cmd: Command,
489 cln: &cleanup_notify::Handle,
494 cmd.stdout(Stdio::piped());
495 cln.arm_hook(&mut cmd)?;
496 let mut child = cmd.spawn().context("spawn")?;
497 let mut report = BufReader::new(child.stdout.take().unwrap())
500 let l = report.next();
502 let s = child.try_wait().context("check on spawned child")?;
504 throw!(anyhow!("failed to start: wait status = {}", &e));
509 None => throw!(anyhow!("EOF (but it's still running?")),
510 Some(Err(e)) => throw!(AE::from(e).context("failed to read")),
513 let what = what.to_owned();
514 thread::spawn(move|| (||{
516 let l: Result<String, io::Error> = l;
517 let l = l.context("reading further output")?;
518 const MAXLEN: usize = 300;
519 if l.len() <= MAXLEN {
520 println!("{} {}", what, l);
522 println!("{} {}...", what, &l[..MAXLEN-3]);
526 })().context(what).just_warn()
529 Ok::<_,AE>((l, child))
530 })().with_context(|| what.to_owned())?
533 // ==================== principal actual setup code ====================
535 pub type EarlyArgPredicate<'f> = &'f mut dyn FnMut(&OsStr) -> bool;
538 pub fn reinvoke_via_bwrap(_opts: &Opts, current_exe: &str,
539 early: EarlyArgPredicate<'_>) -> Void {
540 debug!("running bwrap");
542 let mut bcmd = Command::new("bwrap");
544 .args("--unshare-net \
547 --die-with-parent".split(' '))
550 let (early, late) = {
551 let mut still_early = true;
552 env::args_os().skip(1)
553 .partition::<Vec<_>,_>(|s| {
554 still_early &= early(s);
559 bcmd.arg("--no-bwrap");
562 std::io::stdout().flush().context("flush stdout")?;
563 let e: AE = bcmd.exec().into();
564 throw!(e.context("exec bwrap"));
568 pub fn prepare_tmpdir<'x>(opts: &'x Opts, mut current_exe: &'x str) -> DirSubst {
570 fn getcwd() -> String {
574 .ok_or_else(|| anyhow!("path is not UTF-8"))?
578 if let Some(as_if) = &opts.as_if {
580 } else if let Some(test_name) = &opts.test_name {
581 current_exe = test_name;
584 let start_dir = getcwd()
585 .context("canonicalise our invocation directory (getcwd)")?;
588 match fs::metadata(&opts.tmp_dir) {
591 throw!(anyhow!("existing object is not a directory"));
593 if (m.st_mode() & 0o01002) != 0 {
595 "existing directory mode {:#o} is sticky or world-writeable. \
596 We use predictable pathnames so that would be a tmp race",
601 Err(e) if e.kind() == ErrorKind::NotFound => {
602 fs::create_dir(&opts.tmp_dir)
606 let e: AE = e.into();
607 throw!(e.context("stat existing directory"))
611 env::set_current_dir(&opts.tmp_dir)
612 .context("chdir into it")?;
616 .with_context(|| opts.tmp_dir.to_owned())
617 .context("prepare/create tmp-dir")?;
619 let leaf = current_exe.rsplitn(2, '/').next().unwrap();
620 let our_tmpdir = format!("{}/{}", &opts.tmp_dir, &leaf);
622 match fs::remove_dir_all(&leaf) {
624 Err(e) if e.kind() == ErrorKind::NotFound => {},
625 Err(e) => throw!(AE::from(e).context("remove previous directory")),
628 fs::DirBuilder::new().create(&leaf)
629 .context("create fresh subdirectory")?;
631 env::set_current_dir(&leaf)
632 .context("chdir into it")?;
636 .with_context(|| our_tmpdir.to_owned())
637 .context("prepare/create our tmp subdir")?;
640 getcwd().context("canonicalise our tmp subdir (getcwd)")?;
642 env::set_var("HOME", &abstmp);
643 env::set_var("TMPDIR", &abstmp);
644 env::set_var("OTTER_APITEST_START_DIR", &start_dir);
645 for v in "http_proxy https_proxy XAUTHORITY CDPATH \
646 SSH_AGENT_PID SSH_AUTH_SOCK WINDOWID WWW_HOME".split(' ')
651 let manifest_var = "CARGO_MANIFEST_DIR";
652 let src: String = (|| Ok::<_,AE>(match env::var(manifest_var) {
654 Err(env::VarError::NotPresent) => start_dir.clone(),
655 e@ Err(_) => throw!(e.context(manifest_var).err().unwrap()),
657 .context("find source code")?;
668 pub fn prepare_gameserver(cln: &cleanup_notify::Handle, ds: &DirSubst)
669 -> (MgmtChannelForGame, Child) {
670 let config = ds.subst(r##"
671 change_directory = "@abstmp@"
676 command_socket = "@command_socket@"
677 template_dir = "@src@/templates"
678 specs_dir = "@src@/specs"
679 nwtemplate_dir = "@src@/nwtemplates"
680 bundled_sources = "@target@/bundled-sources"
681 wasm_dir = "@target@/packed-wasm"
682 shapelibs = [ "@src@/library/*.toml" ]
683 libexec_dir = "@target@/debug"
684 usvg_bin = "@target@/release/usvg"
686 authorized_keys = "@abstmp@/authorized_keys"
687 ssh_proxy_command = "@target@/debug/otter-ssh-proxy --config @abstmp@/server-config.toml"
689 debug_js_inject_file = "@src@/templates/test-inject.js"
690 check_bundled_sources = false # For testing only! see LICENCE!
696 global_level = 'debug'
700 'hyper::server' = 'info'
701 "game::debugreader" = 'info'
702 "otter::updates" = 'trace'
703 "otter::hidden" = 'trace'
706 fs::write(CONFIG, &config)
707 .context(CONFIG).context("create server config")?;
709 start_gameserver(cln, ds)?
713 fn start_gameserver(cln: &cleanup_notify::Handle, ds: &DirSubst)
714 -> (MgmtChannelForGame, Child) {
715 let server_exe = ds.subst("@target@/debug/daemon-otter")?;
716 let mut cmd = Command::new(&server_exe);
718 .arg("--report-startup")
722 let (l,child) = fork_something_which_prints(cmd, cln, &server_exe)?;
723 if l != DAEMON_STARTUP_REPORT {
724 throw!(anyhow!("otter-daemon startup report {:?}, expected {:?}",
725 &l, DAEMON_STARTUP_REPORT));
729 .context("game server")?;
731 let mut mgmt_conn = MgmtChannel::connect(
732 &ds.subst("@command_socket@")?
735 mgmt_conn.cmd(&MgmtCommand::SetSuperuser(true))?;
736 mgmt_conn.cmd(&MgmtCommand::SelectAccount("server:".parse()?))?;
738 let mgmt_conn = mgmt_conn.for_game(
740 MgmtGameUpdateMode::Online
748 pub fn restart_gameserver(&mut self) {
749 let (mgmt_conn, child) = start_gameserver(&self.cln, &self.ds)?;
750 self.mgmt_conn = RefCell::new(mgmt_conn);
751 self.server_child = child;
755 // ---------- game spec ----------
757 #[derive(Copy,Clone,Error,Debug)]
758 #[error("wait status: {0}")]
759 pub struct ExitStatusError(pub std::process::ExitStatus);
762 pub struct OtterOutput {
763 output: Option<NamedTempFile>,
765 impl Deref for OtterOutput {
766 type Target = fs::File;
767 fn deref(&self) -> &fs::File { self.output.as_ref().unwrap().as_file() }
769 impl DerefMut for OtterOutput {
770 fn deref_mut(&mut self) -> &mut fs::File {
771 self.output.as_mut().unwrap().as_file_mut()
774 impl From<OtterOutput> for String {
775 fn from(mut oo: OtterOutput) -> String {
776 let mut s = String::new();
777 let mut o = oo.output.take().unwrap();
779 o.read_to_string(&mut s).unwrap();
783 impl From<&mut OtterOutput> for String {
784 fn from(oo: &mut OtterOutput) -> String {
785 let mut s = String::new();
786 let o = oo.output.as_mut().unwrap();
788 o.read_to_string(&mut s).unwrap();
792 impl Drop for OtterOutput {
794 if let Some(mut o) = self.output.take() {
795 io::copy(&mut o, &mut io::stdout()).expect("copy otter stdout");
800 pub trait OtterArgsSpec {
801 fn to_args(&self, ds: &dyn Substitutor) -> Vec<String>;
804 impl<S> OtterArgsSpec for [S] where for <'s> &'s S: Into<String> {
805 fn to_args(&self, _: &dyn Substitutor) -> Vec<String> {
806 self.iter().map(|s| s.into()).collect()
809 impl<S> OtterArgsSpec for Vec<S> where for <'s> &'s S: Into<String> {
810 fn to_args(&self, ds: &dyn Substitutor) -> Vec<String> {
811 self.as_slice().to_args(ds)
814 impl OtterArgsSpec for &str {
815 fn to_args(&self, ds: &dyn Substitutor) -> Vec<String> {
816 ds.ss(self).expect(self)
819 impl OtterArgsSpec for G<&str> {
820 fn to_args(&self, ds: &dyn Substitutor) -> Vec<String> {
821 ds.gss(self.0).expect(self.0)
824 #[derive(Debug,Clone)]
825 pub struct G<T>(pub T);
828 pub fn specs_dir(&self) -> String {
829 format!("{}/specs" , &self.src)
832 pub fn example_bundle(&self) -> String {
833 self.subst("@examples@/test-bundle.zip").unwrap()
837 pub fn otter(&self, xargs: &dyn OtterArgsSpec) -> OtterOutput
839 self.otter_prctx(&default(), xargs)?
843 pub fn otter_prctx(&self, prctx: &PathResolveContext,
844 xargs: &dyn OtterArgsSpec)
848 let exe = ds.subst("@target@/debug/otter")?;
849 let specs = self.subst("@src@/specs")?;
850 let mut args: Vec<String> = vec![];
851 args.push("--config" .to_owned()); args.push(prctx.resolve(CONFIG));
852 args.push("--spec-dir".to_owned()); args.push(prctx.resolve(&specs) );
853 args.extend(xargs.to_args(ds));
854 let dbg = format!("running {} {:?}", &exe, &args);
855 let mut output = NamedTempFile::new_in(
856 ds.subst("@abstmp@").unwrap()
860 let mut cmd = Command::new(&exe);
862 cmd.stdout(output.as_file().try_clone().unwrap());
864 .spawn().context("spawn")?
865 .wait().context("wait")?;
867 throw!(ExitStatusError(st));
872 .context("run otter client")?;
874 output.rewind().unwrap();
875 OtterOutput { output: Some(output) }
879 pub fn game_spec_path(&self) -> String {
880 self.subst("@specs@/demo.game.toml")?
884 pub fn game_spec_data(&self) -> GameSpec {
885 let path = self.game_spec_path()?;
887 let data = fs::read(&path).context("read")?;
888 let data = std::str::from_utf8(&data).context("convert from UTF-8")?;
889 let data: toml::Value = data.parse().context("parse TOM")?;
891 let data = toml_de::from_value(&data).context("interperet TOML")?;
895 .context("game spec")?
900 pub fn prepare_game(ds: &DirSubst, prctx: &PathResolveContext, table: &str)
902 let game_spec = ds.game_spec_path()?;
903 let subst = ds.also(&[
904 ("table", table.to_owned()),
905 ("game_spec", prctx.resolve(&game_spec)),
907 ds.otter_prctx(prctx, &subst.ss(
908 "--account server: --game @table@ \
910 --reset-table @specs@/test.table.toml \
912 ")?).context("reset table")?;
914 let instance: InstanceName = table.parse()
915 .with_context(|| table.to_owned())
916 .context("parse table name")?;
921 // ==================== post-setup facilities ====================
923 // -------------------- static users --------------------
925 pub struct StaticUserSetup {
926 pub nick: &'static str,
928 pub player: PlayerId,
933 pub fn setup_static_users(&self, mgmt_conn: &mut MgmtChannelForGame,
934 layout: PresentationLayout)
935 -> Vec<StaticUserSetup>
938 fn mk(su: &DirSubst, mgmt_conn: &mut MgmtChannelForGame,
939 layout: PresentationLayout, u: StaticUser)
942 let nick: &str = u.into();
943 let token = u.get_str("Token").expect("StaticUser missing Token");
944 let pl = AbbrevPresentationLayout(layout).to_string();
945 let subst = su.also([
952 .ss("--super -g@table@ \
953 --account server:@nick@ \
954 --fixed-token @token@ \
957 let player = mgmt_conn.has_player(
958 &subst.subst("server:@nick@")?.parse()?
961 let url = subst.subst("@url@/@pl@?@token@")?;
962 StaticUserSetup { nick, url, player }
965 StaticUser::iter().map(
967 let ssu = mk(self, mgmt_conn, layout, u).context("create")?;
970 .with_context(|| format!("{:?}", u))
971 .context("make static user")
973 .collect::<Result<Vec<StaticUserSetup>,AE>>()?
977 // -------------------- concurrency management --------------------
979 pub struct OtterPauseable(nix::unistd::Pid);
980 pub struct OtterPaused(nix::unistd::Pid);
983 pub fn otter_pauseable(&self) -> OtterPauseable {
984 OtterPauseable(nix::unistd::Pid::from_raw(
985 self.server_child.id() as nix::libc::pid_t
990 pub fn pause_otter(&self) -> OtterPaused {
991 self.otter_pauseable().pause()?
994 pub fn mgmt_conn<'m>(&'m self) -> RefMut<'m, MgmtChannelForGame> {
995 self.mgmt_conn.borrow_mut()
999 impl OtterPauseable {
1001 pub fn pause(self) -> OtterPaused {
1002 nix::sys::signal::kill(self.0, nix::sys::signal::SIGSTOP)?;
1009 pub fn resume(self) -> OtterPauseable {
1010 nix::sys::signal::kill(self.0, nix::sys::signal::SIGCONT)?;
1011 OtterPauseable(self.0)
1015 impl Drop for OtterPaused {
1016 fn drop(&mut self) {
1017 debug!("note, otter server pid={} was still paused", self.0);
1021 // -------------------- utilities --------------------
1026 fn game_synch(&mut self, game: InstanceName) -> Generation {
1027 let cmd = MgmtCommand::AlterGame {
1028 how: MgmtGameUpdateMode::Online,
1029 insns: vec![ MgmtGameInstruction::SynchLog ],
1032 let gen = if_chain!{
1033 let resp = self.cmd(&cmd)?;
1034 if let MgmtResponse::AlterGame {
1038 if let [MgmtGameResponse::Synch(gen)] = responses[..];
1040 else { throw!(anyhow!("unexpected resp to synch {:?}", resp)) }
1042 trace!("gen={} ...", gen);
1046 fn fakerng_load(&mut self, values: &[&dyn ToString]) -> Result<(),AE> {
1047 let values = values.iter().map(|v| v.to_string()).collect();
1048 self.cmd(&MC::LoadFakeRng(values))?;
1051 fn fakerng_unfake(&mut self) -> Result<(),AE> {
1052 self.cmd(&MC::LoadFakeRng(vec![]))?;
1059 pub fn initial_id_by_desc_glob(&mut self, desc_glob: &str) -> PieceId {
1060 let pieces = self.initial_pieces_cache.get_or_insert_with(||{
1061 self.mgmt_conn.borrow_mut().list_pieces().unwrap().0
1064 let glob = glob::Pattern::new(desc_glob).unwrap();
1065 pieces.iter().filter_map(|mgpi| {
1067 let vis = mgpi.visible.as_ref()?;
1068 glob.matches(vis.desc_html.as_html_str()).then(||())?;
1071 }).exactly_one().map_err(|_| anyhow!("not exactly one {:?}", desc_glob))?
1075 pub fn initial_vpid_by_desc_glob(&mut self, desc_glob: &str) -> String {
1076 let id = self.initial_id_by_desc_glob(desc_glob)?;
1077 VisiblePieceId::from(id.data()).to_string()
1081 // ==================== core entrypoint, for wdriver too ====================
1084 pub fn setup_core<O>(module_paths: &[&str]) ->
1085 (O, Instance, SetupCore)
1086 where O: StructOpt + AsRef<Opts>
1088 let mut builder = env_logger::Builder::new();
1090 .format_timestamp_micros()
1091 .format_level(true);
1092 for too_verbose in &[
1093 "html5ever::tokenizer",
1094 "html5ever::tree_builder",
1095 "selectors::matching",
1097 "hyper::client::pool",
1099 builder.filter_module(too_verbose, log::LevelFilter::Info);
1102 for module in module_paths {
1104 .filter_module(module, log::LevelFilter::Debug);
1108 .filter_level(log::LevelFilter::Debug)
1109 .parse_env("OTTER_TEST_LOG")
1113 let caller_opts = O::from_args();
1114 let opts = caller_opts.as_ref();
1116 let current_exe: String = env::current_exe()
1117 .context("find current executable")?
1119 .ok_or_else(|| anyhow!("current executable path is not UTF-8 !"))?
1125 &mut |s: &OsStr| s.to_str().unwrap().starts_with("--test=")
1127 .context("reinvoke via bwrap")?;
1130 info!("pid = {}", nix::unistd::getpid());
1131 sleep(opts.pause.into());
1133 let cln = cleanup_notify::Handle::new()?;
1134 let ds = prepare_tmpdir(opts, ¤t_exe)?;
1136 let (mgmt_conn, server_child) =
1137 prepare_gameserver(&cln, &ds).did("setup game server")?;
1140 prepare_game(&ds, &default(), TABLE).context("setup game")?;
1142 let wanted_tests = opts.tests.track();
1150 mgmt_conn: mgmt_conn.into(),
1153 initial_pieces_cache: None,
1157 pub struct PortmanteauMember {
1158 pub path: &'static str,
1159 pub f: fn() -> Result<(), Explode>,
1161 inventory::collect!(PortmanteauMember);
1164 macro_rules! portmanteau_has {
1165 ($path:literal, $mod:ident) => {
1166 #[path = $path] mod $mod;
1167 inventory::submit!(PortmanteauMember { path: $path, f: $mod::main });
1172 pub fn portmanteau_main(prefix: &str){
1173 let arg = 'arg: loop {
1174 for (ai, s) in env::args().enumerate() {
1175 let plausible = |s: &str| s.starts_with(&format!("{}-",prefix));
1177 break 'arg if ai == 0 {
1178 let s = s.rsplitn(2,'/').next().unwrap();
1179 if ! plausible(s) { continue }
1182 let s = s.strip_prefix("--test=")
1184 "found non-long-option looking for --test={}-*: {:?}",
1187 panic!("found non --no-bwrap --{}-* option looking for --{}-*",
1193 panic!("ran out of options looking for --test={}-*", prefix);
1196 let f = inventory::iter::<PortmanteauMember>.into_iter()
1198 let n = pm.path.strip_suffix(".rs").unwrap();
1199 if n == arg { Some(pm.f) } else { None }
1201 .expect("unrecognosed {wdt,at}-* portanteau member");