diff options
Diffstat (limited to 'zoned/src/config.rs')
-rw-r--r-- | zoned/src/config.rs | 113 |
1 files changed, 109 insertions, 4 deletions
diff --git a/zoned/src/config.rs b/zoned/src/config.rs index c9cd5f3..f6622f1 100644 --- a/zoned/src/config.rs +++ b/zoned/src/config.rs @@ -1,10 +1,115 @@ +use figment::Figment; use serde::{Deserialize, Serialize}; +use std::net::{IpAddr, SocketAddr}; -#[derive(Default, Serialize, Deserialize)] +use crate::{Error, Result}; + +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Config { - pub(crate) rocket_config: rocket::Config, + pub ip_address: IpAddr, + pub port: u16, + pub zfs: zone_zfs::Config, + pub nspawn: zone_nspawn::Config, +} + +impl Default for Config { + fn default() -> Self { + Self { + ip_address: zone_core::DEFAULT_IP_ADDRESS, + port: zone_core::DEFAULT_PORT, + zfs: Default::default(), + nspawn: Default::default(), + } + } +} + +impl TryFrom<Figment> for Config { + type Error = Error; + + fn try_from(value: Figment) -> Result<Self> { + value.extract().map_err(Into::into) + } +} + +impl From<Config> for SocketAddr { + fn from(val: Config) -> Self { + SocketAddr::from((val.ip_address, val.port)) + } +} +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use figment::providers::{Format, Serialized, Toml}; + + use super::*; + + #[test] + fn defaults() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "Config.toml", + r#" + ip_address = "127.0.0.1" + port = 8000 + + [zfs] + quota = "16G" + pool_name = "pool" + mountpoint = "/srv" + "#, + )?; + + let config: Config = Figment::from(Serialized::defaults(Config::default())) + .merge(Toml::file("Config.toml")) + .extract()?; + + assert_eq!(config, Config::default()); + + Ok(()) + }); + } + + #[test] + fn custom() { + figment::Jail::expect_with(|jail| { + jail.create_file( + "Config.toml", + r#" + ip_address = "192.168.1.1" + port = 6555 + + [zfs] + quota = 42000000 + pool_name = "fool" + mountpoint = "/mnt" + + [nspawn] + network_configs_path = "/etc/zoned/network.d" + "#, + )?; + + let config: Config = Figment::from(Serialized::defaults(Config::default())) + .merge(Toml::file("Config.toml")) + .extract()?; - pub(crate) zfs_config: zone_zfs::Config, + assert_eq!( + config, + Config { + ip_address: [192, 168, 1, 1].into(), + port: 6555, + zfs: zone_zfs::Config { + quota: 42_000_000u64.into(), + pool_name: String::from("fool"), + mountpoint: PathBuf::from("/mnt"), + }, + nspawn: zone_nspawn::Config { + network_configs_path: Some(PathBuf::from("/etc/zoned/network.d")), + }, + } + ); - pub(crate) nspawn_config: zone_nspawn::Config, + Ok(()) + }); + } } |