Skip to content

Commit 15dad16

Browse files
committed
testscript: improve ssh() network diagnosis and logging
We will be able to better understand if a network issue is caused by the VM host or the VM. Now, in ssh() we also check if the network is still reachable or if someone broke on the host. A possible missing IP in VM host error might be reported like this: ``` The VM host is not in the same network as IP 192.168.5.2! Networks: [IPv4Network('192.168.1.0/24'), IPv4Network('192.168.2.0/24'), IPv4Network('192.168.3.0/24'), IPv4Network('192.168.4.0/24'), IPv4Network('192.168.100.0/24')] ``` On-behalf-of: SAP philipp.schuster@sap.com Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
1 parent b03083e commit 15dad16

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

tests/testscript.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,17 +2018,35 @@ def ssh(machine: Machine, cmd, user="root", password="root", ip="192.168.1.2"):
20182018
"""
20192019
Runs the specified command in the Cloud Hypervisor VM via SSH.
20202020
2021-
The specified machine is used as SSH jump host.
2021+
The SSH connection will be established from the specified machine.
20222022
2023-
:param machine: Machine to run SSH on
2023+
Performs some sanity network checks beforehand:
2024+
- does the IP actually share a (local) network with the given machine?
2025+
- is the IP ping-able?
2026+
2027+
:param machine: Machine to execute the SSH command on.
20242028
:param cmd: The command to execute via SSH
20252029
:param user: user for SSH login
20262030
:param password: password for SSH login
20272031
:param ip: SSH host to log into
20282032
:return: status and output
20292033
"""
2034+
2035+
# Sometimes we experienced test runs where the host lost IPs. We therefore
2036+
# check that early and always for better debuggability.
2037+
assert_ip_in_local_192_168_net24(machine, ip)
2038+
2039+
if not is_pingable(machine, ip, 1, 1):
2040+
raise RuntimeError(f"VM with IP {ip} is not pingable")
2041+
2042+
# And here we check if the guest also responds via SSH.
20302043
status, out = machine.execute(
2031-
f"sshpass -p {password} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no {user}@{ip} {cmd}"
2044+
f"sshpass -p {password} "
2045+
+ "ssh "
2046+
+ "-o UserKnownHostsFile=/dev/null "
2047+
+ "-o StrictHostKeyChecking=no "
2048+
+ "-o ConnectTimeout=1 "
2049+
+ f"{user}@{ip} {cmd}"
20322050
)
20332051
return status, out
20342052

0 commit comments

Comments
 (0)