-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jstzd): implement async OctezNode
- Loading branch information
1 parent
62b1ffa
commit 2934738
Showing
6 changed files
with
301 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
use std::{fs::File, path::PathBuf, process::Stdio}; | ||
|
||
#[cfg(test)] | ||
use tests::{Child, Command}; | ||
|
||
#[cfg(not(test))] | ||
use tokio::process::{Child, Command}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
|
||
fn path_or_default<'a>(path: Option<&'a PathBuf>, default: &'a str) -> &'a str { | ||
path.and_then(|bin| bin.to_str()).unwrap_or(default) | ||
} | ||
|
||
async fn run_command(command: &mut Command) -> Result<()> { | ||
let output = command.output().await?; | ||
|
||
if !output.status.success() { | ||
return Err(anyhow!( | ||
"Command {:?} failed:\n {}", | ||
command, | ||
String::from_utf8_lossy(&output.stderr) | ||
)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub struct AsyncOctezNode { | ||
/// Path to the octez-node binary | ||
/// If None, the binary will inside PATH will be used | ||
pub octez_node_bin: Option<PathBuf>, | ||
/// Path to the octez-node directory | ||
pub octez_node_dir: PathBuf, | ||
} | ||
|
||
impl AsyncOctezNode { | ||
fn command(&self) -> Command { | ||
Command::new(path_or_default(self.octez_node_bin.as_ref(), "octez-node")) | ||
} | ||
|
||
pub async fn config_init( | ||
&self, | ||
network: &str, | ||
rpc_endpoint: &str, | ||
num_connections: u32, | ||
) -> Result<()> { | ||
run_command(self.command().args([ | ||
"config", | ||
"init", | ||
"--network", | ||
network, | ||
"--data-dir", | ||
self.octez_node_dir.to_str().expect("Invalid path"), | ||
"--rpc-addr", | ||
rpc_endpoint, | ||
"--connections", | ||
num_connections.to_string().as_str(), | ||
])) | ||
.await | ||
} | ||
|
||
pub async fn generate_identity(&self) -> Result<()> { | ||
run_command(self.command().args([ | ||
"identity", | ||
"generate", | ||
"0", | ||
"--data-dir", | ||
self.octez_node_dir.to_str().expect("Invalid path"), | ||
])) | ||
.await | ||
} | ||
|
||
pub async fn run(&self, log_file: &File, options: &[&str]) -> Result<Child> { | ||
let mut command = self.command(); | ||
|
||
command | ||
.args([ | ||
"run", | ||
"--data-dir", | ||
self.octez_node_dir.to_str().expect("Invalid path"), | ||
"--singleprocess", | ||
]) | ||
.args(options) | ||
.stdout(Stdio::from(log_file.try_clone()?)) | ||
.stderr(Stdio::from(log_file.try_clone()?)); | ||
|
||
Ok(command.spawn()?) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{path_or_default, run_command, AsyncOctezNode}; | ||
use std::{fs::File, path::PathBuf, process::Stdio}; | ||
|
||
pub struct Child { | ||
pub cmd: String, | ||
} | ||
|
||
pub struct CommandOutputStatus { | ||
ok: bool, | ||
} | ||
impl CommandOutputStatus { | ||
pub fn success(&self) -> bool { | ||
self.ok | ||
} | ||
} | ||
pub struct CommandOutput { | ||
pub status: CommandOutputStatus, | ||
pub stderr: Vec<u8>, | ||
} | ||
#[derive(Debug)] | ||
pub struct Command { | ||
inner: String, | ||
} | ||
impl Command { | ||
pub fn new(s: &str) -> Self { | ||
Command { | ||
inner: s.to_owned(), | ||
} | ||
} | ||
pub fn args<I, T>(&mut self, args: I) -> &mut Self | ||
where | ||
I: IntoIterator<Item = T>, | ||
T: AsRef<std::ffi::OsStr>, | ||
{ | ||
let args_str = args | ||
.into_iter() | ||
.map(|v| v.as_ref().to_str().unwrap().to_owned()) | ||
.collect::<Vec<String>>() | ||
.join(" "); | ||
if !args_str.is_empty() { | ||
self.inner += " "; | ||
self.inner += &args_str; | ||
} | ||
self | ||
} | ||
pub async fn output(&self) -> anyhow::Result<CommandOutput> { | ||
let mut msg = Vec::new(); | ||
let mut ok = true; | ||
if !self.inner.starts_with("ok") { | ||
msg = Vec::from("this is the error message"); | ||
ok = false; | ||
} | ||
Ok(CommandOutput { | ||
status: CommandOutputStatus { ok }, | ||
stderr: msg, | ||
}) | ||
} | ||
pub fn spawn(&self) -> Result<Child, std::io::Error> { | ||
Ok(Child { | ||
cmd: self.inner.clone(), | ||
}) | ||
} | ||
|
||
pub fn stdout<T: Into<Stdio>>(&mut self, _cfg: T) -> &mut Self { | ||
self | ||
} | ||
pub fn stderr<T: Into<Stdio>>(&mut self, _cfg: T) -> &mut Self { | ||
self | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn run_command_ok() { | ||
// No surprise in command | ||
let mut command = Command { | ||
inner: "ok".to_owned(), | ||
}; | ||
assert!(run_command(&mut command).await.is_ok()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn run_command_err() { | ||
// When the command is not "ok", the command fails | ||
let mut command = Command { | ||
inner: "fail".to_owned(), | ||
}; | ||
assert!(run_command(&mut command) | ||
.await | ||
.unwrap_err() | ||
.to_string() | ||
.contains("this is the error message")); | ||
} | ||
|
||
#[test] | ||
fn test_path_or_default() { | ||
let path = PathBuf::from("/foo/bar"); | ||
assert_eq!(path_or_default(Some(&path), "not_this_one"), "/foo/bar"); | ||
assert_eq!( | ||
path_or_default(None, "should_return_this"), | ||
"should_return_this" | ||
); | ||
} | ||
|
||
#[test] | ||
fn octez_node_command() { | ||
let node = AsyncOctezNode { | ||
octez_node_bin: None, | ||
octez_node_dir: PathBuf::from("/some_dir"), | ||
}; | ||
assert_eq!(node.command().inner, "octez-node"); | ||
|
||
let node = AsyncOctezNode { | ||
octez_node_bin: Some(PathBuf::from("/path/to/bin")), | ||
octez_node_dir: PathBuf::from("/some_dir"), | ||
}; | ||
assert_eq!(node.command().inner, "/path/to/bin"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn octez_node_config_init() { | ||
// Setting the bin path to `my-node` fails the command. | ||
// Then here we can check if the args were passed into the command correctly. | ||
let node = AsyncOctezNode { | ||
octez_node_bin: Some(PathBuf::from("my-node")), | ||
octez_node_dir: PathBuf::from("/some_dir"), | ||
}; | ||
assert!( | ||
node.config_init("foo", "bar", 42) | ||
.await | ||
.unwrap_err() | ||
.to_string() | ||
.contains( | ||
"my-node config init --network foo --data-dir /some_dir --rpc-addr bar --connections 42" | ||
) | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn octez_node_generate_identity() { | ||
// Setting the bin path to `my-node` fails the command. | ||
// Then here we can check if the args were passed into the command correctly. | ||
let node = AsyncOctezNode { | ||
octez_node_bin: Some(PathBuf::from("my-node")), | ||
octez_node_dir: PathBuf::from("/some_dir"), | ||
}; | ||
assert!(node | ||
.generate_identity() | ||
.await | ||
.unwrap_err() | ||
.to_string() | ||
.contains("my-node identity generate 0 --data-dir /some_dir")); | ||
} | ||
|
||
#[tokio::test] | ||
async fn octez_node_run() { | ||
// Setting the bin path to `my-node` fails the command. | ||
// Then here we can check if the args were passed into the command correctly. | ||
let node = AsyncOctezNode { | ||
octez_node_bin: Some(PathBuf::from("my-node")), | ||
octez_node_dir: PathBuf::from("/some_dir"), | ||
}; | ||
assert!(node | ||
.run(&File::open("/dev/null").unwrap(), &[]) | ||
.await | ||
.unwrap() | ||
.cmd | ||
.contains("my-node run --data-dir /some_dir --singleprocess")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod async_octez_node; | ||
pub mod octez_node; | ||
|
||
use anyhow::Result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.