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
|
# 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 uuid
import unittest
from snaps.openstack import create_flavor
from snaps.openstack.create_flavor import FlavorSettings, OpenStackFlavor
from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
from snaps.openstack.utils import nova_utils
__author__ = 'spisarski'
class FlavorSettingsUnitTests(unittest.TestCase):
"""
Tests the construction of the FlavorSettings class
"""
def test_no_params(self):
with self.assertRaises(Exception):
FlavorSettings()
def test_empty_config(self):
with self.assertRaises(Exception):
FlavorSettings(config=dict())
def test_name_only(self):
with self.assertRaises(Exception):
FlavorSettings(name='foo')
def test_config_with_name_only(self):
with self.assertRaises(Exception):
FlavorSettings(config={'name': 'foo'})
def test_name_ram_only(self):
with self.assertRaises(Exception):
FlavorSettings(name='foo', ram=1)
def test_config_with_name_ram_only(self):
with self.assertRaises(Exception):
FlavorSettings(config={'name': 'foo', 'ram': 1})
def test_name_ram_disk_only(self):
with self.assertRaises(Exception):
FlavorSettings(name='foo', ram=1, disk=1)
def test_config_with_name_ram_disk_only(self):
with self.assertRaises(Exception):
FlavorSettings(config={'name': 'foo', 'ram': 1, 'disk': 1})
def test_ram_string(self):
with self.assertRaises(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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(Exception):
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.assertEquals('foo', settings.name)
self.assertEquals('auto', settings.flavor_id)
self.assertEquals(1, settings.ram)
self.assertEquals(2, settings.disk)
self.assertEquals(3, settings.vcpus)
self.assertEquals(0, settings.ephemeral)
self.assertEquals(0, settings.swap)
self.assertEquals(1.0, settings.rxtx_factor)
self.assertEquals(True, settings.is_public)
self.assertEquals(create_flavor.DEFAULT_METADATA, settings.metadata)
def test_config_with_name_ram_disk_vcpus_only(self):
settings = FlavorSettings(config={'name': 'foo', 'ram': 1, 'disk': 2, 'vcpus': 3})
self.assertEquals('foo', settings.name)
self.assertEquals('auto', settings.flavor_id)
self.assertEquals(1, settings.ram)
self.assertEquals(2, settings.disk)
self.assertEquals(3, settings.vcpus)
self.assertEquals(0, settings.ephemeral)
self.assertEquals(0, settings.swap)
self.assertEquals(1.0, settings.rxtx_factor)
self.assertEquals(True, settings.is_public)
self.assertEquals(create_flavor.DEFAULT_METADATA, 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.assertEquals('foo', settings.name)
self.assertEquals('bar', settings.flavor_id)
self.assertEquals(1, settings.ram)
self.assertEquals(2, settings.disk)
self.assertEquals(3, settings.vcpus)
self.assertEquals(4, settings.ephemeral)
self.assertEquals(5, settings.swap)
self.assertEquals(6.0, settings.rxtx_factor)
self.assertEquals(False, settings.is_public)
self.assertEquals(metadata, settings.metadata)
def test_config_all(self):
metadata = {'foo': 'bar'}
settings = FlavorSettings(config={'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.assertEquals('foo', settings.name)
self.assertEquals('bar', settings.flavor_id)
self.assertEquals(1, settings.ram)
self.assertEquals(2, settings.disk)
self.assertEquals(3, settings.vcpus)
self.assertEquals(4, settings.ephemeral)
self.assertEquals(5, settings.swap)
self.assertEquals(6.0, settings.rxtx_factor)
self.assertEquals(False, settings.is_public)
self.assertEquals(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)
# 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()
def test_create_flavor(self):
"""
Tests the creation of an OpenStack flavor.
"""
# Create Flavor
flavor_settings = FlavorSettings(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(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 = FlavorSettings(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(flavor_settings, flavor))
flavor_creator_2 = OpenStackFlavor(self.os_creds, flavor_settings)
flavor2 = flavor_creator_2.create()
self.assertEquals(flavor.id, flavor2.id)
def test_create_clean_flavor(self):
"""
Tests the creation and cleanup of an OpenStack flavor.
"""
# Create Flavor
flavor_settings = FlavorSettings(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(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 Security Group, the deletion, then cleanup to ensure clean() does not
raise any exceptions.
"""
# Create Flavor
flavor_settings = FlavorSettings(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(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())
# TODO - Add more tests to exercise all configuration options
def validate_flavor(flavor_settings, flavor):
"""
Validates the flavor_settings against the OpenStack flavor object
:param flavor_settings: the settings used to create the flavor
:param flavor: the OpenStack flavor object
"""
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
|