Skip to content

Commit f45f0dc

Browse files
committed
vmtest: add new _ensure_ssh() that will retry 3x to login
Small helper to retry ssh to avoid flakyness when a VM closes the connection too early.
1 parent 0ce140f commit f45f0dc

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

vmtest/vm.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,26 @@ def _sshpass(self, password):
5757
return []
5858
return ["sshpass", "-p", password]
5959

60+
def _ensure_ssh(self, cmd, user, password="", keyfile=None):
61+
if not self.running():
62+
self.start()
63+
for i in range(3):
64+
try:
65+
ret, _ = self._run("true", user, password, keyfile)
66+
if ret == 0:
67+
return
68+
except Exception as e:
69+
print(f"ssh not ready {e}")
70+
time.sleep(10)
71+
6072
def run(self, cmd, user, password="", keyfile=None):
73+
self._ensure_ssh(cmd, user, password, keyfile)
74+
return self._run(cmd, user, password, keyfile)
75+
76+
def _run(self, cmd, user, password="", keyfile=None):
6177
"""
6278
Run a command on the VM via SSH using the provided credentials.
6379
"""
64-
if not self.running():
65-
self.start()
6680
ssh_cmd = self._sshpass(password) + [
6781
"ssh", "-p", str(self._ssh_port),
6882
] + _non_interactive_ssh
@@ -82,8 +96,7 @@ def run(self, cmd, user, password="", keyfile=None):
8296
return p.returncode, output.getvalue()
8397

8498
def scp(self, src, dst, user, password="", keyfile=None):
85-
if not self.running():
86-
self.start()
99+
self._ensure_ssh(user, password, keyfile)
87100
scp_cmd = self._sshpass(password) + [
88101
"scp", "-P", str(self._ssh_port),
89102
] + _non_interactive_ssh

0 commit comments

Comments
 (0)