tests: Pull common code into a separate method

Yet another small refactoring step for WindowsGuest
after f56471566b.

For this particular case - there's currently neither overloading nor
default argument support in Rust (except a macro or other tricky stuff),
so keep the timeout and other options default for now.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski 2021-05-19 16:44:34 +02:00 committed by Rob Bradford
parent 7ae631e0c1
commit 56b9149325

View File

@ -5309,82 +5309,51 @@ mod tests {
&self.guest &self.guest
} }
fn cpu_count(&self) -> u8 { fn ssh_cmd(&self, cmd: &str) -> String {
return ssh_command_ip_with_auth( ssh_command_ip_with_auth(
"powershell -Command \"(Get-CimInstance win32_computersystem).NumberOfLogicalProcessors\"", cmd,
&self.auth, &self.auth,
&self.guest.network.guest_ip, &self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES, DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT, DEFAULT_SSH_TIMEOUT,
) )
.unwrap() .unwrap()
}
fn cpu_count(&self) -> u8 {
self.ssh_cmd("powershell -Command \"(Get-CimInstance win32_computersystem).NumberOfLogicalProcessors\"")
.trim() .trim()
.parse::<u8>() .parse::<u8>()
.unwrap_or(0); .unwrap_or(0)
} }
fn ram_size(&self) -> usize { fn ram_size(&self) -> usize {
return ssh_command_ip_with_auth( self.ssh_cmd("powershell -Command \"(Get-CimInstance win32_computersystem).TotalPhysicalMemory\"")
"powershell -Command \"(Get-CimInstance win32_computersystem).TotalPhysicalMemory\"",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap()
.trim() .trim()
.parse::<usize>() .parse::<usize>()
.unwrap_or(0); .unwrap_or(0)
} }
fn netdev_count(&self) -> u8 { fn netdev_count(&self) -> u8 {
return ssh_command_ip_with_auth( self.ssh_cmd("powershell -Command \"netsh int ipv4 show interfaces | Select-String ethernet | Measure-Object -Line | Format-Table -HideTableHeaders\"")
"powershell -Command \"netsh int ipv4 show interfaces | Select-String ethernet | Measure-Object -Line | Format-Table -HideTableHeaders\"",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap()
.trim() .trim()
.parse::<u8>() .parse::<u8>()
.unwrap_or(0); .unwrap_or(0)
} }
fn disk_count(&self) -> u8 { fn disk_count(&self) -> u8 {
return ssh_command_ip_with_auth( self.ssh_cmd("powershell -Command \"Get-Disk | Measure-Object -Line | Format-Table -HideTableHeaders\"")
"powershell -Command \"Get-Disk | Measure-Object -Line | Format-Table -HideTableHeaders\"",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap()
.trim() .trim()
.parse::<u8>() .parse::<u8>()
.unwrap_or(0); .unwrap_or(0)
} }
fn reboot(&self) { fn reboot(&self) {
ssh_command_ip_with_auth( let _ = self.ssh_cmd("shutdown /r /t 0");
"shutdown /r /t 0",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap();
} }
fn shutdown(&self) { fn shutdown(&self) {
ssh_command_ip_with_auth( let _ = self.ssh_cmd("shutdown /s /t 0");
"shutdown /s",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap();
} }
fn run_dnsmasq(&self) -> std::process::Child { fn run_dnsmasq(&self) -> std::process::Child {
@ -5500,50 +5469,25 @@ mod tests {
} }
fn disks_set_rw(&self) { fn disks_set_rw(&self) {
ssh_command_ip_with_auth( let _ = self.ssh_cmd("powershell -Command \"Get-Disk | Where-Object IsOffline -eq $True | Set-Disk -IsReadOnly $False\"");
"powershell -Command \"Get-Disk | Where-Object IsOffline -eq $True | Set-Disk -IsReadOnly $False\"",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap();
} }
fn disks_online(&self) { fn disks_online(&self) {
ssh_command_ip_with_auth( let _ = self.ssh_cmd("powershell -Command \"Get-Disk | Where-Object IsOffline -eq $True | Set-Disk -IsOffline $False\"");
"powershell -Command \"Get-Disk | Where-Object IsOffline -eq $True | Set-Disk -IsOffline $False\"",
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap();
} }
fn disk_file_put(&self, fname: &str, data: &str) { fn disk_file_put(&self, fname: &str, data: &str) {
ssh_command_ip_with_auth( let _ = self.ssh_cmd(&format!(
&format!(
"powershell -Command \"'{}' | Set-Content -Path {}\"", "powershell -Command \"'{}' | Set-Content -Path {}\"",
data, fname data, fname
), ));
&self.auth,
&self.guest.network.guest_ip,
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap();
} }
fn disk_file_read(&self, fname: &str) -> String { fn disk_file_read(&self, fname: &str) -> String {
ssh_command_ip_with_auth( self.ssh_cmd(&format!(
&format!("powershell -Command \"Get-Content -Path {}\"", fname), "powershell -Command \"Get-Content -Path {}\"",
&self.auth, fname
&self.guest.network.guest_ip, ))
DEFAULT_SSH_RETRIES,
DEFAULT_SSH_TIMEOUT,
)
.unwrap()
} }
} }