fix settings and tests

This commit is contained in:
John Smith 2022-03-10 11:10:11 -05:00
parent bb82811975
commit b6db4f7b8c
2 changed files with 33 additions and 36 deletions

View File

@ -1,10 +1,10 @@
use crate::settings::*; use crate::settings::*;
use clap::{App, Arg, ArgMatches}; use clap::{Arg, ArgMatches, Command};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::str::FromStr; use std::str::FromStr;
fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap::Error> { fn do_clap_matches(default_config_path: &OsStr) -> Result<clap::ArgMatches, clap::Error> {
let matches = App::new("veilid-server") let matches = Command::new("veilid-server")
.version("0.1") .version("0.1")
.about("Veilid Server") .about("Veilid Server")
.color(clap::ColorChoice::Auto) .color(clap::ColorChoice::Auto)
@ -111,10 +111,11 @@ pub fn process_command_line() -> Result<(Settings, ArgMatches), String> {
} }
// Attempt to load configuration // Attempt to load configuration
let settings = Settings::new( let settings = Settings::new(if matches.occurrences_of("config-file") == 0 {
matches.occurrences_of("config-file") == 0, None
matches.value_of_os("config-file").unwrap(), } else {
) Some(matches.value_of_os("config-file").unwrap())
})
.map_err(|e| format!("configuration is invalid: {}", e))?; .map_err(|e| format!("configuration is invalid: {}", e))?;
// write lock the settings // write lock the settings

View File

@ -13,7 +13,7 @@ use std::sync::Arc;
use url::Url; use url::Url;
use veilid_core::xx::*; use veilid_core::xx::*;
pub fn load_default_config(cfg: &mut config::Config) -> Result<(), config::ConfigError> { pub fn load_default_config() -> Result<config::Config, config::ConfigError> {
let default_config = String::from( let default_config = String::from(
r#"--- r#"---
daemon: false daemon: false
@ -138,20 +138,23 @@ core:
"%INSECURE_FALLBACK_DIRECTORY%", "%INSECURE_FALLBACK_DIRECTORY%",
&Settings::get_default_protected_store_insecure_fallback_directory().to_string_lossy(), &Settings::get_default_protected_store_insecure_fallback_directory().to_string_lossy(),
); );
cfg.merge(config::File::from_str( config::Config::builder()
&default_config, .add_source(config::File::from_str(
config::FileFormat::Yaml, &default_config,
)) config::FileFormat::Yaml,
.map(drop) ))
.build()
} }
pub fn load_config( pub fn load_config(
cfg: &mut config::Config, cfg: config::Config,
config_file: &Path, config_file: &Path,
) -> Result<(), config::ConfigError> { ) -> Result<config::Config, config::ConfigError> {
if let Some(config_file_str) = config_file.to_str() { if let Some(config_file_str) = config_file.to_str() {
cfg.merge(config::File::new(config_file_str, config::FileFormat::Yaml)) config::Config::builder()
.map(drop) .add_source(cfg)
.add_source(config::File::new(config_file_str, config::FileFormat::Yaml))
.build()
} else { } else {
Err(config::ConfigError::Message( Err(config::ConfigError::Message(
"config file path is not valid UTF-8".to_owned(), "config file path is not valid UTF-8".to_owned(),
@ -584,25 +587,19 @@ pub struct Settings {
} }
impl Settings { impl Settings {
pub fn new( pub fn new(config_file: Option<&OsStr>) -> Result<Self, config::ConfigError> {
config_file_is_default: bool,
config_file: &OsStr,
) -> Result<Self, config::ConfigError> {
// Create a config
let mut cfg = config::Config::default();
// Load the default config // Load the default config
load_default_config(&mut cfg)?; let mut cfg = load_default_config()?;
// Merge in the config file if we have one // Merge in the config file if we have one
let config_file_path = Path::new(config_file); if let Some(config_file) = config_file {
if !config_file_is_default || config_file_path.exists() { let config_file_path = Path::new(config_file);
// If the user specifies a config file on the command line then it must exist // If the user specifies a config file on the command line then it must exist
load_config(&mut cfg, config_file_path)?; cfg = load_config(cfg, config_file_path)?;
} }
// Generate config // Generate config
let inner: SettingsInner = cfg.try_into()?; let inner: SettingsInner = cfg.try_deserialize()?;
// //
Ok(Self { Ok(Self {
@ -1087,16 +1084,15 @@ mod tests {
#[test] #[test]
#[serial] #[serial]
fn test_default_config() { fn test_default_config() {
let mut cfg = config::Config::default(); let cfg = load_default_config().unwrap();
load_default_config(&mut cfg).unwrap(); let inner = cfg.try_deserialize::<SettingsInner>().unwrap();
let inner = cfg.try_into::<SettingsInner>().unwrap();
println!("default settings: {:?}", inner); println!("default settings: {:?}", inner);
} }
#[test] #[test]
#[serial] #[serial]
fn test_default_config_settings() { fn test_default_config_settings() {
let settings = Settings::new(true, OsStr::new("!!!")).unwrap(); let settings = Settings::new(None).unwrap();
let s = settings.read(); let s = settings.read();
assert_eq!(s.daemon, false); assert_eq!(s.daemon, false);
@ -1214,7 +1210,7 @@ mod tests {
// //
assert_eq!(s.core.network.protocol.udp.enabled, true); assert_eq!(s.core.network.protocol.udp.enabled, true);
assert_eq!(s.core.network.protocol.udp.socket_pool_size, 0); assert_eq!(s.core.network.protocol.udp.socket_pool_size, 0);
assert_eq!(s.core.network.protocol.udp.listen_address.name, "[::]:5150"); assert_eq!(s.core.network.protocol.udp.listen_address.name, ":5150");
assert_eq!( assert_eq!(
s.core.network.protocol.udp.listen_address.addrs, s.core.network.protocol.udp.listen_address.addrs,
listen_address_to_socket_addrs(":5150").unwrap() listen_address_to_socket_addrs(":5150").unwrap()
@ -1225,7 +1221,7 @@ mod tests {
assert_eq!(s.core.network.protocol.tcp.connect, true); assert_eq!(s.core.network.protocol.tcp.connect, true);
assert_eq!(s.core.network.protocol.tcp.listen, true); assert_eq!(s.core.network.protocol.tcp.listen, true);
assert_eq!(s.core.network.protocol.tcp.max_connections, 32); assert_eq!(s.core.network.protocol.tcp.max_connections, 32);
assert_eq!(s.core.network.protocol.tcp.listen_address.name, "[::]:5150"); assert_eq!(s.core.network.protocol.tcp.listen_address.name, ":5150");
assert_eq!( assert_eq!(
s.core.network.protocol.tcp.listen_address.addrs, s.core.network.protocol.tcp.listen_address.addrs,
listen_address_to_socket_addrs(":5150").unwrap() listen_address_to_socket_addrs(":5150").unwrap()
@ -1236,7 +1232,7 @@ mod tests {
assert_eq!(s.core.network.protocol.ws.connect, true); assert_eq!(s.core.network.protocol.ws.connect, true);
assert_eq!(s.core.network.protocol.ws.listen, true); assert_eq!(s.core.network.protocol.ws.listen, true);
assert_eq!(s.core.network.protocol.ws.max_connections, 16); assert_eq!(s.core.network.protocol.ws.max_connections, 16);
assert_eq!(s.core.network.protocol.ws.listen_address.name, "[::]:5150"); assert_eq!(s.core.network.protocol.ws.listen_address.name, ":5150");
assert_eq!( assert_eq!(
s.core.network.protocol.ws.listen_address.addrs, s.core.network.protocol.ws.listen_address.addrs,
listen_address_to_socket_addrs(":5150").unwrap() listen_address_to_socket_addrs(":5150").unwrap()
@ -1250,7 +1246,7 @@ mod tests {
assert_eq!(s.core.network.protocol.wss.connect, true); assert_eq!(s.core.network.protocol.wss.connect, true);
assert_eq!(s.core.network.protocol.wss.listen, false); assert_eq!(s.core.network.protocol.wss.listen, false);
assert_eq!(s.core.network.protocol.wss.max_connections, 16); assert_eq!(s.core.network.protocol.wss.max_connections, 16);
assert_eq!(s.core.network.protocol.wss.listen_address.name, "[::]:5150"); assert_eq!(s.core.network.protocol.wss.listen_address.name, ":5150");
assert_eq!( assert_eq!(
s.core.network.protocol.wss.listen_address.addrs, s.core.network.protocol.wss.listen_address.addrs,
listen_address_to_socket_addrs(":5150").unwrap() listen_address_to_socket_addrs(":5150").unwrap()