feat: Add unit tests for CLI and MasterConnector, including certificate extraction and validation
This commit is contained in:
@@ -318,6 +318,14 @@ where
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{
|
||||
fs,
|
||||
os::unix::fs::PermissionsExt,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -330,4 +338,223 @@ mod tests {
|
||||
assert_send_sync::<LogSettings>();
|
||||
assert_send_sync::<NginxSettings>();
|
||||
}
|
||||
|
||||
fn write_file(path: &Path) {
|
||||
let result = fs::write(path, b"content");
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
fn create_exec_file(path: &Path) {
|
||||
write_file(path);
|
||||
let metadata = fs::metadata(path);
|
||||
assert!(metadata.is_ok());
|
||||
let metadata = metadata.ok();
|
||||
assert!(metadata.is_some());
|
||||
let metadata = metadata.unwrap_or_else(|| unreachable!());
|
||||
|
||||
let mut perms = metadata.permissions();
|
||||
perms.set_mode(0o755);
|
||||
let result = fs::set_permissions(path, perms);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
fn create_non_exec_file(path: &Path) {
|
||||
write_file(path);
|
||||
let metadata = fs::metadata(path);
|
||||
assert!(metadata.is_ok());
|
||||
let metadata = metadata.ok();
|
||||
assert!(metadata.is_some());
|
||||
let metadata = metadata.unwrap_or_else(|| unreachable!());
|
||||
|
||||
let mut perms = metadata.permissions();
|
||||
perms.set_mode(0o644);
|
||||
let result = fs::set_permissions(path, perms);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
fn valid_tls_raw_paths(temp_dir: &TempDir) -> (PathBuf, PathBuf, PathBuf) {
|
||||
let ca_path = temp_dir.path().join("ca.pem");
|
||||
let cert_path = temp_dir.path().join("cert.pem");
|
||||
let key_path = temp_dir.path().join("key.pem");
|
||||
|
||||
write_file(&ca_path);
|
||||
write_file(&cert_path);
|
||||
write_file(&key_path);
|
||||
|
||||
(ca_path, cert_path, key_path)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_raw_path_validate_succeeds_when_all_files_exist() {
|
||||
let temp_dir = TempDir::new();
|
||||
assert!(temp_dir.is_ok());
|
||||
let temp_dir = temp_dir.ok();
|
||||
assert!(temp_dir.is_some());
|
||||
let temp_dir = temp_dir.unwrap_or_else(|| unreachable!());
|
||||
|
||||
let (ca_path, cert_path, key_path) = valid_tls_raw_paths(&temp_dir);
|
||||
let settings = TLSSettings::RawPath {
|
||||
ca_path: ca_path.to_string_lossy().to_string(),
|
||||
cert_path: cert_path.to_string_lossy().to_string(),
|
||||
key_path: key_path.to_string_lossy().to_string(),
|
||||
};
|
||||
|
||||
assert!(settings.validate().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_raw_path_validate_fails_when_ca_missing() {
|
||||
let settings = TLSSettings::RawPath {
|
||||
ca_path: "/tmp/does-not-exist-ca.pem".into(),
|
||||
cert_path: "/tmp/does-not-exist-cert.pem".into(),
|
||||
key_path: "/tmp/does-not-exist-key.pem".into(),
|
||||
};
|
||||
|
||||
let result = settings.validate();
|
||||
assert!(result.is_err());
|
||||
let msg = result.err().unwrap_or_else(|| unreachable!());
|
||||
assert!(msg.contains("CA file not found"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_zip_path_validate_fails_when_zip_missing() {
|
||||
let settings = TLSSettings::ZipPath {
|
||||
cert_zip_path: "/tmp/missing-certs.zip".into(),
|
||||
};
|
||||
|
||||
let result = settings.validate();
|
||||
assert!(result.is_err());
|
||||
let msg = result.err().unwrap_or_else(|| unreachable!());
|
||||
assert!(msg.contains("Certificate zip file not found"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grpc_validate_fails_when_connection_string_empty() {
|
||||
let settings = GrpcSettings {
|
||||
connection_string: "".into(),
|
||||
m_auth: MAuthSettings::Tls(TLSSettings::ZipPath {
|
||||
cert_zip_path: "/tmp/does-not-exist.zip".into(),
|
||||
}),
|
||||
cors: None,
|
||||
};
|
||||
|
||||
let result = settings.validate();
|
||||
assert!(result.is_err());
|
||||
let msg = result.err().unwrap_or_else(|| unreachable!());
|
||||
assert!(msg.contains("gRPC connection string cannot be empty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nginx_validate_succeeds_for_valid_paths_and_commands() {
|
||||
let temp_dir = TempDir::new();
|
||||
assert!(temp_dir.is_ok());
|
||||
let temp_dir = temp_dir.ok();
|
||||
assert!(temp_dir.is_some());
|
||||
let temp_dir = temp_dir.unwrap_or_else(|| unreachable!());
|
||||
|
||||
let nginx_binary = temp_dir.path().join("nginx");
|
||||
let nginx_config = temp_dir.path().join("nginx.conf");
|
||||
|
||||
create_exec_file(&nginx_binary);
|
||||
write_file(&nginx_config);
|
||||
|
||||
let nginx = NginxSettings {
|
||||
nginx_config_path: nginx_config.to_string_lossy().to_string(),
|
||||
nginx_binary_path: Some(nginx_binary.to_string_lossy().to_string()),
|
||||
override_nginx_reload_command: default_nginx_reload_command(),
|
||||
override_nginx_test_command: default_nginx_test_command(),
|
||||
nginx_reload_timeout_seconds: 30,
|
||||
nginx_test_timeout_seconds: 30,
|
||||
};
|
||||
|
||||
assert!(nginx.validate().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nginx_validate_fails_for_non_executable_binary() {
|
||||
let temp_dir = TempDir::new();
|
||||
assert!(temp_dir.is_ok());
|
||||
let temp_dir = temp_dir.ok();
|
||||
assert!(temp_dir.is_some());
|
||||
let temp_dir = temp_dir.unwrap_or_else(|| unreachable!());
|
||||
|
||||
let nginx_binary = temp_dir.path().join("nginx");
|
||||
let nginx_config = temp_dir.path().join("nginx.conf");
|
||||
|
||||
create_non_exec_file(&nginx_binary);
|
||||
write_file(&nginx_config);
|
||||
|
||||
let nginx = NginxSettings {
|
||||
nginx_config_path: nginx_config.to_string_lossy().to_string(),
|
||||
nginx_binary_path: Some(nginx_binary.to_string_lossy().to_string()),
|
||||
override_nginx_reload_command: default_nginx_reload_command(),
|
||||
override_nginx_test_command: default_nginx_test_command(),
|
||||
nginx_reload_timeout_seconds: 30,
|
||||
nginx_test_timeout_seconds: 30,
|
||||
};
|
||||
|
||||
let result = nginx.validate();
|
||||
assert!(result.is_err());
|
||||
let msg = result.err().unwrap_or_else(|| unreachable!());
|
||||
assert!(msg.contains("Nginx binary is not executable"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nginx_validate_fails_when_reload_command_lacks_template() {
|
||||
let temp_dir = TempDir::new();
|
||||
assert!(temp_dir.is_ok());
|
||||
let temp_dir = temp_dir.ok();
|
||||
assert!(temp_dir.is_some());
|
||||
let temp_dir = temp_dir.unwrap_or_else(|| unreachable!());
|
||||
|
||||
let nginx_binary = temp_dir.path().join("nginx");
|
||||
let nginx_config = temp_dir.path().join("nginx.conf");
|
||||
|
||||
create_exec_file(&nginx_binary);
|
||||
write_file(&nginx_config);
|
||||
|
||||
let nginx = NginxSettings {
|
||||
nginx_config_path: nginx_config.to_string_lossy().to_string(),
|
||||
nginx_binary_path: Some(nginx_binary.to_string_lossy().to_string()),
|
||||
override_nginx_reload_command: vec!["nginx".into(), "-s".into(), "reload".into()],
|
||||
override_nginx_test_command: default_nginx_test_command(),
|
||||
nginx_reload_timeout_seconds: 30,
|
||||
nginx_test_timeout_seconds: 30,
|
||||
};
|
||||
|
||||
let result = nginx.validate();
|
||||
assert!(result.is_err());
|
||||
let msg = result.err().unwrap_or_else(|| unreachable!());
|
||||
assert!(msg.contains("Nginx reload command must contain the binary path template"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn level_filter_round_trip_serialization() {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Wrapper {
|
||||
#[serde(
|
||||
deserialize_with = "deserialize_level_filter",
|
||||
serialize_with = "serialize_level_filter"
|
||||
)]
|
||||
level: LevelFilter,
|
||||
}
|
||||
|
||||
let original = Wrapper {
|
||||
level: LevelFilter::DEBUG,
|
||||
};
|
||||
|
||||
let encoded = serde_json::to_string(&original);
|
||||
assert!(encoded.is_ok());
|
||||
let encoded = encoded.ok();
|
||||
assert!(encoded.is_some());
|
||||
let encoded = encoded.unwrap_or_else(|| unreachable!());
|
||||
assert!(encoded.to_lowercase().contains("debug"));
|
||||
|
||||
let decoded = serde_json::from_str::<Wrapper>(&encoded);
|
||||
assert!(decoded.is_ok());
|
||||
let decoded = decoded.ok();
|
||||
assert!(decoded.is_some());
|
||||
let decoded = decoded.unwrap_or_else(|| unreachable!());
|
||||
assert_eq!(decoded.level, LevelFilter::DEBUG);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user