Switch Glance-related tests (dashboards.project.images) to mock

Some duplicated test scenarios were deleted. This patch also doesn't
change Selenium tests because it's out of scope of unit tests.

This patch changes mox to mock for the following test module:
* openstack_dashboard/dashboards/project/images/tests.py
* openstack_dashboard/dashboards/project/images/images/tests.py
* openstack_dashboard/dashboards/project/images/tests.py

Change-Id: I951c2e5ee1c133faa2106e1e1fdba5c72d6f63b7
Partially-Implements: blueprint mock-framework-in-unit-tests
This commit is contained in:
Ivan Kolodyazhny 2017-12-29 14:22:21 +02:00
parent efb6dea20d
commit 0d1d22c85e
4 changed files with 301 additions and 363 deletions

View File

@ -22,10 +22,10 @@ from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.urlresolvers import reverse
from django.forms.widgets import HiddenInput
from django import http
from django.test.utils import override_settings
from mox3.mox import IsA
import mock
import six
from horizon import tables as horizon_tables
from openstack_dashboard import api
@ -39,17 +39,18 @@ IMAGES_INDEX_URL = reverse('horizon:project:images:index')
class CreateImageFormTests(test.ResetImageAPIVersionMixin, test.TestCase):
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_no_location_or_file(self):
filters = {'disk_format': 'aki'}
api.glance.image_list_detailed(
IsA({}), filters=filters).AndReturn(
[self.images.list(), False, False])
filters = {'disk_format': 'ari'}
api.glance.image_list_detailed(
IsA({}), filters=filters).AndReturn(
[self.images.list(), False, False])
self.mox.ReplayAll()
@mock.patch.object(api.glance, 'image_list_detailed')
def test_no_location_or_file(self, mock_image_list):
mock_image_list.side_effect = [
[self.images.list(), False, False],
[self.images.list(), False, False]
]
image_calls = [
mock.call(test.IsA(dict), filters={'disk_format': 'aki'}),
mock.call(test.IsA(dict), filters={'disk_format': 'ari'})
]
post = {
'name': u'Ubuntu 11.10',
'source_type': u'file',
@ -61,26 +62,31 @@ class CreateImageFormTests(test.ResetImageAPIVersionMixin, test.TestCase):
'is_public': 1}
files = {}
form = forms.CreateImageForm(post, files)
self.assertFalse(form.is_valid())
mock_image_list.assert_has_calls(image_calls)
@override_settings(HORIZON_IMAGES_ALLOW_UPLOAD=False)
@override_settings(IMAGES_ALLOW_LOCATION=True)
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_image_upload_disabled(self):
filters = {'disk_format': 'aki'}
api.glance.image_list_detailed(
IsA({}), filters=filters).AndReturn(
[self.images.list(), False, False])
filters = {'disk_format': 'ari'}
api.glance.image_list_detailed(
IsA({}), filters=filters).AndReturn(
[self.images.list(), False, False])
self.mox.ReplayAll()
@mock.patch.object(api.glance, 'image_list_detailed')
def test_image_upload_disabled(self, mock_image_list):
mock_image_list.side_effect = [
[self.images.list(), False, False],
[self.images.list(), False, False]
]
image_calls = [
mock.call(test.IsA(dict), filters={'disk_format': 'aki'}),
mock.call(test.IsA(dict), filters={'disk_format': 'ari'})
]
form = forms.CreateImageForm({})
self.assertEqual(
isinstance(form.fields['image_file'].widget, HiddenInput), True)
source_type_dict = dict(form.fields['source_type'].choices)
self.assertNotIn('file', source_type_dict)
mock_image_list.assert_has_calls(image_calls)
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
def test_create_image_metadata_docker_v1(self):
@ -136,23 +142,25 @@ class UpdateImageFormTests(test.ResetImageAPIVersionMixin, test.TestCase):
disk_format = form.fields['disk_format']
self.assertFalse(disk_format.widget.attrs.get('readonly', False))
@test.create_stubs({api.glance: ('image_get',)})
def test_image_update(self):
@mock.patch.object(api.glance, 'image_get')
def test_image_update(self, mock_image_get):
image = self.images.first()
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
self.mox.ReplayAll()
mock_image_get.return_value = image
url = reverse('horizon:project:images:images:update',
args=[image.id])
res = self.client.get(url)
self.assertNoFormErrors(res)
self.assertEqual(res.context['image'].disk_format,
image.disk_format)
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
image.id)
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_update', 'image_get')})
def test_image_update_post_v1(self):
@mock.patch.object(api.glance, 'image_get')
@mock.patch.object(api.glance, 'image_update')
def test_image_update_post_v1(self, mock_image_update, mock_image_get):
image = self.images.first()
data = {
'name': u'Ubuntu 11.10',
@ -169,30 +177,35 @@ class UpdateImageFormTests(test.ResetImageAPIVersionMixin, test.TestCase):
'is_public': False,
'protected': False,
'method': 'UpdateImageForm'}
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
api.glance.image_update(IsA(http.HttpRequest),
image.id,
is_public=data['is_public'],
protected=data['protected'],
disk_format=data['disk_format'],
container_format="bare",
name=data['name'],
min_ram=data['minimum_ram'],
min_disk=data['minimum_disk'],
properties={
'description': data['description'],
'architecture':
data['architecture']}).AndReturn(image)
self.mox.ReplayAll()
url = reverse('horizon:project:images:images:update',
args=[image.id])
res = self.client.post(url, data)
self.assertNoFormErrors(res)
self.assertEqual(res.status_code, 302)
@test.create_stubs({api.glance: ('image_update', 'image_get')})
def test_image_update_post_v2(self):
mock_image_get.return_value = image
mock_image_update.return_value = image
url = reverse('horizon:project:images:images:update',
args=[image.id])
res = self.client.post(url, data)
self.assertNoFormErrors(res)
self.assertEqual(res.status_code, 302)
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
str(image.id))
mock_image_update.assert_called_once_with(
test.IsHttpRequest(),
image.id,
is_public=data['is_public'],
protected=data['protected'],
disk_format=data['disk_format'],
container_format="bare",
name=data['name'],
min_ram=data['minimum_ram'],
min_disk=data['minimum_disk'],
properties={
'description': data['description'],
'architecture': data['architecture']})
@mock.patch.object(api.glance, 'image_get')
@mock.patch.object(api.glance, 'image_update')
def test_image_update_post_v2(self, mock_image_update, mock_image_get):
image = self.images.first()
data = {
'name': u'Ubuntu 11.10',
@ -209,48 +222,51 @@ class UpdateImageFormTests(test.ResetImageAPIVersionMixin, test.TestCase):
'is_public': False,
'protected': False,
'method': 'UpdateImageForm'}
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
api.glance.image_update(IsA(http.HttpRequest),
image.id,
visibility='private',
protected=data['protected'],
disk_format=data['disk_format'],
container_format="bare",
name=data['name'],
min_ram=data['minimum_ram'],
min_disk=data['minimum_disk'],
description=data['description'],
architecture=data['architecture']).\
AndReturn(image)
self.mox.ReplayAll()
mock_image_get.return_value = image
mock_image_update.return_value = image
url = reverse('horizon:project:images:images:update',
args=[image.id])
res = self.client.post(url, data)
self.assertNoFormErrors(res)
self.assertEqual(res.status_code, 302)
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
str(image.id))
mock_image_update.assert_called_once_with(
test.IsHttpRequest(),
image.id,
visibility='private',
protected=data['protected'],
disk_format=data['disk_format'],
container_format="bare",
name=data['name'],
min_ram=data['minimum_ram'],
min_disk=data['minimum_disk'],
description=data['description'],
architecture=data['architecture'])
class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_image_create_get(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_image_create_get(self, mock_image_list):
mock_image_list.side_effect = [
[self.images.list(), False, False],
[self.images.list(), False, False]
]
image_calls = [
mock.call(test.IsHttpRequest(), filters={'disk_format': 'aki'}),
mock.call(test.IsHttpRequest(), filters={'disk_format': 'ari'})
]
url = reverse('horizon:project:images:images:create')
res = self.client.get(url)
self.assertTemplateUsed(res,
'project/images/images/create.html')
self.assertTemplateUsed(res, 'project/images/images/create.html')
mock_image_list.assert_has_calls(image_calls)
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_copy_from_v1(self):
data = {
'source_type': u'url',
@ -263,7 +279,6 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
self._test_image_create(data, api_data)
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_location_v1(self):
data = {
'source_type': u'url',
@ -276,7 +291,6 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
self._test_image_create(data, api_data)
@override_settings(IMAGES_ALLOW_LOCATION=True)
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_location_v2(self):
data = {
'source_type': u'url',
@ -288,7 +302,6 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
self._test_image_create(data, api_data)
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_upload_v1(self):
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(b'123')
@ -298,10 +311,9 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
data = {'source_type': u'file',
'image_file': temp_file}
api_data = {'data': IsA(InMemoryUploadedFile)}
api_data = {'data': test.IsA(InMemoryUploadedFile)}
self._test_image_create(data, api_data)
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_upload_v2(self):
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(b'123')
@ -311,11 +323,10 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
data = {'source_type': u'file',
'image_file': temp_file}
api_data = {'data': IsA(InMemoryUploadedFile)}
api_data = {'data': test.IsA(InMemoryUploadedFile)}
self._test_image_create(data, api_data)
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_with_kernel_ramdisk_v1(self):
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(b'123')
@ -329,10 +340,9 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
'ramdisk_id': '007e7d55-fe1e-4c5c-bf08-44b4a496482a'
}
api_data = {'data': IsA(InMemoryUploadedFile)}
api_data = {'data': test.IsA(InMemoryUploadedFile)}
self._test_image_create(data, api_data)
@test.create_stubs({api.glance: ('image_create',)})
def test_image_create_post_with_kernel_ramdisk_v2(self):
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(b'123')
@ -346,11 +356,13 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
'ramdisk_id': '007e7d55-fe1e-4c5c-bf08-44b4a496482a'
}
api_data = {'data': IsA(InMemoryUploadedFile)}
api_data = {'data': test.IsA(InMemoryUploadedFile)}
self._test_image_create(data, api_data)
@test.create_stubs({api.glance: ('image_list_detailed',)})
def _test_image_create(self, extra_form_data, extra_api_data):
@mock.patch.object(api.glance, 'image_create')
@mock.patch.object(api.glance, 'image_list_detailed')
def _test_image_create(self, extra_form_data, extra_api_data,
mock_image_list, mock_image_create):
data = {
'name': u'Ubuntu 11.10',
'description': u'Login with admin/admin',
@ -383,19 +395,16 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
api_data.update(extra_api_data)
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])
mock_image_list.side_effect = [
[self.images.list(), False, False],
[self.images.list(), False, False]
]
image_list_calls = [
mock.call(test.IsHttpRequest(), filters={'disk_format': 'aki'}),
mock.call(test.IsHttpRequest(), filters={'disk_format': 'ari'})
]
api.glance.image_create(
IsA(http.HttpRequest),
**api_data).AndReturn(self.images.first())
self.mox.ReplayAll()
mock_image_create.return_value = self.images.first()
url = reverse('horizon:project:images:images:create')
res = self.client.post(url, data)
@ -403,10 +412,13 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
self.assertNoFormErrors(res)
self.assertEqual(res.status_code, 302)
def _test_image_detail_get(self, image):
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
self.mox.ReplayAll()
mock_image_list.assert_has_calls(image_list_calls)
mock_image_create.assert_called_once_with(test.IsHttpRequest(),
**api_data)
@mock.patch.object(api.glance, 'image_get')
def _test_image_detail_get(self, image, mock_image_get):
mock_image_get.return_value = image
res = self.client.get(reverse('horizon:project:images:images:detail',
args=[image.id]))
@ -415,24 +427,23 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
'horizon/common/_detail.html')
self.assertEqual(res.context['image'].name, image.name)
self.assertEqual(res.context['image'].protected, image.protected)
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
six.text_type(image.id))
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_get',)})
def test_image_detail_get_v1(self):
image = self.images.first()
self._test_image_detail_get(image)
@test.create_stubs({api.glance: ('image_get',)})
def test_image_detail_get_v2(self):
image = self.imagesV2.first()
self._test_image_detail_get(image)
def _test_image_detail_custom_props_get(self, image):
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
self.mox.ReplayAll()
@mock.patch.object(api.glance, 'image_get')
def _test_image_detail_custom_props_get(self, image, mock_image_get):
mock_image_get.return_value = image
res = self.client.get(reverse('horizon:project:images:images:detail',
args=[image.id]))
@ -453,23 +464,23 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
self.assertContains(res, '<dt title="foo">foo</dt>')
self.assertContains(res, '<dd>foo val</dd>')
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
six.text_type(image.id))
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_get',)})
def test_image_detail_custom_props_get_v1(self):
image = self.images.list()[8]
self._test_image_detail_custom_props_get(image)
@test.create_stubs({api.glance: ('image_get',)})
def test_image_detail_custom_props_get_v2(self):
image = self.imagesV2.list()[2]
self._test_image_detail_custom_props_get(image)
def _test_protected_image_detail_get(self, image):
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
self.mox.ReplayAll()
@mock.patch.object(api.glance, 'image_get')
def _test_protected_image_detail_get(self, image, mock_image_get):
mock_image_get.return_value = image
res = self.client.get(
reverse('horizon:project:images:images:detail',
@ -478,39 +489,37 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
'horizon/common/_detail.html')
self.assertEqual(res.context['image'].protected, image.protected)
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
six.text_type(image.id))
@override_settings(OPENSTACK_API_VERSIONS={'image': 1})
@test.create_stubs({api.glance: ('image_get',)})
def test_protected_image_detail_get_v1(self):
image = self.images.list()[2]
self._test_protected_image_detail_get(image)
@test.create_stubs({api.glance: ('image_get',)})
def test_protected_image_detail_get_v2(self):
image = self.imagesV2.list()[1]
self._test_protected_image_detail_get(image)
@test.create_stubs({api.glance: ('image_get',)})
def test_image_detail_get_with_exception(self):
@mock.patch.object(api.glance, 'image_get')
def test_image_detail_get_with_exception(self, mock_image_get):
image = self.images.first()
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndRaise(self.exceptions.glance)
self.mox.ReplayAll()
mock_image_get.side_effect = self.exceptions.glance
url = reverse('horizon:project:images:images:detail',
args=[image.id])
res = self.client.get(url)
self.assertRedirectsNoFollow(res, IMAGES_INDEX_URL)
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
six.text_type(image.id))
@test.create_stubs({api.glance: ('image_get',)})
def test_image_update_get(self):
@mock.patch.object(api.glance, 'image_get')
def test_image_update_get(self, mock_image_get):
image = self.images.filter(is_public=True)[0]
api.glance.image_get(IsA(http.HttpRequest), str(image.id)) \
.AndReturn(image)
self.mox.ReplayAll()
mock_image_get.return_value = image
res = self.client.get(
reverse('horizon:project:images:images:update',
@ -524,19 +533,20 @@ class ImageViewTests(test.ResetImageAPIVersionMixin, test.TestCase):
" name='public' checked='checked'>",
html=True,
msg_prefix="The is_public checkbox is not checked")
mock_image_get.assert_called_once_with(test.IsHttpRequest(),
six.text_type(image.id))
class OwnerFilterTests(test.TestCase):
def setUp(self):
super(OwnerFilterTests, self).setUp()
self.table = self.mox.CreateMock(horizon_tables.DataTable)
self.table = mock.Mock(sppec=horizon_tables.DataTable)
self.table.request = self.request
@override_settings(IMAGES_LIST_FILTER_TENANTS=[{'name': 'Official',
'tenant': 'officialtenant',
'icon': 'fa-check'}])
def test_filter(self):
self.mox.ReplayAll()
all_images = self.images.list()
table = self.table
self.filter_tenants = settings.IMAGES_LIST_FILTER_TENANTS

View File

@ -17,9 +17,8 @@
# under the License.
from django.core.urlresolvers import reverse
from django import http
from mox3.mox import IsA
import mock
from openstack_dashboard import api
from openstack_dashboard.test import helpers as test
@ -32,24 +31,20 @@ class SnapshotsViewTests(test.TestCase):
def test_create_snapshot_get(self):
server = self.servers.first()
self.mox.ReplayAll()
url = reverse('horizon:project:images:snapshots:create',
args=[server.id])
res = self.client.get(url)
self.assertTemplateUsed(res,
'project/images/snapshots/create.html')
def test_create_snapshot_post(self):
@mock.patch.object(api.nova, 'snapshot_create')
@mock.patch.object(api.nova, 'server_get')
def test_create_snapshot_post(self, mock_server_get, mock_snapshot_create):
server = self.servers.first()
snapshot = self.snapshots.first()
self.mox.StubOutWithMock(api.nova, 'server_get')
self.mox.StubOutWithMock(api.nova, 'snapshot_create')
api.nova.server_get(IsA(http.HttpRequest), server.id).AndReturn(server)
api.nova.snapshot_create(IsA(http.HttpRequest), server.id,
snapshot.name).AndReturn(snapshot)
self.mox.ReplayAll()
mock_server_get.return_value = server
mock_snapshot_create.return_value = snapshot
formData = {'method': 'CreateSnapshot',
'tenant_id': self.tenant.id,
@ -60,16 +55,18 @@ class SnapshotsViewTests(test.TestCase):
res = self.client.post(url, formData)
self.assertRedirectsNoFollow(res, INDEX_URL)
mock_server_get.assert_called_once_with(test.IsHttpRequest(),
server.id)
mock_snapshot_create.assert_called_once_with(test.IsHttpRequest(),
server.id, snapshot.name)
def test_create_snapshot_post_exception(self):
@mock.patch.object(api.nova, 'snapshot_create')
def test_create_snapshot_post_exception(self,
mock_snapshot_create):
server = self.servers.first()
snapshot = self.snapshots.first()
self.mox.StubOutWithMock(api.nova, 'server_get')
self.mox.StubOutWithMock(api.nova, 'snapshot_create')
api.nova.snapshot_create(IsA(http.HttpRequest), server.id,
snapshot.name).AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
mock_snapshot_create.side_effect = self.exceptions.nova
formData = {'method': 'CreateSnapshot',
'tenant_id': self.tenant.id,
@ -79,4 +76,7 @@ class SnapshotsViewTests(test.TestCase):
args=[server.id])
res = self.client.post(url, formData)
redirect = reverse("horizon:project:instances:index")
self.assertRedirectsNoFollow(res, redirect)
mock_snapshot_create.assert_called_once_with(test.IsHttpRequest(),
server.id, snapshot.name)

View File

@ -24,13 +24,11 @@ import unittest
from django.core.urlresolvers import reverse
from django import http
from glanceclient.common import exceptions as glance_exec
import mock
from mox3.mox import IsA
import six
from horizon import exceptions
from horizon import messages
from openstack_dashboard import api
from openstack_dashboard.dashboards.project.images import utils
@ -42,16 +40,17 @@ INDEX_URL = reverse('horizon:project:images:index')
CREATE_URL = reverse('horizon:project:images:images:create')
class ImagesAndSnapshotsTests(test.TestCase):
@test.create_stubs({api.glance: ('image_list_detailed',)})
class BaseImagesTestCase(test.TestCase):
def setUp(self):
super(BaseImagesTestCase, self).setUp()
self.patcher = mock.patch.object(api.glance, 'image_list_detailed')
self.mock_image_list = self.patcher.start()
class ImagesAndSnapshotsTests(BaseImagesTestCase):
def test_index(self):
images = self.images.list()
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None, paginate=True,
sort_dir='asc', sort_key='name',
reversed_order=False) \
.AndReturn([images, False, False])
self.mox.ReplayAll()
self.mock_image_list.return_value = [images, False, False]
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, INDEX_TEMPLATE)
@ -71,40 +70,43 @@ class ImagesAndSnapshotsTests(test.TestCase):
row_actions = images_table.get_row_actions(images[2])
self.assertEqual(len(row_actions), 4)
@test.create_stubs({api.glance: ('image_list_detailed',)})
self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
marker=None,
paginate=True,
sort_dir='asc',
sort_key='name',
reversed_order=False)
def test_index_no_images(self):
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None, paginate=True,
sort_dir='asc', sort_key='name',
reversed_order=False) \
.AndReturn([(), False, False])
self.mox.ReplayAll()
self.mock_image_list.return_value = [(), False, False]
res = self.client.get(INDEX_URL)
self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
marker=None,
paginate=True,
sort_dir='asc',
sort_key='name',
reversed_order=False)
self.assertTemplateUsed(res, INDEX_TEMPLATE)
self.assertContains(res, 'No items to display')
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_index_error(self):
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None, paginate=True,
sort_dir='asc', sort_key='name',
reversed_order=False) \
.AndRaise(self.exceptions.glance)
self.mox.ReplayAll()
self.mock_image_list.side_effect = self.exceptions.glance
res = self.client.get(INDEX_URL)
self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
marker=None,
paginate=True,
sort_dir='asc',
sort_key='name',
reversed_order=False)
self.assertTemplateUsed(res, INDEX_TEMPLATE)
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_snapshot_actions(self):
snapshots = self.snapshots.list()
api.glance.image_list_detailed(IsA(http.HttpRequest),
marker=None, paginate=True,
sort_dir='asc', sort_key='name',
reversed_order=False) \
.AndReturn([snapshots, False, False])
self.mox.ReplayAll()
self.mock_image_list.return_value = [snapshots, False, False]
res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, INDEX_TEMPLATE)
@ -136,10 +138,15 @@ class ImagesAndSnapshotsTests(test.TestCase):
u"Delete Image")
self.assertEqual(str(row_actions[0]), "<DeleteImage: delete>")
self.mock_image_list.assert_called_once_with(test.IsHttpRequest(),
marker=None,
paginate=True,
sort_dir='asc',
sort_key='name',
reversed_order=False)
class ImagesAndSnapshotsUtilsTests(test.TestCase):
@test.create_stubs({api.glance: ('image_list_detailed',)})
class ImagesAndSnapshotsUtilsTests(BaseImagesTestCase):
def test_list_image(self):
public_images = [image for image in self.images.list()
if image.status == 'active' and image.is_public]
@ -149,30 +156,31 @@ class ImagesAndSnapshotsUtilsTests(test.TestCase):
shared_images = [image for image in self.imagesV2.list()
if (image.status == 'active' and
image.visibility == 'shared')]
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndReturn([public_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([private_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'visibility': 'shared', 'status': 'active'}) \
.AndReturn([shared_images, False, False])
self.mock_image_list.side_effect = [
[public_images, False, False],
[private_images, False, False],
[shared_images, False, False]
]
self.mox.ReplayAll()
image_calls = [
mock.call(test.IsHttpRequest(),
filters={'is_public': True, 'status': 'active'}),
mock.call(test.IsHttpRequest(),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}),
mock.call(test.IsHttpRequest(),
filters={'visibility': 'shared', 'status': 'active'})
]
ret = utils.get_available_images(self.request, self.tenant.id)
expected_images = [image for image in self.images.list()
if (image.status == 'active' and
image.container_format not in ('ami', 'aki'))]
self.mock_image_list.assert_has_calls(image_calls)
self.assertEqual(len(expected_images), len(ret))
@test.create_stubs({api.glance: ('image_list_detailed',)})
def test_list_image_using_cache(self):
public_images = [image for image in self.images.list()
if image.status == 'active' and image.is_public]
@ -182,26 +190,26 @@ class ImagesAndSnapshotsUtilsTests(test.TestCase):
shared_images = [image for image in self.imagesV2.list()
if (image.status == 'active' and
image.visibility == 'shared')]
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndReturn([public_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([private_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'visibility': 'shared', 'status': 'active'}) \
.AndReturn([shared_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': 'other-tenant',
'status': 'active'}) \
.AndReturn([private_images, False, False])
self.mox.ReplayAll()
self.mock_image_list.side_effect = [
[public_images, False, False],
[private_images, False, False],
[shared_images, False, False],
[private_images, False, False]
]
image_calls = [
mock.call(test.IsHttpRequest(),
filters={'is_public': True, 'status': 'active'}),
mock.call(test.IsHttpRequest(),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}),
mock.call(test.IsHttpRequest(),
filters={'visibility': 'shared', 'status': 'active'}),
mock.call(test.IsHttpRequest(),
filters={'property-owner_id': 'other-tenant',
'status': 'active'})
]
expected_images = [image for image in self.images.list()
if (image.status == 'active' and
@ -238,42 +246,40 @@ class ImagesAndSnapshotsUtilsTests(test.TestCase):
len(private_images),
len(images_cache['images_by_project']['other-tenant']))
@test.create_stubs({api.glance: ('image_list_detailed',),
exceptions: ('handle',)})
def test_list_image_error_public_image_list(self):
public_images = [image for image in self.images.list()
if image.status == 'active' and image.is_public]
self.mock_image_list.assert_has_calls(image_calls)
@mock.patch.object(exceptions, 'handle')
def test_list_image_error_public_image_list(self, mock_exception_handle):
private_images = [image for image in self.images.list()
if (image.status == 'active' and
not image.is_public)]
shared_images = [image for image in self.imagesV2.list()
if (image.status == 'active' and
image.visibility == 'shared')]
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndRaise(self.exceptions.glance)
exceptions.handle(IsA(http.HttpRequest),
"Unable to retrieve public images.")
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([private_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'visibility': 'shared', 'status': 'active'}) \
.AndReturn([shared_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndReturn([public_images, False, False])
self.mox.ReplayAll()
self.mock_image_list.side_effect = [
self.exceptions.glance,
[private_images, False, False],
[shared_images, False, False],
]
images_cache = {}
ret = utils.get_available_images(self.request, self.tenant.id,
images_cache)
image_calls = [
mock.call(test.IsHttpRequest(),
filters={'is_public': True, 'status': 'active'}),
mock.call(test.IsHttpRequest(),
filters={'status': 'active', 'property-owner_id': '1'}),
mock.call(test.IsHttpRequest(),
filters={'visibility': 'shared', 'status': 'active'})
]
self.mock_image_list.assert_has_calls(image_calls)
handle_calls = [
mock.call(test.IsHttpRequest(),
"Unable to retrieve public images."),
]
mock_exception_handle.assert_has_calls(handle_calls)
expected_images = [image for image in private_images
if image.container_format not in ('ami', 'aki')]
@ -287,26 +293,8 @@ class ImagesAndSnapshotsUtilsTests(test.TestCase):
len(shared_images),
len(images_cache['shared_images']))
ret = utils.get_available_images(self.request, self.tenant.id,
images_cache)
expected_images = [image for image in self.images.list()
if image.container_format not in ('ami', 'aki')]
self.assertEqual(len(expected_images), len(ret))
self.assertEqual(
len(public_images),
len(images_cache['public_images']))
self.assertEqual(1, len(images_cache['images_by_project']))
self.assertEqual(
len(private_images),
len(images_cache['images_by_project'][self.tenant.id]))
self.assertEqual(
len(shared_images),
len(images_cache['shared_images']))
@test.create_stubs({api.glance: ('image_list_detailed',),
messages: ('error',)})
def test_list_image_communication_error_public_image_list(self):
@mock.patch.object(exceptions, 'handle')
def test_list_image_error_private_image_list(self, mock_exception_handle):
public_images = [image for image in self.images.list()
if image.status == 'active' and image.is_public]
private_images = [image for image in self.images.list()
@ -315,102 +303,16 @@ class ImagesAndSnapshotsUtilsTests(test.TestCase):
shared_images = [image for image in self.imagesV2.list()
if (image.status == 'active' and
image.visibility == 'shared')]
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndRaise(glance_exec.CommunicationError)
# Make sure the exception is handled with the correct
# error message. If the exception cannot be handled,
# the error message will be different.
messages.error(IsA(http.HttpRequest),
"Unable to retrieve public images.")
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([private_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'visibility': 'shared', 'status': 'active'}) \
.AndReturn([shared_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndReturn([public_images, False, False])
self.mox.ReplayAll()
self.mock_image_list.side_effect = [
[public_images, False, False],
self.exceptions.glance,
[shared_images, False, False],
[private_images, False, False]
]
images_cache = {}
ret = utils.get_available_images(self.request, self.tenant.id,
images_cache)
expected_images = [image for image in private_images
if image.container_format not in ('ami', 'aki')]
self.assertEqual(len(expected_images), len(ret))
self.assertNotIn('public_images', images_cache)
self.assertEqual(1, len(images_cache['images_by_project']))
self.assertEqual(
len(private_images),
len(images_cache['images_by_project'][self.tenant.id]))
self.assertEqual(
len(shared_images),
len(images_cache['shared_images']))
ret = utils.get_available_images(self.request, self.tenant.id,
images_cache)
expected_images = [image for image in self.images.list()
if image.container_format not in ('ami', 'aki')]
self.assertEqual(len(expected_images), len(ret))
self.assertEqual(
len(public_images),
len(images_cache['public_images']))
self.assertEqual(1, len(images_cache['images_by_project']))
self.assertEqual(
len(private_images),
len(images_cache['images_by_project'][self.tenant.id]))
self.assertEqual(
len(shared_images),
len(images_cache['shared_images']))
@test.create_stubs({api.glance: ('image_list_detailed',),
exceptions: ('handle',)})
def test_list_image_error_private_image_list(self):
public_images = [image for image in self.images.list()
if image.status == 'active' and image.is_public]
private_images = [image for image in self.images.list()
if (image.status == 'active' and
not image.is_public)]
shared_images = [image for image in self.imagesV2.list()
if (image.status == 'active' and
image.visibility == 'shared')]
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'is_public': True, 'status': 'active'}) \
.AndReturn([public_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndRaise(self.exceptions.glance)
exceptions.handle(IsA(http.HttpRequest),
"Unable to retrieve images for the current project.")
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'visibility': 'shared', 'status': 'active'}) \
.AndReturn([shared_images, False, False])
api.glance.image_list_detailed(
IsA(http.HttpRequest),
filters={'property-owner_id': self.tenant.id,
'status': 'active'}) \
.AndReturn([private_images, False, False])
self.mox.ReplayAll()
images_cache = {}
ret = utils.get_available_images(self.request, self.tenant.id,
images_cache)
expected_images = [image for image in public_images
if image.container_format not in ('ami', 'aki')]
self.assertEqual(len(expected_images), len(ret))
@ -439,6 +341,23 @@ class ImagesAndSnapshotsUtilsTests(test.TestCase):
len(shared_images),
len(images_cache['shared_images']))
image_calls = [
mock.call(test.IsHttpRequest(),
filters={'status': 'active', 'is_public': True}),
mock.call(test.IsHttpRequest(),
filters={'status': 'active', 'property-owner_id': '1'}),
mock.call(test.IsHttpRequest(),
filters={'status': 'active', 'visibility': 'shared'}),
mock.call(test.IsHttpRequest(),
filters={'status': 'active', 'property-owner_id': '1'})
]
self.mock_image_list.assert_has_calls(image_calls)
handle_calls = [
mock.call(test.IsHttpRequest(),
"Unable to retrieve images for the current project."),
]
mock_exception_handle.assert_has_calls(handle_calls)
class SeleniumTests(test.SeleniumTestCase):
@test.create_stubs({api.glance: ('image_list_detailed',)})

View File

@ -693,7 +693,16 @@ def mock_factory(r):
return mocked
class IsHttpRequest(object):
"""Class to compare param is django.http.HttpRequest."""
class IsA(object):
"""Class to compare param is a specified class."""
def __init__(self, cls):
self.cls = cls
def __eq__(self, other):
return isinstance(other, http_request.HttpRequest)
return isinstance(other, self.cls)
class IsHttpRequest(IsA):
"""Class to compare param is django.http.HttpRequest."""
def __init__(self):
super(IsHttpRequest, self).__init__(http_request.HttpRequest)