Increase unit test coverage for catalog and images

Implements bp: murano-unit-test-coverage

Change-Id: I8c89d0cb772f3197c95a051c9c01919e5b1c5149
This commit is contained in:
Samantha Blanco 2016-11-02 21:00:08 -04:00
parent e951030818
commit 2e0e7a5f8b
5 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,94 @@
# Copyright (c) 2016 AT&T Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import testtools
from django import http
from muranodashboard.catalog import views
class TestCatalogViews(testtools.TestCase):
def setUp(self):
super(TestCatalogViews, self).setUp()
self.mock_request = mock.MagicMock()
self.env = mock.MagicMock(id='12', name='test_env', status='READY')
self.addCleanup(mock.patch.stopall)
def test_is_valid_environment(self):
mock_env1 = mock.MagicMock(id='13')
valid_envs = [mock_env1, self.env]
self.assertTrue(views.is_valid_environment(self.env, valid_envs))
@mock.patch.object(views, 'env_api')
def test_get_environments_context(self, mock_env_api):
mock_env_api.environments_list.return_value = [self.env]
self.assertIsNotNone(views.get_environments_context(self.mock_request))
mock_env_api.environments_list.assert_called_with(self.mock_request)
@mock.patch.object(views, 'api')
def test_get_categories_list(self, mock_api):
self.assertEqual([], views.get_categories_list(self.mock_request))
mock_api.handled_exceptions.assert_called_once_with(self.mock_request)
mock_api.muranoclient.assert_called_once_with(self.mock_request)
@mock.patch.object(views, 'env_api')
def test_create_quick_environment(self, mock_env_api):
views.create_quick_environment(self.mock_request)
self.assertTrue(mock_env_api.environment_create.called)
@mock.patch.object(views, 'pkg_api')
def test_get_image(self, mock_pkg_api):
app_id = 13
result = views.get_image(self.mock_request, app_id)
self.assertIsInstance(result, http.HttpResponse)
(mock_pkg_api.get_app_logo.
assert_called_once_with(self.mock_request, app_id))
mock_pkg_api.reset_mock()
mock_pkg_api.get_app_logo.return_value = None
result = views.get_image(self.mock_request, app_id)
self.assertIsInstance(result, http.HttpResponseRedirect)
(mock_pkg_api.get_app_logo.
assert_called_once_with(self.mock_request, app_id))
@mock.patch.object(views, 'pkg_api')
def test_get_supplier_image(self, mock_pkg_api):
app_id = 13
result = views.get_supplier_image(self.mock_request, app_id)
self.assertIsInstance(result, http.HttpResponse)
(mock_pkg_api.get_app_supplier_logo.
assert_called_once_with(self.mock_request, app_id))
mock_pkg_api.reset_mock()
mock_pkg_api.get_app_supplier_logo.return_value = None
result = views.get_supplier_image(self.mock_request, app_id)
self.assertIsInstance(result, http.HttpResponseRedirect)
(mock_pkg_api.get_app_supplier_logo.
assert_called_once_with(self.mock_request, app_id))
@mock.patch.object(views, 'pkg_api')
@mock.patch('muranodashboard.dynamic_ui.services.version')
@mock.patch('muranodashboard.dynamic_ui.services.pkg_api')
def test_quick_deploy_error(self, services_pkg_api,
mock_version, views_pkg_api):
mock_version.check_version.return_value = '0.2'
app_id = 'app_id'
self.assertRaises(ValueError, views.quick_deploy,
self.mock_request, app_id=app_id)
(services_pkg_api.get_app_ui.
assert_called_once_with(self.mock_request, app_id))
views_pkg_api.get_app_fqn.assert_called_with(self.mock_request, app_id)

View File

@ -0,0 +1,58 @@
# Copyright (c) 2016 AT&T Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import testtools
from django.utils.translation import ugettext_lazy as _
from muranodashboard.images import forms
class TestImagesForms(testtools.TestCase):
def setUp(self):
super(TestImagesForms, self).setUp()
metadata = {'murano_image_info': '{"title": "title", "type": "type"}'}
self.mock_img = mock.MagicMock(id=12, properties=metadata)
self.mock_request = mock.MagicMock()
@mock.patch.object(forms, 'LOG')
def test_filter_murano_images(self, mock_log):
mock_blank_img = \
mock.MagicMock(id=13, properties={"murano_image_info": "info"})
images = [mock_blank_img]
msg = _('Invalid metadata for image: {0}').format(images[0].id)
self.assertEqual(images,
forms.filter_murano_images(images, self.mock_request))
mock_log.warning.assert_called_once_with(msg)
images = [self.mock_img]
self.assertEqual(images, forms.filter_murano_images(images))
class TestMarkImageForm(testtools.TestCase):
def setUp(self):
super(TestMarkImageForm, self).setUp()
self.mock_request = mock.MagicMock()
self.mark_img_form = forms.MarkImageForm(self.mock_request)
@mock.patch.object(forms, 'glance')
def test_handle(self, mock_glance_api):
data = {
'title': 'title',
'image': 'id',
'type': 'type'
}
self.mark_img_form.handle(self.mock_request, data)
self.assertTrue(mock_glance_api.image_update_properties.called)

View File

@ -0,0 +1,33 @@
# Copyright (c) 2016 AT&T Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import testtools
from horizon import exceptions
from muranodashboard.images import views
class TestImagesForms(testtools.TestCase):
def setUp(self):
super(TestImagesForms, self).setUp()
self.mock_request = mock.MagicMock()
self.miv = views.MarkedImagesView(request=self.mock_request)
@mock.patch.object(views, 'reverse')
def test_get_data_error(self, mock_reverse):
self.assertRaises(exceptions.Http302, self.miv.get_data)
mock_reverse.assert_called_once_with('horizon:app-catalog:'
'catalog:index')