Skip to content

Commit 1e1074a

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 47128e6 commit 1e1074a

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
@@ -654,6 +654,10 @@ def collect_test_socket(info_add):
654654
if name.startswith('HAVE_')]
655655
copy_attributes(info_add, test_socket, 'test_socket.%s', attributes)
656656

657+
# Get IOCTL_VM_SOCKETS_GET_LOCAL_CID of /dev/vsock
658+
cid = test_socket.get_cid()
659+
info_add('test_socket.get_cid', cid)
660+
657661

658662
def collect_test_support(info_add):
659663
try:

Lib/test/test_socket.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ def clientTearDown(self):
488488
@unittest.skipIf(fcntl is None, "need fcntl")
489489
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
490490
'VSOCK sockets required for this test.')
491-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
492-
"This test can only be run on a virtual guest.")
491+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
492+
"This test can only be run on a virtual guest.")
493493
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
494494

495495
def __init__(self, methodName='runTest'):
@@ -499,7 +499,16 @@ def __init__(self, methodName='runTest'):
499499
def setUp(self):
500500
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
501501
self.addCleanup(self.serv.close)
502-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
502+
cid = get_cid()
503+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
504+
cid = VMADDR_CID_LOCAL
505+
try:
506+
self.serv.bind((cid, VSOCKPORT))
507+
except OSError as exc:
508+
if exc.errno == errno.EADDRNOTAVAIL:
509+
self.skipTest(f"bind() failed with {exc!r}")
510+
else:
511+
raise
503512
self.serv.listen()
504513
self.serverExplicitReady()
505514
self.conn, self.connaddr = self.serv.accept()

0 commit comments

Comments
 (0)