~ubuntu-branches/ubuntu/trusty/horizon/trusty-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/admin/defaults/tests.py

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2013-09-06 11:59:43 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20130906115943-h3td0l7tp16mb9oc
Tags: 1:2013.2~b3-0ubuntu1
* New upstream release.
* debian/control: Minimum python-openstack-auth version >= 1.1.1.
* debian/control: Add python-troveclient.
* debian/static: Refresh static assets for 2013.2~b3.
* debian/patches: ubuntu_local_settings.patch -> ubuntu_settings.patch, also
  patch location of secret key in openstack_dashboard/settings.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2013 Kylin, Inc.
 
4
#
 
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
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
#    under the License.
 
16
 
 
17
from django.core.urlresolvers import reverse  # noqa
 
18
from django import http
 
19
from mox import IsA  # noqa
 
20
 
 
21
from openstack_dashboard import api
 
22
from openstack_dashboard.test import helpers as test
 
23
from openstack_dashboard.usage import quotas
 
24
 
 
25
INDEX_URL = reverse('horizon:admin:defaults:index')
 
26
 
 
27
 
 
28
class ServicesViewTests(test.BaseAdminViewTests):
 
29
    def test_index(self):
 
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())
 
36
 
 
37
        self.mox.ReplayAll()
 
38
 
 
39
        res = self.client.get(INDEX_URL)
 
40
 
 
41
        self.assertTemplateUsed(res, 'admin/defaults/index.html')
 
42
 
 
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)>'],
 
56
                                 ordered=False)
 
57
 
 
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') \
 
66
                .AndReturn(True)
 
67
        api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
 
68
                .MultipleTimes().AndReturn(False)
 
69
 
 
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())
 
74
 
 
75
        self.mox.ReplayAll()
 
76
 
 
77
        res = self.client.get(INDEX_URL)
 
78
 
 
79
        self.assertTemplateUsed(res, 'admin/defaults/index.html')
 
80
 
 
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)>'],
 
96
                                 ordered=False)
 
97
 
 
98
 
 
99
class UpdateDefaultQuotasTests(test.BaseAdminViewTests):
 
100
    def _get_quota_info(self, quota):
 
101
        quota_data = {}
 
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)
 
106
        return quota_data
 
107
 
 
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()
 
113
 
 
114
        # init
 
115
        quotas.get_default_quota_data(IsA(http.HttpRequest)).AndReturn(quota)
 
116
 
 
117
        # update some fields
 
118
        quota[0].limit = 123
 
119
        quota[1].limit = -1
 
120
        updated_quota = self._get_quota_info(quota)
 
121
 
 
122
        # handle
 
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)
 
128
 
 
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)
 
133
 
 
134
        self.mox.ReplayAll()
 
135
 
 
136
        url = reverse('horizon:admin:defaults:update_defaults')
 
137
        res = self.client.post(url, updated_quota)
 
138
 
 
139
        self.assertNoFormErrors(res)
 
140
        self.assertRedirectsNoFollow(res, INDEX_URL)