def test_create_volume_from_snapshot(self): volume = self.volumes.first() usage = {'gigabytes': {'available': 250}, 'volumes': {'available': 6}} snapshot = self.volume_snapshots.first() formData = {'name': u'A Volume I Am Making', 'description': u'This is a volume I am making for a test.', 'method': u'CreateForm', 'size': 50, 'snapshot_source': snapshot.id} # first call- with url param api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) api.nova.volume_snapshot_get(IsA(http.HttpRequest), str(snapshot.id)).AndReturn(snapshot) api.volume_create(IsA(http.HttpRequest), formData['size'], formData['name'], formData['description'], snapshot_id=snapshot.id).\ AndReturn(volume) # second call- with dropdown api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) api.volume_snapshot_list(IsA(http.HttpRequest)).\ AndReturn(self.volume_snapshots.list()) api.nova.volume_snapshot_get(IsA(http.HttpRequest), str(snapshot.id)).AndReturn(snapshot) api.volume_create(IsA(http.HttpRequest), formData['size'], formData['name'], formData['description'], snapshot_id=snapshot.id).\ AndReturn(volume) self.mox.ReplayAll() # get snapshot from url url = reverse('horizon:project:volumes:create') res = self.client.post("?".join([url, "snapshot_id=" + str(snapshot.id)]), formData) redirect_url = reverse('horizon:project:volumes:index') self.assertRedirectsNoFollow(res, redirect_url) # get snapshot from dropdown list url = reverse('horizon:project:volumes:create') res = self.client.post(url, formData) redirect_url = reverse('horizon:project:volumes:index') self.assertRedirectsNoFollow(res, redirect_url)
def get_context_data(self, **kwargs): context = super(AllocateView, self).get_context_data(**kwargs) try: context['usages'] = api.tenant_quota_usages(self.request) except: exceptions.handle(self.request) return context
def get_context_data(self, **kwargs): context = super(CreateView, self).get_context_data(**kwargs) try: context['usages'] = api.tenant_quota_usages(self.request) except: exceptions.handle(self.request) return context
def test_create_volume_gb_used_over_alloted_quota(self): usage = {'gigabytes': {'available': 100, 'used': 20}} formData = {'name': u'This Volume Is Huge!', 'description': u'This is a volume that is just too big!', 'method': u'CreateForm', 'size': 5000} api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) api.volume_snapshot_list(IsA(http.HttpRequest)).\ AndReturn(self.volume_snapshots.list()) api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) self.mox.ReplayAll() url = reverse('horizon:project:volumes:create') res = self.client.post(url, formData) expected_error = [u'A volume of 5000GB cannot be created as you only' ' have 100GB of your quota available.'] self.assertEqual(res.context['form'].errors['__all__'], expected_error)
def test_create_volume_number_over_alloted_quota(self): usage = {'gigabytes': {'available': 100, 'used': 20}, 'volumes': {'available': 0}} formData = {'name': u'Too Many...', 'description': u'We have no volumes left!', 'method': u'CreateForm', 'size': 10} api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) api.volume_snapshot_list(IsA(http.HttpRequest)).\ AndReturn(self.volume_snapshots.list()) api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) self.mox.ReplayAll() url = reverse('horizon:project:volumes:create') res = self.client.post(url, formData) expected_error = [u'You are already using all of your available' ' volumes.'] self.assertEqual(res.context['form'].errors['__all__'], expected_error)
def test_tenant_quota_usages(self): api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) api.nova.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) api.nova.server_list(IsA(http.HttpRequest)) \ .AndReturn(self.servers.list()) api.nova.volume_list(IsA(http.HttpRequest)) \ .AndReturn(self.volumes.list()) self.mox.ReplayAll() quota_usages = api.tenant_quota_usages(self.request) expected_output = { 'gigabytes': { 'used': 80, 'flavor_fields': [], 'quota': 1000 }, 'ram': { 'available': 8976, 'used': 1024, 'flavor_fields': ['ram'], 'quota': 10000 }, 'floating_ips': { 'used': 2, 'flavor_fields': [], 'quota': 1 }, 'instances': { 'used': 2, 'flavor_fields': [], 'quota': 10 }, 'volumes': { 'used': 3, 'flavor_fields': [], 'quota': 1 }, 'cores': { 'used': 2, 'flavor_fields': ['vcpus'], 'quota': 10 } } self.assertEquals(quota_usages, expected_output)
def test_create_volume_from_snapshot_invalid_size(self): usage = {'gigabytes': {'available': 250}, 'volumes': {'available': 6}} snapshot = self.volume_snapshots.first() formData = {'name': u'A Volume I Am Making', 'description': u'This is a volume I am making for a test.', 'method': u'CreateForm', 'size': 20, 'snapshot_source': snapshot.id} api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) api.nova.volume_snapshot_get(IsA(http.HttpRequest), str(snapshot.id)).AndReturn(snapshot) api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) self.mox.ReplayAll() url = reverse('horizon:project:volumes:create') res = self.client.post("?".join([url, "snapshot_id=" + str(snapshot.id)]), formData, follow=True) self.assertEqual(res.redirect_chain, []) self.assertFormError(res, 'form', None, "The volume size cannot be less than the " "snapshot size (40GB)")
def test_create_volume(self): volume = self.volumes.first() usage = {'gigabytes': {'available': 250}, 'volumes': {'available': 6}} formData = {'name': u'A Volume I Am Making', 'description': u'This is a volume I am making for a test.', 'method': u'CreateForm', 'size': 50, 'snapshot_source': ''} api.tenant_quota_usages(IsA(http.HttpRequest)).AndReturn(usage) api.volume_snapshot_list(IsA(http.HttpRequest)).\ AndReturn(self.volume_snapshots.list()) api.volume_create(IsA(http.HttpRequest), formData['size'], formData['name'], formData['description'], snapshot_id=None).AndReturn(volume) self.mox.ReplayAll() url = reverse('horizon:project:volumes:create') res = self.client.post(url, formData) redirect_url = reverse('horizon:project:volumes:index') self.assertRedirectsNoFollow(res, redirect_url)
def handle(self, request, data): try: # FIXME(johnp): cinderclient currently returns a useless # error message when the quota is exceeded when trying to create # a volume, so we need to check for that scenario here before we # send it off to try and create. usages = api.tenant_quota_usages(request) snapshot_id = None if (data.get("snapshot_source", None)): # Create from Snapshot snapshot = self.get_snapshot(request, data["snapshot_source"]) snapshot_id = snapshot.id if (data['size'] < snapshot.size): error_message = _('The volume size cannot be less than ' 'the snapshot size (%sGB)' % snapshot.size) raise ValidationError(error_message) else: if type(data['size']) is str: data['size'] = int(data['size']) if usages['gigabytes']['available'] < data['size']: error_message = _('A volume of %(req)iGB cannot be created as ' 'you only have %(avail)iGB of your quota ' 'available.') params = { 'req': data['size'], 'avail': usages['gigabytes']['available'] } raise ValidationError(error_message % params) elif usages['volumes']['available'] <= 0: error_message = _('You are already using all of your available' ' volumes.') raise ValidationError(error_message) volume = api.volume_create(request, data['size'], data['name'], data['description'], snapshot_id=snapshot_id) message = 'Creating volume "%s"' % data['name'] messages.info(request, message) return volume except ValidationError, e: return self.api_error(e.messages[0])
def handle(self, request, data): try: # FIXME(johnp): cinderclient currently returns a useless # error message when the quota is exceeded when trying to create # a volume, so we need to check for that scenario here before we # send it off to try and create. usages = api.tenant_quota_usages(request) snapshot_id = None if (data.get("snapshot_source", None)): # Create from Snapshot snapshot = self.get_snapshot(request, data["snapshot_source"]) snapshot_id = snapshot.id if (data['size'] < snapshot.size): error_message = _('The volume size cannot be less than ' 'the snapshot size (%sGB)' % snapshot.size) raise ValidationError(error_message) else: if type(data['size']) is str: data['size'] = int(data['size']) if usages['gigabytes']['available'] < data['size']: error_message = _('A volume of %(req)iGB cannot be created as ' 'you only have %(avail)iGB of your quota ' 'available.') params = {'req': data['size'], 'avail': usages['gigabytes']['available']} raise ValidationError(error_message % params) elif usages['volumes']['available'] <= 0: error_message = _('You are already using all of your available' ' volumes.') raise ValidationError(error_message) volume = api.volume_create(request, data['size'], data['name'], data['description'], snapshot_id=snapshot_id) message = 'Creating volume "%s"' % data['name'] messages.info(request, message) return volume except ValidationError, e: return self.api_error(e.messages[0])
def test_tenant_quota_usages(self): api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) api.nova.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) api.nova.server_list(IsA(http.HttpRequest)) \ .AndReturn(self.servers.list()) api.nova.volume_list(IsA(http.HttpRequest)) \ .AndReturn(self.volumes.list()) self.mox.ReplayAll() quota_usages = api.tenant_quota_usages(self.request) expected_output = {'gigabytes': { 'used': 80, 'flavor_fields': [], 'quota': 1000}, 'ram': { 'available': 8976, 'used': 1024, 'flavor_fields': ['ram'], 'quota': 10000}, 'floating_ips': { 'used': 2, 'flavor_fields': [], 'quota': 1}, 'instances': { 'used': 2, 'flavor_fields': [], 'quota': 10}, 'volumes': { 'used': 3, 'flavor_fields': [], 'quota': 1}, 'cores': { 'used': 2, 'flavor_fields': ['vcpus'], 'quota': 10}} self.assertEquals(quota_usages, expected_output)