From c6df7c09d55e858e10f1cd766166221e81647500 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 | 2 ++ crates/jstzd/src/task/octez_client.rs | 15 ++++++++++--- crates/jstzd/tests/octez_client_test.rs | 29 +++++++++++++++++++++++++ 4 files changed, 45 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..df8ac6a4 100644 --- a/crates/jstzd/Cargo.toml +++ b/crates/jstzd/Cargo.toml @@ -13,6 +13,8 @@ bollard.workspace = true futures-util.workspace = true http.workspace = true octez = { path = "../octez" } +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 6a718147..6017d9f3 100644 --- a/crates/jstzd/src/task/octez_client.rs +++ b/crates/jstzd/src/task/octez_client.rs @@ -1,7 +1,11 @@ use super::{directory::Directory, 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; use tokio::process::{Child, Command}; @@ -75,7 +79,6 @@ impl OctezClientBuilder { } } -#[allow(dead_code)] #[derive(Debug)] pub struct OctezClient { binary_path: PathBuf, @@ -84,7 +87,6 @@ pub struct OctezClient { disable_unsafe_disclaimer: bool, } -#[allow(dead_code)] impl OctezClient { fn command, I: IntoIterator>( &self, @@ -112,6 +114,13 @@ 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!("config output path must be a valid utf-8"))?; + 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..7f3f548a --- /dev/null +++ b/crates/jstzd/tests/octez_client_test.rs @@ -0,0 +1,29 @@ +use jstzd::task::{endpoint::Endpoint, octez_client::OctezClientBuilder}; +use serde_json::Value; +use std::fs::{read_to_string, remove_file}; +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()); +}