summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/tests/cinder_utils_tests.py
blob: e8c31dbfcf76d1e132b18b7151463de37a3b1110 (plain)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  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 logging
import uuid

import time
from cinderclient.exceptions import NotFound, BadRequest

from snaps.config.volume_type import (
    VolumeTypeConfig, ControlLocation, VolumeTypeEncryptionConfig)
from snaps.config.qos import Consumer, QoSConfig
from snaps.openstack import create_volume
from snaps.openstack.create_volume import VolumeSettings
from snaps.openstack.tests import validation_utils
from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
from snaps.openstack.utils import cinder_utils

__author__ = 'spisarski'


logger = logging.getLogger('cinder_utils_tests')


class CinderSmokeTests(OSComponentTestCase):
    """
    Tests to ensure that the neutron client can communicate with the cloud
    """

    def test_cinder_connect_success(self):
        """
        Tests to ensure that the proper credentials can connect.
        """
        cinder = cinder_utils.cinder_client(self.os_creds)
        volumes = cinder.volumes.list()
        self.assertIsNotNone(volumes)
        self.assertTrue(isinstance(volumes, list))

    def test_cinder_connect_fail(self):
        """
        Tests to ensure that the improper credentials cannot connect.
        """
        from snaps.openstack.os_credentials import OSCreds

        with self.assertRaises(Exception):
            cinder = cinder_utils.cinder_client(OSCreds(
                username='user', password='pass', auth_url='url',
                project_name='project'))
            cinder.volumes.list()


class CinderUtilsVolumeTests(OSComponentTestCase):
    """
    Test for the CreateVolume class defined in create_volume.py
    """

    def setUp(self):
        """
        Instantiates the CreateVolume object that is responsible for
        downloading and creating an OS volume file within OpenStack
        """
        guid = uuid.uuid4()
        self.volume_name = self.__class__.__name__ + '-' + str(guid)
        self.volume = None
        self.cinder = cinder_utils.cinder_client(self.os_creds)

    def tearDown(self):
        """
        Cleans the remote OpenStack objects
        """
        if self.volume:
            try:
                cinder_utils.delete_volume(self.cinder, self.volume)
            except NotFound:
                pass

        self.assertTrue(volume_deleted(self.cinder, self.volume))

    def test_create_simple_volume(self):
        """
        Tests the cinder_utils.create_volume()
        """
        volume_settings = VolumeSettings(name=self.volume_name)
        self.volume = cinder_utils.create_volume(
            self.cinder, volume_settings)
        self.assertIsNotNone(self.volume)
        self.assertEqual(self.volume_name, self.volume.name)

        self.assertTrue(volume_active(self.cinder, self.volume))

        volume = cinder_utils.get_volume(
            self.cinder, volume_settings=volume_settings)
        self.assertIsNotNone(volume)
        validation_utils.objects_equivalent(self.volume, volume)

    def test_create_delete_volume(self):
        """
        Tests the cinder_utils.create_volume()
        """
        volume_settings = VolumeSettings(name=self.volume_name)
        self.volume = cinder_utils.create_volume(
            self.cinder, volume_settings)
        self.assertIsNotNone(self.volume)
        self.assertEqual(self.volume_name, self.volume.name)

        self.assertTrue(volume_active(self.cinder, self.volume))

        volume = cinder_utils.get_volume(
            self.cinder, volume_settings=volume_settings)
        self.assertIsNotNone(volume)
        validation_utils.objects_equivalent(self.volume, volume)

        cinder_utils.delete_volume(self.cinder, self.volume)
        self.assertTrue(volume_deleted(self.cinder, self.volume))
        self.assertIsNone(
            cinder_utils.get_volume(self.cinder, volume_settings))


def volume_active(cinder, volume):
    """
    Returns true if volume becomes active
    :param cinder:
    :param volume:
    :return:
    """
    end_time = time.time() + create_volume.VOLUME_ACTIVE_TIMEOUT
    while time.time() < end_time:
        status = cinder_utils.get_volume_status(cinder, volume)
        if status == create_volume.STATUS_ACTIVE:
            return True
        elif status == create_volume.STATUS_FAILED:
            return False
        time.sleep(3)

    return False


def volume_deleted(cinder, volume):
    """
    Returns true if volume becomes active
    :param cinder:
    :param volume:
    :return:
    """
    end_time = time.time() + create_volume.VOLUME_ACTIVE_TIMEOUT
    while time.time() < end_time:
        try:
            status = cinder_utils.get_volume_status(cinder, volume)
            if status == create_volume.STATUS_DELETED:
                return True
        except NotFound:
            return True

        time.sleep(3)

    return False


