Skip to content

Commit

Permalink
fix(configs and etc.): further fix for connection_config
Browse files Browse the repository at this point in the history
- add Option<T> wrapper for connection_config, to make toml know it is optional
- change main and testenv_manager to match the Option<T> change
- add is_not_qemu_based_remote and is_not_remote  and modified skipping logic a bit
- add a bit more debugging logs at distro detection process
  • Loading branch information
255doesnotexist committed Oct 20, 2024
1 parent b76c25c commit 61906c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
17 changes: 11 additions & 6 deletions src/config/distro_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use crate::config::connection_config::ConnectionConfig;
///
use serde::Deserialize;

impl DistroConfig {
#[allow(dead_code)]
pub fn is_not_qemu_based_remote(value: &String) -> bool { // keep this function as it is, just for serde plz
value != "qemu-based-remote"
}
#[allow(dead_code)]
fn is_not_qemu_based_remote(value: &String) -> bool { // keep this function as it is, just for serde plz
value != "qemu-based-remote"
}

#[allow(dead_code)]
fn is_not_remote(value: &String) -> bool { // keep this function as it is, just for serde plz
value != "remote" && value != "qemu-based-remote"
}

#[derive(Debug, Deserialize)]
Expand All @@ -30,6 +33,8 @@ pub struct DistroConfig {
pub stop_script: String,

#[serde(rename = "connection")]
pub connection: ConnectionConfig,
#[serde(default, skip_serializing_if = "is_not_remote")]
pub connection: Option<ConnectionConfig>,

pub skip_packages: Option<Vec<String>>,
}
29 changes: 14 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ fn run_tests(distros: &[&str], packages: &[&str], skip_successful: bool, dir: &P

info!(
"Connection method: {}",
if run_locally {
"local"
if let Some(connection) = &distro_config.connection {
&connection.method
} else {
&distro_config.connection.method
"None"
}
);

Expand Down Expand Up @@ -259,18 +259,17 @@ fn run_tests(distros: &[&str], packages: &[&str], skip_successful: bool, dir: &P
} else {
// assert!(distro_config.connection.method == "ssh");

let ip = distro_config
.connection
.ip
.as_deref()
.unwrap_or("localhost");
let port = distro_config.connection.port.unwrap_or(2222);
let username = distro_config
.connection
.username
.as_deref()
.unwrap_or("root");
let password = distro_config.connection.password.as_deref();
let _connection_config = match &distro_config.connection {
Some(c) => c,
None => {
error!("No connection config found for {}", distro);
continue;
}
};
let ip = _connection_config.ip.as_deref().unwrap_or("localhost");
let port = _connection_config.port.unwrap_or(2222);
let username = _connection_config.username.as_deref().unwrap_or("root");
let password = _connection_config.password.as_deref();
debug!("Connecting to environment with credentials: IP={}, Port={}, Username={}, Password={}",ip,port,username,password.unwrap_or("None"));
Box::new(RemoteTestRunner::new(
ip.to_string(),
Expand Down
12 changes: 7 additions & 5 deletions src/testenv_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::process::Command;
pub struct TestEnvManager {
startup_script: String,
stop_script: String,
connection: ConnectionConfig,
connection: Option<ConnectionConfig>,
}

impl TestEnvManager {
Expand Down Expand Up @@ -45,6 +45,8 @@ impl TestEnvManager {
///
/// This function will return an error if the script fails to execute or returns a non-zero exit status.
fn run_script(&self, script: &String) -> Result<(), Error> {
let connection_unwrapped = self.connection.clone().unwrap();

Command::new("bash")
.arg(script)
.env_remove("USER")
Expand All @@ -53,17 +55,17 @@ impl TestEnvManager {
.env_remove("PORT")
.env(
"USER",
self.connection.username.as_deref().unwrap_or("root"),
connection_unwrapped.username.as_deref().unwrap_or("root"),
)
.env(
"PASSWORD",
self.connection.password.as_deref().unwrap_or(""),
connection_unwrapped.password.as_deref().unwrap_or(""),
)
.env(
"ADDRESS",
self.connection.ip.as_deref().unwrap_or("localhost"),
connection_unwrapped.ip.as_deref().unwrap_or("localhost"),
)
.env("PORT", self.connection.port.unwrap_or(2222).to_string())
.env("PORT", connection_unwrapped.port.unwrap_or(2222).to_string())
.spawn()?
.wait()?;
Ok(())
Expand Down
17 changes: 14 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This module provides common structures and utilities used across the project,
//! including report structures, temporary file management, and command output handling.

use log::debug;
use log::{debug, error};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
collections::HashSet,
Expand Down Expand Up @@ -133,7 +133,13 @@ where
T: DeserializeOwned,
{
let content = fs::read_to_string(path)?;
let config: T = toml::de::from_str(&content)?;
let config: T = match toml::de::from_str(&content) {
Ok(config) => config,
Err(e) => {
error!("Failed to parse TOML file: {}", e);
return Err(Box::new(e));
}
};
Ok(config)
}

Expand All @@ -153,21 +159,26 @@ where
/// Returns an error if directory traversal fails.
pub fn get_distros(dir: &Path) -> Result<Vec<String>, Box<dyn Error>> {
let mut distros = Vec::new();
debug!("Scanning distros in directory {}", dir.display());
for subdir in dir.read_dir()? {
let distro = subdir?;
debug!("Scanning subdirectory {}", distro.path().display());
let distro_dir_path = distro.path();
if distro_dir_path.is_dir() {
debug!("Discovered distro directory {}", distro_dir_path.display());
let distro_dir_name = distro.file_name().into_string().unwrap();
let distro_config_path = distro_dir_path.join("config.toml");
let distro_config: DistroConfig = match read_toml_from_file(&distro_config_path) {
Ok(config) => {
debug!("Discovered distro directory {}", distro_dir_path.display());
debug!("Loaded config for distro directory {}", distro_dir_path.display());
config
}
Err(_) => {
debug!("Cannot load config for distro directory {}", distro_dir_path.display());
continue;
}
};
debug!("Loaded config for distro {}: \n{:?}", distro_dir_name, distro_config);
if distro_config.enabled {
distros.push(distro_dir_name);
}
Expand Down

0 comments on commit 61906c0

Please sign in to comment.