File: test_quotas.py

package info (click to toggle)
python-manilaclient 5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,768 kB
  • sloc: python: 49,541; makefile: 99; sh: 2
file content (330 lines) | stat: -rw-r--r-- 13,085 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Copyright 2015 Mirantis Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from random import randint

import ddt
from tempest.lib.cli import output_parser
from tempest.lib.common.utils import data_utils
from tempest.lib import exceptions

from manilaclient import api_versions
from manilaclient.tests.functional import base
from manilaclient.tests.functional import utils

REPLICA_QUOTAS_MICROVERSION = '2.53'


def _get_share_type_quota_values(project_quota_value):
    project_quota_value = int(project_quota_value)
    if project_quota_value == -1:
        return randint(1, 999)
    elif project_quota_value == 0:
        return 0
    else:
        return project_quota_value - 1


@ddt.ddt
@utils.skip_if_microversion_not_supported("2.39")
class QuotasReadWriteTest(base.BaseTestCase):

    def setUp(self):
        super(self.__class__, self).setUp()
        self.microversion = "2.39"
        self.project_id = self.admin_client.get_project_id(
            self.admin_client.tenant_name)

        # Create share type
        self.share_type = self.create_share_type(
            name=data_utils.rand_name("manilaclient_functional_test"),
            driver_handles_share_servers=False,
            is_public=True,
            microversion=self.microversion
        )
        self.st_id = self.share_type["ID"]

    def _verify_current_st_quotas_equal_to(self, quotas, microversion):
        # Read share type quotas
        cmd = 'quota-show --tenant-id %s --share-type %s' % (
            self.project_id, self.st_id)
        st_quotas_raw = self.admin_client.manila(
            cmd, microversion=microversion)
        st_quotas = output_parser.details(st_quotas_raw)

        # Verify that quotas
        self.assertGreater(len(st_quotas), 3)
        for key, value in st_quotas.items():
            if key not in ('shares', 'gigabytes', 'snapshots',
                           'snapshot_gigabytes'):
                continue
            self.assertIn(key, quotas)
            self.assertEqual(int(quotas[key]), int(value))

    def _verify_current_quotas_equal_to(self, quotas, microversion):
        # Read quotas
        cmd = 'quota-show --tenant-id %s' % self.project_id
        quotas_raw = self.admin_client.manila(
            cmd, microversion=microversion)
        quotas = output_parser.details(quotas_raw)

        # Verify that quotas
        self.assertGreater(len(quotas), 3)
        for key, value in quotas.items():
            if key not in ('shares', 'gigabytes', 'snapshots',
                           'snapshot_gigabytes',
                           'share_groups', 'share_group_snapshots'):
                continue
            self.assertIn(key, quotas)
            self.assertEqual(int(quotas[key]), int(value))

    @ddt.data(*set([
        "2.40", api_versions.MAX_VERSION,
    ]))
    def test_update_quotas_for_share_groups(self, microversion):
        if not utils.is_microversion_supported(microversion):
            msg = "Microversion '%s' not supported." % microversion
            raise self.skipException(msg)

        # Get default quotas
        cmd = 'quota-defaults'
        quotas_raw = self.admin_client.manila(cmd, microversion=microversion)
        default_quotas = output_parser.details(quotas_raw)

        # Get project quotas
        cmd = 'quota-show --tenant-id %s ' % self.project_id
        quotas_raw = self.admin_client.manila(cmd, microversion=microversion)
        p_quotas = output_parser.details(quotas_raw)

        # Define custom share group quotas for project
        p_custom_quotas = {
            'share_groups': -1 if int(p_quotas['share_groups']) != -1 else 999,
            'share_group_snapshots': -1 if int(
                p_quotas['share_group_snapshots']) != -1 else 999,
        }

        # Update share group quotas for project
        cmd = ('quota-update %s --share-groups %s '
               '--share-group-snapshots %s') % (
            self.project_id,
            p_custom_quotas['share_groups'],
            p_custom_quotas['share_group_snapshots'],
        )
        self.admin_client.manila(cmd, microversion=microversion)

        # Verify quotas
        self._verify_current_quotas_equal_to(p_custom_quotas, microversion)

        # Reset quotas
        cmd = 'quota-delete --tenant-id %s --share-type %s' % (
            self.project_id, self.st_id)
        self.admin_client.manila(cmd, microversion=microversion)

        # Verify quotas after reset
        self._verify_current_quotas_equal_to(default_quotas, microversion)

        # Return project quotas back
        cmd = ('quota-update %s --share-groups %s '
               '--share-group-snapshots %s') % (
            self.project_id,
            p_quotas['share_groups'], p_quotas['share_group_snapshots'])
        self.admin_client.manila(cmd, microversion=microversion)

        # Verify quotas after reset
        self._verify_current_quotas_equal_to(p_quotas, microversion)

    @ddt.data('--share-groups', '--share-group-snapshots')
    @utils.skip_if_microversion_not_supported("2.39")
    def test_update_quotas_for_share_groups_using_too_old_microversion(self,
                                                                       arg):
        cmd = 'quota-update %s %s 13' % (self.project_id, arg)
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.manila,
            cmd, microversion='2.39')

    @ddt.data('--share-replicas', '--replica-gigabytes')
    @utils.skip_if_microversion_not_supported("2.52")
    def test_update_quotas_for_share_replicas_using_too_old_microversion(self,
                                                                         arg):
        cmd = 'quota-update %s %s 10' % (self.project_id, arg)
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.manila,
            cmd, microversion='2.52')

    @ddt.data('--share-groups', '--share-group-snapshots')
    @utils.skip_if_microversion_not_supported("2.40")
    def test_update_share_type_quotas_for_share_groups(self, arg):
        cmd = 'quota-update %s --share-type %s %s 13' % (
            self.project_id, self.st_id, arg)
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.manila,
            cmd, microversion='2.40')

    @ddt.data(*set([
        "2.39", "2.40", REPLICA_QUOTAS_MICROVERSION, api_versions.MAX_VERSION,
    ]))
    def test_update_share_type_quotas_positive(self, microversion):
        if not utils.is_microversion_supported(microversion):
            msg = "Microversion '%s' not supported." % microversion
            raise self.skipException(msg)

        # Get project quotas
        cmd = 'quota-show --tenant-id %s ' % self.project_id
        quotas_raw = self.admin_client.manila(cmd, microversion=microversion)
        p_quotas = output_parser.details(quotas_raw)

        # Define share type quotas
        st_custom_quotas = {
            'shares': _get_share_type_quota_values(p_quotas['shares']),
            'snapshots': _get_share_type_quota_values(p_quotas['snapshots']),
            'gigabytes': _get_share_type_quota_values(p_quotas['gigabytes']),
            'snapshot_gigabytes': _get_share_type_quota_values(
                p_quotas['snapshot_gigabytes']),
        }
        supports_share_replica_quotas = (
            api_versions.APIVersion(microversion) >= api_versions.APIVersion(
                REPLICA_QUOTAS_MICROVERSION))

        if supports_share_replica_quotas:
            st_custom_quotas['share_replicas'] = _get_share_type_quota_values(
                p_quotas['share_replicas']
            )
            st_custom_quotas['replica_gigabytes'] = (
                _get_share_type_quota_values(p_quotas['replica_gigabytes']))
            replica_params = (' --share-replicas %s '
                              '--replica-gigabytes %s') % (
                st_custom_quotas['share_replicas'],
                st_custom_quotas['replica_gigabytes'])

        # Update quotas for share type
        cmd = ('quota-update %s --share-type %s '
               '--shares %s --gigabytes %s --snapshots %s '
               '--snapshot-gigabytes %s') % (
                   self.project_id, self.st_id,
                   st_custom_quotas['shares'],
                   st_custom_quotas['gigabytes'],
                   st_custom_quotas['snapshots'],
                   st_custom_quotas['snapshot_gigabytes'])

        if supports_share_replica_quotas:
            cmd += replica_params
        self.admin_client.manila(cmd, microversion=microversion)

        # Verify share type quotas
        self._verify_current_st_quotas_equal_to(st_custom_quotas, microversion)

        # Reset share type quotas
        cmd = 'quota-delete --tenant-id %s --share-type %s' % (
            self.project_id, self.st_id)
        self.admin_client.manila(cmd, microversion=microversion)

        # Verify share type quotas after reset
        self._verify_current_st_quotas_equal_to(p_quotas, microversion)

    @utils.skip_if_microversion_not_supported("2.38")
    def test_read_share_type_quotas_with_too_old_microversion(self):
        cmd = 'quota-show --tenant-id %s --share-type %s' % (
            self.project_id, self.st_id)
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.manila,
            cmd, microversion='2.38')

    @utils.skip_if_microversion_not_supported("2.38")
    def test_update_share_type_quotas_with_too_old_microversion(self):
        cmd = 'quota-update --tenant-id %s --share-type %s --shares %s' % (
            self.project_id, self.st_id, '0')
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.manila,
            cmd, microversion='2.38')

    @utils.skip_if_microversion_not_supported("2.38")
    def test_delete_share_type_quotas_with_too_old_microversion(self):
        cmd = 'quota-delete --tenant-id %s --share-type %s' % (
            self.project_id, self.st_id)
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.manila,
            cmd, microversion='2.38')


