Replace custom logging with tracing crate and RUST_LOG env var
- Remove custom logging module and init_logging function - Update main.rs to use tracing_subscriber with EnvFilter - Remove log_level from global config structure - Update documentation and tests to use RUST_LOG - Format long lines in config.rs and test files for better readability
This commit is contained in:
parent
50a4d9bc75
commit
55fe47b172
15 changed files with 787 additions and 459 deletions
|
|
@ -7,87 +7,80 @@ fn test_missing_config_file() {
|
|||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg("nonexistent.toml")
|
||||
.env("RUST_LOG", "error")
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
let stdout = String::from_utf8(output.stdout).unwrap();
|
||||
assert!(stderr.contains("Config file 'nonexistent.toml' not found"));
|
||||
assert!(stderr.contains("Create the config file with") || stderr.contains("Add at least one"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_host_sections() {
|
||||
let temp_dir = common::setup_test_environment();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let config_content = r#"
|
||||
bind_host = "127.0.0.1"
|
||||
port = 1965
|
||||
# No host sections defined
|
||||
"#;
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&config_path)
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(stderr.contains("No host configurations found"));
|
||||
assert!(stderr.contains("Add at least one [hostname] section"));
|
||||
assert!(stdout.contains("Create the config file with"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nonexistent_root_directory() {
|
||||
let temp_dir = common::setup_test_environment();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let config_content = format!(r#"
|
||||
let config_content = format!(
|
||||
r#"
|
||||
bind_host = "127.0.0.1"
|
||||
|
||||
["example.com"]
|
||||
root = "/definitely/does/not/exist"
|
||||
cert = "{}"
|
||||
key = "{}"
|
||||
"#, temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display());
|
||||
"#,
|
||||
temp_dir.path().join("cert.pem").display(),
|
||||
temp_dir.path().join("key.pem").display()
|
||||
);
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&config_path)
|
||||
.env("RUST_LOG", "error")
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(stderr.contains("Error for host 'example.com': Root directory '/definitely/does/not/exist' does not exist"));
|
||||
assert!(stderr.contains("Create the directory and add your Gemini files (.gmi, .txt, images)"));
|
||||
assert!(stderr.contains("Failed to parse config file"));
|
||||
assert!(stderr.contains(
|
||||
"Error for host 'example.com': Root directory '/definitely/does/not/exist' does not exist"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_missing_certificate_file() {
|
||||
let temp_dir = common::setup_test_environment();
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let config_content = format!(r#"
|
||||
let config_content = format!(
|
||||
r#"
|
||||
bind_host = "127.0.0.1"
|
||||
|
||||
["example.com"]
|
||||
root = "{}"
|
||||
cert = "/nonexistent/cert.pem"
|
||||
key = "{}"
|
||||
"#, temp_dir.path().join("content").display(), temp_dir.path().join("key.pem").display());
|
||||
"#,
|
||||
temp_dir.path().join("content").display(),
|
||||
temp_dir.path().join("key.pem").display()
|
||||
);
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&config_path)
|
||||
.env("RUST_LOG", "error")
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(stderr.contains("Error for host 'example.com': Certificate file '/nonexistent/cert.pem' does not exist"));
|
||||
assert!(stderr.contains(
|
||||
"Error for host 'example.com': Certificate file '/nonexistent/cert.pem' does not exist"
|
||||
));
|
||||
assert!(stderr.contains("Generate or obtain TLS certificates for your domain"));
|
||||
}
|
||||
|
||||
|
|
@ -96,7 +89,8 @@ fn test_valid_config_startup() {
|
|||
let temp_dir = common::setup_test_environment();
|
||||
let port = 1967 + (std::process::id() % 1000) as u16;
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let config_content = format!(r#"
|
||||
let config_content = format!(
|
||||
r#"
|
||||
bind_host = "127.0.0.1"
|
||||
port = {}
|
||||
|
||||
|
|
@ -104,7 +98,12 @@ port = {}
|
|||
root = "{}"
|
||||
cert = "{}"
|
||||
key = "{}"
|
||||
"#, port, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display());
|
||||
"#,
|
||||
port,
|
||||
temp_dir.path().join("content").display(),
|
||||
temp_dir.path().join("cert.pem").display(),
|
||||
temp_dir.path().join("key.pem").display()
|
||||
);
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
|
|
@ -117,7 +116,10 @@ key = "{}"
|
|||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
|
||||
// Check server is still running (didn't exit with error)
|
||||
assert!(server_process.try_wait().unwrap().is_none(), "Server should still be running with valid config");
|
||||
assert!(
|
||||
server_process.try_wait().unwrap().is_none(),
|
||||
"Server should still be running with valid config"
|
||||
);
|
||||
|
||||
// Kill server
|
||||
server_process.kill().unwrap();
|
||||
|
|
@ -141,24 +143,38 @@ fn test_valid_multiple_hosts_startup() {
|
|||
// Generate certificate for host1
|
||||
let cert_result1 = std::process::Command::new("openssl")
|
||||
.args(&[
|
||||
"req", "-x509", "-newkey", "rsa:2048",
|
||||
"-keyout", &key1_path.to_string_lossy(),
|
||||
"-out", &cert1_path.to_string_lossy(),
|
||||
"-days", "1",
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-keyout",
|
||||
&key1_path.to_string_lossy(),
|
||||
"-out",
|
||||
&cert1_path.to_string_lossy(),
|
||||
"-days",
|
||||
"1",
|
||||
"-nodes",
|
||||
"-subj", "/CN=host1.com"
|
||||
"-subj",
|
||||
"/CN=host1.com",
|
||||
])
|
||||
.output();
|
||||
|
||||
// Generate certificate for host2
|
||||
let cert_result2 = std::process::Command::new("openssl")
|
||||
.args(&[
|
||||
"req", "-x509", "-newkey", "rsa:2048",
|
||||
"-keyout", &key2_path.to_string_lossy(),
|
||||
"-out", &cert2_path.to_string_lossy(),
|
||||
"-days", "1",
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-keyout",
|
||||
&key2_path.to_string_lossy(),
|
||||
"-out",
|
||||
&cert2_path.to_string_lossy(),
|
||||
"-days",
|
||||
"1",
|
||||
"-nodes",
|
||||
"-subj", "/CN=host2.com"
|
||||
"-subj",
|
||||
"/CN=host2.com",
|
||||
])
|
||||
.output();
|
||||
|
||||
|
|
@ -167,7 +183,8 @@ fn test_valid_multiple_hosts_startup() {
|
|||
}
|
||||
|
||||
let config_path = temp_dir.path().join("config.toml");
|
||||
let config_content = format!(r#"
|
||||
let config_content = format!(
|
||||
r#"
|
||||
bind_host = "127.0.0.1"
|
||||
port = {}
|
||||
|
||||
|
|
@ -181,13 +198,14 @@ root = "{}"
|
|||
cert = "{}"
|
||||
key = "{}"
|
||||
"#,
|
||||
port,
|
||||
temp_dir.path().join("host1").display(),
|
||||
cert1_path.display(),
|
||||
key1_path.display(),
|
||||
temp_dir.path().join("host2").display(),
|
||||
cert2_path.display(),
|
||||
key2_path.display());
|
||||
port,
|
||||
temp_dir.path().join("host1").display(),
|
||||
cert1_path.display(),
|
||||
key1_path.display(),
|
||||
temp_dir.path().join("host2").display(),
|
||||
cert2_path.display(),
|
||||
key2_path.display()
|
||||
);
|
||||
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
|
|
@ -201,7 +219,10 @@ key = "{}"
|
|||
std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
|
||||
// Check server is still running (didn't exit with error)
|
||||
assert!(server_process.try_wait().unwrap().is_none(), "Server should start with valid multiple host config");
|
||||
assert!(
|
||||
server_process.try_wait().unwrap().is_none(),
|
||||
"Server should start with valid multiple host config"
|
||||
);
|
||||
|
||||
// Kill server
|
||||
server_process.kill().unwrap();
|
||||
|
|
@ -222,12 +243,19 @@ fn test_multiple_hosts_missing_certificate() {
|
|||
|
||||
let cert_result = std::process::Command::new("openssl")
|
||||
.args(&[
|
||||
"req", "-x509", "-newkey", "rsa:2048",
|
||||
"-keyout", &key1_path.to_string_lossy(),
|
||||
"-out", &cert1_path.to_string_lossy(),
|
||||
"-days", "1",
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-keyout",
|
||||
&key1_path.to_string_lossy(),
|
||||
"-out",
|
||||
&cert1_path.to_string_lossy(),
|
||||
"-days",
|
||||
"1",
|
||||
"-nodes",
|
||||
"-subj", "/CN=host1.com"
|
||||
"-subj",
|
||||
"/CN=host1.com",
|
||||
])
|
||||
.output();
|
||||
|
||||
|
|
@ -235,7 +263,8 @@ fn test_multiple_hosts_missing_certificate() {
|
|||
panic!("Failed to generate test certificate");
|
||||
}
|
||||
|
||||
let config_content = format!(r#"
|
||||
let config_content = format!(
|
||||
r#"
|
||||
bind_host = "127.0.0.1"
|
||||
|
||||
["host1.com"]
|
||||
|
|
@ -248,22 +277,26 @@ root = "{}"
|
|||
cert = "/nonexistent/cert.pem"
|
||||
key = "/nonexistent/key.pem"
|
||||
"#,
|
||||
temp_dir.path().join("host1").display(),
|
||||
cert1_path.display(),
|
||||
key1_path.display(),
|
||||
temp_dir.path().join("host2").display());
|
||||
temp_dir.path().join("host1").display(),
|
||||
cert1_path.display(),
|
||||
key1_path.display(),
|
||||
temp_dir.path().join("host2").display()
|
||||
);
|
||||
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&config_path)
|
||||
.env("RUST_LOG", "error")
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(stderr.contains("Error for host 'host2.com': Certificate file '/nonexistent/cert.pem' does not exist"));
|
||||
assert!(stderr.contains(
|
||||
"Error for host 'host2.com': Certificate file '/nonexistent/cert.pem' does not exist"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -284,24 +317,38 @@ fn test_multiple_hosts_invalid_hostname() {
|
|||
// Generate certificate for valid host
|
||||
let cert_result1 = std::process::Command::new("openssl")
|
||||
.args(&[
|
||||
"req", "-x509", "-newkey", "rsa:2048",
|
||||
"-keyout", &key1_path.to_string_lossy(),
|
||||
"-out", &cert1_path.to_string_lossy(),
|
||||
"-days", "1",
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-keyout",
|
||||
&key1_path.to_string_lossy(),
|
||||
"-out",
|
||||
&cert1_path.to_string_lossy(),
|
||||
"-days",
|
||||
"1",
|
||||
"-nodes",
|
||||
"-subj", "/CN=valid.com"
|
||||
"-subj",
|
||||
"/CN=valid.com",
|
||||
])
|
||||
.output();
|
||||
|
||||
// Generate certificate for invalid host (hostname validation happens before cert validation)
|
||||
let cert_result2 = std::process::Command::new("openssl")
|
||||
.args(&[
|
||||
"req", "-x509", "-newkey", "rsa:2048",
|
||||
"-keyout", &key2_path.to_string_lossy(),
|
||||
"-out", &cert2_path.to_string_lossy(),
|
||||
"-days", "1",
|
||||
"req",
|
||||
"-x509",
|
||||
"-newkey",
|
||||
"rsa:2048",
|
||||
"-keyout",
|
||||
&key2_path.to_string_lossy(),
|
||||
"-out",
|
||||
&cert2_path.to_string_lossy(),
|
||||
"-days",
|
||||
"1",
|
||||
"-nodes",
|
||||
"-subj", "/CN=invalid.com"
|
||||
"-subj",
|
||||
"/CN=invalid.com",
|
||||
])
|
||||
.output();
|
||||
|
||||
|
|
@ -309,7 +356,8 @@ fn test_multiple_hosts_invalid_hostname() {
|
|||
panic!("Failed to generate test certificates");
|
||||
}
|
||||
|
||||
let config_content = format!(r#"
|
||||
let config_content = format!(
|
||||
r#"
|
||||
bind_host = "127.0.0.1"
|
||||
|
||||
["valid.com"]
|
||||
|
|
@ -322,22 +370,24 @@ root = "{}"
|
|||
cert = "{}"
|
||||
key = "{}"
|
||||
"#,
|
||||
temp_dir.path().join("validhost").display(),
|
||||
cert1_path.display(),
|
||||
key1_path.display(),
|
||||
temp_dir.path().join("invalidhost").display(),
|
||||
cert2_path.display(),
|
||||
key2_path.display());
|
||||
temp_dir.path().join("validhost").display(),
|
||||
cert1_path.display(),
|
||||
key1_path.display(),
|
||||
temp_dir.path().join("invalidhost").display(),
|
||||
cert2_path.display(),
|
||||
key2_path.display()
|
||||
);
|
||||
|
||||
std::fs::write(&config_path, config_content).unwrap();
|
||||
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
|
||||
.arg("--config")
|
||||
.arg(&config_path)
|
||||
.env("RUST_LOG", "error")
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(stderr.contains("Invalid hostname 'bad..host.com'. Hostnames must be valid DNS names."));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue