From 4886e278279f016b7a4f9d83bcf53bc98ae0c2d3 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 22 Aug 2022 15:29:11 +1000 Subject: [PATCH] Start adding CLI tests --- lighthouse/tests/main.rs | 1 + lighthouse/tests/validator_manager.rs | 63 +++++++++++++++++++ validator_manager/src/lib.rs | 2 +- .../src/validators/create_validators.rs | 2 +- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 lighthouse/tests/validator_manager.rs diff --git a/lighthouse/tests/main.rs b/lighthouse/tests/main.rs index 806524cab0..bf587f79df 100644 --- a/lighthouse/tests/main.rs +++ b/lighthouse/tests/main.rs @@ -5,3 +5,4 @@ mod beacon_node; mod boot_node; mod exec; mod validator_client; +mod validator_manager; diff --git a/lighthouse/tests/validator_manager.rs b/lighthouse/tests/validator_manager.rs new file mode 100644 index 0000000000..d96f4c1a97 --- /dev/null +++ b/lighthouse/tests/validator_manager.rs @@ -0,0 +1,63 @@ +use serde::de::DeserializeOwned; +use std::fs; +use std::marker::PhantomData; +use std::path::PathBuf; +use std::process::Command; +use tempfile::{tempdir, TempDir}; +use validator_manager::validators::create_validators::CreateConfig; + +struct CommandLineTest { + cmd: Command, + dir: TempDir, + config_path: PathBuf, + _phantom: PhantomData, +} + +impl Default for CommandLineTest { + fn default() -> Self { + let dir = tempdir().unwrap(); + let config_path = dir.path().join("config.json"); + let mut cmd = Command::new(env!("CARGO_BIN_EXE_lighthouse")); + cmd.arg("--dump_config") + .arg(format!("{:?}", config_path)) + .arg("validator-manager"); + Self { + cmd, + dir, + config_path, + _phantom: PhantomData, + } + } +} + +impl CommandLineTest { + fn flag(mut self, flag: &str, value: Option<&str>) -> Self { + self.cmd.arg(flag); + if let Some(value) = value { + self.cmd.arg(value); + } + self + } +} + +impl CommandLineTest { + fn assert_success(mut self, func: F) { + let output = self.cmd.output().expect("should run command"); + assert!(output.status.success(), "command should succeed"); + + let contents = fs::read_to_string(self.config_path).unwrap(); + let config: T = serde_json::from_str(&contents).unwrap(); + func(config) + } +} + +impl CommandLineTest { + fn validator_create() -> Self { + Self::default().flag("validator", None).flag("create", None) + } +} + +#[test] +pub fn validator_create_defaults() { + todo!() +} diff --git a/validator_manager/src/lib.rs b/validator_manager/src/lib.rs index 79d542e6c2..439db1449b 100644 --- a/validator_manager/src/lib.rs +++ b/validator_manager/src/lib.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use types::EthSpec; use validators::create_validators::write_to_json_file; -mod validators; +pub mod validators; pub const CMD: &str = "validator_manager"; diff --git a/validator_manager/src/validators/create_validators.rs b/validator_manager/src/validators/create_validators.rs index e3e00481ee..ee73d02d2d 100644 --- a/validator_manager/src/validators/create_validators.rs +++ b/validator_manager/src/validators/create_validators.rs @@ -181,7 +181,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { /// The CLI arguments are parsed into this struct before running the application. This step of /// indirection allows for testing the underlying logic without needing to parse CLI arguments. -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Serialize, Deserialize)] pub struct CreateConfig { pub output_path: PathBuf, pub first_index: u32,