1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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();
}
}