summaryrefslogtreecommitdiffhomepage
path: root/test/test-manager/src/config/manifest.rs
blob: 6fed25fa0215e1176d87cb04d6c4e35b7cc63aef (plain)
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
//! Config definition, see [`Config`].

mod test_locations;
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use test_locations::TestLocationList;

use super::VmConfig;
use crate::tests::config::DEFAULT_MULLVAD_HOST;

/// Global configuration for the `test-manager`.
///
/// Can be modified using either the setting file, see
/// [`crate::config::io::ConfigFile::get_config_path`] or
/// the `test-manager config` CLI subcommand.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct Config {
    #[serde(skip)]
    pub runtime_opts: RuntimeOptions,
    pub vms: BTreeMap<String, VmConfig>,
    pub mullvad_host: Option<String>,
    #[serde(default)]
    pub test_locations: TestLocationList,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct RuntimeOptions {
    pub display: Display,
    pub keep_changes: bool,
}

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub enum Display {
    #[default]
    None,
    Local,
    Vnc,
}

impl Config {
    pub fn get_vm(&self, name: &str) -> Option<&VmConfig> {
        self.vms.get(name)
    }

    /// Get the Mullvad host to use.
    ///
    /// Defaults to [`DEFAULT_MULLVAD_HOST`] if the host was not provided in the [`ConfigFile`].
    pub fn get_host(&self) -> String {
        self.mullvad_host.clone().unwrap_or_else(|| {
            log::debug!("No Mullvad host has been set explicitly. Falling back to default host");
            DEFAULT_MULLVAD_HOST.to_owned()
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_test_location_empty() {
        let config = r#"
            {
                "vms": {},
                "mullvad_host": "mullvad.net"
            }"#;

        let config: Config = serde_json::from_str(config).unwrap();
        assert!(config.test_locations.0.is_empty());
    }

    #[test]
    fn parse_test_location_not_empty() {
        let config = r#"
            {
                "vms": {},
                "mullvad_host": "mullvad.net",
                "test_locations": [
                    { "*daita": [ "se-got-wg-001", "se-got-wg-002" ] },
                    { "*": [ "se" ] }
                ]
            }"#;

        let config: Config = serde_json::from_str(config).unwrap();
        assert!(
            config
                .test_locations
                .lookup("test_daita")
                .unwrap()
                .contains(&"se-got-wg-002".to_string())
        );
        assert!(!config.test_locations.0.is_empty());
    }

    #[test]
    fn parse_multiple_keys_in_map_should_fail() {
        let config = r#"
            {
                "vms": {},
                "mullvad_host": "mullvad.net",
                "test_locations": [
                    {
                        "*daita": [ "se-got-wg-001", "se-got-wg-002" ],
                        "*test": ["se"]
                    },
                ]
            }"#;

        let _err = serde_json::from_str::<Config>(config).unwrap_err();
    }
}