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

feat: provide smoke-test command #10

Merged
merged 1 commit into from
Sep 21, 2023
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ inquire = "0.6.2"
rand = "0.8.5"
regex = "1.9.5"
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
sha2 = "0.10.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0.23"
tar = "0.4"
tempfile = "3.8.0"
tokio = { version = "1.26", features = ["full"] }
tokio-stream = "0.1.14"

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum Error {
SerdeJson(#[from] serde_json::Error),
#[error("An unexpected error occurred during the setup process")]
SetupError,
#[error("Smoke test failed for this testnet: {0}")]
SmokeTestFailed(String),
#[error("SSH command failed: {0}")]
SshCommandFailed(String),
#[error("After several retry attempts an SSH connection could not be established")]
Expand Down
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::ssh::{SshClient, SshClientInterface};
use crate::terraform::{TerraformRunner, TerraformRunnerInterface};
use flate2::read::GzDecoder;
use log::debug;
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
Expand Down Expand Up @@ -62,8 +63,9 @@ impl CloudProvider {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeploymentInventory {
pub name: String,
pub branch_info: (String, String),
pub vm_list: Vec<(String, String)>,
pub ssh_user: String,
Expand Down Expand Up @@ -91,13 +93,25 @@ impl DeploymentInventory {
self.uploaded_files.extend_from_slice(&uploaded_files);
}

pub fn get_random_peer(&self) -> String {
let mut rng = rand::thread_rng();
let i = rng.gen_range(0..self.peers.len());
let random_peer = &self.peers[i];
random_peer.to_string()
}

pub fn print_report(&self) {
println!("**************************************");
println!("* *");
println!("* Inventory Report *");
println!("* *");
println!("**************************************");

println!("Branch details");
println!("==============");
println!("Repo owner: {}", self.branch_info.0);
println!("Branch name: {}", self.branch_info.1);

for vm in self.vm_list.iter() {
println!("{}: {}", vm.0, vm.1);
}
Expand Down Expand Up @@ -611,6 +625,7 @@ impl TestnetDeploy {
}

let inventory = DeploymentInventory {
name: name.to_string(),
branch_info: custom_branch_info.unwrap_or(("maidsafe".to_string(), "main".to_string())),
vm_list,
ssh_user: self.cloud_provider.get_ssh_user(),
Expand Down
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ enum Commands {
#[clap(name = "logstash", subcommand)]
Logstash(LogstashCommands),
Setup {},
/// Run a smoke test against a given network.
#[clap(name = "smoke-test")]
SmokeTest {
/// The name of the environment
#[arg(short = 'n', long)]
name: String,
},
/// Clean a deployed testnet environment.
#[clap(name = "upload-test-data")]
UploadTestData {
Expand Down Expand Up @@ -331,6 +338,18 @@ async fn main() -> Result<()> {
setup_dotenv_file()?;
Ok(())
}
Some(Commands::SmokeTest { name }) => {
let inventory_path = get_data_directory()?.join(format!("{name}-inventory.json"));
if !inventory_path.exists() {
return Err(eyre!("There is no inventory for the {name} testnet")
.suggestion("Please run the inventory command to generate it"));
}

let inventory = DeploymentInventory::read(&inventory_path)?;
let test_data_client = TestDataClientBuilder::default().build()?;
test_data_client.smoke_test(inventory).await?;
Ok(())
}
Some(Commands::UploadTestData { name }) => {
let inventory_path = get_data_directory()?.join(format!("{name}-inventory.json"));
if !inventory_path.exists() {
Expand Down
127 changes: 118 additions & 9 deletions src/manage_test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ use crate::error::{Error, Result};
use crate::get_and_extract_archive;
use crate::s3::{S3Repository, S3RepositoryInterface};
use crate::safe::{SafeClient, SafeClientInterface};
use crate::DeploymentInventory;
use rand::Rng;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Write};
use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

pub struct TestDataClient {
pub working_directory_path: PathBuf,
Expand Down Expand Up @@ -69,25 +76,95 @@ impl TestDataClient {
}
}

pub async fn smoke_test(&self, inventory: DeploymentInventory) -> Result<()> {
Self::download_and_extract_safe_client(
&*self.s3_repository,
&inventory.name,
&self.working_directory_path,
&inventory.branch_info.0,
&inventory.branch_info.1,
)
.await?;

let faucet_addr: SocketAddr = inventory.faucet_address.parse()?;
let random_peer = inventory.get_random_peer();
self.safe_client
.wallet_get_faucet(&random_peer, faucet_addr)?;

// Generate 10 random files to be uploaded, increasing in size from 1 to 10k.
// They will then be re-downloaded by `safe` and compared to make sure they are right.
let mut file_hash_map = HashMap::new();
let temp_dir_path = tempfile::tempdir()?.into_path();
for i in 1..=10 {
let file_size = i * 1024;
let mut rng = rand::thread_rng();
let content: Vec<u8> = (0..file_size).map(|_| rng.gen()).collect();

let mut hasher = Sha256::new();
hasher.update(&content);
let hash = format!("{:x}", hasher.finalize());

let file_path = temp_dir_path.join(format!("file_{}.bin", i));
let mut file = File::create(&file_path)?;
file.write_all(&content)?;
let file_name = file_path
.clone()
.file_name()
.unwrap()
.to_string_lossy()
.to_string();
file_hash_map.insert(file_name, hash);

self.safe_client.upload_file(&random_peer, &file_path)?;
}

self.safe_client.download_files(&random_peer)?;

let downloaded_files_path = Self::get_downloaded_files_dir_path()?;
for entry in std::fs::read_dir(downloaded_files_path)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let mut file = File::open(&path)?;
let mut content = Vec::new();
file.read_to_end(&mut content)?;

let mut hasher = Sha256::new();
hasher.update(&content);
let hash = format!("{:x}", hasher.finalize());
let file_name = path.file_name().unwrap().to_string_lossy().to_string();

if let Some(stored_hash) = file_hash_map.get(&file_name) {
if *stored_hash == hash {
println!("Hash match for file {}", file_name);
} else {
return Err(Error::SmokeTestFailed(format!(
"Hash mismatch for file {}",
file_name
)));
}
}
}
}

Ok(())
}

pub async fn upload_test_data(
&self,
name: &str,
peer_multiaddr: &str,
branch_info: (String, String),
) -> Result<Vec<(String, String)>> {
let (repo_owner, branch) = branch_info;
println!("Downloading the safe client from S3...");
get_and_extract_archive(
Self::download_and_extract_safe_client(
&*self.s3_repository,
"sn-node",
&format!("{repo_owner}/{branch}/safe-{name}-x86_64-unknown-linux-musl.tar.gz"),
name,
&self.working_directory_path,
&repo_owner,
&branch,
)
.await?;
let safe_client_path = self.working_directory_path.join("safe");
let mut permissions = std::fs::metadata(&safe_client_path)?.permissions();
permissions.set_mode(0o755); // rwxr-xr-x
std::fs::set_permissions(&safe_client_path, permissions)?;

println!("Downloading test data archive from S3...");
let test_data_dir_path = &self.working_directory_path.join("test-data");
Expand Down Expand Up @@ -127,4 +204,36 @@ impl TestDataClient {

Ok(uploaded_files)
}

fn get_downloaded_files_dir_path() -> Result<PathBuf> {
Ok(dirs_next::data_dir()
.ok_or_else(|| Error::CouldNotRetrieveDataDirectory)?
.join("safe")
.join("client")
.join("downloaded_files"))
}

async fn download_and_extract_safe_client(
s3_repository: &dyn S3RepositoryInterface,
name: &str,
working_directory_path: &Path,
repo_owner: &str,
branch: &str,
) -> Result<()> {
let safe_client_path = working_directory_path.join("safe");
if !safe_client_path.exists() {
println!("Downloading the safe client from S3...");
get_and_extract_archive(
s3_repository,
"sn-node",
&format!("{repo_owner}/{branch}/safe-{name}-x86_64-unknown-linux-musl.tar.gz"),
working_directory_path,
)
.await?;
let mut permissions = std::fs::metadata(&safe_client_path)?.permissions();
permissions.set_mode(0o755); // rwxr-xr-x
std::fs::set_permissions(&safe_client_path, permissions)?;
}
Ok(())
}
}
34 changes: 34 additions & 0 deletions src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::run_external_command;
#[cfg(test)]
use mockall::automock;
use regex::Regex;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};

/// Provides an interface for using the `safe` client.
Expand All @@ -17,6 +18,8 @@ use std::path::{Path, PathBuf};
/// safe process.
#[cfg_attr(test, automock)]
pub trait SafeClientInterface {
fn wallet_get_faucet(&self, peer_multiaddr: &str, faucet_addr: SocketAddr) -> Result<()>;
fn download_files(&self, peer_multiaddr: &str) -> Result<()>;
fn upload_file(&self, peer_multiaddr: &str, path: &Path) -> Result<String>;
}

Expand All @@ -34,6 +37,21 @@ impl SafeClient {
}

impl SafeClientInterface for SafeClient {
fn download_files(&self, peer_multiaddr: &str) -> Result<()> {
run_external_command(
self.binary_path.clone(),
self.working_directory_path.clone(),
vec![
"--peer".to_string(),
peer_multiaddr.to_string(),
"files".to_string(),
"download".to_string(),
],
false,
)?;
Ok(())
}

fn upload_file(&self, peer_multiaddr: &str, path: &Path) -> Result<String> {
let output = run_external_command(
self.binary_path.clone(),
Expand All @@ -59,4 +77,20 @@ impl SafeClientInterface for SafeClient {
"could not obtain hex address of uploaded file".to_string(),
))
}

fn wallet_get_faucet(&self, peer_multiaddr: &str, faucet_addr: SocketAddr) -> Result<()> {
run_external_command(
self.binary_path.clone(),
self.working_directory_path.clone(),
vec![
"--peer".to_string(),
peer_multiaddr.to_string(),
"wallet".to_string(),
"get-faucet".to_string(),
faucet_addr.to_string(),
],
false,
)?;
Ok(())
}
}
Loading