summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/tests/create_flavor_tests.py
blob: a69de40e7f5c5f2ffc486f5fbbd98a71a97db536 (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
# 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 unittest
import uuid

from snaps.config.flavor import FlavorConfig, FlavorConfigError
from snaps.openstack import create_flavor
from snaps.openstack.create_flavor import OpenStackFlavor, FlavorSettings
from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
from snaps.openstack.utils import nova_utils
from snaps.openstack.tests import openstack_tests

__author__ = 'spisarski'


class FlavorSettingsUnitTests(unittest.TestCase):
    """
    Tests the construction of the FlavorSettings class
    """

    def test_no_params(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings()

    def test_empty_config(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(config=dict())

    def test_name_only(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo')

    def test_config_with_name_only(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(config={'name': 'foo'})

    def test_name_ram_only(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1)

    def test_config_with_name_ram_only(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(config={'name': 'foo', 'ram': 1})

    def test_name_ram_disk_only(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=1)

    def test_config_with_name_ram_disk_only(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(config={'name': 'foo', 'ram': 1, 'disk': 1})

    def test_ram_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram='bar', disk=2, vcpus=3, ephemeral=4,
                           swap=5, rxtx_factor=6.0,
                           is_public=False)

    def test_config_ram_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 'bar', 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_ram_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1.5, disk=2, vcpus=3, ephemeral=4,
                           swap=5, rxtx_factor=6.0, is_public=False)

    def test_config_ram_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1.5, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_disk_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk='bar', vcpus=3, ephemeral=4,
                           swap=5, rxtx_factor=6.0,
                           is_public=False)

    def test_config_disk_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 'bar', 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_disk_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2.5, vcpus=3, ephemeral=4,
                           swap=5, rxtx_factor=6.0, is_public=False)

    def test_config_disk_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2.5, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_vcpus_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus='bar', ephemeral=4,
                           swap=5, rxtx_factor=6.0,
                           is_public=False)

    def test_config_vcpus_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 'bar',
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_ephemeral_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus=3, ephemeral='bar',
                           swap=5, rxtx_factor=6.0,
                           is_public=False)

    def test_config_ephemeral_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 'bar', 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_ephemeral_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus=3, ephemeral=4.5,
                           swap=5, rxtx_factor=6.0, is_public=False)

    def test_config_ephemeral_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4.5, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_swap_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus=3, ephemeral=4,
                           swap='bar', rxtx_factor=6.0,
                           is_public=False)

    def test_config_swap_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 'bar',
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_swap_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus=3, ephemeral=4,
                           swap=5.5, rxtx_factor=6.0, is_public=False)

    def test_config_swap_float(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5.5,
                        'rxtx_factor': 6.0, 'is_public': False})

    def test_rxtx_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus=3, ephemeral=4,
                           swap=5, rxtx_factor='bar', is_public=False)

    def test_config_rxtx_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 'bar', 'is_public': False})

    def test_is_pub_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(name='foo', ram=1, disk=2, vcpus=3, ephemeral=4,
                           swap=5, rxtx_factor=6.0, is_public='bar')

    def test_config_is_pub_string(self):
        with self.assertRaises(FlavorConfigError):
            FlavorSettings(
                config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3,
                        'ephemeral': 4, 'swap': 5,
                        'rxtx_factor': 6.0, 'is_public': 'bar'})

    def test_name_ram_disk_vcpus_only(self):
        settings = FlavorSettings(name='foo', ram=1, disk=2, vcpus=3)
        self.assertEqual('foo', settings.name)
        self.assertEqual('auto', settings.flavor_id)
        self.assertEqual(1, settings.ram)
        self.assertEqual(2, settings.disk)
        self.assertEqual(3, settings.vcpus)
        self.assertEqual(0, settings.ephemeral)
        self.assertEqual(0, settings.swap)
        self.assertEqual(1.0, settings.rxtx_factor)
        self.assertEqual(True, settings.is_public)
        self.assertEqual(None, settings.metadata)

    def test_config_with_name_ram_disk_vcpus_only(self):
        settings = FlavorSettings(
            **{'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3})
        self.assertEqual('foo', settings.name)
        self.assertEqual('auto', settings.flavor_id)
        self.assertEqual(1, settings.ram)
        self.assertEqual(2, settings.disk)
        self.assertEqual(3, settings.vcpus)
        self.assertEqual(0, settings.ephemeral)
        self.assertEqual(0, settings.swap)
        self.assertEqual(1.0, settings.rxtx_factor)
        self.assertEqual(True, settings.is_public)
        self.assertEqual(None, settings.metadata)

    def test_all(self):
        metadata = {'foo': 'bar'}
        settings = FlavorSettings(
            name='foo', flavor_id='bar', ram=1, disk=2, vcpus=3, ephemeral=4,
            swap=5, rxtx_factor=6.0, is_public=False, metadata=metadata)
        self.assertEqual('foo', settings.name)
        self.assertEqual('bar', settings.flavor_id)
        self.assertEqual(1, settings.ram)
        self.assertEqual(2, settings.disk)
        self.assertEqual(3, settings.vcpus)
        self.assertEqual(4, settings.ephemeral)
        self.assertEqual(5, settings.swap)
        self.assertEqual(6.0, settings.rxtx_factor)
        self.assertEqual(False, settings.is_public)
        self.assertEqual(metadata, settings.metadata)

    def test_config_all(self):
        metadata = {'foo': 'bar'}
        settings = FlavorSettings(
            **{'name': 'foo', 'flavor_id': 'bar', 'ram': 1, 'disk': 2,
               'vcpus': 3,
               'ephemeral': 4, 'swap': 5, 'rxtx_factor': 6.0,
               'is_public': False,
               'metadata': metadata})
        self.assertEqual('foo', settings.name)
        self.assertEqual('bar', settings.flavor_id)
        self.assertEqual(1, settings.ram)
        self.assertEqual(2, settings.disk)
        self.assertEqual(3, settings.vcpus)
        self.assertEqual(4, settings.ephemeral)
        self.assertEqual(5, settings.swap)
        self.assertEqual(6.0, settings.rxtx_factor)
        self.assertEqual(False, settings.is_public)
        self.assertEqual(metadata, settings.metadata)


class CreateFlavorTests(OSComponentTestCase):
    """
    Test for the CreateSecurityGroup class defined in create_security_group.py
    """

    def setUp(self):
        """
        Instantiates the CreateSecurityGroup object that is responsible for
        downloading and creating an OS image file within OpenStack
        """
        guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
        self.flavor_name = guid + 'name'

        self.nova = nova_utils.nova_client(self.os_creds, self.os_session)

        # Initialize for cleanup
        self.flavor_creator = None

    def tearDown(self):
        """
        Cleans the image and downloaded image file
        """
        if self.flavor_creator:
            self.flavor_creator.clean()

        super(self.__class__, self).__clean__()

    def test_create_flavor(self):
        """
        Tests the creation of an OpenStack flavor.
        """
        # Create Flavor
        flavor_settings = FlavorConfig(
            name=self.flavor_name, ram=1, disk=1, vcpus=1)
        self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_settings)
        flavor = self.flavor_creator.create()
        self.assertTrue(validate_flavor(self.nova, flavor_settings, flavor))

    def test_create_flavor_existing(self):
        """
        Tests the creation of an OpenStack flavor then starts another creator
        to ensure it has not been done twice.
        """
        # Create Flavor
        flavor_settings = FlavorConfig(
            name=self.flavor_name, ram=1, disk=1, vcpus=1)
        self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_settings)
        flavor = self.flavor_creator.create()
        self.assertTrue(validate_flavor(self.nova, flavor_settings, flavor))

        flavor_creator_2 = OpenStackFlavor(self.os_creds, flavor_settings)
        flavor2 = flavor_creator_2.create()

        self.assertEqual(flavor.id, flavor2.id)

    def test_create_clean_flavor(self):
        """
        Tests the creation and cleanup of an OpenStack flavor.
        """
        # Create Flavor
        flavor_settings = FlavorConfig(
            name=self.flavor_name, ram=1, disk=1, vcpus=1)
        self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_settings)
        flavor = self.flavor_creator.create()
        self.assertTrue(validate_flavor(self.nova, flavor_settings, flavor))

        # Clean Flavor
        self.flavor_creator.clean()

        self.assertIsNone(self.flavor_creator.get_flavor())
        self.assertIsNone(
            nova_utils.get_flavor_by_name(self.nova, flavor_settings.name))

    def test_create_delete_flavor(self):
        """
        Tests the creation of an OpenStack Flavor, the deletion, then
        cleanup to ensure clean() does not
        raise any exceptions.
        """
        # Create Flavor
        flavor_settings = FlavorConfig(
            name=self.flavor_name, ram=1, disk=1, vcpus=1)
        self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_settings)
        flavor = self.flavor_creator.create()
        self.assertTrue(validate_flavor(self.nova, flavor_settings, flavor))

        # Delete Flavor
        nova_utils.delete_flavor(self.nova, flavor)
        self.assertIsNone(
            nova_utils.get_flavor_by_name(self.nova, flavor_settings.name))

        # Attempt to cleanup
        self.flavor_creator.clean()

        self.assertIsNone(self.flavor_creator.get_flavor())

    def test_create_flavor_all_settings(self):
        """
        Tests the creation of an OpenStack Flavor, the deletion, then
        cleanup to ensure clean() does not
        raise any exceptions.
        """
        # Create Flavor
        if self.flavor_metadata:
            self.flavor_metadata.update(create_flavor.MEM_PAGE_SIZE_ANY)
        flavor_settings = openstack_tests.get_flavor_config(
            name=self.flavor_name, ram=1, disk=1, vcpus=1, ephemeral=2, swap=3,
            rxtx_factor=2.2, is_public=False,
            metadata=self.flavor_metadata)
        self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_settings)
        flavor = self.flavor_creator.create()
        self.assertTrue(validate_flavor(self.nova, flavor_settings, flavor))

        # Delete Flavor
        nova_utils.delete_flavor(self.nova, flavor)
        self.assertIsNone(
            nova_utils.get_flavor_by_name(self.nova, flavor_settings.name))

        # Attempt to cleanup
        self.flavor_creator.clean()

        self.assertIsNone(self.flavor_creator.get_flavor())


def validate_flavor(nova, flavor_settings, flavor):
    """
    Validates the flavor_settings against the OpenStack flavor object
    :param nova: the nova client
    :param flavor_settings: the settings used to create the flavor
    :param flavor: the OpenStack flavor object
    """
    setting_meta = dict()
    if flavor_settings.metadata:
        setting_meta = flavor_settings.metadata
    metadata = nova_utils.get_flavor_keys(nova, flavor)

    equals = True
    for key, value in setting_meta.items():
        if metadata[key] != value:
            equals = False
            break

    swap = None
    if flavor_settings.swap != 0:
        swap = flavor_settings.swap

    return (
        flavor is not None and
        flavor_settings.name == flavor.name and
        flavor_settings.ram == flavor.ram and
        flavor_settings.disk == flavor.disk and
        flavor_settings.vcpus == flavor.vcpus and
        flavor_settings.ephemeral == flavor.ephemeral and
        swap == flavor.swap and
        flavor_settings.rxtx_factor == flavor.rxtx_factor and
        flavor_settings.is_public == flavor.is_public and
        equals)