From a518733c3010ab913dfc63f6712bdb1d96c51a71 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 30 Sep 2024 16:05:01 +0100 Subject: [PATCH] feat(jstzd): add config init for client --- Cargo.lock | 2 ++ crates/jstzd/Cargo.toml | 3 ++ crates/jstzd/src/task/octez_client.rs | 13 ++++++-- crates/jstzd/tests/octez_client_test.rs | 44 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 crates/jstzd/tests/octez_client_test.rs diff --git a/Cargo.lock b/Cargo.lock index 34bc1f21..f5ca1d10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2645,6 +2645,8 @@ dependencies = [ "octez", "rand 0.8.5", "reqwest", + "serde", + "serde_json", "tempfile", "tokio", ] diff --git a/crates/jstzd/Cargo.toml b/crates/jstzd/Cargo.toml index d26dbf5d..0265e565 100644 --- a/crates/jstzd/Cargo.toml +++ b/crates/jstzd/Cargo.toml @@ -13,6 +13,9 @@ bollard.workspace = true futures-util.workspace = true http.workspace = true octez = { path = "../octez" } +reqwest.workspace = true +serde.workspace = true +serde_json.workspace = true tempfile.workspace = true tokio.workspace = true diff --git a/crates/jstzd/src/task/octez_client.rs b/crates/jstzd/src/task/octez_client.rs index 1093f2c5..7e28310a 100644 --- a/crates/jstzd/src/task/octez_client.rs +++ b/crates/jstzd/src/task/octez_client.rs @@ -1,7 +1,11 @@ use super::{endpoint::Endpoint, octez_node::DEFAULT_RPC_ENDPOINT}; use anyhow::{anyhow, bail, Result}; use http::Uri; -use std::{ffi::OsStr, path::PathBuf, str::FromStr}; +use std::{ + ffi::OsStr, + path::{Path, PathBuf}, + str::FromStr, +}; use tempfile::{tempdir, TempDir}; use tokio::process::{Child, Command}; @@ -100,7 +104,6 @@ impl Directory { } } -#[allow(dead_code)] pub struct OctezClient { binary_path: PathBuf, base_dir: Directory, @@ -108,7 +111,6 @@ pub struct OctezClient { disable_unsafe_disclaimer: bool, } -#[allow(dead_code)] impl OctezClient { fn command, I: IntoIterator>( &self, @@ -133,6 +135,11 @@ impl OctezClient { let mut command = self.command(args)?; Ok(command.spawn()?) } + + pub async fn config_init(&self, output_path: &Path) -> Result { + let output = output_path.to_str().ok_or(anyhow!("non utf-8 path"))?; + self.spawn_command(["config", "init", "--output", output]) + } } #[cfg(test)] diff --git a/crates/jstzd/tests/octez_client_test.rs b/crates/jstzd/tests/octez_client_test.rs new file mode 100644 index 00000000..0ff0ee54 --- /dev/null +++ b/crates/jstzd/tests/octez_client_test.rs @@ -0,0 +1,44 @@ +use jstzd::task::{endpoint::Endpoint, octez_client::OctezClientBuilder}; +use serde_json::Value; +use std::{ + ffi::OsStr, + fs::{read_to_string, remove_file}, + os::unix::ffi::OsStrExt, + path::PathBuf, +}; +use tempfile::{NamedTempFile, TempDir}; + +#[tokio::test] +async fn config_init() { + let temp_dir = TempDir::new().unwrap(); + let expected_base_dir = temp_dir.path().to_path_buf(); + let expected_endpoint: Endpoint = Endpoint::localhost(3000); + let config_file = NamedTempFile::new().unwrap(); + let _ = remove_file(config_file.path()); + let octez_client = OctezClientBuilder::new() + .set_base_dir(expected_base_dir.clone()) + .set_endpoint(expected_endpoint.clone()) + .build() + .unwrap(); + let mut res = octez_client.config_init(config_file.path()).await.unwrap(); + let status = res.wait().await; + assert!(status.is_ok_and(|s| s.success())); + let actual: Value = + serde_json::from_str(&read_to_string(config_file).expect("Unable to read file")) + .expect("Unable to parse JSON"); + assert_eq!( + actual["base_dir"], + expected_base_dir.to_str().unwrap().to_owned() + ); + assert_eq!(actual["endpoint"], expected_endpoint.to_string()); +} + +#[tokio::test] +async fn command_fails_on_non_utf8_path() { + let invalid_bytes = b"/tmp/\xFF\xFE"; + let invalid_file_path = PathBuf::from(OsStr::from_bytes(invalid_bytes)); + let _ = remove_file(&invalid_file_path); + let octez_client = OctezClientBuilder::new().build().unwrap(); + let res = octez_client.config_init(&invalid_file_path).await; + assert!(res.is_err_and(|e| { e.to_string().contains("non utf-8 path") })); +}