Skip to content

Commit

Permalink
Merge pull request #2728 from input-output-hk/clippy
Browse files Browse the repository at this point in the history
[Tests] Resolves Clippy too_many_arguments in tests
  • Loading branch information
dkijania committed Nov 19, 2020
2 parents 4c28778 + c9702af commit 6e8c38e
Show file tree
Hide file tree
Showing 4 changed files with 356 additions and 273 deletions.
Expand Up @@ -14,36 +14,9 @@ impl CertificateBuilder {
pub fn new(jcli: JCli) -> Self {
Self { jcli }
}
#[allow(clippy::too_many_arguments)]
pub fn new_signed_stake_pool_cert(
&self,
pool_kes_pk: &str,
pool_vrf_pk: &str,
stake_key_file: &Path,
start_validity: u32,
management_threshold: u32,
owner_pk: &str,
tax_type: Option<TaxType>,
) {
let temp_dir = TempDir::new().unwrap();

let stake_pool_cert = self.jcli.certificate().new_stake_pool_registration(
&pool_kes_pk,
&pool_vrf_pk,
start_validity,
management_threshold,
owner_pk,
tax_type,
);
let stake_pool_cert_file = temp_dir.child("stake_pool.cert");
stake_pool_cert_file.write_str(&stake_pool_cert).unwrap();

let stake_pool_signcert_file = temp_dir.child("stake_pool.signcert");
self.jcli.certificate().sign(
&stake_key_file,
stake_pool_cert_file.path(),
stake_pool_signcert_file.path(),
);
pub fn new_signed_stake_pool_cert(self) -> SignedStakePoolCertBuilder {
SignedStakePoolCertBuilder::new(self.jcli)
}

pub fn new_signed_stake_pool_delegation(
Expand Down Expand Up @@ -91,3 +64,86 @@ impl CertificateBuilder {
PathBuf::from(signcert_file.path())
}
}

pub struct SignedStakePoolCertBuilder {
jcli: JCli,
pool_kes_pk: String,
pool_vrf_pk: String,
stake_key_file: PathBuf,
start_validity: u32,
management_threshold: u32,
owner_pk: String,
tax_type: Option<TaxType>,
}

impl SignedStakePoolCertBuilder {
pub fn new(jcli: JCli) -> Self {
Self {
jcli,
pool_kes_pk: "".to_owned(),
pool_vrf_pk: "".to_string(),
stake_key_file: PathBuf::new(),
start_validity: 0u32,
management_threshold: 0u32,
owner_pk: "".to_string(),
tax_type: None,
}
}

pub fn pool_kes_pk<S: Into<String>>(&mut self, pool_kes_pk: S) -> &mut Self {
self.pool_kes_pk = pool_kes_pk.into();
self
}

pub fn pool_vrf_pk<S: Into<String>>(&mut self, pool_vrf_pk: S) -> &mut Self {
self.pool_vrf_pk = pool_vrf_pk.into();
self
}

pub fn owner_pk<S: Into<String>>(&mut self, owner_pk: S) -> &mut Self {
self.owner_pk = owner_pk.into();
self
}

pub fn stake_key_file<P: AsRef<Path>>(&mut self, stake_key_file: P) -> &mut Self {
self.stake_key_file = stake_key_file.as_ref().to_path_buf();
self
}

pub fn start_validity(&mut self, start_validity: u32) -> &mut Self {
self.start_validity = start_validity;
self
}

pub fn management_threshold(&mut self, management_threshold: u32) -> &mut Self {
self.management_threshold = management_threshold;
self
}

pub fn tax_type(&mut self, tax_type: TaxType) -> &mut Self {
self.tax_type = Some(tax_type);
self
}

pub fn build(self) {
let temp_dir = TempDir::new().unwrap();

let stake_pool_cert = self.jcli.certificate().new_stake_pool_registration(
&self.pool_kes_pk,
&self.pool_vrf_pk,
self.start_validity,
self.management_threshold,
&self.owner_pk,
self.tax_type,
);
let stake_pool_cert_file = temp_dir.child("stake_pool.cert");
stake_pool_cert_file.write_str(&stake_pool_cert).unwrap();

let stake_pool_signcert_file = temp_dir.child("stake_pool.signcert");
self.jcli.certificate().sign(
&self.stake_key_file,
stake_pool_cert_file.path(),
stake_pool_signcert_file.path(),
);
}
}
137 changes: 19 additions & 118 deletions testing/jormungandr-scenario-tests/src/legacy/node.rs
Expand Up @@ -3,7 +3,7 @@
/// Specialized node which is supposed to be compatible with 5 last jormungandr releases
use crate::{
legacy::LegacySettings,
node::{Error, ProgressBarController, Result, Status},
node::{Error, ProgressBarController, Result, SpawnBuilder, Status},
style, Context,
};
use chain_impl_mockchain::{
Expand All @@ -13,10 +13,7 @@ use chain_impl_mockchain::{
};
use jormungandr_lib::{
crypto::hash::Hash,
interfaces::{
EnclaveLeaderId, FragmentLog, FragmentStatus, Info, Log, LogEntry, LogOutput, PeerRecord,
PeerStats,
},
interfaces::{EnclaveLeaderId, FragmentLog, FragmentStatus, Info, PeerRecord, PeerStats},
};
pub use jormungandr_testing_utils::testing::{
network_builder::{
Expand All @@ -27,14 +24,13 @@ pub use jormungandr_testing_utils::testing::{
};

use futures::executor::block_on;
use indicatif::ProgressBar;
use rand_core::RngCore;
use yaml_rust::{Yaml, YamlLoader};

use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::path::PathBuf;
use std::process::Child;
use std::sync::{Arc, Mutex};
use std::time::Duration;

Expand All @@ -49,16 +45,16 @@ pub struct LegacyNodeController {
}

pub struct LegacyNode {
alias: NodeAlias,
pub alias: NodeAlias,

#[allow(unused)]
dir: PathBuf,
pub dir: PathBuf,

process: Child,
pub process: Child,

progress_bar: ProgressBarController,
node_settings: LegacySettings,
status: Arc<Mutex<Status>>,
pub progress_bar: ProgressBarController,
pub node_settings: LegacySettings,
pub status: Arc<Mutex<Status>>,
}

const NODE_CONFIG: &str = "node_config.yaml";
Expand Down Expand Up @@ -535,111 +531,16 @@ impl LegacyNode {
}
}

#[allow(clippy::too_many_arguments)]
pub fn spawn<R: RngCore>(
jormungandr: &Path,
context: &Context<R>,
progress_bar: ProgressBar,
alias: &str,
node_settings: &mut LegacySettings,
block0: NodeBlock0,
working_dir: &Path,
peristence_mode: PersistenceMode,
) -> Result<Self> {
let mut command = Command::new(jormungandr);
let dir = working_dir.join(alias);
std::fs::DirBuilder::new().recursive(true).create(&dir)?;

let progress_bar = ProgressBarController::new(
progress_bar,
format!("{}@{}", alias, node_settings.config().rest.listen),
context.progress_bar_mode(),
);

let config_file = dir.join(NODE_CONFIG);
let config_secret = dir.join(NODE_SECRET);
let log_file = dir.join(NODE_LOG);

let format = "plain";
let level = context.log_level();
node_settings.config.log = Some(Log(vec![
LogEntry {
format: format.to_string(),
level: level.to_string(),
output: LogOutput::Stderr,
},
LogEntry {
format: format.to_string(),
level,
output: LogOutput::File(log_file),
},
]));

if peristence_mode == PersistenceMode::Persistent {
let path_to_storage = dir.join(NODE_STORAGE);
node_settings.config.storage = Some(path_to_storage);
}

serde_yaml::to_writer(
std::fs::File::create(&config_file).map_err(|e| Error::CannotCreateFile {
path: config_file.clone(),
cause: e,
})?,
node_settings.config(),
)
.map_err(|e| Error::CannotWriteYamlFile {
path: config_file.clone(),
cause: e,
})?;

serde_yaml::to_writer(
std::fs::File::create(&config_secret).map_err(|e| Error::CannotCreateFile {
path: config_secret.clone(),
cause: e,
})?,
node_settings.secrets(),
)
.map_err(|e| Error::CannotWriteYamlFile {
path: config_secret.clone(),
cause: e,
})?;

command.arg("--config");
command.arg(&config_file);

match block0 {
NodeBlock0::File(path) => {
command.arg("--genesis-block");
command.arg(&path);
command.arg("--secret");
command.arg(&config_secret);
}
NodeBlock0::Hash(hash) => {
command.args(&["--genesis-block-hash", &hash.to_string()]);
}
}

command.stderr(Stdio::piped());

let process = command.spawn().map_err(Error::CannotSpawnNode)?;

let node = LegacyNode {
alias: alias.into(),

dir,

process,

progress_bar,
node_settings: node_settings.clone(),
status: Arc::new(Mutex::new(Status::Running)),
};

node.progress_bar_start();

Ok(node)
pub fn progress_bar(&self) -> &ProgressBarController {
&self.progress_bar
}

pub fn spawn<'a, R: RngCore>(
context: &'a Context<R>,
node_settings: &'a mut NodeSetting,
) -> SpawnBuilder<'a, R, LegacyNode> {
SpawnBuilder::new(&context, node_settings)
}
pub fn capture_logs(&mut self) {
let stderr = self.process.stderr.take().unwrap();
let reader = BufReader::new(stderr);
Expand Down Expand Up @@ -668,7 +569,7 @@ impl LegacyNode {
}
}

fn progress_bar_start(&self) {
pub fn progress_bar_start(&self) {
self.progress_bar.set_style(
indicatif::ProgressStyle::default_spinner()
.template("{spinner:.green} {wide_msg}")
Expand Down

0 comments on commit 6e8c38e

Please sign in to comment.