[stable only] Wait chassis creation before retrieving agent

To be sure the agent is present, first its needed to create
a chassis with the correct references and wait for the creation
event.

This patch is an extract of [1].

[1]https://review.opendev.org/c/openstack/neutron/+/752795

Change-Id: Id13ecb68349ca96907c7c1fc959fc89319a10c72
Related-Bug: #1899004
This commit is contained in:
Rodolfo Alonso Hernandez 2021-02-05 12:22:40 +00:00
parent 04e1a63979
commit d3e95a1462
1 changed files with 61 additions and 13 deletions

View File

@ -19,6 +19,7 @@ from neutron_lib.api.definitions import portbindings
from neutron_lib import constants
from oslo_config import cfg
from oslo_utils import uuidutils
from ovsdbapp.backend.ovs_idl import event
from ovsdbapp.tests.functional import base as ovs_base
from neutron.common.ovn import constants as ovn_const
@ -744,23 +745,70 @@ class TestProvnetPorts(base.TestOVNFunctionalBase):
self.assertIsNone(ovn_localnetport)
class TestAgentApi(base.TestOVNFunctionalBase):
class AgentWaitEvent(event.WaitEvent):
"""Wait for a list of Chassis to be created"""
def setUp(self):
ONETIME = False
def __init__(self, driver, chassis_names):
table = driver.agent_chassis_table
events = (self.ROW_CREATE,)
self.chassis_names = chassis_names
super().__init__(events, table, None)
self.event_name = 'AgentWaitEvent'
def match_fn(self, event, row, old):
return row.name in self.chassis_names
def run(self, event, row, old):
self.chassis_names.remove(row.name)
if not self.chassis_names:
self.event.set()
class TestAgentApi(base.TestOVNFunctionalBase):
TEST_AGENT = 'test'
def setUp(self, *args):
super().setUp()
self.host = 'test-host'
self.controller_agent = self.add_fake_chassis(self.host)
self.host = n_utils.get_rand_name(prefix='testhost-')
self.plugin = self.mech_driver._plugin
agent = {'agent_type': 'test', 'binary': '/bin/test',
'host': self.host, 'topic': 'test_topic'}
_, status = self.plugin.create_or_update_agent(self.context, agent)
self.test_agent = status['id']
mock.patch.object(self.mech_driver, 'ping_all_chassis',
return_value=False).start()
def test_agent_show_non_ovn(self):
self.assertTrue(self.plugin.get_agent(self.context, self.test_agent))
metadata_agent_id = uuidutils.generate_uuid()
# To be *mostly* sure the agent cache has been updated, we need to
# wait for the Chassis events to run. So add a new event that should
# run afterthey do and wait for it. I've only had to do this when
# adding *a bunch* of Chassis at a time, but better safe than sorry.
chassis_name = uuidutils.generate_uuid()
agent_event = AgentWaitEvent(self.mech_driver, [chassis_name])
self.sb_api.idl.notify_handler.watch_event(agent_event)
def test_agent_show_ovn_controller(self):
self.assertTrue(self.plugin.get_agent(self.context,
self.controller_agent))
self.chassis = self.add_fake_chassis(
self.host, name=chassis_name,
external_ids={
ovn_const.OVN_AGENT_METADATA_ID_KEY: metadata_agent_id})
self.assertTrue(agent_event.wait())
self.agent_types = {
self.TEST_AGENT: self._create_test_agent(),
ovn_const.OVN_CONTROLLER_AGENT: self.chassis,
ovn_const.OVN_METADATA_AGENT: metadata_agent_id,
}
def _create_test_agent(self):
agent = {'agent_type': self.TEST_AGENT, 'binary': '/bin/test',
'host': self.host, 'topic': 'test_topic'}
_, status = self.plugin.create_or_update_agent(self.context, agent)
return status['id']
def test_agent_show(self):
for agent_id in self.agent_types.values():
self.assertTrue(self.plugin.get_agent(self.context, agent_id))
def test_agent_list(self):
agent_ids = [a['id'] for a in self.plugin.get_agents(
self.context, filters={'host': self.host})]
self.assertCountEqual(list(self.agent_types.values()), agent_ids)