Skip to content

Commit 6c8c72f

Browse files
authored
gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (#145589)
Prefer VMADDR_CID_LOCAL instead of VMADDR_CID_ANY for bind() in the server. Skip the test if bind() fails with EADDRNOTAVAIL. Log vsock CID in test.pythoninfo.
1 parent d931725 commit 6c8c72f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Lib/test/pythoninfo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,10 @@ def collect_test_socket(info_add):
751751
if name.startswith('HAVE_')]
752752
copy_attributes(info_add, test_socket, 'test_socket.%s', attributes)
753753

754+
# Get IOCTL_VM_SOCKETS_GET_LOCAL_CID of /dev/vsock
755+
cid = test_socket.get_cid()
756+
info_add('test_socket.get_cid', cid)
757+
754758

755759
def collect_support(info_add):
756760
try:

Lib/test/test_socket.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ def clientTearDown(self):
563563
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
564564
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
565565
'VSOCK sockets required for this test.')
566-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
567-
"This test can only be run on a virtual guest.")
566+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
567+
"This test can only be run on a virtual guest.")
568568
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
569569

570570
def __init__(self, methodName='runTest'):
@@ -574,7 +574,16 @@ def __init__(self, methodName='runTest'):
574574
def setUp(self):
575575
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
576576
self.addCleanup(self.serv.close)
577-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
577+
cid = get_cid()
578+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
579+
cid = socket.VMADDR_CID_LOCAL
580+
try:
581+
self.serv.bind((cid, VSOCKPORT))
582+
except OSError as exc:
583+
if exc.errno == errno.EADDRNOTAVAIL:
584+
self.skipTest(f"bind() failed with {exc!r}")
585+
else:
586+
raise
578587
self.serv.listen()
579588
self.serverExplicitReady()
580589
self.serv.settimeout(support.LOOPBACK_TIMEOUT)

0 commit comments

Comments
 (0)