File: test_shares_listing.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 (370 lines) | stat: -rw-r--r-- 14,916 bytes parent folder | download | duplicates (2)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
# 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.

import ddt
from tempest.lib.common.utils import data_utils
from tempest.lib import exceptions
import testtools

from manilaclient.common import constants
from manilaclient import config
from manilaclient.tests.functional import base

CONF = config.CONF


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

    @ddt.data('admin', 'user')
    def test_shares_list(self, role):
        self.clients[role].manila('list')

    @ddt.data('admin', 'user')
    def test_list_with_debug_flag(self, role):
        self.clients[role].manila('list', flags='--debug')

    @ddt.data('admin', 'user')
    def test_shares_list_all_tenants(self, role):
        self.clients[role].manila('list', params='--all-tenants')

    @ddt.data('admin', 'user')
    def test_shares_list_filter_by_name(self, role):
        self.clients[role].manila('list', params='--name name')

    @ddt.data('admin', 'user')
    def test_shares_list_filter_by_export_location(self, role):
        self.clients[role].manila('list', params='--export_location fake')

    @ddt.data('admin', 'user')
    def test_shares_list_filter_by_inexact_name(self, role):
        self.clients[role].manila('list', params='--name~ na')

    @ddt.data('admin', 'user')
    def test_shares_list_filter_by_inexact_description(self, role):
        self.clients[role].manila('list', params='--description~ des')

    @ddt.data('admin', 'user')
    def test_shares_list_filter_by_status(self, role):
        self.clients[role].manila('list', params='--status status')

    def test_shares_list_filter_by_share_server_as_admin(self):
        self.clients['admin'].manila('list', params='--share-server fake')

    def test_shares_list_filter_by_share_server_as_user(self):
        self.assertRaises(
            exceptions.CommandFailed,
            self.clients['user'].manila,
            'list',
            params='--share-server fake')

    @ddt.data('admin', 'user')
    def test_shares_list_filter_by_project_id(self, role):
        self.clients[role].manila('list', params='--project-id fake')

    def test_shares_list_filter_by_host(self):
        self.clients['admin'].manila('list', params='--host fake')

    @ddt.data('admin', 'user')
    def test_shares_list_with_limit_and_offset(self, role):
        self.clients[role].manila('list', params='--limit 1 --offset 1')

    @ddt.data(
        {'role': 'admin', 'direction': 'asc'},
        {'role': 'admin', 'direction': 'desc'},
        {'role': 'user', 'direction': 'asc'},
        {'role': 'user', 'direction': 'desc'})
    @ddt.unpack
    def test_shares_list_with_sorting(self, role, direction):
        self.clients[role].manila(
            'list', params='--sort-key host --sort-dir ' + direction)

    @ddt.data('admin', 'user')
    def test_snapshot_list(self, role):
        self.clients[role].manila('snapshot-list')

    @ddt.data('admin', 'user')
    def test_snapshot_list_all_tenants(self, role):
        self.clients[role].manila('snapshot-list', params='--all-tenants')

    @ddt.data('admin', 'user')
    def test_snapshot_list_filter_by_name(self, role):
        self.clients[role].manila('snapshot-list', params='--name name')

    @ddt.data('admin', 'user')
    def test_snapshot_list_filter_by_status(self, role):
        self.clients[role].manila('snapshot-list', params='--status status')


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

    def setUp(self):
        super(SharesListReadWriteTest, self).setUp()
        self.private_name = data_utils.rand_name('autotest_share_name')
        self.private_description = data_utils.rand_name(
            'autotest_share_description')
        self.public_name = data_utils.rand_name('autotest_public_share_name')
        self.public_description = data_utils.rand_name(
            'autotest_public_share_description')

        self.admin_private_name = data_utils.rand_name(
            'autotest_admin_private_share_name')
        self.admin_private_description = data_utils.rand_name(
            'autotest_admin_private_share_description')

        self.soft_name = data_utils.rand_name('soft_delete_share_name')

        self.admin_private_share = self.create_share(
            name=self.admin_private_name,
            description=self.admin_private_description,
            public=False,
            client=None,
            wait_for_creation=False)

        self.private_share = self.create_share(
            name=self.private_name,
            description=self.private_description,
            public=False,
            client=self.get_user_client(),
            wait_for_creation=False)

        self.public_share = self.create_share(
            name=self.public_name,
            description=self.public_description,
            public=True,
            client=self.admin_client)

        self.wait_soft_delete_share = self.create_share(
            name=self.soft_name,
            public=False,
            client=self.get_user_client(),
            wait_for_creation=False)

        self.shares_created = (self.private_share['id'],
                               self.public_share['id'],
                               self.admin_private_share['id'],
                               self.wait_soft_delete_share['id'])

        for share_id in self.shares_created:
            self.admin_client.wait_for_resource_status(
                share_id, constants.STATUS_AVAILABLE)

        self.soft_delete_share([self.wait_soft_delete_share['id']],
                               client=self.get_user_client(),
                               microversion='2.69')

    def _list_shares(self, filters=None):
        filters = filters or dict()
        shares = self.user_client.list_shares(filters=filters)

        self.assertGreater(len(shares), 0)
        if filters:
            for share in shares:
                try:
                    share_get = self.user_client.get_share(share['ID'])
                except exceptions.NotFound:
                    # NOTE(vponomaryov): Case when some share was deleted
                    # between our 'list' and 'get' requests. Skip such case.
                    # It occurs with concurrently running tests.
                    continue
                if 'migrating' in share_get['status']:
                    # all bets are off, a fair chance share migration
                    # started between the 'list' and 'get' requests. No need
                    # to verify these shares.
                    continue
                for filter_key, expected_value in filters.items():
                    if filter_key in ('share_network', 'share-network'):
                        filter_key = 'share_network_id'
                        if share_get[filter_key] != expected_value:
                            # Possibly a mismatch because the share was
                            # migrated to a new network in the time that
                            # elapsed between the 'list' and 'get' requests.
                            # If this isn't one of the shares created in
                            # this class, don't worry about such mismatches
                            self.assertNotIn(share_get['id'],
                                             self.shares_created)
                            continue

                    if (expected_value != 'deleting' and
                            share_get[filter_key] == 'deleting'):
                        continue
                    self.assertEqual(expected_value, share_get[filter_key])

    def test_list_shares(self):
        self._list_shares()

    @ddt.data(1, 0)
    def test_list_shares_for_all_tenants(self, all_tenants):
        shares = self.admin_client.list_shares(all_tenants=all_tenants)
        self.assertLessEqual(1, len(shares))

        if all_tenants:
            self.assertTrue(all('Project ID' in s for s in shares))
            for s_id in (self.private_share['id'], self.public_share['id'],
                         self.admin_private_share['id']):
                self.assertTrue(any(s_id == s['ID'] for s in shares))
        else:
            self.assertTrue(all('Project ID' not in s for s in shares))
            self.assertTrue(any(self.admin_private_share['id'] == s['ID']
                                for s in shares))
            if self.private_share['project_id'] != (
                    self.admin_private_share['project_id']):
                for s_id in (
                        self.private_share['id'], self.public_share['id']):
                    self.assertFalse(any(s_id == s['ID'] for s in shares))

    @ddt.data(True, False)
    def test_list_shares_with_public(self, public):
        shares = self.user_client.list_shares(is_public=public)
        self.assertGreater(len(shares), 1)
        if public:
            self.assertTrue(all('Project ID' in s for s in shares))
        else:
            self.assertTrue(all('Project ID' not in s for s in shares))

    def test_list_shares_by_name(self):
        shares = self.user_client.list_shares(
            filters={'name': self.private_name})

        self.assertEqual(1, len(shares))
        self.assertTrue(
            any(self.private_share['id'] == s['ID'] for s in shares))
        for share in shares:
            get = self.user_client.get_share(share['ID'])
            self.assertEqual(self.private_name, get['name'])

    def test_list_shares_by_share_type(self):
        share_type_id = self.user_client.get_share_type(
            self.private_share['share_type'])['ID']
        # NOTE(vponomaryov): this is API 2.6+ specific
        self._list_shares({'share_type': share_type_id})

    def test_list_shares_by_status(self):
        self._list_shares({'status': 'available'})

    def test_list_shares_by_project_id(self):
        project_id = self.user_client.get_project_id(
            self.user_client.tenant_name)
        self._list_shares({'project_id': project_id})

    @testtools.skipUnless(
        CONF.share_network, "Usage of Share networks is disabled")
    def test_list_shares_by_share_network(self):
        share_network_id = self.user_client.get_share_network(
            CONF.share_network)['id']
        self._list_shares({'share_network': share_network_id})

    @ddt.data(
        {'limit': 1},
        {'limit': 2},
        {'limit': 1, 'offset': 1},
        {'limit': 2, 'offset': 0},
    )
    def test_list_shares_with_limit(self, filters):
        shares = self.user_client.list_shares(filters=filters)
        self.assertEqual(filters['limit'], len(shares))

    def test_list_share_select_column(self):
        shares = self.user_client.list_shares(columns="Name,Size")
        self.assertTrue(any(s['Name'] is not None for s in shares))
        self.assertTrue(any(s['Size'] is not None for s in shares))
        self.assertTrue(all('Description' not in s for s in shares))

    @ddt.data('ID', 'Path')
    def test_list_shares_by_export_location(self, option):
        export_locations = self.admin_client.list_share_export_locations(
            self.public_share['id'])
        shares = self.admin_client.list_shares(
            filters={'export_location': export_locations[0][option]})

        self.assertEqual(1, len(shares))
        self.assertTrue(
            any(self.public_share['id'] == s['ID'] for s in shares))
        for share in shares:
            get = self.admin_client.get_share(share['ID'])
            self.assertEqual(self.public_name, get['name'])

    @ddt.data('ID', 'Path')
    def test_list_share_instances_by_export_location(self, option):
        export_locations = self.admin_client.list_share_export_locations(
            self.public_share['id'])
        share_instances = self.admin_client.list_share_instances(
            filters={'export_location': export_locations[0][option]})

        self.assertEqual(1, len(share_instances))

        share_instance_id = share_instances[0]['ID']
        except_export_locations = (
            self.admin_client.list_share_instance_export_locations(
                share_instance_id))
        self.assertGreater(len(except_export_locations), 0)
        self.assertTrue(
            any(export_locations[0][option] == e[option] for e in
                except_export_locations))

    def test_list_share_by_export_location_with_invalid_version(self):
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.list_shares,
            filters={'export_location': 'fake'},
            microversion='2.34')

    def test_list_share_instance_by_export_location_invalid_version(self):
        self.assertRaises(
            exceptions.CommandFailed,
            self.admin_client.list_share_instances,
            filters={'export_location': 'fake'},
            microversion='2.34')

    @ddt.data('name', 'description')
    def test_list_shares_by_inexact_option(self, option):
        shares = self.user_client.list_shares(
            filters={option + '~': option})

        # We know we have to have atleast three shares.
        # Due to test concurrency, there can be
        # more than three shares (some created by other tests).
        self.assertGreaterEqual(len(shares), 3)
        self.assertTrue(
            any(self.private_share['id'] == s['ID'] for s in shares))

    def test_list_shares_by_inexact_unicode_option(self):
        self.create_share(
            name=u'共享名称',
            description=u'共享描述',
            client=self.user_client)
        filters = {'name~': u'名称'}
        shares = self.user_client.list_shares(filters=filters)
        self.assertGreater(len(shares), 0)

        filters = {'description~': u'描述'}
        shares = self.user_client.list_shares(filters=filters)
        self.assertGreater(len(shares), 0)

    def test_list_shares_by_description(self):
        shares = self.user_client.list_shares(
            filters={'description': self.private_description})

        self.assertEqual(1, len(shares))
        self.assertTrue(
            any(self.private_share['id'] == s['ID'] for s in shares))

    def test_list_shares_in_recycle_bin(self):
        shares = self.user_client.list_shares(is_soft_deleted=True)

        self.assertTrue(
            any(self.wait_soft_delete_share['id'] == s['ID'] for s in shares))