@ddt.ddt
class ManilaClientTestQuotasReadOnly(base.BaseTestCase):

    def test_quota_class_show_by_admin(self):
        roles = self.parser.listing(
            self.clients['admin'].manila('quota-class-show', params='abc'))
        self.assertTableStruct(roles, ('Property', 'Value'))

    def test_quota_class_show_by_user(self):
        self.assertRaises(
            exceptions.CommandFailed,
            self.clients['user'].manila,
            'quota-class-show',
            params='abc')

    def _get_quotas(self, role, operation, microversion):
        roles = self.parser.listing(self.clients[role].manila(
            'quota-%s' % operation, microversion=microversion))
        self.assertTableStruct(roles, ('Property', 'Value'))

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("1.0")
    def test_quota_defaults_api_1_0(self, role):
        self._get_quotas(role, "defaults", "1.0")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.0")
    def test_quota_defaults_api_2_0(self, role):
        self._get_quotas(role, "defaults", "2.0")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.6")
    def test_quota_defaults_api_2_6(self, role):
        self._get_quotas(role, "defaults", "2.6")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.7")
    def test_quota_defaults_api_2_7(self, role):
        self._get_quotas(role, "defaults", "2.7")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("1.0")
    def test_quota_show_api_1_0(self, role):
        self._get_quotas(role, "show", "1.0")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.0")
    def test_quota_show_api_2_0(self, role):
        self._get_quotas(role, "show", "2.0")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.6")
    def test_quota_show_api_2_6(self, role):
        self._get_quotas(role, "show", "2.6")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.7")
    def test_quota_show_api_2_7(self, role):
        self._get_quotas(role, "show", "2.7")

    @ddt.data('admin', 'user')
    @utils.skip_if_microversion_not_supported("2.25")
    def test_quota_show_api_2_25(self, role):
        self._get_quotas(role, "show  --detail", "2.25")