use bssh::executor::{DownloadResult, ParallelExecutor, UploadResult};
use bssh::node::Node;
use std::path::PathBuf;
use tempfile::TempDir;
#[tokio::test]
async fn test_upload_result_is_success() {
let node = Node::new("localhost".to_string(), 22, "test".to_string());
let success_result = UploadResult {
node: node.clone(),
result: Ok(()),
};
assert!(success_result.is_success());
let failure_result = UploadResult {
node: node.clone(),
result: Err(anyhow::anyhow!("Upload failed")),
};
assert!(!failure_result.is_success());
}
#[tokio::test]
async fn test_download_result_is_success() {
let node = Node::new("localhost".to_string(), 22, "test".to_string());
let success_result = DownloadResult {
node: node.clone(),
result: Ok(PathBuf::from("/tmp/downloaded_file")),
};
assert!(success_result.is_success());
let failure_result = DownloadResult {
node: node.clone(),
result: Err(anyhow::anyhow!("Download failed")),
};
assert!(!failure_result.is_success());
}
#[tokio::test]
async fn test_parallel_executor_creation() {
let nodes = vec![
Node::new("host1".to_string(), 22, "user1".to_string()),
Node::new("host2".to_string(), 2222, "user2".to_string()),
];
let _executor = ParallelExecutor::new(nodes.clone(), 10, Some("/path/to/key".to_string()));
}
#[tokio::test]
async fn test_upload_result_print_summary() {
let node = Node::new("test-host".to_string(), 22, "user".to_string());
let success_result = UploadResult {
node: node.clone(),
result: Ok(()),
};
success_result.print_summary();
let failure_result = UploadResult {
node: node.clone(),
result: Err(anyhow::anyhow!("Connection refused")),
};
failure_result.print_summary();
}
#[tokio::test]
async fn test_download_result_print_summary() {
let node = Node::new("test-host".to_string(), 22, "user".to_string());
let temp_dir = TempDir::new().unwrap();
let download_path = temp_dir.path().join("downloaded_file.txt");
let success_result = DownloadResult {
node: node.clone(),
result: Ok(download_path.clone()),
};
success_result.print_summary();
let failure_result = DownloadResult {
node: node.clone(),
result: Err(anyhow::anyhow!("File not found")),
};
failure_result.print_summary();
}
#[cfg(test)]
mod mock_tests {
use super::*;
#[tokio::test]
async fn test_executor_with_invalid_host() {
let nodes = vec![Node::new(
"nonexistent.invalid.host".to_string(),
22,
"user".to_string(),
)];
let executor = ParallelExecutor::new(nodes, 1, None);
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.txt");
std::fs::write(&test_file, "test content").unwrap();
let results = executor
.upload_file(&test_file, "/tmp/remote_test.txt")
.await;
assert!(results.is_ok());
let results = results.unwrap();
assert_eq!(results.len(), 1);
assert!(!results[0].is_success());
}
#[tokio::test]
async fn test_executor_with_invalid_download() {
let nodes = vec![Node::new(
"nonexistent.invalid.host".to_string(),
22,
"user".to_string(),
)];
let executor = ParallelExecutor::new(nodes, 1, None);
let temp_dir = TempDir::new().unwrap();
let results = executor
.download_file("/nonexistent/file.txt", temp_dir.path())
.await;
assert!(results.is_ok());
let results = results.unwrap();
assert_eq!(results.len(), 1);
assert!(!results[0].is_success());
}
}