Merge "Switch Glance-related tests (dashboards.admin.images) to mock"

This commit is contained in:
Zuul 2017-12-27 17:57:01 +00:00 committed by Gerrit Code Review
commit 0b11ccb98f

View File

@ -14,10 +14,9 @@
from django.conf import settings
from django.core.urlresolvers import reverse
from django import http
from django.test.utils import override_settings
from mox3.mox import IsA
import mock
from openstack_dashboard import api
from openstack_dashboard.test import helpers as test
@ -28,43 +27,44 @@ INDEX_TEMPLATE = 'horizon/common/_data_table_view.html'
class ImageCreateViewTest(test.BaseAdminViewTests):
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_admin_image_create_view_uses_admin_template(self):
filters = {'disk_format': 'aki'}
api.glance.image_list_detailed(
IsA(http.HttpRequest), filters=filters).AndReturn(
[self.images.list(), False, False])
filters = {'disk_format': 'ari'}
api.glance.image_list_detailed(
IsA(http.HttpRequest), filters=filters).AndReturn(
[self.images.list(), False, False])
self.mox.ReplayAll()
@mock.patch.object(api.glance, 'image_list_detailed')
def test_admin_image_create_view_uses_admin_template(self,
mock_image_list):
filters1 = {'disk_format': 'aki'}
filters2 = {'disk_format': 'ari'}
mock_image_list.return_value = [self.images.list(), False, False]
res = self.client.get(
reverse('horizon:admin:images:create'))
calls = [mock.call(test.IsHttpRequest(), filters=filters1),
mock.call(test.IsHttpRequest(), filters=filters2)]
mock_image_list.assert_has_calls(calls)
self.assertTemplateUsed(res, 'admin/images/create.html')
class ImagesViewTest(test.BaseAdminViewTests):
@test.create_stubs({api.glance: ('image_list_detailed',),
api.keystone: ('tenant_list',)})
def test_images_list(self):
@mock.patch.object(api.glance, 'image_list_detailed')
@mock.patch.object(api.keystone, 'tenant_list')
def test_images_list(self, mock_tenant_list, mock_image_list):
filters = {'is_public': None}
api.glance.image_list_detailed(IsA(http.HttpRequest),
mock_image_list.return_value = [self.images.list(), False, False]
mock_tenant_list.return_value = [self.tenants.list(), False]
# Test tenant list
res = self.client.get(
reverse('horizon:admin:images:index'))
mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
mock_image_list.assert_called_once_with(test.IsHttpRequest(),
marker=None,
paginate=True,
filters=filters,
sort_dir='asc',
sort_key='name',
reversed_order=False) \
.AndReturn([self.images.list(),
False, False])
# Test tenant list
api.keystone.tenant_list(IsA(http.HttpRequest)).\
AndReturn([self.tenants.list(), False])
self.mox.ReplayAll()
res = self.client.get(
reverse('horizon:admin:images:index'))
reversed_order=False)
self.assertContains(res, 'test_tenant', 9, 200)
self.assertTemplateUsed(res, INDEX_TEMPLATE)
self.assertEqual(len(res.context['images_table'].data),
@ -78,37 +78,31 @@ class ImagesViewTest(test.BaseAdminViewTests):
self.assertItemsEqual(images, [])
@override_settings(API_RESULT_PAGE_SIZE=2)
@test.create_stubs({api.glance: ('image_list_detailed',),
api.keystone: ('tenant_list',)})
def test_images_list_get_pagination(self):
@mock.patch.object(api.glance, 'image_list_detailed')
@mock.patch.object(api.keystone, 'tenant_list')
def test_images_list_get_pagination(self, mock_tenant_list,
mock_image_list):
images = self.images.list()[:5]
filters = {'is_public': None}
kwargs = {'paginate': True, 'filters': filters,
'sort_dir': 'asc', 'sort_key': 'name',
'reversed_order': False}
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None,
**kwargs) \
.AndReturn([images, True, True])
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None,
**kwargs) \
.AndReturn([images[:2], True, True])
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=images[2].id,
**kwargs) \
.AndReturn([images[2:4], True, True])
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=images[4].id,
**kwargs) \
.AndReturn([images[4:], True, True])
# Test tenant list
api.keystone.tenant_list(IsA(http.HttpRequest)).MultipleTimes().\
AndReturn([self.tenants.list(), False])
self.mox.ReplayAll()
mock_image_list.side_effect = [[images, True, True],
[images[:2], True, True],
[images[2:4], True, True],
[images[4:], True, True]]
mock_tenant_list.return_value = [self.tenants.list(), False]
# Test tenant list
url = reverse('horizon:admin:images:index')
res = self.client.get(url)
image_calls = [mock.call(test.IsHttpRequest(), marker=None, **kwargs),
mock.call(test.IsHttpRequest(), marker=None, **kwargs),
mock.call(test.IsHttpRequest(),
marker=images[2].id, **kwargs),
mock.call(test.IsHttpRequest(),
marker=images[4].id, **kwargs)]
# get all
self.assertEqual(len(res.context['images_table'].data),
len(images))
@ -133,43 +127,40 @@ class ImagesViewTest(test.BaseAdminViewTests):
images[4].id])
url = "?".join([reverse('horizon:admin:images:index'), params])
res = self.client.get(url)
# get third page (item 5)
self.assertEqual(len(res.context['images_table'].data),
1)
self.assertContains(res, 'test_tenant', 3, 200)
mock_image_list.assert_has_calls(image_calls)
mock_tenant_list.assert_called_with(test.IsHttpRequest())
@override_settings(API_RESULT_PAGE_SIZE=2)
@test.create_stubs({api.glance: ('image_list_detailed',),
api.keystone: ('tenant_list',)})
def test_images_list_get_prev_pagination(self):
@mock.patch.object(api.glance, 'image_list_detailed')
@mock.patch.object(api.keystone, 'tenant_list')
def test_images_list_get_prev_pagination(self, mock_tenant_list,
mock_image_list):
images = self.images.list()[:3]
filters = {'is_public': None}
kwargs = {'paginate': True, 'filters': filters,
'sort_dir': 'asc', 'sort_key': 'name'}
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None,
reversed_order=False,
**kwargs) \
.AndReturn([images, True, False])
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None,
reversed_order=False,
**kwargs) \
.AndReturn([images[:2], True, True])
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=images[2].id,
reversed_order=False,
**kwargs) \
.AndReturn([images[2:], True, True])
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=images[2].id,
reversed_order=True,
**kwargs) \
.AndReturn([images[:2], True, True])
# Test tenant list
api.keystone.tenant_list(IsA(http.HttpRequest)).MultipleTimes().\
AndReturn([self.tenants.list(), False])
self.mox.ReplayAll()
mock_image_list.side_effect = [[images, True, False],
[images[:2], True, True],
[images[2:], True, True],
[images[:2], True, True]]
mock_tenant_list.return_value = [self.tenants.list(), False]
image_calls = [mock.call(test.IsHttpRequest(), marker=None,
reversed_order=False, **kwargs),
mock.call(test.IsHttpRequest(), marker=None,
reversed_order=False, **kwargs),
mock.call(test.IsHttpRequest(), marker=images[2].id,
reversed_order=False, **kwargs),
mock.call(test.IsHttpRequest(), marker=images[2].id,
reversed_order=True, **kwargs)]
url = reverse('horizon:admin:images:index')
res = self.client.get(url)
@ -201,3 +192,6 @@ class ImagesViewTest(test.BaseAdminViewTests):
self.assertEqual(len(res.context['images_table'].data),
settings.API_RESULT_PAGE_SIZE)
self.assertContains(res, 'test_tenant', 4, 200)
mock_image_list.assert_has_calls(image_calls)
mock_tenant_list.assert_called_with(test.IsHttpRequest())