Skip to content

Commit 1d5fa34

Browse files
miss-islingtonencukouvstinner
authored
[3.10] gh-145548: Use VMADDR_CID_LOCAL in VSOCK socket tests (GH-145589) (GH-145809) (#148617)
[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 451ece9 commit 1d5fa34

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
@@ -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
@@ -507,8 +507,8 @@ def clientTearDown(self):
507507
@unittest.skipIf(fcntl is None, "need fcntl")
508508
@unittest.skipUnless(HAVE_SOCKET_VSOCK,
509509
'VSOCK sockets required for this test.')
510-
@unittest.skipUnless(get_cid() != 2, # VMADDR_CID_HOST
511-
"This test can only be run on a virtual guest.")
510+
@unittest.skipIf(get_cid() == getattr(socket, 'VMADDR_CID_HOST', 2),
511+
"This test can only be run on a virtual guest.")
512512
class ThreadedVSOCKSocketStreamTest(unittest.TestCase, ThreadableTest):
513513

514514
def __init__(self, methodName='runTest'):
@@ -518,7 +518,16 @@ def __init__(self, methodName='runTest'):
518518
def setUp(self):
519519
self.serv = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
520520
self.addCleanup(self.serv.close)
521-
self.serv.bind((socket.VMADDR_CID_ANY, VSOCKPORT))
521+
cid = get_cid()
522+
if cid in (socket.VMADDR_CID_HOST, socket.VMADDR_CID_ANY):
523+
cid = VMADDR_CID_LOCAL
524+
try:
525+
self.serv.bind((cid, VSOCKPORT))
526+
except OSError as exc:
527+
if exc.errno == errno.EADDRNOTAVAIL:
528+
self.skipTest(f"bind() failed with {exc!r}")
529+
else:
530+
raise
522531
self.serv.listen()
523532
self.serverExplicitReady()
524533
self.conn, self.connaddr = self.serv.accept()

0 commit comments

Comments
 (0)