class CinderUtilsQoSTests(OSComponentTestCase):
    """
    Test for the CreateQos class defined in create_qos.py
    """

    def setUp(self):
        """
        Creates objects for testing cinder_utils.py
        """
        guid = uuid.uuid4()
        self.qos_name = self.__class__.__name__ + '-' + str(guid)
        self.specs = {'foo': 'bar '}
        self.qos = None
        self.cinder = cinder_utils.cinder_client(self.os_creds)

    def tearDown(self):
        """
        Cleans the remote OpenStack objects
        """
        if self.qos:
            try:
                cinder_utils.delete_qos(self.cinder, self.qos)
            except NotFound:
                pass

    def test_create_qos_both(self):
        """
        Tests the cinder_utils.create_qos()
        """
        qos_settings = QoSConfig(
            name=self.qos_name, specs=self.specs, consumer=Consumer.both)
        self.qos = cinder_utils.create_qos(self.cinder, qos_settings)
        self.assertIsNotNone(self.qos)

        qos1 = cinder_utils.get_qos(self.cinder, qos_settings=qos_settings)
        self.assertIsNotNone(qos1)
        validation_utils.objects_equivalent(self.qos, qos1)

        qos2 = cinder_utils.get_qos_by_id(self.cinder, qos1.id)
        self.assertIsNotNone(qos2)
        validation_utils.objects_equivalent(self.qos, qos2)

    def test_create_qos_front(self):
        """
        Tests the cinder_utils.create_qos()
        """
        qos_settings = QoSConfig(
            name=self.qos_name, specs=self.specs, consumer=Consumer.front_end)
        self.qos = cinder_utils.create_qos(self.cinder, qos_settings)
        self.assertIsNotNone(self.qos)

        qos1 = cinder_utils.get_qos(self.cinder, qos_settings=qos_settings)
        self.assertIsNotNone(qos1)
        validation_utils.objects_equivalent(self.qos, qos1)

        qos2 = cinder_utils.get_qos_by_id(self.cinder, qos1.id)
        self.assertIsNotNone(qos2)
        validation_utils.objects_equivalent(self.qos, qos2)

    def test_create_qos_back(self):
        """
        Tests the cinder_utils.create_qos()
        """
        qos_settings = QoSConfig(
            name=self.qos_name, specs=self.specs, consumer=Consumer.back_end)
        self.qos = cinder_utils.create_qos(self.cinder, qos_settings)
        self.assertIsNotNone(self.qos)

        qos1 = cinder_utils.get_qos(self.cinder, qos_settings=qos_settings)
        self.assertIsNotNone(qos1)
        validation_utils.objects_equivalent(self.qos, qos1)

        qos2 = cinder_utils.get_qos_by_id(self.cinder, qos1.id)
        self.assertIsNotNone(qos2)
        validation_utils.objects_equivalent(self.qos, qos2)

    def test_create_delete_qos(self):
        """
        Tests the cinder_utils.create_qos()
        """
        qos_settings = QoSConfig(name=self.qos_name, consumer=Consumer.both)
        self.qos = cinder_utils.create_qos(self.cinder, qos_settings)
        self.assertIsNotNone(self.qos)
        self.assertEqual(self.qos_name, self.qos.name)

        qos = cinder_utils.get_qos(
            self.cinder, qos_settings=qos_settings)
        self.assertIsNotNone(qos)
        validation_utils.objects_equivalent(self.qos, qos)

        cinder_utils.delete_qos(self.cinder, self.qos)
        self.assertIsNone(cinder_utils.get_qos(
            self.cinder, qos_settings=qos_settings))


class CinderUtilsSimpleVolumeTypeTests(OSComponentTestCase):
    """
    Tests the creation of a Volume Type without any external settings such as
    QoS Specs or encryption
    """

    def setUp(self):
        """
        Instantiates the CreateVolume object that is responsible for
        downloading and creating an OS volume file within OpenStack
        """
        guid = uuid.uuid4()
        volume_type_name = self.__class__.__name__ + '-' + str(guid)
        self.volume_type_settings = VolumeTypeConfig(name=volume_type_name)
        self.volume_type = None
        self.cinder = cinder_utils.cinder_client(self.os_creds)

    def tearDown(self):
        """
        Cleans the remote OpenStack objects
        """
        if self.volume_type:
            try:
                cinder_utils.delete_volume_type(self.cinder, self.volume_type)
            except NotFound:
                pass

    def test_create_simple_volume_type(self):
        """
        Tests the cinder_utils.create_volume_type(), get_volume_type(), and
        get_volume_type_by_id()
        """
        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, self.volume_type_settings)
        self.assertIsNotNone(self.volume_type)
        self.assertEqual(self.volume_type_settings.name, self.volume_type.name)

        volume_type1 = cinder_utils.get_volume_type(
            self.cinder, volume_type_settings=self.volume_type_settings)
        self.assertEquals(self.volume_type, volume_type1)
        self.assertEquals(self.volume_type_settings.public,
                          volume_type1.public)

        volume_type2 = cinder_utils.get_volume_type_by_id(
            self.cinder, volume_type1.id)
        self.assertEquals(self.volume_type, volume_type2)
        self.assertIsNone(self.volume_type.encryption)

    def test_create_delete_volume_type(self):
        """
        Primarily tests the cinder_utils.delete_volume()
        """
        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, self.volume_type_settings)
        self.assertIsNotNone(self.volume_type)
        self.assertEqual(self.volume_type_settings.name, self.volume_type.name)

        volume_type = cinder_utils.get_volume_type(
            self.cinder, volume_type_settings=self.volume_type_settings)
        self.assertIsNotNone(volume_type)
        validation_utils.objects_equivalent(self.volume_type, volume_type)
        self.assertIsNone(self.volume_type.encryption)

        cinder_utils.delete_volume_type(self.cinder, self.volume_type)
        self.assertIsNone(cinder_utils.get_volume_type_by_id(
            self.cinder, self.volume_type.id))


