From a7204879d42db0430fc0972cfc02364759c2208f Mon Sep 17 00:00:00 2001 From: Andrey Kuprianov Date: Thu, 6 Aug 2020 16:06:53 +0200 Subject: [PATCH] #414: refactor: add commonly used test utils --- tendermint/tests/lite-model-based.rs | 41 +++------ tendermint/tests/lite.rs | 4 +- tendermint/tests/utils/apalache.rs | 18 ++++ tendermint/tests/utils/command.rs | 88 +++++++++++++++++++ .../{lite_tests/mod.rs => utils/lite.rs} | 86 +----------------- tendermint/tests/utils/mod.rs | 3 + 6 files changed, 125 insertions(+), 115 deletions(-) create mode 100644 tendermint/tests/utils/apalache.rs create mode 100644 tendermint/tests/utils/command.rs rename tendermint/tests/{lite_tests/mod.rs => utils/lite.rs} (53%) create mode 100644 tendermint/tests/utils/mod.rs diff --git a/tendermint/tests/lite-model-based.rs b/tendermint/tests/lite-model-based.rs index fbfb63c27..ebd661d49 100644 --- a/tendermint/tests/lite-model-based.rs +++ b/tendermint/tests/lite-model-based.rs @@ -1,14 +1,13 @@ -use std::{process, io}; -use std::io::Read; +use std::{io}; use std::{fs, path::PathBuf}; use serde::Deserialize; -use tempfile::tempdir; -use tendermint::{block::signed_header::SignedHeader, evidence::Duration, lite, Hash, Time}; -use tendermint::block::{Header, Height}; -use tendermint::lite::{Requester, TrustThresholdFraction, TrustedState}; -mod lite_tests; -use lite_tests::*; -use tendermint::lite::error::Error; +//use tempfile::tempdir; +use tendermint::{block::signed_header::SignedHeader, lite}; +use tendermint::block::{Header}; +use tendermint::lite::{TrustThresholdFraction, TrustedState}; + +mod utils; +use utils::{apalache::*, command::*, lite::*}; type Trusted = lite::TrustedState; @@ -49,24 +48,6 @@ pub struct BlockVerdict { verdict: LiteVerdict, } -#[derive(Deserialize, Clone, Debug)] -pub struct ApalacheTestBatch { - pub description: String, - pub kind: LiteTestKind, - pub model: String, - pub length: Option, - pub timeout: Option, - pub tests: Vec, -} - -#[derive(Deserialize, Clone, Debug)] -pub struct ApalacheTestCase { - pub model: String, - pub test: String, - pub length: Option, - pub timeout: Option, -} - const TEST_DIR: &str = "./tests/support/lite-model-based/"; fn read_file(dir: &str, file: &str) -> String { @@ -126,8 +107,6 @@ fn single_step_test() { run_single_step_test(&tc); } - - fn run_apalache_test(dir: &str, test: ApalacheTestCase) -> io::Result { let mut cmd = Command::new(); if let Some(timeout) = test.timeout { @@ -163,6 +142,10 @@ fn run_apalache_test(dir: &str, test: ApalacheTestCase) -> io::Result, + pub timeout: Option, + pub tests: Vec, +} + +#[derive(Deserialize, Clone, Debug)] +pub struct ApalacheTestCase { + pub model: String, + pub test: String, + pub length: Option, + pub timeout: Option, +} diff --git a/tendermint/tests/utils/command.rs b/tendermint/tests/utils/command.rs new file mode 100644 index 000000000..e8e44fd5e --- /dev/null +++ b/tendermint/tests/utils/command.rs @@ -0,0 +1,88 @@ +use std::{process, io}; +use std::io::Read; + +/// A thin wrapper around process::Command to facilitate running external commands. +pub struct Command { + program: Option, + args: Vec, + dir: Option +} + +impl Command { + + /// Check whether the given program can be executed + pub fn exists_program(program: &str) -> bool { + Command::new().program(program).spawn().is_ok() + } + + /// Constructs a new Command for the given program with arguments. + pub fn new() -> Command { + Command { + program: None, + args: vec![], + dir: None + } + } + + /// Sets the program to run + pub fn program(&mut self, program: &str) -> &mut Self { + self.program = Some(program.to_owned()); + self + } + + /// Adds a new program argument + pub fn arg(&mut self, arg: &str) -> &mut Self { + self.args.push(arg.to_owned()); + self + } + + /// Adds a new program argument, concatenated from several parts + pub fn arg_from_parts(&mut self, parts: Vec<&str>) -> &mut Self { + let mut arg: String = String::new(); + for part in parts { + arg = arg + part; + } + self.args.push(arg); + self + } + + /// Sets the working directory for the child process + pub fn current_dir(&mut self, dir: &str) -> &mut Self { + self.dir = Some(dir.to_owned()); + self + } + + /// Executes the command as a child process, and extracts its status, stdout, stderr. + pub fn spawn(&mut self) -> io::Result { + match &self.program { + None => Err(io::Error::new(io::ErrorKind::InvalidInput, "")), + Some(program) => { + let mut command = process::Command::new(program); + command.args(&self.args) + .stdout(process::Stdio::piped()) + .stderr(process::Stdio::piped()); + if let Some(dir) = &self.dir { + command.current_dir(dir); + } + let mut process = command.spawn()?; + let status = process.wait()?; + let mut stdout = String::new(); + process.stdout.unwrap().read_to_string(&mut stdout)?; + let mut stderr = String::new(); + process.stderr.unwrap().read_to_string(&mut stderr)?; + Ok(CommandRun { + status, + stdout, + stderr + }) + } + } + } +} + +/// The result of a command execution if managed to run the child process +pub struct CommandRun { + pub status: process::ExitStatus, + pub stdout: String, + pub stderr: String +} \ No newline at end of file diff --git a/tendermint/tests/lite_tests/mod.rs b/tendermint/tests/utils/lite.rs similarity index 53% rename from tendermint/tests/lite_tests/mod.rs rename to tendermint/tests/utils/lite.rs index ecd640e0c..d60fd0581 100644 --- a/tendermint/tests/lite_tests/mod.rs +++ b/tendermint/tests/utils/lite.rs @@ -9,8 +9,6 @@ use tendermint::lite::Requester; use tendermint::{ block::signed_header::SignedHeader, evidence::Duration, lite, validator::Set, Time, }; -use std::{process, io}; -use std::io::Read; /// Test that a struct `T` can be: /// @@ -18,8 +16,8 @@ use std::io::Read; /// - parsed back from the serialized JSON of the previous step /// - that the two parsed structs are equal according to their `PartialEq` impl pub fn test_serialization_roundtrip(obj: &T) -where - T: Debug + PartialEq + Serialize + DeserializeOwned, + where + T: Debug + PartialEq + Serialize + DeserializeOwned, { let serialized = serde_json::to_string(obj).unwrap(); let parsed = serde_json::from_str(&serialized).unwrap(); @@ -92,83 +90,3 @@ impl MockRequester { } } } - -/// A thin wrapper around process::Command to facilitate running external commands. -pub struct Command { - program: Option, - args: Vec, - dir: Option -} - -impl Command { - /// Constructs a new Command for the given program with arguments. - pub fn new() -> Command { - Command { - program: None, - args: vec![], - dir: None - } - } - - /// Sets the program to run - pub fn program(&mut self, program: &str) -> &mut Self { - self.program = Some(program.to_owned()); - self - } - - /// Adds a new program argument - pub fn arg(&mut self, arg: &str) -> &mut Self { - self.args.push(arg.to_owned()); - self - } - - /// Adds a new program argument, concatenated from several parts - pub fn arg_from_parts(&mut self, parts: Vec<&str>) -> &mut Self { - let mut arg: String = String::new(); - for part in parts { - arg = arg + part; - } - self.args.push(arg); - self - } - - /// Sets the working directory for the child process - pub fn current_dir(&mut self, dir: &str) -> &mut Self { - self.dir = Some(dir.to_owned()); - self - } - - /// Executes the command as a child process, and extracts its status, stdout, stderr. - pub fn spawn(&mut self) -> io::Result { - match &self.program { - None => Err(io::Error::new(io::ErrorKind::InvalidInput, "")), - Some(program) => { - let mut command = process::Command::new(program); - command.args(&self.args) - .stdout(process::Stdio::piped()) - .stderr(process::Stdio::piped()); - if let Some(dir) = &self.dir { - command.current_dir(dir); - } - let mut process = command.spawn()?; - let status = process.wait()?; - let mut stdout = String::new(); - process.stdout.unwrap().read_to_string(&mut stdout)?; - let mut stderr = String::new(); - process.stderr.unwrap().read_to_string(&mut stderr)?; - Ok(CommandRun { - status, - stdout, - stderr - }) - } - } - } -} - -/// The result of a command execution if managed to run the child process -pub struct CommandRun { - pub status: process::ExitStatus, - pub stdout: String, - pub stderr: String -} \ No newline at end of file diff --git a/tendermint/tests/utils/mod.rs b/tendermint/tests/utils/mod.rs new file mode 100644 index 000000000..b1ce0b023 --- /dev/null +++ b/tendermint/tests/utils/mod.rs @@ -0,0 +1,3 @@ +pub mod apalache; +pub mod command; +pub mod lite;