aboutsummaryrefslogtreecommitdiff
path: root/src/config/options_test.rs
blob: 0dfaaacf3eba232352ef9f03338b3a4b0baa9878 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::collections::HashMap;
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_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_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
}

// #[test]
// #[should_panic]
// fn err_empty_env() {
// 	 std::env::remove_var("DIPLONAT_CONSUL_NODE_NAME");
// 	 ConfigOpts::from_env().unwrap();
// }

#[test]
#[should_panic]
fn err_empty_env() {
  std::env::remove_var("DIPLONAT_CONSUL_NODE_NAME");
  let opts: HashMap<String, String> = HashMap::new();
  ConfigOpts::from_iter(opts).unwrap();
}

#[test]
fn ok_minimal_valid_options() {
  let opts = minimal_valid_options();
  let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();

  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());
  /*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_invalid_igd_options() {
  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();
}

#[test]
fn ok_all_valid_options() {
  let opts = all_valid_options();
  let rt_config = ConfigOpts::from_iter(opts.clone()).unwrap();

  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(),
  );

  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!(rt_config.acme.is_some());
  let acme = rt_config.acme.unwrap();
  assert_eq!(
    &acme.email,
    opts.get(&"DIPLONAT_ACME_EMAIL".to_string()).unwrap()
  );

  assert!(rt_config.firewall.is_some());
  let firewall = rt_config.firewall.unwrap();
  assert_eq!(firewall.refresh_time, firewall_refresh_time);

  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);
}