Skip to content

Commit 16dbbe5

Browse files
[3.13] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (#145594)
gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-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. (cherry picked from commit 6c8c72f) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 5db0177 commit 16dbbe5

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
@@ -738,6 +738,10 @@ def collect_test_socket(info_add):
738738
if name.startswith('HAVE_')]
739739
copy_attributes(info_add, test_socket, 'test_socket.%s', attributes)
740740

741+
# Get IOCTL_VM_SOCKETS_GET_LOCAL_CID of /dev/vsock
742+
cid = test_socket.get_cid()
743+
info_add('test_socket.get_cid', cid)
744+
741745

742746
def collect_support(info_add):
743747
try:

Lib/test/test_socket.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ def clientTearDown(self):
555555
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
556556
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
557557
'VSOCK sockets required for this test.')
558-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
559-
"This test can only be run on a virtual guest.")
558+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
559+
"This test can only be run on a virtual guest.")
560560
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
561561

562562
def __init__(self, methodName='runTest'):
@@ -566,7 +566,16 @@ def __init__(self, methodName='runTest'):
566566
def setUp(self):
567567
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
568568
self.addCleanup(self.serv.close)
569-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
569+
cid = get_cid()
570+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
571+
cid = socket.VMADDR_CID_LOCAL
572+
try:
573+
self.serv.bind((cid, VSOCKPORT))
574+
except OSError as exc:
575+
if exc.errno == errno.EADDRNOTAVAIL:
576+
self.skipTest(f"bind() failed with {exc!r}")
577+
else:
578+
raise
570579
self.serv.listen()
571580
self.serverExplicitReady()
572581
self.serv.settimeout(support.LOOPBACK_TIMEOUT)

0 commit comments

Comments
 (0)