Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified temp dir builder #1529

Merged
merged 15 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.4.40"
version = "0.4.41"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
20 changes: 6 additions & 14 deletions mithril-aggregator/src/snapshotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl CompressedArchiveSnapshotter {
return Err(SnapshotError::InvalidArchiveError(format!(
"can't unpack entry with error: '{:?}'",
e
)))
)));
}
Ok(_) => {
if let Err(e) = fs::remove_file(unpack_file_path) {
Expand Down Expand Up @@ -391,20 +391,12 @@ mod tests {
use std::sync::Arc;

use mithril_common::digesters::DummyImmutablesDbBuilder;
use mithril_common::test_utils::TempDir;

use super::*;

fn get_test_directory(dir_name: &str) -> PathBuf {
let test_dir = std::env::temp_dir()
.join("mithril_test")
.join("snapshotter")
.join(dir_name);
if test_dir.exists() {
std::fs::remove_dir_all(&test_dir).unwrap();
}
std::fs::create_dir_all(&test_dir).unwrap();

test_dir
TempDir::create("snapshotter", dir_name)
}

#[test]
Expand All @@ -430,7 +422,7 @@ mod tests {
fn should_create_directory_if_does_not_exist() {
let test_dir = get_test_directory("should_create_directory_if_does_not_exist");
let pending_snapshot_directory = test_dir.join("pending_snapshot");
let db_directory = std::env::temp_dir().join("whatever");
let db_directory = test_dir.join("whatever");

Arc::new(
CompressedArchiveSnapshotter::new(
Expand All @@ -449,9 +441,9 @@ mod tests {
let test_dir =
get_test_directory("should_clean_pending_snapshot_directory_if_already_exists");
let pending_snapshot_directory = test_dir.join("pending_snapshot");
let db_directory = std::env::temp_dir().join("whatever");
let db_directory = test_dir.join("whatever");

std::fs::create_dir_all(&pending_snapshot_directory).unwrap();
fs::create_dir_all(&pending_snapshot_directory).unwrap();

File::create(pending_snapshot_directory.join("whatever.txt")).unwrap();

Expand Down
17 changes: 3 additions & 14 deletions mithril-aggregator/src/tools/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,15 @@ mod tests {
use mithril_common::{
certificate_chain::MithrilCertificateVerifier,
crypto_helper::{ProtocolClerk, ProtocolGenesisSigner},
test_utils::{fake_data, MithrilFixtureBuilder},
test_utils::{fake_data, MithrilFixtureBuilder, TempDir},
};
use sqlite::Connection;
use std::{fs, path::PathBuf};
use std::path::PathBuf;

use super::*;

fn get_temp_dir(dir_name: &str) -> PathBuf {
let dir = std::env::temp_dir()
.join("mithril_test")
.join("genesis")
.join(dir_name);

if dir.exists() {
let _ = fs::remove_dir_all(&dir);
}

let _ = fs::create_dir_all(&dir);

dir
TempDir::create("genesis", dir_name)
}

fn create_fake_genesis_avk() -> ProtocolAggregateVerificationKey {
Expand Down
12 changes: 2 additions & 10 deletions mithril-aggregator/tests/test_extensions/utilities.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use mithril_common::test_utils::TempDir;
use slog_scope::debug;
use std::{
path::PathBuf,
Expand All @@ -10,16 +11,7 @@ pub static COMMENT_COUNT: AtomicUsize = AtomicUsize::new(0);
/// already exists, it is created if not. This directory is kept at the end to
/// allow debuging.
pub fn get_test_dir(subdir_name: &str) -> PathBuf {
let parent_dir = std::env::temp_dir().join("mithril_test").join(subdir_name);

if parent_dir.exists() {
std::fs::remove_dir_all(&parent_dir)
.unwrap_or_else(|e| panic!("Could not remove dir {parent_dir:?}: {e}"));
}
std::fs::create_dir_all(&parent_dir)
.unwrap_or_else(|e| panic!("Could not create dir {parent_dir:?}: {e}"));

parent_dir
TempDir::create("aggregator-integration", subdir_name)
}

pub fn comment(comment: String) {
Expand Down
5 changes: 4 additions & 1 deletion mithril-client-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client-cli"
version = "0.7.0"
version = "0.7.1"
description = "A Mithril Client"
authors = { workspace = true }
edition = { workspace = true }
Expand Down Expand Up @@ -49,6 +49,9 @@ slog-term = "2.9.0"
thiserror = "1.0.56"
tokio = { version = "1.35.1", features = ["full"] }

[dev-dependencies]
mithril-common = { path = "../mithril-common", features = ["test_tools"] }

[features]
portable = ["mithril-client/portable"]
bundle_openssl = ["dep:openssl", "dep:openssl-probe"]
13 changes: 6 additions & 7 deletions mithril-client-cli/src/commands/snapshot/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ mod tests {
common::{Beacon, ProtocolMessagePartKey},
MithrilCertificateMetadata,
};
use mithril_common::test_utils::TempDir;

use super::*;

Expand Down Expand Up @@ -367,7 +368,7 @@ mod tests {
}

#[tokio::test]
async fn verify_snapshot_signature_should_remove_db_dir_if_messages_dismatch() {
async fn verify_snapshot_signature_should_remove_db_dir_if_messages_mismatch() {
let progress_printer = ProgressPrinter::new(ProgressOutputType::Tty, 1);
let certificate = dummy_certificate();
let mut message = ProtocolMessage::new();
Expand All @@ -377,12 +378,10 @@ mod tests {
"avk".to_string(),
);
let snapshot = Snapshot::dummy();
let db_dir = std::env::temp_dir().join("db");
if db_dir.exists() {
std::fs::remove_dir_all(&db_dir).unwrap();
}
std::fs::create_dir_all(&db_dir).unwrap();
println!("db_dir: '{:?}'", db_dir);
let db_dir = TempDir::create(
"client-cli",
"verify_snapshot_signature_should_remove_db_dir_if_messages_mismatch",
);

let result = SnapshotDownloadCommand::verify_snapshot_signature(
1,
Expand Down
9 changes: 2 additions & 7 deletions mithril-client-cli/src/utils/unpacker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,10 @@ impl SnapshotUnpacker {
#[cfg(test)]
mod test {
use super::*;
use std::fs::remove_dir_all;
use mithril_common::test_utils::TempDir;

fn create_temporary_empty_directory(name: &str) -> PathBuf {
let pathdir = std::env::temp_dir().join(name);
if pathdir.exists() {
remove_dir_all(&pathdir).unwrap();
}
create_dir_all(&pathdir).unwrap();
pathdir
TempDir::create("client-cli", name)
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion mithril-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client"
version = "0.6.4"
version = "0.6.5"
description = "Mithril client library"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
11 changes: 2 additions & 9 deletions mithril-client/tests/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ pub mod fake;
pub mod mock;
mod routes;

use mithril_common::test_utils::TempDir;
use std::path::PathBuf;

pub fn get_test_dir(subdir_name: &str) -> PathBuf {
let dir = std::env::temp_dir().join("mithril_test").join(subdir_name);

if dir.exists() {
std::fs::remove_dir_all(&dir)
.unwrap_or_else(|e| panic!("Could not remove dir {dir:?}: {e}"));
}
std::fs::create_dir_all(&dir).unwrap_or_else(|e| panic!("Could not create dir {dir:?}: {e}"));

dir
TempDir::create("client_integration", subdir_name)
}

pub fn test_logger() -> slog::Logger {
Expand Down
2 changes: 1 addition & 1 deletion mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-common"
version = "0.3.8"
version = "0.3.9"
description = "Common types, interfaces, and utilities for Mithril nodes."
authors = { workspace = true }
edition = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions mithril-common/src/chain_observer/cli_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ impl ChainObserver for CardanoCliChainObserver {
) -> Result<Option<KESPeriod>, ChainObserverError> {
let dir = std::env::temp_dir().join("mithril_kes_period");
jpraynaud marked this conversation as resolved.
Show resolved Hide resolved
fs::create_dir_all(&dir).map_err(|e| ChainObserverError::General(e.into()))?;
let opcert_file =
std::env::temp_dir().join(format!("opcert_kes_period-{}", opcert.compute_hash()));
let opcert_file = dir.join(format!("opcert_kes_period-{}", opcert.compute_hash()));
opcert
.to_file(&opcert_file)
.map_err(|e| ChainObserverError::General(e.into()))?;
Expand Down
17 changes: 2 additions & 15 deletions mithril-common/src/chain_observer/pallas_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ mod tests {
use tokio::net::UnixListener;

use super::*;
use crate::test_utils::TempDir;
use crate::{chain_observer::test_cli_runner::TestCliRunner, CardanoNetwork};

fn get_fake_utxo_by_address() -> UTxOByAddress {
Expand Down Expand Up @@ -463,21 +464,7 @@ mod tests {

/// Creates a new work directory in the system's temporary folder.
fn create_temp_dir(folder_name: &str) -> PathBuf {
#[cfg(not(target_os = "macos"))]
let temp_dir = std::env::temp_dir()
.join("mithril_test")
.join("pallas_chain_observer_test")
.join(folder_name);

// macOS-domain addresses are variable-length filesystem pathnames of at most 104 characters.
#[cfg(target_os = "macos")]
let temp_dir: PathBuf = std::env::temp_dir().join(folder_name);

if temp_dir.exists() {
fs::remove_dir_all(&temp_dir).expect("Previous work dir removal failed");
}
fs::create_dir_all(&temp_dir).expect("Work dir creation failed");
temp_dir
TempDir::create_with_short_path("pallas_chain_observer_test", folder_name)
}

/// Sets up a mock server.
Expand Down
4 changes: 2 additions & 2 deletions mithril-common/src/crypto_helper/cardano/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ impl<'a> TryFrom<&'a mut Sum6KesBytes> for Sum6Kes<'a> {
#[cfg(test)]
mod test {
use super::*;
use crate::test_utils::TempDir;

#[test]
fn compat_with_shelly_format() {
let temp_dir = std::env::temp_dir().join("testing");
fs::create_dir_all(&temp_dir).expect("temp dir creation should not fail");
let temp_dir = TempDir::create("crypto_helper", "compat_with_shelly_format");
let sk_dir = temp_dir.join("dummy.skey");
let cbor_string = "590260fe77acdfa56281e4b05198f5136018057a65f425411f0990cac4aca0f2917aa00a3d51e191f6f425d870aca3c6a2a41833621f5729d7bc0e3dfc3ae77d057e5e1253b71def7a54157b9f98973ca3c49edd9f311e5f4b23ac268b56a6ac040c14c6d2217925492e42f00dc89a2a01ff363571df0ca0db5ba37001cee56790cc01cd69c6aa760fca55a65a110305ea3c11da0a27be345a589329a584ebfc499c43c55e8c6db5d9c0b014692533ee78abd7ac1e79f7ec9335c7551d31668369b4d5111db78072f010043e35e5ca7f11acc3c05b26b9c7fe56f02aa41544f00cb7685e87f34c73b617260ade3c7b8d8c4df46693694998f85ad80d2cbab0b575b6ccd65d90574e84368169578bff57f751bc94f7eec5c0d7055ec88891a69545eedbfbd3c5f1b1c1fe09c14099f6b052aa215efdc5cb6cdc84aa810db41dbe8cb7d28f7c4beb75cc53915d3ac75fc9d0bf1c734a46e401e15150c147d013a938b7e07cc4f25a582b914e94783d15896530409b8acbe31ef471de8a1988ac78dfb7510729eff008084885f07df870b65e4f382ca15908e1dcda77384b5c724350de90cec22b1dcbb1cdaed88da08bb4772a82266ec154f5887f89860d0920dba705c45957ef6d93e42f6c9509c966277d368dd0eefa67c8147aa15d40a222f7953a4f34616500b310d00aa1b5b73eb237dc4f76c0c16813d321b2fc5ac97039be25b22509d1201d61f4ccc11cd4ff40fffe39f0e937b4722074d8e073a775d7283b715d46f79ce128e3f1362f35615fa72364d20b6db841193d96e58d9d8e86b516bbd1f05e45b39823a93f6e9f29d9e01acf2c12c072d1c64e0afbbabf6903ef542e".to_string();

Expand Down
13 changes: 6 additions & 7 deletions mithril-common/src/crypto_helper/cardano/key_certification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pub enum ProtocolInitializerErrorWrapper {
#[error("Period of key file, {0}, does not match with period provided by user, {1}")]
KesMismatch(KESPeriod, KESPeriod),
}

/// Wrapper structure for [MithrilStm:StmInitializer](mithril_stm::stm::StmInitializer).
/// It now obtains a KES signature over the Mithril key. This allows the signers prove
/// their correct identity with respect to a Cardano PoolID.
Expand Down Expand Up @@ -303,22 +304,20 @@ impl KeyRegWrapper {

#[cfg(test)]
mod test {

use super::*;
use crate::crypto_helper::{cardano::ColdKeyGenerator, OpCert};

use crate::test_utils::TempDir;
use rand_chacha::ChaCha20Rng;
use rand_core::SeedableRng;
use std::{fs, path::PathBuf};
use std::path::PathBuf;

fn setup_temp_directory() -> PathBuf {
let temp_dir = std::env::temp_dir().join("mithril_cardano_key_certification");
fs::create_dir_all(&temp_dir).expect("temp dir creation should not fail");
temp_dir
fn setup_temp_directory(test_name: &str) -> PathBuf {
TempDir::create("mithril_cardano_key_certification", test_name)
}

fn create_cryptographic_material(party_idx: u64) -> (ProtocolPartyId, PathBuf, PathBuf) {
let temp_dir = setup_temp_directory();
let temp_dir = setup_temp_directory(&format!("create_cryptographic_material_{party_idx}"));
let keypair = ColdKeyGenerator::create_deterministic_keypair([party_idx as u8; 32]);
let mut dummy_buffer = [0u8; Sum6Kes::SIZE + 4];
let mut dummy_seed = [party_idx as u8; 32];
Expand Down
Loading
Loading