Merge "Improve mocking in REST API tests (test_network)"

This commit is contained in:
Zuul 2018-04-06 10:08:07 +00:00 committed by Gerrit Code Review
commit 7c09346e08

View File

@ -12,8 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import mock
from openstack_dashboard import api
from openstack_dashboard.api.rest import network from openstack_dashboard.api.rest import network
from openstack_dashboard.test import helpers as test from openstack_dashboard.test import helpers as test
@ -22,87 +22,86 @@ class RestNetworkApiSecurityGroupTests(test.TestCase):
use_mox = False use_mox = False
@mock.patch.object(network.api, 'neutron') @test.create_mocks({api.neutron: ['security_group_list']})
def test_security_group_detailed(self, client): def test_security_group_detailed(self):
request = self.mock_rest_request() request = self.mock_rest_request()
client.security_group_list.return_value = [ self.mock_security_group_list.return_value = \
mock.Mock(**{'to_dict.return_value': {'name': 'default'}}), self.security_groups.list()
]
response = network.SecurityGroups().get(request) response = network.SecurityGroups().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{"items": [{"name": "default"}]}) {"items": [sg.to_dict() for sg
client.security_group_list.assert_called_once_with(request) in self.security_groups.list()]})
self.mock_security_group_list.assert_called_once_with(request)
class RestNetworkApiFloatingIpTests(test.TestCase): class RestNetworkApiFloatingIpTests(test.TestCase):
use_mox = False use_mox = False
@mock.patch.object(network.api, 'neutron') @test.create_mocks({api.neutron: ['tenant_floating_ip_list']})
def test_floating_ip_list(self, client): def test_floating_ip_list(self):
request = self.mock_rest_request() request = self.mock_rest_request()
client.tenant_floating_ip_list.return_value = ([ self.mock_tenant_floating_ip_list.return_value = \
mock.Mock(**{'to_dict.return_value': {'ip': '1.2.3.4'}}), self.floating_ips.list()
mock.Mock(**{'to_dict.return_value': {'ip': '2.3.4.5'}})
])
response = network.FloatingIPs().get(request) response = network.FloatingIPs().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{'items': [{'ip': '1.2.3.4'}, {'ip': '2.3.4.5'}]}) {'items': [fip.to_dict() for fip
client.tenant_floating_ip_list.assert_called_once_with(request) in self.floating_ips.list()]})
self.mock_tenant_floating_ip_list.assert_called_once_with(request)
@mock.patch.object(network.api, 'neutron') @test.create_mocks({api.neutron: ['floating_ip_pools_list']})
def test_floating_ip_pool_list(self, client): def test_floating_ip_pool_list(self):
pools = [api.neutron.FloatingIpPool(n)
for n in self.api_networks.list()
if n['router:external']]
request = self.mock_rest_request() request = self.mock_rest_request()
client.floating_ip_pools_list.return_value = ([ self.mock_floating_ip_pools_list.return_value = pools
mock.Mock(**{'to_dict.return_value': {'name': '1'}}),
mock.Mock(**{'to_dict.return_value': {'name': '2'}})
])
response = network.FloatingIPPools().get(request) response = network.FloatingIPPools().get(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json,
{'items': [{'name': '1'}, {'name': '2'}]}) {'items': [p.to_dict() for p in pools]})
client.floating_ip_pools_list.assert_called_once_with(request) self.mock_floating_ip_pools_list.assert_called_once_with(request)
@mock.patch.object(network.api, 'neutron') @test.create_mocks({api.neutron: ['tenant_floating_ip_allocate']})
def test_allocate_floating_ip(self, client): def test_allocate_floating_ip(self):
request = self.mock_rest_request( request = self.mock_rest_request(
body='{"pool_id": "pool"}' body='{"pool_id": "pool"}'
) )
client.tenant_floating_ip_allocate.return_value = ( fip = self.floating_ips.first()
mock.Mock(**{'to_dict.return_value': {'ip': '1.2.3.4'}}) self.mock_tenant_floating_ip_allocate.return_value = fip
)
response = network.FloatingIP().post(request) response = network.FloatingIP().post(request)
self.assertStatusCode(response, 200) self.assertStatusCode(response, 200)
self.assertEqual(response.json, self.assertEqual(response.json, fip.to_dict())
{'ip': '1.2.3.4'}) self.mock_tenant_floating_ip_allocate.assert_called_once_with(request,
client.tenant_floating_ip_allocate.assert_called_once_with(request,
'pool') 'pool')
@mock.patch.object(network.api, 'neutron') @test.create_mocks({api.neutron: ['floating_ip_associate']})
def test_associate_floating_ip(self, client): def test_associate_floating_ip(self):
self.mock_floating_ip_associate.return_value = None
request = self.mock_rest_request( request = self.mock_rest_request(
body='{"address_id": "address", "port_id": "port"}' body='{"address_id": "address", "port_id": "port"}'
) )
response = network.FloatingIP().patch(request) response = network.FloatingIP().patch(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
client.floating_ip_associate.assert_called_once_with(request, self.mock_floating_ip_associate.assert_called_once_with(request,
'address', 'address',
'port') 'port')
@mock.patch.object(network.api, 'neutron') @test.create_mocks({api.neutron: ['floating_ip_disassociate']})
def test_disassociate_floating_ip(self, client): def test_disassociate_floating_ip(self):
self.mock_floating_ip_disassociate.return_value = None
request = self.mock_rest_request( request = self.mock_rest_request(
body='{"address_id": "address"}' body='{"address_id": "address"}'
) )
response = network.FloatingIP().patch(request) response = network.FloatingIP().patch(request)
self.assertStatusCode(response, 204) self.assertStatusCode(response, 204)
client.floating_ip_disassociate.assert_called_once_with(request, self.mock_floating_ip_disassociate.assert_called_once_with(request,
'address') 'address')