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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-31 17:31:49 UTC
  • mfrom: (1.1.38)
  • Revision ID: package-import@ubuntu.com-20140331173149-f28yjk2s8pt15fqj
Tags: 1:2014.1~rc1-0ubuntu1
* New upstream release candidate (LP: #1288245).
  - d/static/*: Refreshed assets for new upstream release.
* d/theme/*: Refresh Ubuntu theme against Icehouse templates (LP: #1291653).

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
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._test_index(neutron_enabled=True)
31
 
 
32
 
    def test_index_with_neutron_disabled(self):
33
 
        self._test_index(neutron_enabled=False)
34
 
 
35
 
    def test_index_with_neutron_sg_disabled(self):
36
 
        self._test_index(neutron_enabled=True,
37
 
                         neutron_sg_enabled=False)
38
 
 
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')
46
 
        if neutron_enabled:
47
 
            self.mox.StubOutWithMock(api.neutron, 'is_extension_supported')
48
 
 
49
 
        api.base.is_service_enabled(IsA(http.HttpRequest), 'volume') \
50
 
                .AndReturn(True)
51
 
        api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \
52
 
                .MultipleTimes().AndReturn(neutron_enabled)
53
 
 
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())
58
 
        if neutron_enabled:
59
 
            api.neutron.is_extension_supported(
60
 
                IsA(http.HttpRequest),
61
 
                'security-group').AndReturn(neutron_sg_enabled)
62
 
 
63
 
        self.mox.ReplayAll()
64
 
 
65
 
        res = self.client.get(INDEX_URL)
66
 
 
67
 
        self.assertTemplateUsed(res, 'admin/defaults/index.html')
68
 
 
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)>']
83
 
        if neutron_enabled:
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)>')
89
 
 
90
 
        self.assertQuerysetEqual(quotas_tab._tables['quotas'].data,
91
 
                                 expected_tabs,
92
 
                                 ordered=False)
93
 
 
94
 
 
95
 
class UpdateDefaultQuotasTests(test.BaseAdminViewTests):
96
 
    def _get_quota_info(self, quota):
97
 
        quota_data = {}
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)
102
 
        return quota_data
103
 
 
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()
110
 
 
111
 
        # init
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)
115
 
 
116
 
        # update some fields
117
 
        quota[0].limit = 123
118
 
        quota[1].limit = -1
119
 
        updated_quota = self._get_quota_info(quota)
120
 
 
121
 
        # handle
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)
127
 
 
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)
132
 
 
133
 
        self.mox.ReplayAll()
134
 
 
135
 
        url = reverse('horizon:admin:defaults:update_defaults')
136
 
        res = self.client.post(url, updated_quota)
137
 
 
138
 
        self.assertNoFormErrors(res)
139
 
        self.assertRedirectsNoFollow(res, INDEX_URL)