2021-09-20 14:07:58 +00:00
|
|
|
use std::{collections::HashMap, time::Duration};
|
2021-08-14 17:12:18 +00:00
|
|
|
|
|
|
|
use crate::config::*;
|
|
|
|
|
|
|
|
// Environment variables are set for the entire process and
|
|
|
|
// tests are run whithin the same process.
|
2021-09-20 09:30:00 +00:00
|
|
|
// => We cannot test ConfigOpts::from_env(),
|
2021-08-14 17:12:18 +00:00
|
|
|
// because tests modify each other's environment.
|
|
|
|
// This is why we only test ConfigOpts::from_iter(iter).
|
|
|
|
|
|
|
|
fn minimal_valid_options() -> HashMap<String, String> {
|
2021-09-20 09:30:00 +00:00
|
|
|
let mut opts = HashMap::new();
|
|
|
|
opts.insert(
|
|
|
|
"DIPLONAT_CONSUL_NODE_NAME".to_string(),
|
|
|
|
"consul_node".to_string(),
|
|
|
|
);
|
|
|
|
opts
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn all_valid_options() -> HashMap<String, String> {
|
2021-09-20 09:30:00 +00:00
|
|
|
let mut opts = minimal_valid_options();
|
|
|
|
opts.insert(
|
|
|
|
"DIPLONAT_CONSUL_URL".to_string(),
|
|
|
|
"http://127.0.0.1:9999".to_string(),
|
|
|
|
);
|
|
|
|
opts.insert("DIPLONAT_ACME_ENABLE".to_string(), "true".to_string());
|
|
|
|
opts.insert(
|
|
|
|
"DIPLONAT_ACME_EMAIL".to_string(),
|
|
|
|
"bozo@bozo.net".to_string(),
|
|
|
|
);
|
|
|
|
opts.insert("DIPLONAT_FIREWALL_ENABLE".to_string(), "true".to_string());
|
|
|
|
opts.insert(
|
|
|
|
"DIPLONAT_FIREWALL_REFRESH_TIME".to_string(),
|
|
|
|
"20".to_string(),
|
|
|
|
);
|
|
|
|
opts.insert("DIPLONAT_IGD_ENABLE".to_string(), "true".to_string());
|
|
|
|
opts.insert(
|
|
|
|
"DIPLONAT_IGD_PRIVATE_IP".to_string(),
|
|
|
|
"172.123.43.555".to_string(),
|
|
|
|
);
|
|
|
|
opts.insert("DIPLONAT_IGD_EXPIRATION_TIME".to_string(), "60".to_string());
|
|
|
|
opts.insert("DIPLONAT_IGD_REFRESH_TIME".to_string(), "10".to_string());
|
|
|
|
opts
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn err_empty_env() {
|
2021-09-20 09:30:00 +00:00
|
|
|
std::env::remove_var("DIPLONAT_CONSUL_NODE_NAME");
|
|
|
|
let opts: HashMap<String, String> = HashMap::new();
|
|
|
|
ConfigOpts::from_iter(opts).unwrap();
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-08-25 15:20:31 +00:00
|
|
|
fn ok_minimal_valid_options() {
|
2021-09-20 09:30:00 +00:00
|
|
|
let opts = minimal_valid_options();
|
|
|
|
let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
&rt_config.consul.node_name,
|
|
|
|
opts.get(&"DIPLONAT_CONSUL_NODE_NAME".to_string()).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(rt_config.consul.url, CONSUL_URL.to_string());
|
|
|
|
assert!(rt_config.acme.is_none());
|
|
|
|
assert!(rt_config.firewall.is_none());
|
|
|
|
assert!(rt_config.igd.is_none());
|
2021-09-20 14:07:58 +00:00
|
|
|
// assert_eq!(
|
|
|
|
// rt_config.firewall.refresh_time,
|
|
|
|
// Duration::from_secs(REFRESH_TIME.into())
|
|
|
|
// );
|
|
|
|
// assert_eq!(
|
|
|
|
// &rt_config.igd.private_ip,
|
|
|
|
// opts.get(&"DIPLONAT_PRIVATE_IP".to_string()).unwrap()
|
|
|
|
// );
|
|
|
|
// assert_eq!(
|
|
|
|
// rt_config.igd.expiration_time,
|
|
|
|
// Duration::from_secs(EXPIRATION_TIME.into())
|
|
|
|
// );
|
|
|
|
// assert_eq!(
|
|
|
|
// rt_config.igd.refresh_time,
|
|
|
|
// Duration::from_secs(REFRESH_TIME.into())
|
|
|
|
// );
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2021-08-25 15:20:31 +00:00
|
|
|
fn err_invalid_igd_options() {
|
2021-09-20 09:30:00 +00:00
|
|
|
let mut opts = minimal_valid_options();
|
|
|
|
opts.insert("DIPLONAT_IGD_ENABLE".to_string(), "true".to_string());
|
|
|
|
opts.insert("DIPLONAT_IGD_EXPIRATION_TIME".to_string(), "60".to_string());
|
|
|
|
opts.insert("DIPLONAT_IGD_REFRESH_TIME".to_string(), "60".to_string());
|
|
|
|
ConfigOpts::from_iter(opts).unwrap();
|
2021-08-14 17:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-08-25 15:20:31 +00:00
|
|
|
fn ok_all_valid_options() {
|
2021-09-20 09:30:00 +00:00
|
|
|
let opts = all_valid_options();
|
|
|
|
let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
let firewall_refresh_time = Duration::from_secs(
|
|
|
|
opts
|
|
|
|
.get(&"DIPLONAT_FIREWALL_REFRESH_TIME".to_string())
|
|
|
|
.unwrap()
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap()
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
let igd_expiration_time = Duration::from_secs(
|
|
|
|
opts
|
|
|
|
.get(&"DIPLONAT_IGD_EXPIRATION_TIME".to_string())
|
|
|
|
.unwrap()
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap()
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
let igd_refresh_time = Duration::from_secs(
|
|
|
|
opts
|
|
|
|
.get(&"DIPLONAT_IGD_REFRESH_TIME".to_string())
|
|
|
|
.unwrap()
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap()
|
|
|
|
.into(),
|
|
|
|
);
|
2021-08-14 17:12:18 +00:00
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
assert_eq!(
|
|
|
|
&rt_config.consul.node_name,
|
|
|
|
opts.get(&"DIPLONAT_CONSUL_NODE_NAME".to_string()).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
&rt_config.consul.url,
|
|
|
|
opts.get(&"DIPLONAT_CONSUL_URL".to_string()).unwrap()
|
|
|
|
);
|
2021-08-25 15:20:31 +00:00
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
assert!(rt_config.acme.is_some());
|
|
|
|
let acme = rt_config.acme.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
&acme.email,
|
|
|
|
opts.get(&"DIPLONAT_ACME_EMAIL".to_string()).unwrap()
|
|
|
|
);
|
2021-08-25 15:20:31 +00:00
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
assert!(rt_config.firewall.is_some());
|
|
|
|
let firewall = rt_config.firewall.unwrap();
|
|
|
|
assert_eq!(firewall.refresh_time, firewall_refresh_time);
|
2021-08-25 15:20:31 +00:00
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
assert!(rt_config.igd.is_some());
|
|
|
|
let igd = rt_config.igd.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
&igd.private_ip,
|
|
|
|
opts.get(&"DIPLONAT_IGD_PRIVATE_IP".to_string()).unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(igd.expiration_time, igd_expiration_time);
|
|
|
|
assert_eq!(igd.refresh_time, igd_refresh_time);
|
|
|
|
}
|