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
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._test_index(neutron_enabled=True)
32
def test_index_with_neutron_disabled(self):
33
self._test_index(neutron_enabled=False)
35
def test_index_with_neutron_sg_disabled(self):
36
self._test_index(neutron_enabled=True,
37
neutron_sg_enabled=False)
39
def _test_index(self, neutron_enabled=True, neutron_sg_enabled=True):
40
# Neutron does not have an API for getting default system
41
# quotas. When not using Neutron, the floating ips quotas
42
# should be in the list.
43
self.mox.StubOutWithMock(api.nova, 'default_quota_get')
44
self.mox.StubOutWithMock(api.cinder, 'default_quota_get')
45
self.mox.StubOutWithMock(api.base, 'is_service_enabled')
47
self.mox.StubOutWithMock(api.neutron, 'is_extension_supported')
49
api.base.is_service_enabled(IsA(http.HttpRequest), 'volume') \
51
api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
52
.MultipleTimes().AndReturn(neutron_enabled)
54
api.nova.default_quota_get(IsA(http.HttpRequest),
55
self.tenant.id).AndReturn(self.quotas.nova)
56
api.cinder.default_quota_get(IsA(http.HttpRequest), self.tenant.id) \
57
.AndReturn(self.cinder_quotas.first())
59
api.neutron.is_extension_supported(
60
IsA(http.HttpRequest),
61
'security-group').AndReturn(neutron_sg_enabled)
65
res = self.client.get(INDEX_URL)
67
self.assertTemplateUsed(res, 'admin/defaults/index.html')
69
quotas_tab = res.context['tab_group'].get_tab('quotas')
70
expected_tabs = ['<Quota: (injected_file_content_bytes, 1)>',
71
'<Quota: (metadata_items, 1)>',
72
'<Quota: (injected_files, 1)>',
73
'<Quota: (gigabytes, 1000)>',
74
'<Quota: (ram, 10000)>',
75
'<Quota: (instances, 10)>',
76
'<Quota: (snapshots, 1)>',
77
'<Quota: (volumes, 1)>',
78
'<Quota: (cores, 10)>',
79
'<Quota: (floating_ips, 1)>',
80
'<Quota: (fixed_ips, 10)>',
81
'<Quota: (security_groups, 10)>',
82
'<Quota: (security_group_rules, 20)>']
84
expected_tabs.remove('<Quota: (floating_ips, 1)>')
85
expected_tabs.remove('<Quota: (fixed_ips, 10)>')
86
if neutron_sg_enabled:
87
expected_tabs.remove('<Quota: (security_groups, 10)>')
88
expected_tabs.remove('<Quota: (security_group_rules, 20)>')
90
self.assertQuerysetEqual(quotas_tab._tables['quotas'].data,
95
class UpdateDefaultQuotasTests(test.BaseAdminViewTests):
96
def _get_quota_info(self, quota):
98
for field in (quotas.QUOTA_FIELDS + quotas.MISSING_QUOTA_FIELDS):
99
if field != 'fixed_ips':
100
limit = quota.get(field).limit or 10
101
quota_data[field] = int(limit)
104
@test.create_stubs({api.nova: ('default_quota_update', ),
105
api.cinder: ('default_quota_update', ),
106
quotas: ('get_default_quota_data',
107
'get_disabled_quotas')})
108
def test_update_default_quotas(self):
109
quota = self.quotas.first()
112
quotas.get_disabled_quotas(IsA(http.HttpRequest)) \
113
.AndReturn(self.disabled_quotas.first())
114
quotas.get_default_quota_data(IsA(http.HttpRequest)).AndReturn(quota)
119
updated_quota = self._get_quota_info(quota)
122
nova_fields = quotas.NOVA_QUOTA_FIELDS + quotas.MISSING_QUOTA_FIELDS
123
nova_updated_quota = dict([(key, updated_quota[key]) for key in
124
nova_fields if key != 'fixed_ips'])
125
api.nova.default_quota_update(IsA(http.HttpRequest),
126
**nova_updated_quota)
128
cinder_updated_quota = dict([(key, updated_quota[key]) for key in
129
quotas.CINDER_QUOTA_FIELDS])
130
api.cinder.default_quota_update(IsA(http.HttpRequest),
131
**cinder_updated_quota)
135
url = reverse('horizon:admin:defaults:update_defaults')
136
res = self.client.post(url, updated_quota)
138
self.assertNoFormErrors(res)
139
self.assertRedirectsNoFollow(res, INDEX_URL)