Change try..except to assertRaises in UT

Following patch changes the try..except condition for
testing to assertRaises.

Change-Id: I2253b4e75f778ad077bd905487ede075a430a1fa
This commit is contained in:
reedip 2016-02-19 12:56:21 +09:00 committed by Reedip
parent 97ed202387
commit fc2950c2bb
4 changed files with 29 additions and 45 deletions

View File

@ -695,13 +695,10 @@ class ClientV2TestJson(CLITestV20Base):
def test_do_request_with_long_uri_exception(self):
long_string = 'x' * 8200 # 8200 > MAX_URI_LEN:8192
params = {'id': long_string}
try:
self.client.do_request('GET', '/test', body='', params=params)
except exceptions.RequestURITooLong as cm:
self.assertNotEqual(0, cm.excess)
else:
self.fail('Expected exception NOT raised')
exception = self.assertRaises(exceptions.RequestURITooLong,
self.client.do_request,
'GET', '/test', body='', params=params)
self.assertNotEqual(0, exception.excess)
def test_do_request_request_ids(self):
self.mox.StubOutWithMock(self.client.httpclient, "request")

View File

@ -260,13 +260,10 @@ class CLITestV20RouterJSON(test_cli20.CLITestV20Base):
'--no-routes',
'--route',
'destination=10.0.3.0/24,nexthop=10.0.0.10']
actual_error_code = 0
try:
self._test_update_resource(resource, cmd, myid, args, None)
except SystemExit:
exc_type, exc_value, exc_traceback = sys.exc_info()
actual_error_code = exc_value.code
self.assertEqual(2, actual_error_code)
exception = self.assertRaises(SystemExit,
self._test_update_resource,
resource, cmd, myid, args, None)
self.assertEqual(2, exception.code)
def test_delete_router(self):
# Delete router: myid.

View File

@ -648,15 +648,11 @@ class CLITestV20SubnetJSON(test_cli20.CLITestV20Base):
# Update sbunet: --enable-dhcp and --disable-dhcp.
resource = 'subnet'
cmd = subnet.UpdateSubnet(test_cli20.MyApp(sys.stdout), None)
try:
self._test_update_resource(resource, cmd, 'myid',
['myid', '--name', 'myname',
'--enable-dhcp', '--disable-dhcp'],
{'name': 'myname', }
)
except exceptions.CommandError:
return
self.fail('No exception for --enable-dhcp --disable-dhcp option')
self.assertRaises(exceptions.CommandError,
self._test_update_resource,
resource, cmd, 'myid',
['myid', '--name', 'myname', '--enable-dhcp',
'--disable-dhcp'], {'name': 'myname', })
def test_show_subnet(self):
# Show subnet: --fields id --fields name myid.

View File

@ -120,11 +120,10 @@ class CLITestNameorID(testtools.TestCase):
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
).AndReturn((test_cli20.MyResp(200), resstr))
self.mox.ReplayAll()
try:
neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', name)
except exceptions.NeutronClientNoUniqueMatch as ex:
self.assertIn('Multiple', ex.message)
exception = self.assertRaises(exceptions.NeutronClientNoUniqueMatch,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'network', name)
self.assertIn('Multiple', exception.message)
def test_get_id_from_name_notfound(self):
name = 'myname'
@ -141,12 +140,11 @@ class CLITestNameorID(testtools.TestCase):
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
).AndReturn((test_cli20.MyResp(200), resstr))
self.mox.ReplayAll()
try:
neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', name)
except exceptions.NotFound as ex:
self.assertIn('Unable to find', ex.message)
self.assertEqual(404, ex.status_code)
exception = self.assertRaises(exceptions.NotFound,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'network', name)
self.assertIn('Unable to find', exception.message)
self.assertEqual(404, exception.status_code)
def test_get_id_from_name_multiple_with_project(self):
name = 'web_server'
@ -175,9 +173,7 @@ class CLITestNameorID(testtools.TestCase):
def test_get_id_from_name_multiple_with_project_not_found(self):
name = 'web_server'
project = str(uuid.uuid4())
reses = {'security_groups':
[{'id': str(uuid.uuid4()), 'tenant_id': str(uuid.uuid4())}]}
resstr = self.client.serialize(reses)
resstr_notfound = self.client.serialize({'security_groups': []})
self.mox.StubOutWithMock(self.client.httpclient, "request")
path = getattr(self.client, "security_groups_path")
self.client.httpclient.request(
@ -187,12 +183,10 @@ class CLITestNameorID(testtools.TestCase):
'GET',
body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
).AndReturn((test_cli20.MyResp(200), resstr))
).AndReturn((test_cli20.MyResp(200), resstr_notfound))
self.mox.ReplayAll()
try:
neutronV20.find_resourceid_by_name_or_id(
self.client, 'security_group', name, project)
except exceptions.NotFound as ex:
self.assertIn('Unable to find', ex.message)
self.assertEqual(404, ex.status_code)
exc = self.assertRaises(exceptions.NotFound,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'security_group', name, project)
self.assertIn('Unable to find', exc.message)
self.assertEqual(404, exc.status_code)