From 9b190ac6782a3eb690da97e8c146a6bbfb66bc99 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Thu, 15 Aug 2013 21:48:17 -0400 Subject: [PATCH] Expanded flavor template test coverage Added tests have to do with exception handling; thus, this patch also formally brings tuskar exceptions into the tuskar_ui codebase Change-Id: I198c66ecfe3eb539c803f50f6a1a21fa16ff707a Implements: blueprint flavors-dashboard-tests --- local_settings.py.example | 2 +- tuskar_ui/exceptions.py | 26 +++++++ .../flavor_templates/tests.py | 78 +++++++++++++++++++ tuskar_ui/test/settings.py | 6 +- tuskar_ui/test/test_data/exceptions.py | 4 + tuskar_ui/test/test_data/utils.py | 2 +- 6 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 tuskar_ui/exceptions.py diff --git a/local_settings.py.example b/local_settings.py.example index f69ecd5d2..0c8107d3b 100644 --- a/local_settings.py.example +++ b/local_settings.py.example @@ -2,7 +2,7 @@ import os from django.utils.translation import ugettext_lazy as _ -from openstack_dashboard import exceptions +from tuskar_ui import exceptions DEBUG = True TEMPLATE_DEBUG = DEBUG diff --git a/tuskar_ui/exceptions.py b/tuskar_ui/exceptions.py new file mode 100644 index 000000000..cf79ac866 --- /dev/null +++ b/tuskar_ui/exceptions.py @@ -0,0 +1,26 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Copyright 2012 Nebula, 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. + +from openstack_dashboard import exceptions +import tuskarclient.exc as tuskarclient + +NOT_FOUND = exceptions.NOT_FOUND +RECOVERABLE = exceptions.RECOVERABLE + (tuskarclient.ClientException,) +UNAUTHORIZED = exceptions.UNAUTHORIZED diff --git a/tuskar_ui/infrastructure/resource_management/flavor_templates/tests.py b/tuskar_ui/infrastructure/resource_management/flavor_templates/tests.py index 544a7b35a..a255fef90 100644 --- a/tuskar_ui/infrastructure/resource_management/flavor_templates/tests.py +++ b/tuskar_ui/infrastructure/resource_management/flavor_templates/tests.py @@ -40,6 +40,35 @@ class FlavorTemplatesTests(test.BaseAdminViewTests): self.assertRedirectsNoFollow( resp, reverse('horizon:infrastructure:resource_management:index')) + @test.create_stubs({tuskar.FlavorTemplate: ('list', 'create')}) + def test_create_flavor_template_post_exception(self): + template = self.tuskar_flavor_templates.first() + + tuskar.FlavorTemplate.list( + IsA(http.HttpRequest)).AndReturn([]) + tuskar.FlavorTemplate.create( + IsA(http.HttpRequest), + name=template.name, + cpu=0, + memory=0, + storage=0, + ephemeral_disk=0, + swap_disk=0).AndRaise(self.exceptions.tuskar) + + self.mox.ReplayAll() + + url = reverse('horizon:infrastructure:resource_management:' + 'flavor_templates:create') + data = {'name': template.name, + 'cpu': 0, + 'memory': 0, + 'storage': 0, + 'ephemeral_disk': 0, + 'swap_disk': 0} + resp = self.client.post(url, data) + + self.assertMessageCount(resp, error=1) + @test.create_stubs({tuskar.FlavorTemplate: ('list', 'update', 'get')}) def test_edit_flavor_template_get(self): template = self.tuskar_flavor_templates.first() # has no extra spec @@ -90,6 +119,37 @@ class FlavorTemplatesTests(test.BaseAdminViewTests): self.assertRedirectsNoFollow( resp, reverse('horizon:infrastructure:resource_management:index')) + @test.create_stubs({tuskar.FlavorTemplate: ('list', 'update')}) + def test_edit_flavor_template_post_exception(self): + template = self.tuskar_flavor_templates.first() # has no extra spec + + tuskar.FlavorTemplate.list(IsA(http.HttpRequest)).AndReturn( + self.tuskar_flavor_templates.list()) + tuskar.FlavorTemplate.update( + IsA(http.HttpRequest), + template_id=template.id, + name=template.name, + cpu=0, + memory=0, + storage=0, + ephemeral_disk=0, + swap_disk=0).AndRaise(self.exceptions.tuskar) + self.mox.ReplayAll() + + data = {'flavor_template_id': template.id, + 'name': template.name, + 'cpu': 0, + 'memory': 0, + 'storage': 0, + 'ephemeral_disk': 0, + 'swap_disk': 0} + url = reverse( + 'horizon:infrastructure:resource_management:flavor_templates:edit', + args=[template.id]) + resp = self.client.post(url, data) + + self.assertMessageCount(resp, error=1) + @test.create_stubs({tuskar.FlavorTemplate: ('list', 'delete')}) def test_delete_flavor_template(self): template = self.tuskar_flavor_templates.first() @@ -123,3 +183,21 @@ class FlavorTemplatesTests(test.BaseAdminViewTests): res = self.client.get(url) self.assertTemplateUsed(res, "infrastructure/resource_management/" "flavor_templates/detail.html") + + @test.create_stubs({tuskar.FlavorTemplate: ('get',)}) + def test_detail_flavor_template_exception(self): + template = self.tuskar_flavor_templates.first() + + tuskar.FlavorTemplate.get( + IsA(http.HttpRequest), + template.id).AndRaise(self.exceptions.tuskar) + tuskar.FlavorTemplate.resource_classes = self.tuskar_resource_classes + + self.mox.ReplayAll() + + url = reverse('horizon:infrastructure:resource_management:' + 'flavor_templates:detail', args=[template.id]) + res = self.client.get(url) + + self.assertRedirectsNoFollow( + res, reverse('horizon:infrastructure:resource_management:index')) diff --git a/tuskar_ui/test/settings.py b/tuskar_ui/test/settings.py index 182e20542..e300b32c5 100644 --- a/tuskar_ui/test/settings.py +++ b/tuskar_ui/test/settings.py @@ -5,9 +5,9 @@ from django.utils.translation import ugettext_lazy as _ from horizon.test.settings import * # noqa from horizon.utils.secret_key import generate_or_read_from_file -from openstack_dashboard.exceptions import NOT_FOUND -from openstack_dashboard.exceptions import RECOVERABLE -from openstack_dashboard.exceptions import UNAUTHORIZED +from tuskar_ui.exceptions import NOT_FOUND +from tuskar_ui.exceptions import RECOVERABLE +from tuskar_ui.exceptions import UNAUTHORIZED TEST_DIR = os.path.dirname(os.path.abspath(__file__)) diff --git a/tuskar_ui/test/test_data/exceptions.py b/tuskar_ui/test/test_data/exceptions.py index 4dc358667..7706b3439 100644 --- a/tuskar_ui/test/test_data/exceptions.py +++ b/tuskar_ui/test/test_data/exceptions.py @@ -18,6 +18,7 @@ from keystoneclient import exceptions as keystone_exceptions from neutronclient.common import exceptions as neutron_exceptions from novaclient import exceptions as nova_exceptions from swiftclient import client as swift_exceptions +import tuskarclient.exc as tuskar_exceptions from openstack_dashboard.test.test_data.utils import TestDataContainer @@ -68,3 +69,6 @@ def data(TEST): cinder_exception = cinder_exceptions.BadRequest TEST.exceptions.cinder = create_stubbed_exception(cinder_exception) + + tuskar_exception = tuskar_exceptions.ClientException + TEST.exceptions.tuskar = create_stubbed_exception(tuskar_exception) diff --git a/tuskar_ui/test/test_data/utils.py b/tuskar_ui/test/test_data/utils.py index 297804569..9f84d9389 100644 --- a/tuskar_ui/test/test_data/utils.py +++ b/tuskar_ui/test/test_data/utils.py @@ -15,13 +15,13 @@ def load_test_data(load_onto=None): from openstack_dashboard.test.test_data import cinder_data - from openstack_dashboard.test.test_data import exceptions from openstack_dashboard.test.test_data import glance_data from openstack_dashboard.test.test_data import heat_data from openstack_dashboard.test.test_data import keystone_data from openstack_dashboard.test.test_data import neutron_data from openstack_dashboard.test.test_data import nova_data from openstack_dashboard.test.test_data import swift_data + from tuskar_ui.test.test_data import exceptions from tuskar_ui.test.test_data import tuskar_data # The order of these loaders matters, some depend on others.