Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beac0n committed Sep 30, 2024
1 parent 865ff19 commit 7ca3408
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 19 deletions.
95 changes: 93 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ release-build = ["openssl/vendored"]

[dev-dependencies]
rand = "0.8.5"
rusty-fork = "0.3.0"

[profile.release]
opt-level = "z" # Optimize for binary size
Expand Down
20 changes: 20 additions & 0 deletions src/config_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ mod tests {
default_config_path, default_ntp, default_socket_group, default_socket_user, ConfigServer,
};
use std::collections::HashMap;
use std::path::PathBuf;

#[test]
fn test_get_pem_files() {
let expected: Vec<PathBuf> = vec![];
assert_eq!(ConfigServer::get_pem_files(&PathBuf::from("/foo/bar/baz")), expected);
}

#[test]
fn test_get_pem_path() {
let config_server = ConfigServer {
config_dir: PathBuf::from("/foo/bar/baz"),
..Default::default()
};

assert_eq!(
config_server.get_pem_path().unwrap_err().to_string(),
r#"Could not find any .pem files in "/foo/bar/baz""#
);
}

#[test]
fn test_create_deserialize() {
Expand Down
43 changes: 26 additions & 17 deletions tests/server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
mod tests {
use ruroco::config_server::ConfigServer;
use ruroco::server::Server;
use rusty_fork::rusty_fork_test;
use std::env;
use std::path::PathBuf;

#[test]
fn test_create_invalid_pid() {
env::set_var("LISTEN_PID", "12345");

let config_dir =
env::current_dir().unwrap_or(PathBuf::from("/tmp")).join("tests").join("conf_dir");

let result = Server::create(
ConfigServer {
config_dir,
..Default::default()
},
None,
);

assert!(result.is_err());
assert_eq!(result.err().unwrap(), "LISTEN_PID was set, but not to our PID");
rusty_fork_test! {
#[test]
fn test_create_server_udp_socket() {
let result = ConfigServer::default().create_server_udp_socket(None).unwrap();
assert_eq!(format!("{result:?}"), "UdpSocket { addr: [::]:34020, fd: 3 }");
}

#[test]
fn test_create_invalid_pid() {
env::set_var("LISTEN_PID", "12345");

let config_dir =
env::current_dir().unwrap_or(PathBuf::from("/tmp")).join("tests").join("conf_dir");

let result = Server::create(
ConfigServer {
config_dir,
..Default::default()
},
None,
);

assert!(result.is_err());
assert_eq!(result.err().unwrap(), "LISTEN_PID was set, but not to our PID");
}
}

#[test]
Expand Down

0 comments on commit 7ca3408

Please sign in to comment.