Skip to content

Commit 7bc66f3

Browse files
miss-islingtonvstinner
authored andcommitted
[3.12] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-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) (cherry picked from commit 16dbbe5) Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 57cc1bd commit 7bc66f3

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
@@ -723,6 +723,10 @@ def collect_test_socket(info_add):
723723
if name.startswith('HAVE_')]
724724
copy_attributes(info_add, test_socket, 'test_socket.%s', attributes)
725725

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

727731
def collect_support(info_add):
728732
try:

Lib/test/test_socket.py

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

493493
def __init__(self, methodName='runTest'):
@@ -497,7 +497,16 @@ def __init__(self, methodName='runTest'):
497497
def setUp(self):
498498
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
499499
self.addCleanup(self.serv.close)
500-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
500+
cid = get_cid()
501+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
502+
cid = socket.VMADDR_CID_LOCAL
503+
try:
504+
self.serv.bind((cid, VSOCKPORT))
505+
except OSError as exc:
506+
if exc.errno == errno.EADDRNOTAVAIL:
507+
self.skipTest(f"bind() failed with {exc!r}")
508+
else:
509+
raise
501510
self.serv.listen()
502511
self.serverExplicitReady()
503512
self.serv.settimeout(support.LOOPBACK_TIMEOUT)

0 commit comments

Comments
 (0)