Skip to content

Commit e3b5c78

Browse files
committed
Refactor TCP port test with netcat into netutil
Specific error messages are no longer available with this refactoring. Signed-off-by: Emmanuel Varagnat <[email protected]>
1 parent 5ff8705 commit e3b5c78

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
vm_image,
2626
wait_for,
2727
)
28-
from lib.netutil import is_ipv6
2928
from lib.host import Host
29+
from lib.netutil import is_ipv6, wait_for_ssh
3030
from lib.pool import Pool
3131
from lib.sr import SR
3232
from lib.vm import VM, vm_cache_key_from_def
@@ -203,9 +203,7 @@ def setup_host(hostname_or_ip, *, config=None):
203203
assert len(ips) == 1
204204
host_vm.ip = ips[0]
205205

206-
wait_for(lambda: commands.local_cmd(['nc', '-zw5', str(host_vm.ip), '22'],
207-
check=False, simple_output=False).returncode == 0,
208-
"Wait for ssh up on nested host", retry_delay_secs=5)
206+
wait_for_ssh(host_vm.ip, host_desc="nested host")
209207

210208
hostname_or_ip = host_vm.ip
211209

lib/commands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import lib.config as config
77
from lib.common import HostAddress
8-
from lib.netutil import wrap_ip
98

109
from typing import List, Literal, Union, overload
1110

@@ -191,6 +190,9 @@ def ssh_with_result(hostname_or_ip, cmd, suppress_fingerprint_warnings=True,
191190
assert False, "unexpected type"
192191

193192
def scp(hostname_or_ip, src, dest, check=True, suppress_fingerprint_warnings=True, local_dest=False):
193+
# local import to avoid cyclic import; lib.netutils also import lib.commands
194+
from lib.netutil import wrap_ip
195+
194196
opts = '-o "BatchMode yes"'
195197
if suppress_fingerprint_warnings:
196198
# Suppress warnings and questions related to host key fingerprints

lib/host.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
wait_for,
3333
wait_for_not,
3434
)
35-
from lib.netutil import wrap_ip
35+
from lib.netutil import wait_for_ssh, wrap_ip
3636
from lib.sr import SR
3737
from lib.vdi import VDI
3838
from lib.vm import VM
@@ -551,10 +551,7 @@ def reboot(self, verify=False):
551551
raise
552552
if verify:
553553
wait_for_not(self.is_enabled, "Wait for host down")
554-
wait_for(lambda: commands.local_cmd(['ping', '-c1', self.hostname_or_ip], simple_output=False).returncode == 0,
555-
"Wait for host up", timeout_secs=10 * 60, retry_delay_secs=10)
556-
wait_for(lambda: commands.local_cmd(['nc', '-zw5', self.hostname_or_ip, '22'], simple_output=False).returncode == 0,
557-
"Wait for ssh up on host", timeout_secs=10 * 60, retry_delay_secs=5)
554+
wait_for_ssh(self.hostname_or_ip)
558555
wait_for(self.is_enabled, "Wait for XAPI to be ready", timeout_secs=30 * 60)
559556

560557
def management_network(self):

lib/netutil.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import socket
22

3+
from lib.commands import local_cmd
4+
from lib.common import wait_for
5+
6+
def wait_for_tcp_port(host, port, port_desc, ping=True, host_desc=None):
7+
if host_desc:
8+
host_desc = f" ({host_desc})"
9+
if ping:
10+
wait_for(lambda: local_cmd(['ping', '-c1', host], check=False, simple_output=False).returncode == 0,
11+
"Wait for host up (ICMP ping)", timeout_secs=10 * 60, retry_delay_secs=10)
12+
wait_for(lambda: local_cmd(['nc', '-zw5', host, str(port)], check=False, simple_output=False).returncode == 0,
13+
f"Wait for {port_desc} up on host{host_desc}", timeout_secs=10 * 60, retry_delay_secs=5)
14+
15+
def wait_for_ssh(host, host_desc=None, ping=True):
16+
wait_for_tcp_port(host, 22, "SSH", ping, host_desc)
17+
318
def is_ipv6(ip):
419
try:
520
socket.inet_pton(socket.AF_INET6, ip)

tests/install/conftest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from lib.commands import local_cmd
1212
from lib.common import callable_marker, url_download, wait_for
1313
from lib.installer import AnswerFile
14+
from lib.netutil import wait_for_ssh
1415

1516
from typing import Generator, Sequence, Union
1617

@@ -292,10 +293,7 @@ def vm_booted_with_installer(host, create_vms, remastered_iso):
292293
host_vm.ip = ips[0]
293294

294295
# host may not be up if ARP cache was filled
295-
wait_for(lambda: local_cmd(["ping", "-c1", host_vm.ip], check=False),
296-
"Wait for host up", timeout_secs=10 * 60, retry_delay_secs=10)
297-
wait_for(lambda: local_cmd(["nc", "-zw5", host_vm.ip, "22"], check=False),
298-
"Wait for ssh up on host", timeout_secs=10 * 60, retry_delay_secs=5)
296+
wait_for_ssh(host_vm.ip)
299297

300298
yield host_vm
301299

tests/install/test.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from lib import commands, installer, pxe
88
from lib.common import safe_split, wait_for
99
from lib.installer import AnswerFile
10+
from lib.netutil import wait_for_ssh
1011
from lib.pif import PIF
1112
from lib.pool import Pool
1213
from lib.vdi import VDI
@@ -187,10 +188,7 @@ def _test_firstboot(self, create_vms, mode, *, machine='DEFAULT', is_restore=Fal
187188
assert len(ips) == 1
188189
host_vm.ip = ips[0]
189190

190-
wait_for(
191-
lambda: commands.local_cmd(
192-
["nc", "-zw5", host_vm.ip, "22"], check=False).returncode == 0,
193-
"Wait for ssh back up on Host VM", retry_delay_secs=5, timeout_secs=4 * 60)
191+
wait_for_ssh(host_vm.ip, host_desc="host VM")
194192

195193
logging.info("Checking installed version (expecting %r %r)",
196194
expected_dist, expected_rel)

0 commit comments

Comments
 (0)