Skip to main content

asterust_junos/system/
processes.rs

1//! `system { processes { ... } }`.
2
3use crate::active_children;
4use asterust::config::ConfigLine;
5
6/// A process under `system processes`, e.g. `routing` or `dhcp-service`; `all-processes` appears here too.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct Process {
10    pub name: String,
11    /// `Some(true)` for `enable`, `Some(false)` for `disable`, `None` if neither is written.
12    pub enabled: Option<bool>,
13    /// `alternate-media` or `other-routing-engine`.
14    pub failover: Option<String>,
15    /// Seconds.
16    pub timeout: Option<String>,
17    pub no_livecore_dump_on_crash: bool,
18}
19
20/// Processes written either as `<name> disable;` or as `<name> { disable; failover ...; }`.
21pub(super) fn parse_processes(processes: &ConfigLine) -> Vec<Process> {
22    active_children(processes)
23        .filter_map(|child| {
24            let mut name_and_rest = child.text.split_whitespace();
25            let name = name_and_rest.next()?.to_string();
26            let mut words = name_and_rest.chain(active_children(child).flat_map(|c| c.text.split_whitespace()));
27            let mut process = Process {
28                name,
29                enabled: None,
30                failover: None,
31                timeout: None,
32                no_livecore_dump_on_crash: false,
33            };
34            while let Some(word) = words.next() {
35                match word {
36                    "enable" => process.enabled = Some(true),
37                    "disable" => process.enabled = Some(false),
38                    "failover" => process.failover = words.next().map(str::to_string),
39                    "timeout" => process.timeout = words.next().map(str::to_string),
40                    "no-livecore-dump-on-crash" => process.no_livecore_dump_on_crash = true,
41                    _ => {}
42                }
43            }
44            Some(process)
45        })
46        .collect()
47}
48
49#[cfg(test)]
50mod tests {
51    use crate::system::system;
52    use asterust::config::juniper;
53
54    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
55    const PROCESSES: &str = r#"
56system {
57    processes {
58        dhcp-service disable;
59        routing enable failover other-routing-engine;
60        mib-process {
61            enable;
62            timeout 60;
63            traceoptions {
64                file mib.log;
65                flag all;
66            }
67        }
68        inactive: ntp disable;
69        all-processes {
70            no-livecore-dump-on-crash;
71        }
72    }
73}
74"#;
75
76    #[test]
77    fn extracts_processes_in_line_and_block_form() {
78        let processes = system(&juniper::parse_braces(PROCESSES)).unwrap().processes;
79        let names: Vec<_> = processes.iter().map(|p| p.name.as_str()).collect();
80        assert_eq!(names, ["dhcp-service", "routing", "mib-process", "all-processes"]);
81
82        assert_eq!(processes[0].enabled, Some(false));
83        assert_eq!(processes[1].enabled, Some(true));
84        assert_eq!(processes[1].failover.as_deref(), Some("other-routing-engine"));
85        assert_eq!(processes[2].enabled, Some(true));
86        assert_eq!(processes[2].timeout.as_deref(), Some("60"));
87    }
88
89    #[test]
90    fn extracts_process_flags() {
91        let processes = system(&juniper::parse_braces(PROCESSES)).unwrap().processes;
92        assert!(processes[3].no_livecore_dump_on_crash);
93        assert_eq!(processes[3].enabled, None);
94        assert!(!processes[0].no_livecore_dump_on_crash);
95    }
96
97    #[test]
98    fn no_processes_is_empty() {
99        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
100        assert!(system.processes.is_empty());
101    }
102}