Skip to content

Commit

Permalink
style: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
255doesnotexist committed Sep 7, 2024
1 parent 4709b77 commit e7a20c6
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 49 deletions.
4 changes: 1 addition & 3 deletions src/config/connection_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/// Represents the configuration for a connection.
///
/// This struct is used to store the connection details such as the method, IP address, port, username, and password.
Expand All @@ -12,7 +11,6 @@
/// - `username`: An optional string representing the username.
/// - `password`: An optional string representing the password.
///
use serde::Deserialize;

#[derive(Clone, Debug, Deserialize)]
Expand All @@ -22,4 +20,4 @@ pub struct ConnectionConfig {
pub port: Option<u16>,
pub username: Option<String>,
pub password: Option<String>,
}
}
5 changes: 2 additions & 3 deletions src/config/distro_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::connection_config::ConnectionConfig;
/// Represents the configuration for each distro.
///
/// This struct is used to deserialize the configuration from a file using the `from_file` method.
Expand All @@ -8,17 +9,15 @@
/// - `connection`: An instance of `ConnectionConfig` struct representing the connection configuration.
/// - `skip_packages`: An optional vector of strings representing the packages to be skipped.
///
use serde::Deserialize;
use std::fs;
use crate::config::connection_config::ConnectionConfig;

#[derive(Debug, Deserialize)]
pub struct DistroConfig {
pub testing_type: String, // 'locally' or 'remote' or 'qemu-based-remote'
#[serde(rename = "startup_script")]
#[serde(default, skip_serializing_if = "is_not_qemu_based_remote")]
pub startup_script: String,
pub startup_script: String,

#[serde(rename = "stop_script")]
#[serde(default, skip_serializing_if = "is_not_qemu_based_remote")]
Expand Down
4 changes: 2 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod root_config;
pub mod connection_config;
pub mod distro_config;
pub mod distro_config;
pub mod root_config;
3 changes: 1 addition & 2 deletions src/config/root_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
/// }
/// }
/// ```
use serde::Deserialize;
use std::fs;

Expand All @@ -39,4 +38,4 @@ impl Config {
let config: Config = toml::from_str(&content)?;
Ok(config)
}
}
}
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod testenv_manager;
mod testscript_manager;
mod utils;

use crate::config::{distro_config::DistroConfig, root_config::Config};
use crate::test_runner::{local::LocalTestRunner, remote::RemoteTestRunner, TestRunner};
use crate::config::{root_config::Config, distro_config::DistroConfig};
use clap::{Arg, ArgMatches, Command};
use std::fs::remove_file;
use std::path::Path;
Expand Down Expand Up @@ -166,7 +166,12 @@ fn run_tests(distros: &[&str], packages: &[&str], cleanup: bool, verbose: bool)
continue;
}

println!("Running test for {}/{}, {}.", distro, package, if run_locally {"locally"} else {"with QEMU"});
println!(
"Running test for {}/{}, {}.",
distro,
package,
if run_locally { "locally" } else { "with QEMU" }
);

