Merge "Use string substitution before raising exception"

This commit is contained in:
Jenkins 2015-08-13 14:23:24 +00:00 committed by Gerrit Code Review
commit 1b92f480a9
3 changed files with 41 additions and 22 deletions

View File

@ -301,7 +301,7 @@ class API(base_api.NetworkAPI):
'for network %(network_id)s.'),
{'ip': fixed_ip, 'network_id': network_id})
msg = (_('Fixed IP %(ip)s is not a valid ip address for '
'network %(network_id)s.'),
'network %(network_id)s.') %
{'ip': fixed_ip, 'network_id': network_id})
raise exception.InvalidInput(reason=msg)
except neutron_client_exc.IpAddressInUseClient:
@ -852,7 +852,7 @@ class API(base_api.NetworkAPI):
except neutron_client_exc.Unauthorized:
raise exception.Forbidden()
except neutron_client_exc.NeutronClientException as exc:
msg = (_("Failed to access port %(port_id)s: %(reason)s"),
msg = (_("Failed to access port %(port_id)s: %(reason)s") %
{'port_id': port_id, 'reason': exc})
raise exception.NovaException(message=msg)
@ -1126,10 +1126,10 @@ class API(base_api.NetworkAPI):
else:
free_ports = quotas.get('port') - len(ports)
if free_ports < 0:
msg = (_("The number of defined ports: %(ports)d"
"is over the limit: %(quota)d"),
{'ports': len(ports),
'quota': quotas.get('port')})
msg = (_("The number of defined ports: %(ports)d "
"is over the limit: %(quota)d") %
{'ports': len(ports),
'quota': quotas.get('port')})
raise exception.PortLimitExceeded(msg)
ports_needed = ports_needed_per_instance * num_instances
if free_ports >= ports_needed:

View File

@ -1602,14 +1602,15 @@ class TestNeutronv2(TestNeutronv2Base):
api.validate_networks,
self.context, requested_networks, 1)
def test_validate_networks_port_show_rasies_non404(self):
def test_validate_networks_port_show_raises_non404(self):
# Verify that the correct exception is thrown when a non existent
# port is passed to validate_networks.
fake_port_id = '3123-ad34-bc43-32332ca33e'
requested_networks = objects.NetworkRequestList(
objects=[objects.NetworkRequest(
network_id='my_netid1',
port_id='3123-ad34-bc43-32332ca33e')])
port_id=fake_port_id)])
NeutronNotFound = exceptions.NeutronClientException(status_code=0)
self.moxed_client.show_port(requested_networks[0].port_id).AndRaise(
@ -1618,9 +1619,13 @@ class TestNeutronv2(TestNeutronv2Base):
# Expected call from setUp.
neutronapi.get_client(None)
api = neutronapi.API()
self.assertRaises(exception.NovaException,
api.validate_networks,
self.context, requested_networks, 1)
exc = self.assertRaises(exception.NovaException,
api.validate_networks,
self.context, requested_networks, 1)
expected_exception_message = ('Failed to access port %(port_id)s: '
'An unknown exception occurred.' %
{'port_id': fake_port_id})
self.assertEqual(expected_exception_message, str(exc))
def test_validate_networks_port_in_use(self):
requested_networks = objects.NetworkRequestList(
@ -2917,9 +2922,15 @@ class TestNeutronv2WithMock(test.TestCase):
mock.patch.object(client.Client, 'show_quota',
return_value={'quota': {'port': 1}})):
self.assertRaises(exception.PortLimitExceeded,
self.api.validate_networks,
self.context, requested_networks, 1)
exc = self.assertRaises(exception.PortLimitExceeded,
self.api.validate_networks,
self.context, requested_networks, 1)
expected_exception_msg = ('The number of defined ports: '
'%(ports)d is over the limit: '
'%(quota)d' %
{'ports': 5,
'quota': 1})
self.assertEqual(expected_exception_msg, str(exc))
def test_validate_networks_fixed_ip_no_dup1(self):
# Test validation for a request for a network with a
@ -3088,11 +3099,19 @@ class TestNeutronv2WithMock(test.TestCase):
'mac_address': 'XX:XX:XX:XX:XX:XX'}}
fake_ip = '1.1.1.1'
# Run the code.
self.assertRaises(exception.InvalidInput,
self.api._create_port,
neutronapi.get_client(self.context),
instance, net['id'], port_req_body,
fixed_ip=fake_ip)
exc = self.assertRaises(exception.InvalidInput,
self.api._create_port,
neutronapi.get_client(self.context),
instance, net['id'], port_req_body,
fixed_ip=fake_ip)
# Assert the exception message
expected_exception_msg = ('Invalid input received: Fixed IP %(ip)s is '
'not a valid ip address for network '
'%(net_id)s.' %
{'ip': fake_ip, 'net_id': net['id']})
self.assertEqual(expected_exception_msg, str(exc))
# Assert the calls.
create_port_mock.assert_called_once_with(port_req_body)

View File

@ -261,8 +261,8 @@ class PathUtils(object):
except wmi.x_wmi as exc:
err_msg = (_(
'Unable to mount SMBFS share: %(smbfs_share)s '
'WMI exception: %(wmi_exc)s'), {'smbfs_share': smbfs_share,
'wmi_exc': exc})
'WMI exception: %(wmi_exc)s') % {'smbfs_share': smbfs_share,
'wmi_exc': exc})
raise vmutils.HyperVException(err_msg)
def unmount_smb_share(self, smbfs_share, force=False):
@ -285,4 +285,4 @@ class PathUtils(object):
# case.
if force:
raise vmutils.HyperVException(
_("Could not unmount share: %s"), smbfs_share)
_("Could not unmount share: %s") % smbfs_share)