Skip to content

Commit 477f29d

Browse files
miss-islingtonencukouvstinner
authored
[3.11] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-145809) (#148616)
[3.12] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-145809) * [3.12] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-145594) 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) (cherry picked from commit 16dbbe5) * [3.13] gh-145548: Don't use VMADDR_CID_LOCAL from `socket` (GH-145735) VMADDR_CID_LOCAL was added to `socekt` in 3.14. The test needs a local constant in setUp(), as in clientSetUp(). (cherry picked from commit e378eda) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 87536d5 commit 477f29d

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

Lib/test/pythoninfo.py

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

725+
# Get IOCTL_VM_SOCKETS_GET_LOCAL_CID of /dev/vsock
726+
cid = test_socket.get_cid()
727+
info_add('test_socket.get_cid', cid)
728+
725729

726730
def collect_support(info_add):
727731
try:

Lib/test/test_socket.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ def clientTearDown(self):
491491
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
492492
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
493493
'VSOCK sockets required for this test.')
494-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
495-
"This test can only be run on a virtual guest.")
494+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
495+
"This test can only be run on a virtual guest.")
496496
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
497497

498498
def __init__(self, methodName='runTest'):
@@ -502,7 +502,16 @@ def __init__(self, methodName='runTest'):
502502
def setUp(self):
503503
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
504504
self.addCleanup(self.serv.close)
505-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
505+
cid = get_cid()
506+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
507+
cid = VMADDR_CID_LOCAL
508+
try:
509+
self.serv.bind((cid, VSOCKPORT))
510+
except OSError as exc:
511+
if exc.errno == errno.EADDRNOTAVAIL:
512+
self.skipTest(f"bind() failed with {exc!r}")
513+
else:
514+
raise
506515
self.serv.listen()
507516
self.serverExplicitReady()
508517
self.serv.settimeout(support.LOOPBACK_TIMEOUT)

0 commit comments

Comments
 (0)