1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2013 Kylin, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
from django.core.urlresolvers import reverse # noqa
18
from django import http
19
from mox import IsA # noqa
21
from openstack_dashboard import api
22
from openstack_dashboard.test import helpers as test
23
from openstack_dashboard.usage import quotas
25
INDEX_URL = reverse('horizon:admin:defaults:index')
28
class ServicesViewTests(test.BaseAdminViewTests):
30
self.mox.StubOutWithMock(api.nova, 'default_quota_get')
31
self.mox.StubOutWithMock(api.cinder, 'default_quota_get')
32
api.nova.default_quota_get(IsA(http.HttpRequest),
33
self.tenant.id).AndReturn(self.quotas.nova)
34
api.cinder.default_quota_get(IsA(http.HttpRequest), self.tenant.id) \
35
.AndReturn(self.cinder_quotas.first())
39
res = self.client.get(INDEX_URL)
41
self.assertTemplateUsed(res, 'admin/defaults/index.html')
43
quotas_tab = res.context['tab_group'].get_tab('quotas')
44
self.assertQuerysetEqual(quotas_tab._tables['quotas'].data,
45
['<Quota: (injected_file_content_bytes, 1)>',
46
'<Quota: (metadata_items, 1)>',
47
'<Quota: (injected_files, 1)>',
48
'<Quota: (gigabytes, 1000)>',
49
'<Quota: (ram, 10000)>',
50
'<Quota: (instances, 10)>',
51
'<Quota: (snapshots, 1)>',
52
'<Quota: (volumes, 1)>',
53
'<Quota: (cores, 10)>',
54
'<Quota: (security_groups, 10)>',
55
'<Quota: (security_group_rules, 20)>'],
58
@test.create_stubs({api.base: ('is_service_enabled',),
59
api.nova: ('default_quota_get', 'service_list',),
60
api.cinder: ('default_quota_get',)})
61
def test_index_with_neutron_disabled(self):
62
# Neutron does not have an API for getting default system
63
# quotas. When not using Neutron, the floating ips quotas
64
# should be in the list.
65
api.base.is_service_enabled(IsA(http.HttpRequest), 'volume') \
67
api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
68
.MultipleTimes().AndReturn(False)
70
api.nova.default_quota_get(IsA(http.HttpRequest),
71
self.tenant.id).AndReturn(self.quotas.nova)
72
api.cinder.default_quota_get(IsA(http.HttpRequest), self.tenant.id) \
73
.AndReturn(self.cinder_quotas.first())
77
res = self.client.get(INDEX_URL)
79
self.assertTemplateUsed(res, 'admin/defaults/index.html')
81
quotas_tab = res.context['tab_group'].get_tab('quotas')
82
self.assertQuerysetEqual(quotas_tab._tables['quotas'].data,
83
['<Quota: (injected_file_content_bytes, 1)>',
84
'<Quota: (metadata_items, 1)>',
85
'<Quota: (injected_files, 1)>',
86
'<Quota: (gigabytes, 1000)>',
87
'<Quota: (ram, 10000)>',
88
'<Quota: (floating_ips, 1)>',
89
'<Quota: (fixed_ips, 10)>',
90
'<Quota: (instances, 10)>',
91
'<Quota: (snapshots, 1)>',
92
'<Quota: (volumes, 1)>',
93
'<Quota: (cores, 10)>',
94
'<Quota: (security_groups, 10)>',
95
'<Quota: (security_group_rules, 20)>'],
99
class UpdateDefaultQuotasTests(test.BaseAdminViewTests):
100
def _get_quota_info(self, quota):
102
for field in (quotas.QUOTA_FIELDS + quotas.MISSING_QUOTA_FIELDS):
103
if field != 'fixed_ips':
104
limit = quota.get(field).limit or 10
105
quota_data[field] = int(limit)
108
@test.create_stubs({api.nova: ('default_quota_update', ),
109
api.cinder: ('default_quota_update', ),
110
quotas: ('get_default_quota_data', )})
111
def test_update_default_quotas(self):
112
quota = self.quotas.first()
115
quotas.get_default_quota_data(IsA(http.HttpRequest)).AndReturn(quota)
120
updated_quota = self._get_quota_info(quota)
123
nova_fields = quotas.NOVA_QUOTA_FIELDS + quotas.MISSING_QUOTA_FIELDS
124
nova_updated_quota = dict([(key, updated_quota[key]) for key in
125
nova_fields if key != 'fixed_ips'])
126
api.nova.default_quota_update(IsA(http.HttpRequest),
127
**nova_updated_quota)
129
cinder_updated_quota = dict([(key, updated_quota[key]) for key in
130
quotas.CINDER_QUOTA_FIELDS])
131
api.cinder.default_quota_update(IsA(http.HttpRequest),
132
**cinder_updated_quota)
136
url = reverse('horizon:admin:defaults:update_defaults')
137
res = self.client.post(url, updated_quota)
139
self.assertNoFormErrors(res)
140
self.assertRedirectsNoFollow(res, INDEX_URL)