Save socket fd to be cleaned up in functional test

Functional tests that create a netlink dispatcher thread should close
the netlink socket and terminate the netlink dispatcher thread.  This
requires the dispatcher thread to store the netlink socket's socket FD,
such that a functional test case can close the socket to trigger the
existing code to terminate the dispatcher thread.

Related-Bug: #2144617
Assisted-By: Claude Opus 4.6
Change-Id: I6e3ae037a8575be137bad65f2fd0cf4c719033b3
Signed-off-by: Helen Chen <ichen@redhat.com>
This commit is contained in:
Helen Chen committed 2026-07-14 09:53:12 -04:00
1 parent 32f99902dd
commit 5ba251e192
2 files changed
+19

No files matched your search

+3
View File
@@ -19,6 +19,7 @@ import logging
import os
import threading
import time
import weakref
from oslo_log import log
from pyroute2 import iproute
@@ -43,6 +44,7 @@ class NetlinkDispatcher:
self._groups = groups
self._replay_start_callbacks = []
self._replay_end_callbacks = []
self._ipr = lambda: None
def register_handler(self, event_type, handler):
"""Register a handler for a specific netlink message type.
@@ -75,6 +77,7 @@ class NetlinkDispatcher:
def _sock(self):
ipr = iproute.IPRoute()
ipr.bind(self._groups)
self._ipr = weakref.ref(ipr)
try:
yield ipr
finally:
@@ -17,6 +17,7 @@ import socket
from unittest import mock
from oslo_utils import uuidutils
from pyroute2 import iproute
from pyroute2.netlink import rtnl
from neutron.agent.linux import ip_lib
@@ -44,6 +45,20 @@ class TestVrfHandlerLifecycle(functional_base.BaseSudoTestCase):
except Exception:
pass
def _stop_dispatcher(self, dispatcher):
dispatcher.RETRY_BACKOFF = 0
with mock.patch.object(nl_dispatcher.os, '_exit',
side_effect=SystemExit):
with mock.patch.object(iproute, 'IPRoute',
side_effect=RuntimeError):
ipr = dispatcher._ipr()
if ipr:
ipr.close()
try:
dispatcher._thread.join(timeout=5)
except SystemExit:
pass
def test_vrf_handler_lifecycle(self):
vrf_handler = netlink_monitor.VrfHandler(fsm.EvpnFSM())
@@ -63,6 +78,7 @@ class TestVrfHandlerLifecycle(functional_base.BaseSudoTestCase):
self.addCleanup(self._safe_delete, preexisting_vrf)
dispatcher.start()
self.addCleanup(self._stop_dispatcher, dispatcher)
utils.wait_until_true(
lambda: preexisting_vrf in vrf_handler._known_vrfs,
timeout=10, sleep=0.1)