From 3bad59c2345eb23f4ac9d068d27dc4a35226e06f Mon Sep 17 00:00:00 2001 From: lin-hua-cheng Date: Tue, 30 Dec 2014 11:55:33 -0800 Subject: [PATCH] Filter current flavor in resize instance/database Removed the validation that checks if the new flavor selected is the same as the old flavor, this case now will never happen since the current flavor is removed from the choices. Change-Id: I1c41a48fc9805b1b730dec5f13a43b73989b349b Closes-Bug: #1395777 --- .../dashboards/project/databases/forms.py | 13 ++--- .../dashboards/project/databases/tests.py | 51 +++++++++---------- .../dashboards/project/instances/tests.py | 15 +++++- .../instances/workflows/resize_instance.py | 13 ++--- 4 files changed, 46 insertions(+), 46 deletions(-) diff --git a/openstack_dashboard/dashboards/project/databases/forms.py b/openstack_dashboard/dashboards/project/databases/forms.py index da06ae2c75..74a4e29ffe 100644 --- a/openstack_dashboard/dashboards/project/databases/forms.py +++ b/openstack_dashboard/dashboards/project/databases/forms.py @@ -68,22 +68,17 @@ class ResizeInstanceForm(forms.SelfHandlingForm): def __init__(self, request, *args, **kwargs): super(ResizeInstanceForm, self).__init__(request, *args, **kwargs) + old_flavor_id = kwargs.get('initial', {}).get('old_flavor_id') choices = kwargs.get('initial', {}).get('flavors') + # Remove current flavor from the list of flavor choices + choices = [(flavor_id, name) for (flavor_id, name) in choices + if flavor_id != old_flavor_id] if choices: choices.insert(0, ("", _("Select a new flavor"))) else: choices.insert(0, ("", _("No flavors available"))) self.fields['new_flavor'].choices = choices - def clean(self): - cleaned_data = super(ResizeInstanceForm, self).clean() - flavor = cleaned_data.get('new_flavor', None) - - if flavor is None or flavor == self.initial['old_flavor_id']: - raise forms.ValidationError(_('Please choose a new flavor that ' - 'is not the same as the old one.')) - return cleaned_data - def handle(self, request, data): instance = data.get('instance_id') flavor = data.get('new_flavor') diff --git a/openstack_dashboard/dashboards/project/databases/tests.py b/openstack_dashboard/dashboards/project/databases/tests.py index 501c724924..6e915c03eb 100644 --- a/openstack_dashboard/dashboards/project/databases/tests.py +++ b/openstack_dashboard/dashboards/project/databases/tests.py @@ -388,6 +388,31 @@ class DatabaseTests(test.TestCase): self.assertContains( res, "New size for volume must be greater than current size.") + @test.create_stubs( + {api.trove: ('instance_get', + 'flavor_list')}) + def test_resize_instance_get(self): + database = self.databases.first() + + # views.py: DetailView.get_data + api.trove.instance_get(IsA(http.HttpRequest), database.id)\ + .AndReturn(database) + api.trove.flavor_list(IsA(http.HttpRequest)).\ + AndReturn(self.database_flavors.list()) + + self.mox.ReplayAll() + url = reverse('horizon:project:databases:resize_instance', + args=[database.id]) + + res = self.client.get(url) + self.assertTemplateUsed(res, 'project/databases/resize_instance.html') + option = '' + for flavor in self.database_flavors.list(): + if flavor.id == database.flavor['id']: + self.assertNotContains(res, option % (flavor.id, flavor.name)) + else: + self.assertContains(res, option % (flavor.id, flavor.name)) + @test.create_stubs( {api.trove: ('instance_get', 'flavor_list', @@ -420,29 +445,3 @@ class DatabaseTests(test.TestCase): res = self.client.post(url, post) self.assertNoFormErrors(res) self.assertRedirectsNoFollow(res, INDEX_URL) - - @test.create_stubs( - {api.trove: ('instance_get', 'flavor_list')}) - def test_resize_instance_bad_value(self): - database = self.databases.first() - - api.trove.instance_get(IsA(http.HttpRequest), - database.id).AndReturn(database) - api.trove.flavor_list(IsA(http.HttpRequest)).\ - AndReturn(self.database_flavors.list()) - - old_flavor = self.database_flavors.list()[0] - - self.mox.ReplayAll() - url = reverse('horizon:project:databases:resize_instance', - args=[database.id]) - post = { - 'instance_id': database.id, - 'old_flavor_name': old_flavor.name, - 'old_flavor_id': old_flavor.id, - 'new_flavor': old_flavor.id - } - res = self.client.post(url, post) - self.assertContains(res, - "Please choose a new flavor that is " - "not the same as the old one.") diff --git a/openstack_dashboard/dashboards/project/instances/tests.py b/openstack_dashboard/dashboards/project/instances/tests.py index 95f678589a..708c04591e 100644 --- a/openstack_dashboard/dashboards/project/instances/tests.py +++ b/openstack_dashboard/dashboards/project/instances/tests.py @@ -3474,6 +3474,13 @@ class InstanceTests(helpers.TestCase): config_drive_field_label = 'Configuration Drive' self.assertNotContains(res, config_drive_field_label) + option = '' + for flavor in self.flavors.list(): + if flavor.id == server.flavor['id']: + self.assertNotContains(res, option % (flavor.id, flavor.name)) + else: + self.assertContains(res, option % (flavor.id, flavor.name)) + @helpers.create_stubs({api.nova: ('server_get', 'flavor_list',)}) def test_instance_resize_get_server_get_exception(self): @@ -3552,7 +3559,9 @@ class InstanceTests(helpers.TestCase): @helpers.create_stubs(instance_resize_post_stubs) def test_instance_resize_post(self): server = self.servers.first() - flavor = self.flavors.first() + flavors = [flavor for flavor in self.flavors.list() + if flavor.id != server.flavor['id']] + flavor = flavors[0] api.nova.server_get(IsA(http.HttpRequest), server.id) \ .AndReturn(server) @@ -3573,7 +3582,9 @@ class InstanceTests(helpers.TestCase): @helpers.create_stubs(instance_resize_post_stubs) def test_instance_resize_post_api_exception(self): server = self.servers.first() - flavor = self.flavors.first() + flavors = [flavor for flavor in self.flavors.list() + if flavor.id != server.flavor['id']] + flavor = flavors[0] api.nova.server_get(IsA(http.HttpRequest), server.id) \ .AndReturn(server) diff --git a/openstack_dashboard/dashboards/project/instances/workflows/resize_instance.py b/openstack_dashboard/dashboards/project/instances/workflows/resize_instance.py index f94b4209e3..126c80458b 100644 --- a/openstack_dashboard/dashboards/project/instances/workflows/resize_instance.py +++ b/openstack_dashboard/dashboards/project/instances/workflows/resize_instance.py @@ -44,17 +44,12 @@ class SetFlavorChoiceAction(workflows.Action): help_text_template = ("project/instances/" "_flavors_and_quotas.html") - def clean(self): - cleaned_data = super(SetFlavorChoiceAction, self).clean() - flavor = cleaned_data.get('flavor', None) - - if flavor is None or flavor == cleaned_data['old_flavor_id']: - raise forms.ValidationError(_('Please choose a new flavor that ' - 'is not the same as the old one.')) - return cleaned_data - def populate_flavor_choices(self, request, context): + old_flavor_id = context.get('old_flavor_id') flavors = context.get('flavors').values() + + # Remove current flavor from the list of flavor choices + flavors = [flavor for flavor in flavors if flavor.id != old_flavor_id] if len(flavors) > 1: flavors = instance_utils.sort_flavor_list(request, flavors) if flavors: