use clap::Parser;
use config::ConfigError;
use modelexpress_common::config::{LogFormat, LogLevel, load_layered_config};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::num::NonZeroU16;
use std::path::PathBuf;
use tracing::Level;
use crate::cache::CacheEvictionConfig;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum, Default)]
#[serde(rename_all = "lowercase")]
pub enum AuthMode {
#[default]
Off,
Enforce,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ServiceAccountRef {
pub namespace: String,
pub service_account: String,
}
impl std::fmt::Display for ServiceAccountRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.namespace, self.service_account)
}
}
impl std::str::FromStr for ServiceAccountRef {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (namespace, service_account) = s
.split_once(':')
.ok_or_else(|| format!("expected '<namespace>:<serviceaccount>', got '{s}'"))?;
let namespace = namespace.trim();
let service_account = service_account.trim();
if namespace.is_empty() || service_account.is_empty() {
return Err(format!(
"namespace and service account must both be non-empty in '{s}'"
));
}
Ok(Self {
namespace: namespace.to_string(),
service_account: service_account.to_string(),
})
}
}
#[derive(Debug, Clone)]
pub struct CommaList<T>(Vec<T>);
impl<T> CommaList<T> {
#[must_use]
pub fn into_inner(self) -> Vec<T> {
self.0
}
}
impl<T: std::str::FromStr> std::str::FromStr for CommaList<T> {
type Err = T::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let items = s
.split(',')
.map(str::trim)
.filter(|part| !part.is_empty())
.map(T::from_str)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self(items))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SecurityConfig {
pub mode: Option<AuthMode>,
pub token_audiences: Vec<String>,
pub allowed_service_accounts: Vec<ServiceAccountRef>,
pub cache_ttl_secs: u64,
}
impl Default for SecurityConfig {
fn default() -> Self {
Self {
mode: None,
token_audiences: Vec::new(),
allowed_service_accounts: Vec::new(),
cache_ttl_secs: 60,
}
}
}
impl SecurityConfig {
#[must_use]
pub fn resolve_mode(&self) -> AuthMode {
self.mode.unwrap_or(AuthMode::Off)
}
pub fn validate_resolved(&self, mode: AuthMode) -> Result<(), String> {
if mode == AuthMode::Enforce {
if self.token_audiences.is_empty() {
return Err(
"security mode 'enforce' requires at least one token audience \
(security.token_audiences)"
.to_string(),
);
}
if self.allowed_service_accounts.is_empty() {
return Err(
"security mode 'enforce' requires a non-empty service account \
allowlist (security.allowed_service_accounts)"
.to_string(),
);
}
}
Ok(())
}
}
#[derive(clap::Args, Debug, Default)]
pub struct SecurityArgs {
#[arg(
long = "security-mode",
env = modelexpress_common::envs::MODEL_EXPRESS_SECURITY_MODE,
value_enum
)]
pub mode: Option<AuthMode>,
#[arg(
long = "security-token-audiences",
env = modelexpress_common::envs::MODEL_EXPRESS_SECURITY_TOKEN_AUDIENCES
)]
pub token_audiences: Option<CommaList<String>>,
#[arg(
long = "security-allowed-service-accounts",
env = modelexpress_common::envs::MODEL_EXPRESS_SECURITY_ALLOWED_SERVICE_ACCOUNTS
)]
pub allowed_service_accounts: Option<CommaList<ServiceAccountRef>>,
#[arg(
long = "security-cache-ttl-secs",
env = modelexpress_common::envs::MODEL_EXPRESS_SECURITY_CACHE_TTL_SECS
)]
pub cache_ttl_secs: Option<u64>,
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct ServerArgs {
#[arg(short, long, value_name = "FILE")]
pub config: Option<PathBuf>,
#[arg(short, long, env = modelexpress_common::envs::MODEL_EXPRESS_SERVER_PORT)]
pub port: Option<NonZeroU16>,
#[arg(long, env = modelexpress_common::envs::MODEL_EXPRESS_SERVER_HOST)]
pub host: Option<String>,
#[arg(long, env = modelexpress_common::envs::MODEL_EXPRESS_SERVER_METRICS_PORT)]
pub metrics_port: Option<u16>,
#[arg(short, long, env = modelexpress_common::envs::MODEL_EXPRESS_LOG_LEVEL, value_enum)]
pub log_level: Option<LogLevel>,
#[arg(long, env = modelexpress_common::envs::MODEL_EXPRESS_LOG_FORMAT, value_enum)]
pub log_format: Option<LogFormat>,
#[arg(long, env = modelexpress_common::envs::MODEL_EXPRESS_CACHE_DIRECTORY)]
pub cache_directory: Option<PathBuf>,
#[arg(long, env = modelexpress_common::envs::MODEL_EXPRESS_CACHE_EVICTION_ENABLED)]
pub cache_eviction_enabled: Option<bool>,
#[command(flatten)]
pub security: SecurityArgs,
#[arg(long)]
pub validate_config: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ServerConfig {
pub server: ServerSettings,
pub cache: CacheConfig,
pub logging: LoggingConfig,
#[serde(default)]
pub security: SecurityConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerSettings {
pub host: String,
pub port: NonZeroU16,
#[serde(default = "default_metrics_port")]
pub metrics_port: u16,
}
fn default_metrics_port() -> u16 {
modelexpress_common::constants::DEFAULT_METRICS_PORT.get()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
pub eviction: CacheEvictionConfig,
pub directory: PathBuf,
pub max_size_bytes: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LoggingConfig {
#[serde(default)]
pub level: LogLevel,
#[serde(default)]
pub format: LogFormat,
pub file: Option<PathBuf>,
pub structured: bool,
}
impl Default for ServerSettings {
fn default() -> Self {
Self {
host: "0.0.0.0".to_string(),
port: modelexpress_common::constants::DEFAULT_GRPC_PORT,
metrics_port: default_metrics_port(),
}
}
}
impl Default for CacheConfig {
fn default() -> Self {
Self {
eviction: CacheEvictionConfig::default(),
directory: PathBuf::from("./cache"),
max_size_bytes: None,
}
}
}
impl ServerConfig {
pub fn load(args: ServerArgs) -> Result<Self, ConfigError> {
Self::load_internal(args, false)
}
pub fn load_and_validate_strict(args: ServerArgs) -> Result<Self, ConfigError> {
Self::load_internal(args, true)
}
fn load_internal(args: ServerArgs, strict_mode: bool) -> Result<Self, ConfigError> {
let mut config = if strict_mode {
if let Some(ref config_file) = args.config {
modelexpress_common::config::validate_config_file(config_file)?
} else {
Self::default()
}
} else {
load_layered_config(
args.config.clone(),
modelexpress_common::envs::MODEL_EXPRESS_PREFIX,
Self::default(),
)?
};
if let Some(port) = args.port {
config.server.port = port;
}
if let Some(host) = args.host {
config.server.host = host;
}
if let Some(metrics_port) = args.metrics_port {
config.server.metrics_port = metrics_port;
}
if let Some(log_level) = args.log_level {
config.logging.level = log_level;
}
if let Some(log_format) = args.log_format {
config.logging.format = log_format;
}
if let Some(cache_directory) = args.cache_directory {
config.cache.directory = cache_directory;
}
if let Some(cache_eviction_enabled) = args.cache_eviction_enabled {
config.cache.eviction.enabled = cache_eviction_enabled;
}
if let Some(mode) = args.security.mode {
config.security.mode = Some(mode);
}
if let Some(token_audiences) = args.security.token_audiences {
config.security.token_audiences = token_audiences.into_inner();
}
if let Some(allowed) = args.security.allowed_service_accounts {
config.security.allowed_service_accounts = allowed.into_inner();
}
if let Some(cache_ttl_secs) = args.security.cache_ttl_secs {
config.security.cache_ttl_secs = cache_ttl_secs;
}
config.validate()?;
Ok(config)
}
pub fn validate(&self) -> Result<(), ConfigError> {
if let Some(parent) = self.cache.directory.parent()
&& !parent.exists()
{
return Err(ConfigError::Message(format!(
"Cache directory parent does not exist: {}",
parent.display()
)));
}
let mode = self.security.resolve_mode();
self.security
.validate_resolved(mode)
.map_err(ConfigError::Message)?;
Ok(())
}
pub fn socket_addr(&self) -> Result<SocketAddr, ConfigError> {
let addr = format!("{}:{}", self.server.host, self.server.port);
addr.parse()
.map_err(|e| ConfigError::Message(format!("Invalid server address {addr}: {e}")))
}
pub fn metrics_socket_addr(&self) -> Result<Option<SocketAddr>, ConfigError> {
let Some(metrics_port) = NonZeroU16::new(self.server.metrics_port) else {
return Ok(None);
};
let addr = format!("{}:{}", self.server.host, metrics_port);
addr.parse()
.map(Some)
.map_err(|e| ConfigError::Message(format!("Invalid metrics address {addr}: {e}")))
}
pub fn log_level(&self) -> Level {
self.logging.level.into()
}
pub fn print_config(&self) {
use tracing::info;
info!("Server Configuration:");
info!(" Host: {}", self.server.host);
info!(" Port: {}", self.server.port);
match NonZeroU16::new(self.server.metrics_port) {
Some(port) => info!(" Metrics Port: {port} (/metrics)"),
None => info!(" Metrics Port: disabled"),
}
info!("Cache Configuration:");
info!(" Directory: {}", self.cache.directory.display());
info!(" Max Size: {:?}", self.cache.max_size_bytes);
info!(" Eviction Enabled: {}", self.cache.eviction.enabled);
info!(
" Eviction Check Interval: {}s",
self.cache.eviction.check_interval.num_seconds()
);
info!("Logging Configuration:");
info!(" Level: {}", self.logging.level);
info!(" Format: {}", self.logging.format);
info!(" File: {:?}", self.logging.file);
info!(" Structured: {}", self.logging.structured);
info!("Security Configuration:");
info!(" Mode: {:?}", self.security.resolve_mode());
info!(" Token audiences: {:?}", self.security.token_audiences);
info!(
" Allowed service accounts: {}",
self.security
.allowed_service_accounts
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(", ")
);
info!(" Cache TTL: {}s", self.security.cache_ttl_secs);
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Duration;
use clap::Parser;
use modelexpress_common::config::{DurationConfig, parse_duration_string};
use std::fs;
use tempfile::tempdir;
#[test]
fn test_log_level_enum_parsing() {
let valid_levels = [
("trace", LogLevel::Trace),
("debug", LogLevel::Debug),
("info", LogLevel::Info),
("warn", LogLevel::Warn),
("error", LogLevel::Error),
];
for (level_str, expected_level) in &valid_levels {
let args = vec!["test", "--log-level", level_str];
if let Ok(parsed_args) = ServerArgs::try_parse_from(args) {
assert_eq!(parsed_args.log_level, Some(*expected_level));
assert_eq!(expected_level.to_string(), *level_str);
let tracing_level: Level = (*expected_level).into();
match expected_level {
LogLevel::Trace => assert_eq!(tracing_level, Level::TRACE),
LogLevel::Debug => assert_eq!(tracing_level, Level::DEBUG),
LogLevel::Info => assert_eq!(tracing_level, Level::INFO),
LogLevel::Warn => assert_eq!(tracing_level, Level::WARN),
LogLevel::Error => assert_eq!(tracing_level, Level::ERROR),
}
} else {
panic!("Failed to parse valid log level: {level_str}");
}
}
}
#[test]
fn test_log_level_enum_invalid() {
let args = vec!["test", "--log-level", "invalid"];
let result = ServerArgs::try_parse_from(args);
assert!(result.is_err());
}
#[test]
fn test_log_level_display() {
assert_eq!(LogLevel::Trace.to_string(), "trace");
assert_eq!(LogLevel::Debug.to_string(), "debug");
assert_eq!(LogLevel::Info.to_string(), "info");
assert_eq!(LogLevel::Warn.to_string(), "warn");
assert_eq!(LogLevel::Error.to_string(), "error");
}
#[test]
#[allow(clippy::expect_used)]
fn test_log_level_from_str() {
assert_eq!(
"trace"
.parse::<LogLevel>()
.expect("Failed to parse 'trace'"),
LogLevel::Trace
);
assert_eq!(
"debug"
.parse::<LogLevel>()
.expect("Failed to parse 'debug'"),
LogLevel::Debug
);
assert_eq!(
"info".parse::<LogLevel>().expect("Failed to parse 'info'"),
LogLevel::Info
);
assert_eq!(
"warn".parse::<LogLevel>().expect("Failed to parse 'warn'"),
LogLevel::Warn
);
assert_eq!(
"error"
.parse::<LogLevel>()
.expect("Failed to parse 'error'"),
LogLevel::Error
);
assert_eq!(
"TRACE"
.parse::<LogLevel>()
.expect("Failed to parse 'TRACE'"),
LogLevel::Trace
);
assert_eq!(
"Info".parse::<LogLevel>().expect("Failed to parse 'Info'"),
LogLevel::Info
);
assert!("invalid".parse::<LogLevel>().is_err());
}
#[test]
fn test_log_format_display() {
assert_eq!(LogFormat::Json.to_string(), "json");
assert_eq!(LogFormat::Pretty.to_string(), "pretty");
assert_eq!(LogFormat::Compact.to_string(), "compact");
}
#[test]
#[allow(clippy::expect_used)]
fn test_log_format_from_str() {
assert_eq!(
"json".parse::<LogFormat>().expect("Failed to parse 'json'"),
LogFormat::Json
);
assert_eq!(
"pretty"
.parse::<LogFormat>()
.expect("Failed to parse 'pretty'"),
LogFormat::Pretty
);
assert_eq!(
"compact"
.parse::<LogFormat>()
.expect("Failed to parse 'compact'"),
LogFormat::Compact
);
assert_eq!(
"JSON".parse::<LogFormat>().expect("Failed to parse 'JSON'"),
LogFormat::Json
);
assert_eq!(
"Pretty"
.parse::<LogFormat>()
.expect("Failed to parse 'Pretty'"),
LogFormat::Pretty
);
assert!("invalid".parse::<LogFormat>().is_err());
}
#[test]
#[allow(clippy::expect_used)]
fn test_parse_duration_string() {
assert_eq!(
parse_duration_string("30m")
.expect("Failed to parse 30m")
.num_seconds(),
30 * 60
);
assert_eq!(
parse_duration_string("45s")
.expect("Failed to parse 45s")
.num_seconds(),
45
);
assert_eq!(
parse_duration_string("1d")
.expect("Failed to parse 1d")
.num_seconds(),
24 * 3600
);
assert_eq!(
parse_duration_string("2h")
.expect("Failed to parse 2h")
.num_seconds(),
2 * 3600
);
assert_eq!(
parse_duration_string("2h30m")
.expect("Failed to parse 2h30m")
.num_seconds(),
2 * 3600 + 30 * 60
);
assert!(parse_duration_string("invalid").is_err());
}
#[test]
fn test_duration_config() {
let duration_config = DurationConfig::new(Duration::hours(2));
assert_eq!(duration_config.num_seconds(), 2 * 3600);
assert_eq!(duration_config.as_chrono_duration(), Duration::hours(2));
let duration_config = DurationConfig::hours(3);
assert_eq!(duration_config.num_seconds(), 3 * 3600);
assert_eq!(duration_config.to_string(), "10800s");
}
#[test]
#[allow(clippy::expect_used)]
fn test_duration_config_serde() {
let json = r#""2h""#;
let duration_config: DurationConfig = serde_json::from_str(json).expect("Failed to parse");
assert_eq!(duration_config.num_seconds(), 2 * 3600);
let json = r#"3600"#;
let duration_config: DurationConfig = serde_json::from_str(json).expect("Failed to parse");
assert_eq!(duration_config.num_seconds(), 3600);
let duration_config = DurationConfig::hours(1);
let serialized = serde_json::to_string(&duration_config).expect("Failed to serialize");
assert_eq!(serialized, r#"3600"#);
}
#[test]
#[allow(clippy::expect_used)]
fn test_server_config_load_and_validate_strict_valid_config() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let config_file = temp_dir.path().join("valid_server_config.yaml");
let valid_config = r#"
server:
host: "127.0.0.1"
port: 8002
cache:
eviction:
enabled: false
policy:
type: lru
unused_threshold: "3d"
max_models: 10
min_free_space_bytes: 1000000
check_interval: "30m"
directory: "./test_cache"
max_size_bytes: 5000000
logging:
level: Debug
format: Json
file: null
structured: true
"#;
fs::write(&config_file, valid_config).expect("Failed to write config file");
let args = ServerArgs {
config: Some(config_file),
port: None,
host: None,
metrics_port: None,
log_level: None,
log_format: None,
cache_directory: None,
cache_eviction_enabled: None,
security: SecurityArgs::default(),
validate_config: false,
};
let result = ServerConfig::load_and_validate_strict(args);
assert!(result.is_ok());
let config = result.expect("Expected successful config parsing");
assert_eq!(config.server.host, "127.0.0.1");
assert_eq!(config.server.port.get(), 8002);
assert!(!config.cache.eviction.enabled);
assert_eq!(config.logging.level, LogLevel::Debug);
assert_eq!(config.logging.format, LogFormat::Json);
}
#[test]
#[allow(clippy::expect_used)]
fn test_server_config_load_and_validate_strict_invalid_config() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let config_file = temp_dir.path().join("invalid_server_config.yaml");
let invalid_config = r#"
server:
host: "127.0.0.1"
port: 8002
cache:
eviction:
enabled: "not_a_boolean" # Invalid type
"#;
fs::write(&config_file, invalid_config).expect("Failed to write config file");
let args = ServerArgs {
config: Some(config_file),
port: None,
host: None,
metrics_port: None,
log_level: None,
log_format: None,
cache_directory: None,
cache_eviction_enabled: None,
security: SecurityArgs::default(),
validate_config: false,
};
let result = ServerConfig::load_and_validate_strict(args);
assert!(result.is_err());
}
#[test]
#[allow(clippy::expect_used)]
fn test_server_config_load_and_validate_strict_with_cli_overrides() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let config_file = temp_dir.path().join("override_test_config.yaml");
let base_config = r#"
server:
host: "127.0.0.1"
port: 8002
cache:
eviction:
enabled: true
policy:
type: lru
unused_threshold: "1d"
max_models: null
min_free_space_bytes: null
check_interval: "1h"
directory: "./cache"
max_size_bytes: null
logging:
level: Info
format: Pretty
file: null
structured: false
"#;
fs::write(&config_file, base_config).expect("Failed to write config file");
let args = ServerArgs {
config: Some(config_file),
port: Some(NonZeroU16::new(9000).expect("9000 is non-zero")),
host: Some("0.0.0.0".to_string()),
metrics_port: None,
log_level: Some(LogLevel::Error),
log_format: Some(LogFormat::Json),
cache_directory: Some(PathBuf::from("/tmp/override_cache")),
cache_eviction_enabled: Some(false),
security: SecurityArgs::default(),
validate_config: false,
};
let result = ServerConfig::load_and_validate_strict(args);
assert!(result.is_ok());
let config = result.expect("Expected successful config parsing");
assert_eq!(config.server.host, "0.0.0.0");
assert_eq!(config.server.port.get(), 9000);
assert_eq!(config.logging.level, LogLevel::Error);
assert_eq!(config.logging.format, LogFormat::Json);
assert_eq!(config.cache.directory, PathBuf::from("/tmp/override_cache"));
assert!(!config.cache.eviction.enabled);
}
#[test]
#[allow(clippy::expect_used)]
fn test_server_config_load_and_validate_strict_no_config_file() {
let args = ServerArgs {
config: None,
port: Some(NonZeroU16::new(9001).expect("9001 is non-zero")),
host: Some("localhost".to_string()),
metrics_port: None,
log_level: Some(LogLevel::Warn),
log_format: None,
cache_directory: None,
cache_eviction_enabled: None,
security: SecurityArgs::default(),
validate_config: false,
};
let result = ServerConfig::load_and_validate_strict(args);
assert!(result.is_ok());
let config = result.expect("Expected successful config parsing");
assert_eq!(config.server.host, "localhost");
assert_eq!(config.server.port.get(), 9001);
assert_eq!(config.logging.level, LogLevel::Warn);
}
#[test]
fn security_defaults_to_off() {
let security = SecurityConfig::default();
assert_eq!(security.resolve_mode(), AuthMode::Off);
assert!(security.token_audiences.is_empty());
assert!(security.allowed_service_accounts.is_empty());
}
#[test]
#[allow(clippy::expect_used)]
fn service_account_ref_parses_namespace_and_sa() {
let parsed: ServiceAccountRef = "vllm:worker".parse().expect("valid ns:sa");
assert_eq!(parsed.namespace, "vllm");
assert_eq!(parsed.service_account, "worker");
assert_eq!(parsed.to_string(), "vllm:worker");
let trimmed: ServiceAccountRef = " ns : sa ".parse().expect("trims");
assert_eq!(trimmed, "ns:sa".parse().expect("valid"));
}
#[test]
fn service_account_ref_rejects_malformed() {
assert!("noseparator".parse::<ServiceAccountRef>().is_err());
assert!(":sa".parse::<ServiceAccountRef>().is_err());
assert!("ns:".parse::<ServiceAccountRef>().is_err());
}
#[test]
#[allow(clippy::expect_used)]
fn comma_list_parses_and_skips_blanks() {
let list: CommaList<ServiceAccountRef> =
"a:one, b:two ,, c:three".parse().expect("valid list");
let items = list.into_inner();
assert_eq!(items.len(), 3);
assert_eq!(items[1].namespace, "b");
assert_eq!(items[2].service_account, "three");
let empty: CommaList<String> = " ".parse().expect("empty");
assert!(empty.into_inner().is_empty());
}
#[test]
fn enforce_requires_audience_and_allowlist() {
let mut security = SecurityConfig {
mode: Some(AuthMode::Enforce),
..SecurityConfig::default()
};
assert!(security.validate_resolved(AuthMode::Enforce).is_err());
security.token_audiences = vec!["modelexpress".to_string()];
assert!(security.validate_resolved(AuthMode::Enforce).is_err());
security.allowed_service_accounts = vec![ServiceAccountRef {
namespace: "vllm".to_string(),
service_account: "worker".to_string(),
}];
assert!(security.validate_resolved(AuthMode::Enforce).is_ok());
}
#[test]
#[allow(clippy::expect_used)]
fn metrics_port_never_costs_the_rest_of_the_config_file() {
let cases = [
("metrics_port: 0", 0_u16, None),
("", default_metrics_port(), Some(default_metrics_port())),
];
for (metrics_line, expected_metrics_port, expects_addr) in cases {
let temp_dir = tempdir().expect("Failed to create temp dir");
let config_file = temp_dir.path().join("metrics_port.yaml");
fs::write(
&config_file,
format!(
r#"
server:
host: "127.0.0.1"
port: 18012
{metrics_line}
cache:
eviction:
enabled: false
policy:
type: lru
unused_threshold: "3d"
max_models: 10
min_free_space_bytes: 1000000
check_interval: "30m"
directory: "./cache"
max_size_bytes: 5000000
logging:
level: Debug
format: Json
file: null
structured: true
"#
),
)
.expect("Failed to write config file");
let args = ServerArgs {
config: Some(config_file),
port: None,
host: None,
metrics_port: None,
log_level: None,
log_format: None,
cache_directory: None,
cache_eviction_enabled: None,
security: SecurityArgs::default(),
validate_config: false,
};
let config = ServerConfig::load(args).expect("config should load");
assert_eq!(config.server.metrics_port, expected_metrics_port);
assert_eq!(
config
.metrics_socket_addr()
.expect("metrics address should resolve")
.map(|addr| addr.port()),
expects_addr,
"0 must mean disabled and nothing else"
);
assert_eq!(config.server.port.get(), 18012, "the file was dropped");
assert_eq!(config.server.host, "127.0.0.1");
assert!(!config.cache.eviction.enabled);
assert_eq!(config.logging.level, LogLevel::Debug);
}
}
#[test]
fn metrics_port_is_reachable_from_the_command_line() {
use clap::Parser as _;
let args = ServerArgs::parse_from(["modelexpress-server", "--metrics-port", "9999"]);
assert_eq!(args.metrics_port, Some(9999));
let off = ServerArgs::parse_from(["modelexpress-server", "--metrics-port", "0"]);
assert_eq!(
off.metrics_port,
Some(0),
"0 is the documented disable value"
);
}
}