ovs: raise RuntimeError in _get_dp if id is None

If the switch misbehaves, we may receive None from db_get_val. In this
case, int() on the return value will raise TypeError which is not
expected by callers and may result in ovs agent crash.

Instead of bubbling up the TypeError exception, we raise RuntimeError if
datapath id is None.

Change-Id: I53bea00b9a7302d694b8066e969c894bf64cb2d4
Closes-Bug: #1731494
Ihar Hrachyshka 5 years ago
parent c16d15fff2
commit 38d0b2b52d

@ -42,10 +42,12 @@ class OVSAgentBridge(ofswitch.OpenFlowSwitchMixin,
while True:
if self._cached_dpid is None:
dpid_str = self.get_datapath_id()
dpid = self.get_datapath_id()
LOG.info("Bridge %(br_name)s has datapath-ID %(dpid)s",
{"br_name": self.br_name, "dpid": dpid_str})
self._cached_dpid = int(dpid_str, 16)
{"br_name": self.br_name, "dpid": dpid})
if dpid is None:
raise RuntimeError("Unknown datapath id.")
self._cached_dpid = int(dpid, 16)
dp = self._get_dp_by_dpid(self._cached_dpid)
return dp, dp.ofproto, dp.ofproto_parser

@ -49,3 +49,8 @@ class OVSAgentBridgeTestCase(ovs_test_base.OVSRyuTestBase):
# make sure that in case of any misconfiguration when no datapath is
# found a proper exception, not a TypeError is raised
self.assertRaises(RuntimeError, br._get_dp)
def test__get_dp_when_get_datapath_id_returns_None(self):
br = self.br_int_cls('br-int')
with mock.patch.object(br, 'get_datapath_id', return_value=None):
self.assertRaises(RuntimeError, br._get_dp)