isothrum_machine/providers/
needs.rs1use std::time::Duration;
7
8use serde::{Deserialize, Serialize};
9
10use crate::host::{Host, HostOs};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
18#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
19#[serde(rename_all = "camelCase")]
20pub enum Need {
21 Vagrant,
23 VmwareUtility,
27 Plugin,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
33#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
34#[serde(rename_all = "camelCase")]
35pub enum UtilityState {
36 Missing,
38 Stopped,
40 Running,
42}
43
44#[must_use]
48pub fn needs(vagrant_installed: bool, plugin_installed: bool, utility: Option<UtilityState>) -> Vec<Need> {
49 let mut needs = Vec::new();
50 if !vagrant_installed {
51 needs.push(Need::Vagrant);
52 }
53 if utility.is_some_and(|u| u != UtilityState::Running) {
54 needs.push(Need::VmwareUtility);
55 }
56 if !plugin_installed {
57 needs.push(Need::Plugin);
58 }
59 needs
60}
61
62const UTILITY_WAIT: Duration = Duration::from_secs(2);
64
65pub async fn vmware_utility(host: &Host) -> UtilityState {
76 if host.os == HostOs::Linux && !host.devices.netstat.iter().any(|p| p.is_file()) {
77 return UtilityState::Missing;
78 }
79 let answers = tokio::time::timeout(UTILITY_WAIT, tokio::net::TcpStream::connect(host.devices.vmware_utility_service)).await;
80 if matches!(answers, Ok(Ok(_))) {
81 UtilityState::Running
82 } else if host.devices.vmware_utility.exists() {
83 UtilityState::Stopped
84 } else {
85 UtilityState::Missing
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 fn host(root: &std::path::Path) -> Host {
95 crate::testing::linux_host(root)
96 }
97
98 #[test]
99 fn what_is_needed_is_in_setups_order_and_nothing_when_all_is_there() {
100 assert_eq!(
101 needs(false, false, Some(UtilityState::Missing)),
102 [Need::Vagrant, Need::VmwareUtility, Need::Plugin]
103 );
104 assert_eq!(needs(true, true, Some(UtilityState::Stopped)), [Need::VmwareUtility]);
105 assert_eq!(needs(true, true, Some(UtilityState::Running)), []);
106 assert_eq!(needs(true, true, None), []);
107 assert_eq!(serde_json::to_value(Need::VmwareUtility).unwrap(), "vmwareUtility");
108 assert_eq!(serde_json::to_value(UtilityState::Stopped).unwrap(), "stopped");
109 }
110
111 #[tokio::test]
112 async fn the_utility_runs_when_its_service_answers_else_its_marker_says_whether_it_is_installed() {
113 let dir = tempfile::tempdir().unwrap();
114 let mut host = host(dir.path());
115 given_netstat(&host);
116 assert_eq!(vmware_utility(&host).await, UtilityState::Missing);
117 let marker = &host.devices.vmware_utility;
118 std::fs::create_dir_all(marker.parent().unwrap()).unwrap();
119 std::fs::write(marker, "").unwrap();
120 assert_eq!(vmware_utility(&host).await, UtilityState::Stopped);
121 let service = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
122 host.devices.vmware_utility_service = service.local_addr().unwrap();
123 assert_eq!(vmware_utility(&host).await, UtilityState::Running);
124 std::fs::remove_file(&host.devices.vmware_utility).unwrap();
125 assert_eq!(vmware_utility(&host).await, UtilityState::Running, "installed otherwise: it answers");
126 }
127
128 fn given_netstat(host: &Host) {
130 let netstat = &host.devices.netstat[0];
131 std::fs::create_dir_all(netstat.parent().unwrap()).unwrap();
132 std::fs::write(netstat, "").unwrap();
133 }
134
135 #[tokio::test]
136 async fn on_linux_the_utility_is_missing_while_no_netstat_is_there_for_the_plugin() {
137 let dir = tempfile::tempdir().unwrap();
141 let mut host = host(dir.path());
142 let marker = &host.devices.vmware_utility;
143 std::fs::create_dir_all(marker.parent().unwrap()).unwrap();
144 std::fs::write(marker, "").unwrap();
145 let service = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
146 host.devices.vmware_utility_service = service.local_addr().unwrap();
147 assert_eq!(vmware_utility(&host).await, UtilityState::Missing, "no netstat");
148 given_netstat(&host);
149 assert_eq!(vmware_utility(&host).await, UtilityState::Running);
150 let mut mac = host.clone();
151 mac.os = HostOs::Macos;
152 std::fs::remove_file(&host.devices.netstat[0]).unwrap();
153 assert_eq!(vmware_utility(&mac).await, UtilityState::Running, "Linux's need alone");
154 }
155}