Skip to content

Commit cdf554c

Browse files
encukoumiss-islingtonvstinner
committed
[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: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent e20c6c9 commit cdf554c

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
@@ -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
@@ -472,8 +472,8 @@ def clientTearDown(self):
472472
@unittest.skipIf(WSL, 'VSOCK does not work on Microsoft WSL')
473473
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
474474
'VSOCK sockets required for this test.')
475-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
476-
"This test can only be run on a virtual guest.")
475+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
476+
"This test can only be run on a virtual guest.")
477477
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
478478

479479
def __init__(self, methodName='runTest'):
@@ -483,7 +483,16 @@ def __init__(self, methodName='runTest'):
483483
def setUp(self):
484484
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
485485
self.addCleanup(self.serv.close)
486-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
486+
cid = get_cid()
487+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
488+
cid = VMADDR_CID_LOCAL
489+
try:
490+
self.serv.bind((cid, VSOCKPORT))
491+
except OSError as exc:
492+
if exc.errno == errno.EADDRNOTAVAIL:
493+
self.skipTest(f"bind() failed with {exc!r}")
494+
else:
495+
raise
487496
self.serv.listen()
488497
self.serverExplicitReady()
489498
self.serv.settimeout(support.LOOPBACK_TIMEOUT)

0 commit comments

Comments
 (0)