From 5a2da5d7c41ba565a9d4fe7b9d7872d17722b440 Mon Sep 17 00:00:00 2001 From: Andy Chong Date: Wed, 14 Mar 2012 11:59:19 +0800 Subject: [PATCH] set minimum instance launch count to 1 * added assertion test for form errors Change-Id: I573a459facfbad6980390d97b26f490613cabc87 --- .../nova/images_and_snapshots/images/forms.py | 1 + .../nova/images_and_snapshots/images/tests.py | 47 +++++++++++++++++++ horizon/test.py | 21 ++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/horizon/dashboards/nova/images_and_snapshots/images/forms.py b/horizon/dashboards/nova/images_and_snapshots/images/forms.py index 96ae043c1..ac7eb10d4 100644 --- a/horizon/dashboards/nova/images_and_snapshots/images/forms.py +++ b/horizon/dashboards/nova/images_and_snapshots/images/forms.py @@ -101,6 +101,7 @@ class LaunchForm(forms.SelfHandlingForm): "authentication.")) count = forms.IntegerField(label=_("Instance Count"), required=True, + min_value=1, initial=1, help_text=_("Number of instances to launch.")) security_groups = forms.MultipleChoiceField( diff --git a/horizon/dashboards/nova/images_and_snapshots/images/tests.py b/horizon/dashboards/nova/images_and_snapshots/images/tests.py index dfbf1fb39..403ec9a6e 100644 --- a/horizon/dashboards/nova/images_and_snapshots/images/tests.py +++ b/horizon/dashboards/nova/images_and_snapshots/images/tests.py @@ -215,6 +215,53 @@ class ImageViewTests(test.TestCase): res = self.client.post(url, form_data) self.assertRedirectsNoFollow(res, IMAGES_INDEX_URL) + def test_launch_form_instance_count_error(self): + flavor = self.flavors.first() + image = self.images.first() + keypair = self.keypairs.first() + server = self.servers.first() + volume = self.volumes.first() + sec_group = self.security_groups.first() + USER_DATA = 'user data' + device_name = u'vda' + volume_choice = "%s:vol" % volume.id + block_device_mapping = {device_name: u"%s::0" % volume_choice} + + self.mox.StubOutWithMock(api, 'image_get_meta') + self.mox.StubOutWithMock(api, 'flavor_list') + self.mox.StubOutWithMock(api, 'keypair_list') + self.mox.StubOutWithMock(api, 'security_group_list') + self.mox.StubOutWithMock(api, 'volume_list') + self.mox.StubOutWithMock(api, 'volume_snapshot_list') + self.mox.StubOutWithMock(api, 'tenant_quota_usages') + + api.flavor_list(IsA(http.HttpRequest)).AndReturn(self.flavors.list()) + api.keypair_list(IsA(http.HttpRequest)).AndReturn(self.keypairs.list()) + api.security_group_list(IsA(http.HttpRequest)) \ + .AndReturn(self.security_groups.list()) + api.image_get_meta(IsA(http.HttpRequest), image.id).AndReturn(image) + api.volume_list(IsA(http.HttpRequest)).AndReturn(self.volumes.list()) + api.volume_snapshot_list(IsA(http.HttpRequest)).AndReturn([]) + api.tenant_quota_usages(IsA(http.HttpRequest)) \ + .AndReturn(self.quota_usages.first()) + self.mox.ReplayAll() + + form_data = {'method': 'LaunchForm', + 'flavor': flavor.id, + 'image_id': image.id, + 'keypair': keypair.name, + 'name': server.name, + 'user_data': USER_DATA, + 'tenant_id': self.tenants.first().id, + 'security_groups': sec_group.name, + 'volume': volume_choice, + 'device_name': device_name, + 'count': 0} + url = reverse('horizon:nova:images_and_snapshots:images:launch', + args=[image.id]) + res = self.client.post(url, form_data) + self.assertFormErrors(res, count=1) + def test_image_detail_get(self): image = self.images.first() self.mox.StubOutWithMock(api.glance, 'image_get_meta') diff --git a/horizon/test.py b/horizon/test.py index e07536f6a..be8f20f24 100644 --- a/horizon/test.py +++ b/horizon/test.py @@ -142,8 +142,8 @@ class TestCase(django_test.TestCase): Asserts that the given response issued a 302 redirect without processing the view which is redirected to. """ - if response.status_code / 100 != 3: - assert("The response did not return a redirect.") + assert (response.status_code / 100 == 3), \ + "The response did not return a redirect." self.assertEqual(response._headers.get('location', None), ('Location', settings.TESTSERVER + expected_url)) self.assertEqual(response.status_code, 302) @@ -198,6 +198,23 @@ class TestCase(django_test.TestCase): assert len(errors) == 0, \ "Unexpected errors were found on the form: %s" % errors + def assertFormErrors(self, response, count=0, context_name="form"): + """ + Asserts that the response does contain a form in it's + context, and that form has errors, if count were given, + it must match the exact numbers of errors + """ + context = getattr(response, "context", {}) + assert (context and context_name in context), \ + "The response did not contain a form." + errors = response.context[context_name]._errors + if count: + assert len(errors) == count, \ + "%d errors were found on the form, %d expected" % \ + (len(errors), count) + else: + assert len(errors) > 0, "No errors were found on the form" + class BaseAdminViewTests(TestCase): """