class CinderUtilsAddEncryptionTests(OSComponentTestCase):
    """
    Tests the creation of an encryption and association to and existing
    VolumeType object
    """

    def setUp(self):
        """
        Instantiates the CreateVolume object that is responsible for
        downloading and creating an OS volume file within OpenStack
        """
        guid = uuid.uuid4()
        self.encryption_name = self.__class__.__name__ + '-' + str(guid)
        self.encryption = None

        self.cinder = cinder_utils.cinder_client(self.os_creds)

        volume_type_name = self.__class__.__name__ + '-' + str(guid) + '-type'
        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, VolumeTypeConfig(name=volume_type_name))

    def tearDown(self):
        """
        Cleans the remote OpenStack objects
        """
        if self.encryption:
            try:
                cinder_utils.delete_volume_type_encryption(
                    self.cinder, self.volume_type)
            except NotFound:
                pass

        if self.volume_type:
            try:
                cinder_utils.delete_volume_type(self.cinder, self.volume_type)
            except NotFound:
                pass

    def test_create_simple_encryption(self):
        """
        Tests the cinder_utils.create_volume_encryption(),
        get_volume_encryption(), and get_volume_encryption_by_id()
        """
        encryption_settings = VolumeTypeEncryptionConfig(
            name=self.encryption_name, provider_class='foo',
            control_location=ControlLocation.front_end)
        self.encryption = cinder_utils.create_volume_encryption(
            self.cinder, self.volume_type, encryption_settings)
        self.assertIsNotNone(self.encryption)
        self.assertEqual('foo', self.encryption.provider)
        self.assertEqual(ControlLocation.front_end.value,
                         self.encryption.control_location)

        encryption1 = cinder_utils.get_volume_encryption_by_type(
            self.cinder, self.volume_type)
        self.assertEquals(self.encryption, encryption1)

    def test_create_delete_encryption(self):
        """
        Primarily tests the cinder_utils.delete_volume_type_encryption()
        """
        encryption_settings = VolumeTypeEncryptionConfig(
            name=self.encryption_name, provider_class='LuksEncryptor',
            control_location=ControlLocation.back_end)
        self.encryption = cinder_utils.create_volume_encryption(
            self.cinder, self.volume_type, encryption_settings)
        self.assertIsNotNone(self.encryption)
        self.assertEqual('LuksEncryptor', self.encryption.provider)
        self.assertEqual(ControlLocation.back_end.value,
                         self.encryption.control_location)

        encryption1 = cinder_utils.get_volume_encryption_by_type(
            self.cinder, self.volume_type)
        self.assertEquals(self.encryption, encryption1)

        cinder_utils.delete_volume_type_encryption(
            self.cinder, self.volume_type)

        encryption2 = cinder_utils.get_volume_encryption_by_type(
            self.cinder, self.volume_type)
        self.assertIsNone(encryption2)

    def test_create_with_all_attrs(self):
        """
        Tests the cinder_utils.create_volume_encryption() with all valid
        settings
        """
        encryption_settings = VolumeTypeEncryptionConfig(
            name=self.encryption_name, provider_class='foo',
            cipher='bar', control_location=ControlLocation.back_end,
            key_size=1)
        self.encryption = cinder_utils.create_volume_encryption(
            self.cinder, self.volume_type, encryption_settings)
        self.assertIsNotNone(self.encryption)
        self.assertEqual('foo', self.encryption.provider)
        self.assertEqual('bar', self.encryption.cipher)
        self.assertEqual(1, self.encryption.key_size)
        self.assertEqual(ControlLocation.back_end.value,
                         self.encryption.control_location)

        encryption1 = cinder_utils.get_volume_encryption_by_type(
            self.cinder, self.volume_type)
        self.assertEquals(self.encryption, encryption1)

    def test_create_bad_key_size(self):
        """
        Tests the cinder_utils.create_volume_encryption() raises an exception
        when the provider class does not exist
        """
        encryption_settings = VolumeTypeEncryptionConfig(
            name=self.encryption_name, provider_class='foo',
            cipher='bar', control_location=ControlLocation.back_end,
            key_size=-1)

        with self.assertRaises(BadRequest):
            self.encryption = cinder_utils.create_volume_encryption(
                self.cinder, self.volume_type, encryption_settings)


