Merge "Raise an error from ovs_lib list operations"

This commit is contained in:
Jenkins 2014-02-12 22:32:51 +00:00 committed by Gerrit Code Review
commit 46e2c5b4ae
2 changed files with 54 additions and 30 deletions

View File

@ -24,6 +24,7 @@ from oslo.config import cfg
from neutron.agent.linux import ip_lib from neutron.agent.linux import ip_lib
from neutron.agent.linux import utils from neutron.agent.linux import utils
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging from neutron.openstack.common import log as logging
from neutron.plugins.common import constants as p_const from neutron.plugins.common import constants as p_const
@ -68,10 +69,12 @@ class BaseOVS(object):
try: try:
return utils.execute(full_args, root_helper=self.root_helper) return utils.execute(full_args, root_helper=self.root_helper)
except Exception as e: except Exception as e:
LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"), with excutils.save_and_reraise_exception() as ctxt:
{'cmd': full_args, 'exception': e}) LOG.error(_("Unable to execute %(cmd)s. "
if check_error: "Exception: %(exception)s"),
raise {'cmd': full_args, 'exception': e})
if not check_error:
ctxt.reraise = False
def add_bridge(self, bridge_name): def add_bridge(self, bridge_name):
self.run_vsctl(["--", "--may-exist", "add-br", bridge_name]) self.run_vsctl(["--", "--may-exist", "add-br", bridge_name])
@ -84,17 +87,19 @@ class BaseOVS(object):
try: try:
self.run_vsctl(['br-exists', bridge_name], check_error=True) self.run_vsctl(['br-exists', bridge_name], check_error=True)
except RuntimeError as e: except RuntimeError as e:
if 'Exit code: 2\n' in str(e): with excutils.save_and_reraise_exception() as ctxt:
return False if 'Exit code: 2\n' in str(e):
raise ctxt.reraise = False
return False
return True return True
def get_bridge_name_for_port_name(self, port_name): def get_bridge_name_for_port_name(self, port_name):
try: try:
return self.run_vsctl(['port-to-br', port_name], check_error=True) return self.run_vsctl(['port-to-br', port_name], check_error=True)
except RuntimeError as e: except RuntimeError as e:
if 'Exit code: 1\n' not in str(e): with excutils.save_and_reraise_exception() as ctxt:
raise if 'Exit code: 1\n' in str(e):
ctxt.reraise = False
def port_exists(self, port_name): def port_exists(self, port_name):
return bool(self.get_bridge_name_for_port_name(port_name)) return bool(self.get_bridge_name_for_port_name(port_name))
@ -282,15 +287,15 @@ class OVSBridge(BaseOVS):
"type=patch", "options:peer=%s" % remote_name]) "type=patch", "options:peer=%s" % remote_name])
return self.get_port_ofport(local_name) return self.get_port_ofport(local_name)
def db_get_map(self, table, record, column): def db_get_map(self, table, record, column, check_error=False):
output = self.run_vsctl(["get", table, record, column]) output = self.run_vsctl(["get", table, record, column], check_error)
if output: if output:
output_str = output.rstrip("\n\r") output_str = output.rstrip("\n\r")
return self.db_str_to_map(output_str) return self.db_str_to_map(output_str)
return {} return {}
def db_get_val(self, table, record, column): def db_get_val(self, table, record, column, check_error=False):
output = self.run_vsctl(["get", table, record, column]) output = self.run_vsctl(["get", table, record, column], check_error)
if output: if output:
return output.rstrip("\n\r") return output.rstrip("\n\r")
@ -305,7 +310,7 @@ class OVSBridge(BaseOVS):
return ret return ret
def get_port_name_list(self): def get_port_name_list(self):
res = self.run_vsctl(["list-ports", self.br_name]) res = self.run_vsctl(["list-ports", self.br_name], check_error=True)
if res: if res:
return res.strip().split("\n") return res.strip().split("\n")
return [] return []
@ -319,16 +324,20 @@ class OVSBridge(BaseOVS):
try: try:
return utils.execute(args, root_helper=self.root_helper).strip() return utils.execute(args, root_helper=self.root_helper).strip()
except Exception as e: except Exception as e:
LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"), with excutils.save_and_reraise_exception():
{'cmd': args, 'exception': e}) LOG.error(_("Unable to execute %(cmd)s. "
"Exception: %(exception)s"),
{'cmd': args, 'exception': e})
# returns a VIF object for each VIF port # returns a VIF object for each VIF port
def get_vif_ports(self): def get_vif_ports(self):
edge_ports = [] edge_ports = []
port_names = self.get_port_name_list() port_names = self.get_port_name_list()
for name in port_names: for name in port_names:
external_ids = self.db_get_map("Interface", name, "external_ids") external_ids = self.db_get_map("Interface", name, "external_ids",
ofport = self.db_get_val("Interface", name, "ofport") check_error=True)
ofport = self.db_get_val("Interface", name, "ofport",
check_error=True)
if "iface-id" in external_ids and "attached-mac" in external_ids: if "iface-id" in external_ids and "attached-mac" in external_ids:
p = VifPort(name, ofport, external_ids["iface-id"], p = VifPort(name, ofport, external_ids["iface-id"],
external_ids["attached-mac"], self) external_ids["attached-mac"], self)
@ -349,7 +358,7 @@ class OVSBridge(BaseOVS):
edge_ports = set() edge_ports = set()
args = ['--format=json', '--', '--columns=name,external_ids', args = ['--format=json', '--', '--columns=name,external_ids',
'list', 'Interface'] 'list', 'Interface']
result = self.run_vsctl(args) result = self.run_vsctl(args, check_error=True)
if not result: if not result:
return edge_ports return edge_ports
for row in jsonutils.loads(result)['data']: for row in jsonutils.loads(result)['data']:
@ -420,8 +429,8 @@ def get_bridges(root_helper):
try: try:
return utils.execute(args, root_helper=root_helper).strip().split("\n") return utils.execute(args, root_helper=root_helper).strip().split("\n")
except Exception as e: except Exception as e:
LOG.exception(_("Unable to retrieve bridges. Exception: %s"), e) with excutils.save_and_reraise_exception():
return [] LOG.exception(_("Unable to retrieve bridges. Exception: %s"), e)
def get_installed_ovs_usr_version(root_helper): def get_installed_ovs_usr_version(root_helper):

