pub mod ffi;
use error::Result;
use host::Host;
use target::Target;
#[cfg_attr(feature = "local-run", doc = "let path: Option<String> = None;")]
#[cfg_attr(feature = "local-run", doc = "let mut host = Host::local(path).unwrap();")]
#[cfg_attr(feature = "remote-run", doc = "let mut host = Host::connect(\"hosts/myhost.json\").unwrap();")]
#[cfg_attr(feature = "local-run", doc = "let path: Option<String> = None;")]
#[cfg_attr(feature = "local-run", doc = "let mut host = Host::local(path).unwrap();")]
#[cfg_attr(feature = "remote-run", doc = "# let mut host = Host::connect(\"hosts/myhost.json\").unwrap();")]
pub struct Command {
cmd: String,
}
#[derive(Debug)]
pub struct CommandResult {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
}
impl Command {
pub fn new(cmd: &str) -> Command {
Command {
cmd: cmd.to_string(),
}
}
#[cfg_attr(feature = "local-run", doc = "let path: Option<String> = None;")]
#[cfg_attr(feature = "local-run", doc = "let mut web1 = Host::local(path).unwrap();")]
#[cfg_attr(feature = "remote-run", doc = "let mut web1 = Host::connect(\"data/hosts/web1.json\").unwrap();")]
#[cfg_attr(feature = "local-run", doc = "let path: Option<String> = None;")]
#[cfg_attr(feature = "local-run", doc = "let mut web2 = Host::local(path).unwrap();")]
#[cfg_attr(feature = "remote-run", doc = "let mut web2 = Host::connect(\"data/hosts/web2.json\").unwrap();")]
#[allow(unused_variables)]
pub fn exec(&self, host: &mut Host) -> Result<CommandResult> {
Target::exec(host, &self.cmd)
}
}
pub trait CommandTarget {
fn exec(host: &mut Host, cmd: &str) -> Result<CommandResult>;
}
#[cfg(test)]
mod tests {
use Host;
#[cfg(feature = "remote-run")]
use czmq::{ZMsg, ZSys};
#[cfg(feature = "local-run")]
use std::{process, str};
#[cfg(feature = "remote-run")]
use std::thread;
use super::*;
#[cfg(feature = "local-run")]
#[test]
fn test_exec() {
let path: Option<String> = None;
let mut host = Host::local(path).unwrap();
let cmd = Command::new("whoami");
let result = cmd.exec(&mut host).unwrap();
let output = process::Command::new("sh").arg("-c").arg(&cmd.cmd).output().unwrap();
assert_eq!(result.exit_code, output.status.code().unwrap());
assert_eq!(result.stdout, str::from_utf8(&output.stdout).unwrap().trim().to_string());
assert_eq!(result.stderr, str::from_utf8(&output.stderr).unwrap().trim().to_string());
}
#[cfg(feature = "remote-run")]
#[test]
fn test_exec() {
ZSys::init();
let (client, mut server) = ZSys::create_pipe().unwrap();
let agent_mock = thread::spawn(move || {
let req = ZMsg::recv(&mut server).unwrap();
assert_eq!("command::exec", req.popstr().unwrap().unwrap());
assert_eq!("moo", req.popstr().unwrap().unwrap());
let rep = ZMsg::new();
rep.addstr("Ok").unwrap();
rep.addstr("0").unwrap();
rep.addstr("cow").unwrap();
rep.addstr("err").unwrap();
rep.send(&mut server).unwrap();
});
let mut host = Host::test_new(None, Some(client), None, None);
let cmd = Command::new("moo");
let result = cmd.exec(&mut host).unwrap();
assert_eq!(result.exit_code, 0);
assert_eq!(result.stdout, "cow");
assert_eq!(result.stderr, "err");
agent_mock.join().unwrap();
}
}