Skip to main content

asterust_junos/system/
processes.rs

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