From fc91e158f202112ae5cc576ad3e3fa0f89deff1e Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Tue, 1 Jul 2014 23:51:15 +0000 Subject: [PATCH] Fix OVSBridge.get_port_ofport to handle empty output Previously ovs_lib.OVSBridge.get_port_ofport() was not handling the case where db_get_val() was returning None when no output was recieved from ovs-vsctl. This patch ensures that the TypeError that results from this condition is appropriately handled. Change-Id: I168eb0f2c6f0cb98a3eb3e118972bbaf0383ff89 Partial-bug: #1336172 --- neutron/agent/linux/ovs_lib.py | 2 +- .../tests/unit/agent/linux/test_ovs_lib.py | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/neutron/agent/linux/ovs_lib.py b/neutron/agent/linux/ovs_lib.py index 4197b4ec81..9adb2d35dd 100644 --- a/neutron/agent/linux/ovs_lib.py +++ b/neutron/agent/linux/ovs_lib.py @@ -182,7 +182,7 @@ class OVSBridge(BaseOVS): try: int(ofport) return ofport - except ValueError: + except (ValueError, TypeError): return constants.INVALID_OFPORT def get_datapath_id(self): diff --git a/neutron/tests/unit/agent/linux/test_ovs_lib.py b/neutron/tests/unit/agent/linux/test_ovs_lib.py index 8a19ed39a5..e60e62cbdb 100644 --- a/neutron/tests/unit/agent/linux/test_ovs_lib.py +++ b/neutron/tests/unit/agent/linux/test_ovs_lib.py @@ -319,23 +319,22 @@ class OVS_Lib_Test(base.BaseTestCase): process_input=None, root_helper=self.root_helper) - def test_get_port_ofport(self): + def _test_get_port_ofport(self, ofport, expected_result): pname = "tap99" - ofport = "6" self.execute.return_value = ofport - self.assertEqual(self.br.get_port_ofport(pname), ofport) + self.assertEqual(self.br.get_port_ofport(pname), expected_result) self.execute.assert_called_once_with( ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], root_helper=self.root_helper) - def test_get_port_ofport_non_int(self): - pname = "tap99" - ofport = "[]" - self.execute.return_value = ofport - self.assertEqual(self.br.get_port_ofport(pname), const.INVALID_OFPORT) - self.execute.assert_called_once_with( - ["ovs-vsctl", self.TO, "get", "Interface", pname, "ofport"], - root_helper=self.root_helper) + def test_get_port_ofport_succeeds_for_valid_ofport(self): + self._test_get_port_ofport("6", "6") + + def test_get_port_ofport_returns_invalid_ofport_for_non_int(self): + self._test_get_port_ofport("[]", const.INVALID_OFPORT) + + def test_get_port_ofport_returns_invalid_ofport_for_none(self): + self._test_get_port_ofport(None, const.INVALID_OFPORT) def test_get_datapath_id(self): datapath_id = '"0000b67f4fbcc149"'