class CinderUtilsVolumeTypeCompleteTests(OSComponentTestCase):
    """
    Tests to ensure that a volume type can have a QoS Spec added to it
    """

    def setUp(self):
        """
        Creates objects for testing cinder_utils.py
        """
        guid = uuid.uuid4()
        self.qos_name = self.__class__.__name__ + '-' + str(guid) + '-qos'
        self.vol_type_name = self.__class__.__name__ + '-' + str(guid)
        self.specs = {'foo': 'bar'}
        self.cinder = cinder_utils.cinder_client(self.os_creds)
        qos_settings = QoSConfig(
            name=self.qos_name, specs=self.specs, consumer=Consumer.both)
        self.qos = cinder_utils.create_qos(self.cinder, qos_settings)
        self.volume_type = None

    def tearDown(self):
        """
        Cleans the remote OpenStack objects
        """
        if self.volume_type:
            if self.volume_type.encryption:
                try:
                    cinder_utils.delete_volume_type_encryption(
                        self.cinder, self.volume_type)
                except NotFound:
                    pass
            try:
                cinder_utils.delete_volume_type(self.cinder, self.volume_type)
            except NotFound:
                pass

        if self.qos:
            try:
                cinder_utils.delete_qos(self.cinder, self.qos)
            except NotFound:
                pass

    def test_create_with_encryption(self):
        """
        Tests the cinder_utils.create_volume_type() where encryption has been
        configured
        """
        encryption_settings = VolumeTypeEncryptionConfig(
            name='foo', provider_class='bar',
            control_location=ControlLocation.back_end)
        volume_type_settings = VolumeTypeConfig(
            name=self.vol_type_name, encryption=encryption_settings)
        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, volume_type_settings)

        vol_encrypt = cinder_utils.get_volume_encryption_by_type(
            self.cinder, self.volume_type)
        self.assertIsNotNone(vol_encrypt)
        self.assertIsNone(self.volume_type.qos_spec)
        self.assertEqual(self.volume_type.encryption, vol_encrypt)
        self.assertEqual(self.volume_type.id, vol_encrypt.volume_type_id)

    def test_create_with_qos(self):
        """
        Tests the cinder_utils.create_volume_type() with an associated QoS Spec
        """
        volume_type_settings = VolumeTypeConfig(
            name=self.vol_type_name, qos_spec_name=self.qos_name)
        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, volume_type_settings)

        self.assertIsNotNone(self.volume_type)
        self.assertIsNone(self.volume_type.encryption)
        self.assertIsNotNone(self.volume_type.qos_spec)
        self.assertEqual(self.qos.id, self.volume_type.qos_spec.id)

    def test_create_with_invalid_qos(self):
        """
        Tests the cinder_utils.create_volume_type() when the QoS Spec name
        does not exist
        """
        volume_type_settings = VolumeTypeConfig(
            name=self.vol_type_name, qos_spec_name='foo')

        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, volume_type_settings)

        self.assertIsNone(self.volume_type.qos_spec)

    def test_create_with_qos_and_encryption(self):
        """
        Tests the cinder_utils.create_volume_type() with encryption and an
        associated QoS Spec
        """
        encryption_settings = VolumeTypeEncryptionConfig(
            name='foo', provider_class='bar',
            control_location=ControlLocation.back_end)
        volume_type_settings = VolumeTypeConfig(
            name=self.vol_type_name, qos_spec_name=self.qos_name,
            encryption=encryption_settings)
        self.volume_type = cinder_utils.create_volume_type(
            self.cinder, volume_type_settings)

        self.assertIsNotNone(self.volume_type)
        vol_encrypt = cinder_utils.get_volume_encryption_by_type(
            self.cinder, self.volume_type)
        self.assertIsNotNone(vol_encrypt)
        self.assertEqual(self.volume_type.id, vol_encrypt.volume_type_id)
        self.assertIsNotNone(self.volume_type.qos_spec)
        self.assertEqual(self.qos.id, self.volume_type.qos_spec.id)