aboutsummaryrefslogtreecommitdiff
path: root/src/config/options_test.rs
diff options
context:
space:
mode:
authorLUXEY Adrien <adrien.luxey@inria.fr>2021-08-14 19:12:18 +0200
committerLUXEY Adrien <adrien.luxey@inria.fr>2021-08-16 10:26:06 +0200
commitae9550ce23bbc85b05669fe5ec4406c8a67417ec (patch)
treebab55a23444b06ded2374dd0380b53a304426b33 /src/config/options_test.rs
parentade0090cdb4a021830439dc0d9a04b2748601ea5 (diff)
downloaddiplonat-ae9550ce23bbc85b05669fe5ec4406c8a67417ec.tar.gz
diplonat-ae9550ce23bbc85b05669fe5ec4406c8a67417ec.zip
New configuration parsing using envy. Added minimal functionnality for
the future ACME parameters. Tests written and passing. WIP: added envy dependncy and ConfigOpts structs that will constitute Diplonat's configuration WIP: ConfigOpts from_env() and validate() methods written. No API change (the env names remain unchanged)! Now need to use our new ConfigOpts struct instead of Environment, and update references to the environment variables in the code. WIP: RuntimeConfig with business logic done. Tests written, but they are all running from the same process - setting environment variables in each test produces incoherent results. Another solution for testing is needed. WIP: tests are fully written using 'from_iter' and all passing
Diffstat (limited to 'src/config/options_test.rs')
-rw-r--r--src/config/options_test.rs119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/config/options_test.rs b/src/config/options_test.rs
new file mode 100644
index 0000000..7c41fce
--- /dev/null
+++ b/src/config/options_test.rs
@@ -0,0 +1,119 @@
+use std::collections::HashMap;
+use std::env;
+use std::time::Duration;
+
+use crate::config::*;
+
+// Environment variables are set for the entire process and
+// tests are run whithin the same process.
+// => We cannot test ConfigOpts::from_env(),
+// because tests modify each other's environment.
+// This is why we only test ConfigOpts::from_iter(iter).
+
+fn minimal_valid_options() -> HashMap<String, String> {
+ let mut opts = HashMap::new();
+ opts.insert("DIPLONAT_PRIVATE_IP".to_string(), "172.123.43.555".to_string());
+ opts.insert("DIPLONAT_CONSUL_NODE_NAME".to_string(), "consul_node".to_string());
+ opts
+}
+
+fn all_valid_options() -> HashMap<String, String> {
+ let mut opts = minimal_valid_options();
+ opts.insert("DIPLONAT_EXPIRATION_TIME".to_string(), "30".to_string());
+ opts.insert("DIPLONAT_REFRESH_TIME".to_string(), "10".to_string());
+ 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
+}
+
+#[test]
+#[should_panic]
+fn err_empty_env() {
+ ConfigOpts::from_env();
+}
+
+#[test]
+fn ok_from_iter_minimal_valid_options() {
+ let opts = minimal_valid_options();
+ let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();
+
+ assert!(rt_config.acme.is_none());
+ 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_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())
+ );
+}
+
+#[test]
+#[should_panic]
+fn err_from_iter_invalid_refresh_time() {
+ let mut opts = minimal_valid_options();
+ opts.insert("DIPLONAT_EXPIRATION_TIME".to_string(), "60".to_string());
+ opts.insert("DIPLONAT_REFRESH_TIME".to_string(), "60".to_string());
+ let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();
+}
+
+#[test]
+fn ok_from_iter_all_valid_options() {
+ let opts = all_valid_options();
+ let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();
+
+ let expiration_time = Duration::from_secs(
+ opts.get(&"DIPLONAT_EXPIRATION_TIME".to_string()).unwrap()
+ .parse::<u64>().unwrap()
+ .into());
+ let refresh_time = Duration::from_secs(
+ opts.get(&"DIPLONAT_REFRESH_TIME".to_string()).unwrap()
+ .parse::<u64>().unwrap()
+ .into());
+
+ assert!(rt_config.acme.is_some());
+ assert_eq!(
+ &rt_config.acme.unwrap().email,
+ opts.get(&"DIPLONAT_ACME_EMAIL".to_string()).unwrap());
+ 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()
+ );
+ assert_eq!(
+ rt_config.firewall.refresh_time,
+ refresh_time
+ );
+ assert_eq!(
+ &rt_config.igd.private_ip,
+ opts.get(&"DIPLONAT_PRIVATE_IP".to_string()).unwrap()
+ );
+ assert_eq!(
+ rt_config.igd.expiration_time,
+ expiration_time
+ );
+ assert_eq!(
+ rt_config.igd.refresh_time,
+ refresh_time
+ );
+} \ No newline at end of file