View File

@ -457,10 +457,10 @@ class OVS_Lib_Test(base.BaseTestCase):
get_xapi_iface_id.assert_called_once_with('tap99id') get_xapi_iface_id.assert_called_once_with('tap99id')
def test_get_vif_ports_nonxen(self): def test_get_vif_ports_nonxen(self):
self._test_get_vif_ports(False) self._test_get_vif_ports(is_xen=False)
def test_get_vif_ports_xen(self): def test_get_vif_ports_xen(self):
self._test_get_vif_ports(True) self._test_get_vif_ports(is_xen=True)
def test_get_vif_port_set_nonxen(self): def test_get_vif_port_set_nonxen(self):
self._test_get_vif_port_set(False) self._test_get_vif_port_set(False)
@ -468,19 +468,24 @@ class OVS_Lib_Test(base.BaseTestCase):
def test_get_vif_port_set_xen(self): def test_get_vif_port_set_xen(self):
self._test_get_vif_port_set(True) self._test_get_vif_port_set(True)
def test_get_vif_ports_list_ports_error(self):
expected_calls_and_values = [
(mock.call(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME],
root_helper=self.root_helper),
RuntimeError()),
]
tools.setup_mock_calls(self.execute, expected_calls_and_values)
self.assertRaises(RuntimeError, self.br.get_vif_ports)
tools.verify_mock_calls(self.execute, expected_calls_and_values)
def test_get_vif_port_set_list_ports_error(self): def test_get_vif_port_set_list_ports_error(self):
expected_calls_and_values = [ expected_calls_and_values = [
(mock.call(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME], (mock.call(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME],
root_helper=self.root_helper), root_helper=self.root_helper),
RuntimeError()), RuntimeError()),
(mock.call(["ovs-vsctl", self.TO, "--format=json",
"--", "--columns=name,external_ids",
"list", "Interface"],
root_helper=self.root_helper),
self._encode_ovs_json(['name', 'external_ids'], []))
] ]
tools.setup_mock_calls(self.execute, expected_calls_and_values) tools.setup_mock_calls(self.execute, expected_calls_and_values)
self.assertEqual(set(), self.br.get_vif_port_set()) self.assertRaises(RuntimeError, self.br.get_vif_port_set)
tools.verify_mock_calls(self.execute, expected_calls_and_values) tools.verify_mock_calls(self.execute, expected_calls_and_values)
def test_get_vif_port_set_list_interface_error(self): def test_get_vif_port_set_list_interface_error(self):
@ -495,7 +500,7 @@ class OVS_Lib_Test(base.BaseTestCase):
RuntimeError()), RuntimeError()),
] ]
tools.setup_mock_calls(self.execute, expected_calls_and_values) tools.setup_mock_calls(self.execute, expected_calls_and_values)
self.assertEqual(set(), self.br.get_vif_port_set()) self.assertRaises(RuntimeError, self.br.get_vif_port_set)
tools.verify_mock_calls(self.execute, expected_calls_and_values) tools.verify_mock_calls(self.execute, expected_calls_and_values)
def test_clear_db_attribute(self): def test_clear_db_attribute(self):
@ -572,6 +577,16 @@ class OVS_Lib_Test(base.BaseTestCase):
mock.call('tap5678') mock.call('tap5678')
]) ])
def test_delete_neutron_ports_list_error(self):
expected_calls_and_values = [
(mock.call(["ovs-vsctl", self.TO, "list-ports", self.BR_NAME],
root_helper=self.root_helper),
RuntimeError()),
]
tools.setup_mock_calls(self.execute, expected_calls_and_values)
self.assertRaises(RuntimeError, self.br.delete_ports, all_ports=False)
tools.verify_mock_calls(self.execute, expected_calls_and_values)
def _test_get_bridges(self, exp_timeout=None): def _test_get_bridges(self, exp_timeout=None):
bridges = ['br-int', 'br-ex'] bridges = ['br-int', 'br-ex']
root_helper = 'sudo' root_helper = 'sudo'