Remove mox/mox3 usage from test_name_or_id.py

Change-Id: Iefaeb45312177be172858203715bdc1db88e6add
Partial-Bug: #1753504
This commit is contained in:
Hongbin Lu
2018-03-08 20:48:02 +00:00
parent f2abd8ce16
commit d8a605c591

View File

@@ -14,8 +14,7 @@
# under the License. # under the License.
# #
import mock
from mox3 import mox
from oslo_utils import uuidutils from oslo_utils import uuidutils
import testtools import testtools
@@ -30,30 +29,29 @@ class CLITestNameorID(testtools.TestCase):
def setUp(self): def setUp(self):
"""Prepare the test environment.""" """Prepare the test environment."""
super(CLITestNameorID, self).setUp() super(CLITestNameorID, self).setUp()
self.mox = mox.Mox()
self.endurl = test_cli20.ENDURL self.endurl = test_cli20.ENDURL
self.client = client.Client(token=test_cli20.TOKEN, self.client = client.Client(token=test_cli20.TOKEN,
endpoint_url=self.endurl) endpoint_url=self.endurl)
self.addCleanup(self.mox.VerifyAll)
self.addCleanup(self.mox.UnsetStubs)
def test_get_id_from_id(self): def test_get_id_from_id(self):
_id = uuidutils.generate_uuid() _id = uuidutils.generate_uuid()
reses = {'networks': [{'id': _id, }, ], } reses = {'networks': [{'id': _id, }, ], }
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr)
path = getattr(self.client, "networks_path") path = getattr(self.client, "networks_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
returned_id = neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', _id)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&id=" + _id), test_cli20.end_url(path, "fields=id&id=" + _id),
self.client), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
returned_id = neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', _id)
self.assertEqual(_id, returned_id) self.assertEqual(_id, returned_id)
def test_get_id_from_id_then_name_empty(self): def test_get_id_from_id_then_name_empty(self):
@@ -61,27 +59,32 @@ class CLITestNameorID(testtools.TestCase):
reses = {'networks': [{'id': _id, }, ], } reses = {'networks': [{'id': _id, }, ], }
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
resstr1 = self.client.serialize({'networks': []}) resstr1 = self.client.serialize({'networks': []})
self.mox.StubOutWithMock(self.client.httpclient, "request")
path = getattr(self.client, "networks_path") path = getattr(self.client, "networks_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient,
test_cli20.MyUrlComparator( "request") as mock_request:
test_cli20.end_url(path, "fields=id&id=" + _id), mock_request.side_effect = [(test_cli20.MyResp(200), resstr1),
self.client), (test_cli20.MyResp(200), resstr)]
'GET', returned_id = neutronV20.find_resourceid_by_name_or_id(
body=None, self.client, 'network', _id)
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN)
).AndReturn((test_cli20.MyResp(200), resstr1)) self.assertEqual(2, mock_request.call_count)
self.client.httpclient.request( mock_request.assert_has_calls([
test_cli20.MyUrlComparator( mock.call(
test_cli20.end_url(path, "fields=id&name=" + _id), test_cli20.MyUrlComparator(
self.client), test_cli20.end_url(path, "fields=id&id=" + _id),
'GET', self.client),
body=None, 'GET',
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) body=None,
).AndReturn((test_cli20.MyResp(200), resstr)) headers=test_cli20.ContainsKeyValue(
self.mox.ReplayAll() {'X-Auth-Token': test_cli20.TOKEN})),
returned_id = neutronV20.find_resourceid_by_name_or_id( mock.call(
self.client, 'network', _id) test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&name=" + _id),
self.client),
'GET',
body=None,
headers=test_cli20.ContainsKeyValue(
{'X-Auth-Token': test_cli20.TOKEN}))])
self.assertEqual(_id, returned_id) self.assertEqual(_id, returned_id)
def test_get_id_from_name(self): def test_get_id_from_name(self):
@@ -89,19 +92,21 @@ class CLITestNameorID(testtools.TestCase):
_id = uuidutils.generate_uuid() _id = uuidutils.generate_uuid()
reses = {'networks': [{'id': _id, }, ], } reses = {'networks': [{'id': _id, }, ], }
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr)
path = getattr(self.client, "networks_path") path = getattr(self.client, "networks_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
returned_id = neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', name)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&name=" + name), test_cli20.end_url(path, "fields=id&name=" + name),
self.client), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
returned_id = neutronV20.find_resourceid_by_name_or_id(
self.client, 'network', name)
self.assertEqual(_id, returned_id) self.assertEqual(_id, returned_id)
def test_get_id_from_name_multiple(self): def test_get_id_from_name_multiple(self):
@@ -109,40 +114,46 @@ class CLITestNameorID(testtools.TestCase):
reses = {'networks': [{'id': uuidutils.generate_uuid()}, reses = {'networks': [{'id': uuidutils.generate_uuid()},
{'id': uuidutils.generate_uuid()}]} {'id': uuidutils.generate_uuid()}]}
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr)
path = getattr(self.client, "networks_path") path = getattr(self.client, "networks_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
exception = self.assertRaises(
exceptions.NeutronClientNoUniqueMatch,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'network', name)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&name=" + name), test_cli20.end_url(path, "fields=id&name=" + name),
self.client), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
exception = self.assertRaises(exceptions.NeutronClientNoUniqueMatch,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'network', name)
self.assertIn('Multiple', exception.message) self.assertIn('Multiple', exception.message)
def test_get_id_from_name_notfound(self): def test_get_id_from_name_notfound(self):
name = 'myname' name = 'myname'
reses = {'networks': []} reses = {'networks': []}
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr)
path = getattr(self.client, "networks_path") path = getattr(self.client, "networks_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
exception = self.assertRaises(
exceptions.NotFound,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'network', name)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&name=" + name), test_cli20.end_url(path, "fields=id&name=" + name),
self.client), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
exception = self.assertRaises(exceptions.NotFound,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'network', name)
self.assertIn('Unable to find', exception.message) self.assertIn('Unable to find', exception.message)
self.assertEqual(404, exception.status_code) self.assertEqual(404, exception.status_code)
@@ -153,41 +164,44 @@ class CLITestNameorID(testtools.TestCase):
reses = {'security_groups': reses = {'security_groups':
[{'id': expect_id, 'tenant_id': project}]} [{'id': expect_id, 'tenant_id': project}]}
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr)
path = getattr(self.client, "security_groups_path") path = getattr(self.client, "security_groups_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
observed_id = neutronV20.find_resourceid_by_name_or_id(
self.client, 'security_group', name, project)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&name=%s&tenant_id=%s" % test_cli20.end_url(path, "fields=id&name=%s&tenant_id=%s" %
(name, project)), self.client), (name, project)), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
observed_id = neutronV20.find_resourceid_by_name_or_id(
self.client, 'security_group', name, project)
self.assertEqual(expect_id, observed_id) self.assertEqual(expect_id, observed_id)
def test_get_id_from_name_multiple_with_project_not_found(self): def test_get_id_from_name_multiple_with_project_not_found(self):
name = 'web_server' name = 'web_server'
project = uuidutils.generate_uuid() project = uuidutils.generate_uuid()
resstr_notfound = self.client.serialize({'security_groups': []}) resstr_notfound = self.client.serialize({'security_groups': []})
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr_notfound)
path = getattr(self.client, "security_groups_path") path = getattr(self.client, "security_groups_path")
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
exc = self.assertRaises(exceptions.NotFound,
neutronV20.find_resourceid_by_name_or_id,
self.client, 'security_group', name,
project)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, "fields=id&name=%s&tenant_id=%s" % test_cli20.end_url(path, "fields=id&name=%s&tenant_id=%s" %
(name, project)), self.client), (name, project)), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr_notfound)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
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.assertIn('Unable to find', exc.message)
self.assertEqual(404, exc.status_code) self.assertEqual(404, exc.status_code)
@@ -196,29 +210,31 @@ class CLITestNameorID(testtools.TestCase):
net = {'id': _id, 'name': 'test'} net = {'id': _id, 'name': 'test'}
reses = {'networks': [net], } reses = {'networks': [net], }
resstr = self.client.serialize(reses) resstr = self.client.serialize(reses)
self.mox.StubOutWithMock(self.client.httpclient, "request") resp = (test_cli20.MyResp(200), resstr)
path = getattr(self.client, "networks_path") path = getattr(self.client, "networks_path")
if id_only: if id_only:
query_params = "fields=id&id=%s" % _id query_params = "fields=id&id=%s" % _id
else: else:
query_params = "id=%s" % _id query_params = "id=%s" % _id
self.client.httpclient.request( with mock.patch.object(self.client.httpclient, "request",
return_value=resp) as mock_request:
if id_only:
returned_id = neutronV20.find_resourceid_by_id(
self.client, 'network', _id)
self.assertEqual(_id, returned_id)
else:
result = neutronV20.find_resource_by_id(
self.client, 'network', _id)
self.assertEqual(net, result)
mock_request.assert_called_once_with(
test_cli20.MyUrlComparator( test_cli20.MyUrlComparator(
test_cli20.end_url(path, query_params), test_cli20.end_url(path, query_params),
self.client), self.client),
'GET', 'GET',
body=None, body=None,
headers=mox.ContainsKeyValue('X-Auth-Token', test_cli20.TOKEN) headers=test_cli20.ContainsKeyValue(
).AndReturn((test_cli20.MyResp(200), resstr)) {'X-Auth-Token': test_cli20.TOKEN}))
self.mox.ReplayAll()
if id_only:
returned_id = neutronV20.find_resourceid_by_id(
self.client, 'network', _id)
self.assertEqual(_id, returned_id)
else:
result = neutronV20.find_resource_by_id(
self.client, 'network', _id)
self.assertEqual(net, result)
def test_get_resource_by_id(self): def test_get_resource_by_id(self):
self._test_get_resource_by_id(id_only=False) self._test_get_resource_by_id(id_only=False)