Skip to content

Commit

Permalink
fix IndexError when trying to create the first flavor (bug 959232)
Browse files Browse the repository at this point in the history
this happens when all the default flavors have been deleted and a user tries to add a new one through the WebUI

Change-Id: Ic7779ac2ead409626c1b6ec0540865629e78277e
  • Loading branch information
iartarisi committed Mar 22, 2012
1 parent 479ebdc commit 2acd536
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
26 changes: 26 additions & 0 deletions horizon/dashboards/syspanel/flavors/tests.py
@@ -0,0 +1,26 @@
from django import http
from django.core import urlresolvers
from mox import IsA

from horizon import api
from horizon.tests.base_tests import BaseHorizonTests


class FlavorsTests(BaseHorizonTests):
def test_create_new_flavor_when_none_exist(self):
# Set admin role
self.setActiveUser(token=self.token.id,
username=self.user.name,
tenant_id=self.tenant.id,
service_catalog=self.request.user.service_catalog,
roles=[{'name': 'admin'}])

self.mox.StubOutWithMock(api, 'flavor_list')
# no pre-existing flavors
api.flavor_list(IsA(http.HttpRequest)).AndReturn([])
self.mox.ReplayAll()

resp = self.client.get(
urlresolvers.reverse('horizon:syspanel:flavors:create'))
self.assertEqual(resp.status_code, 200)
self.assertTemplateUsed(resp, "syspanel/flavors/create.html")
7 changes: 5 additions & 2 deletions horizon/dashboards/syspanel/flavors/views.py
Expand Up @@ -64,5 +64,8 @@ def get_initial(self):
# TODO(tres): Get rid of this hacky bit of nonsense after flavors get
# converted to nova client.
flavors = api.flavor_list(self.request)
flavors.sort(key=lambda f: f.id, reverse=True)
return {'flavor_id': int(flavors[0].id) + 1}
if flavors:
largest_id = max(flavors, key=lambda f: f.id).id
return {'flavor_id': int(largest_id) + 1}
else:
return {'flavor_id': 1}

0 comments on commit 2acd536

Please sign in to comment.