let test_runner: Box<dyn TestRunner> = if run_locally {
Box::new(LocalTestRunner::new(distro, package, verbose))
Expand All @@ -190,7 +195,7 @@ fn run_tests(distros: &[&str], packages: &[&str], cleanup: bool, verbose: bool)
ip.to_string(),
port,
username.to_string(),
password.map(|p| p.to_string(),),
password.map(|p| p.to_string()),
verbose,
))
};
Expand Down
15 changes: 7 additions & 8 deletions src/test_runner/local.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::aggregator::generate_report;
use crate::test_runner::TestRunner;
use crate::testscript_manager::TestScriptManager;
use crate::utils::{Report, TestResult, REMOTE_TMP_DIR};
use crate::aggregator::generate_report;
use std::fs::read_to_string;
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -35,11 +35,7 @@ impl TestRunner for LocalTestRunner {
/// * Reading the package version from the temporary file fails.
/// * Generating the report fails.
/// * Not all tests passed for the given distribution and package.
fn run_test(
&self,
distro: &str,
package: &str,
) -> Result<(), Box<dyn std::error::Error>> {
fn run_test(&self, distro: &str, package: &str) -> Result<(), Box<dyn std::error::Error>> {
let script_manager = TestScriptManager::new(distro, package);

let os_version = read_to_string("/proc/version")?;
Expand All @@ -53,7 +49,10 @@ impl TestRunner for LocalTestRunner {
for script in script_manager?.get_test_scripts() {
let output = Command::new("bash")
.arg("-c")
.arg(&format!("source {} && echo -n $PACKAGE_VERSION > {}", script, pkgver_tmpfile))
.arg(&format!(
"source {} && echo -n $PACKAGE_VERSION > {}",
script, pkgver_tmpfile
))
.stdout(if self.verbose {
Stdio::inherit()
} else {
Expand Down Expand Up @@ -101,4 +100,4 @@ impl TestRunner for LocalTestRunner {

Ok(())
}
}
}
8 changes: 2 additions & 6 deletions src/test_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ pub mod local;
pub mod remote;

pub trait TestRunner {
fn run_test(
&self,
distro: &str,
package: &str,
) -> Result<(), Box<dyn std::error::Error>>;
}
fn run_test(&self, distro: &str, package: &str) -> Result<(), Box<dyn std::error::Error>>;
}
41 changes: 25 additions & 16 deletions src/test_runner/remote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::aggregator::generate_report;
use crate::test_runner::TestRunner;
use crate::testscript_manager::TestScriptManager;
use crate::aggregator::generate_report;
use crate::utils::{CommandOutput, Report, TempFile, TestResult, REMOTE_TMP_DIR};
use ssh2::Session;
use std::fs::File;
Expand All @@ -18,7 +18,13 @@ pub struct RemoteTestRunner {
}

impl RemoteTestRunner {
pub fn new(remote_ip: String, port: u16, username: String, password: Option<String>, verbose: bool) -> Self {
pub fn new(
remote_ip: String,
port: u16,
username: String,
password: Option<String>,
verbose: bool,
) -> Self {
RemoteTestRunner {
remote_ip,
port,
Expand Down Expand Up @@ -55,7 +61,6 @@ impl RemoteTestRunner {
}
}


/// Implements the `TestRunner` trait for the `RemoteTestRunner` struct.
///
/// This struct allows running tests on a remote server using SSH.
Expand All @@ -70,11 +75,7 @@ impl TestRunner for RemoteTestRunner {
/// # Errors
///
/// Returns an error if the test fails or encounters any issues.
fn run_test(
&self,
distro: &str,
package: &str,
) -> Result<(), Box<dyn std::error::Error>> {
fn run_test(&self, distro: &str, package: &str) -> Result<(), Box<dyn std::error::Error>> {
// 创建 SSH 会话
let tcp = TcpStream::connect((self.remote_ip.as_str(), self.port))?;
let mut sess = Session::new()?;
Expand Down Expand Up @@ -130,12 +131,20 @@ impl TestRunner for RemoteTestRunner {
// 上传 prerequisite.sh 到远程服务器
let prerequisite_path = format!("{}/prerequisite.sh", distro);
let remote_prerequisite_path = "/tmp/prerequisite.sh".to_string();
let mut remote_file = sess.scp_send(Path::new(&remote_prerequisite_path), 0o644, std::fs::metadata(&prerequisite_path)?.len(), None)?;
let mut remote_file = sess.scp_send(
Path::new(&remote_prerequisite_path),
0o644,
std::fs::metadata(&prerequisite_path)?.len(),
None,
)?;
let mut local_file = File::open(&prerequisite_path)?;
let mut buffer = Vec::new();
local_file.read_to_end(&mut buffer)?;
remote_file.write_all(&buffer)?;
self.print_ssh_msg(&format!("File {} uploaded to remote server", prerequisite_path));
self.print_ssh_msg(&format!(
"File {} uploaded to remote server",
prerequisite_path
));

// 确保远程文件在继续之前关闭
drop(remote_file);
Expand Down Expand Up @@ -173,7 +182,10 @@ impl TestRunner for RemoteTestRunner {
for script in script_manager?.get_test_scripts() {
let result = self.run_command(
&sess,
&format!("source {} && echo -n $PACKAGE_VERSION > {}", script, pkgver_tmpfile),
&format!(
"source {} && echo -n $PACKAGE_VERSION > {}",
script, pkgver_tmpfile
),
);
let test_passed = result.is_ok();
all_tests_passed &= test_passed;
Expand All @@ -194,10 +206,7 @@ impl TestRunner for RemoteTestRunner {
if all_tests_passed {
self.print_ssh_msg(&format!("Test successful for {}/{}", distro, package));
} else {
self.print_ssh_msg(&format!(
"Test failed for {}/{}",
distro, package
));
self.print_ssh_msg(&format!("Test failed for {}/{}", distro, package));
}
let report: Report = Report {
distro: distro.to_string(),
Expand Down Expand Up @@ -278,4 +287,4 @@ impl TestRunner for RemoteTestRunner {
}
Ok(())
}
}
}
2 changes: 1 addition & 1 deletion src/testenv_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ impl TestEnvManager {
pub fn stop(&self) -> Result<(), Error> {
self.run_script(&self.stop_script)
}
}
}
6 changes: 2 additions & 4 deletions src/testscript_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ impl TestScriptManager {
test_scripts.push(path.to_str().unwrap_or_default().to_string());
}
}
Ok(TestScriptManager {
test_scripts,
})
Ok(TestScriptManager { test_scripts })
}

/// Returns a slice containing the paths of all discovered test scripts.
Expand All @@ -71,4 +69,4 @@ impl TestScriptManager {
pub fn get_test_scripts(&self) -> &[String] {
&self.test_scripts
}
}
}
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ pub struct CommandOutput {
pub exit_status: i32,
/// The output (stdout and stderr) of the command.
pub output: String,
}
}

0 comments on commit e7a20c6

Please sign in to comment.