1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 from cinderclient.exceptions import NotFound, BadRequest
17 from snaps.config.volume import VolumeConfig, VolumeConfigError
18 from snaps.config.volume_type import VolumeTypeConfig
19 from snaps.openstack.create_image import OpenStackImage
20 from snaps.openstack.create_volume_type import OpenStackVolumeType
21 from snaps.openstack.tests import openstack_tests
24 from urllib.request import URLError
26 from urllib2 import URLError
32 from snaps.openstack.create_volume import (
33 VolumeSettings, OpenStackVolume)
34 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
35 from snaps.openstack.utils import cinder_utils
37 __author__ = 'spisarski'
39 logger = logging.getLogger('create_volume_tests')
42 class VolumeSettingsUnitTests(unittest.TestCase):
44 Tests the construction of the VolumeSettings class
47 def test_no_params(self):
48 with self.assertRaises(VolumeConfigError):
51 def test_empty_config(self):
52 with self.assertRaises(VolumeConfigError):
53 VolumeSettings(**dict())
55 def test_name_only(self):
56 settings = VolumeSettings(name='foo')
57 self.assertEqual('foo', settings.name)
58 self.assertIsNone(settings.description)
59 self.assertEquals(1, settings.size)
60 self.assertIsNone(settings.image_name)
61 self.assertIsNone(settings.type_name)
62 self.assertIsNone(settings.availability_zone)
63 self.assertFalse(settings.multi_attach)
65 def test_config_with_name_only(self):
66 settings = VolumeSettings(**{'name': 'foo'})
67 self.assertEqual('foo', settings.name)
68 self.assertIsNone(settings.description)
69 self.assertEquals(1, settings.size)
70 self.assertIsNone(settings.image_name)
71 self.assertIsNone(settings.type_name)
72 self.assertIsNone(settings.availability_zone)
73 self.assertFalse(settings.multi_attach)
75 def test_all_strings(self):
76 settings = VolumeSettings(
77 name='foo', description='desc', size='2', image_name='image',
78 type_name='type', availability_zone='zone1', multi_attach='true')
80 self.assertEqual('foo', settings.name)
81 self.assertEqual('desc', settings.description)
82 self.assertEqual(2, settings.size)
83 self.assertEqual('image', settings.image_name)
84 self.assertEqual('type', settings.type_name)
85 self.assertEqual('zone1', settings.availability_zone)
86 self.assertTrue(settings.multi_attach)
88 def test_all_correct_type(self):
89 settings = VolumeSettings(
90 name='foo', description='desc', size=2, image_name='image',
91 type_name='bar', availability_zone='zone1', multi_attach=True)
93 self.assertEqual('foo', settings.name)
94 self.assertEqual('desc', settings.description)
95 self.assertEqual(2, settings.size)
96 self.assertEqual('image', settings.image_name)
97 self.assertEqual('bar', settings.type_name)
98 self.assertEqual('zone1', settings.availability_zone)
99 self.assertTrue(settings.multi_attach)
101 def test_config_all(self):
102 settings = VolumeSettings(
103 **{'name': 'foo', 'description': 'desc', 'size': '2',
104 'image_name': 'foo', 'type_name': 'bar',
105 'availability_zone': 'zone1', 'multi_attach': 'true'})
107 self.assertEqual('foo', settings.name)
108 self.assertEqual('desc', settings.description)
109 self.assertEqual(2, settings.size)
110 self.assertEqual('foo', settings.image_name)
111 self.assertEqual('bar', settings.type_name)
112 self.assertEqual('zone1', settings.availability_zone)
113 self.assertTrue(settings.multi_attach)
116 class CreateSimpleVolumeSuccessTests(OSIntegrationTestCase):
118 Test for the CreateVolume class defined in create_volume.py
123 Instantiates the CreateVolume object that is responsible for
124 downloading and creating an OS volume file within OpenStack
126 super(self.__class__, self).__start__()
129 self.volume_settings = VolumeConfig(
130 name=self.__class__.__name__ + '-' + str(guid))
132 self.cinder = cinder_utils.cinder_client(self.os_creds)
133 self.volume_creator = None
137 Cleans the volume and downloaded volume file
139 if self.volume_creator:
140 self.volume_creator.clean()
142 super(self.__class__, self).__clean__()
144 def test_create_volume_simple(self):
146 Tests the creation of a simple OpenStack volume.
149 self.volume_creator = OpenStackVolume(
150 self.os_creds, self.volume_settings)
151 created_volume = self.volume_creator.create(block=True)
152 self.assertIsNotNone(created_volume)
154 retrieved_volume = cinder_utils.get_volume(
155 self.cinder, volume_settings=self.volume_settings)
157 self.assertIsNotNone(retrieved_volume)
158 self.assertEqual(created_volume.id, retrieved_volume.id)
159 self.assertTrue(created_volume == retrieved_volume)
161 def test_create_delete_volume(self):
163 Tests the creation then deletion of an OpenStack volume to ensure
164 clean() does not raise an Exception.
167 self.volume_creator = OpenStackVolume(
168 self.os_creds, self.volume_settings)
169 created_volume = self.volume_creator.create(block=True)
170 self.assertIsNotNone(created_volume)
172 retrieved_volume = cinder_utils.get_volume(
173 self.cinder, volume_settings=self.volume_settings)
174 self.assertIsNotNone(retrieved_volume)
175 self.assertEqual(created_volume, retrieved_volume)
177 # Delete Volume manually
178 self.volume_creator.clean()
180 self.assertIsNone(cinder_utils.get_volume(
181 self.cinder, volume_settings=self.volume_settings))
183 # Must not throw an exception when attempting to cleanup non-existent
185 self.volume_creator.clean()
186 self.assertIsNone(self.volume_creator.get_volume())
188 def test_create_same_volume(self):
190 Tests the creation of an OpenStack volume when one already exists.
193 self.volume_creator = OpenStackVolume(
194 self.os_creds, self.volume_settings)
195 volume1 = self.volume_creator.create(block=True)
197 retrieved_volume = cinder_utils.get_volume(
198 self.cinder, volume_settings=self.volume_settings)
199 self.assertEqual(volume1, retrieved_volume)
201 # Should be retrieving the instance data
202 os_volume_2 = OpenStackVolume(
203 self.os_creds, self.volume_settings)
204 volume2 = os_volume_2.create(block=True)
205 self.assertEqual(volume1, volume2)
208 class CreateSimpleVolumeFailureTests(OSIntegrationTestCase):
210 Test for the CreateVolume class defined in create_volume.py
215 Instantiates the CreateVolume object that is responsible for
216 downloading and creating an OS volume file within OpenStack
218 super(self.__class__, self).__start__()
220 self.guid = uuid.uuid4()
221 self.cinder = cinder_utils.cinder_client(self.os_creds)
222 self.volume_creator = None
226 Cleans the volume and downloaded volume file
228 if self.volume_creator:
229 self.volume_creator.clean()
231 super(self.__class__, self).__clean__()
233 def test_create_volume_bad_size(self):
235 Tests the creation of an OpenStack volume with a negative size to
236 ensure it raises a BadRequest exception.
238 volume_settings = VolumeConfig(
239 name=self.__class__.__name__ + '-' + str(self.guid), size=-1)
242 self.volume_creator = OpenStackVolume(self.os_creds, volume_settings)
244 with self.assertRaises(BadRequest):
245 self.volume_creator.create(block=True)
247 def test_create_volume_bad_type(self):
249 Tests the creation of an OpenStack volume with a type that does not
250 exist to ensure it raises a NotFound exception.
252 volume_settings = VolumeConfig(
253 name=self.__class__.__name__ + '-' + str(self.guid),
257 self.volume_creator = OpenStackVolume(self.os_creds, volume_settings)
259 with self.assertRaises(NotFound):
260 self.volume_creator.create(block=True)
262 def test_create_volume_bad_image(self):
264 Tests the creation of an OpenStack volume with an image that does not
265 exist to ensure it raises a BadRequest exception.
267 volume_settings = VolumeConfig(
268 name=self.__class__.__name__ + '-' + str(self.guid),
272 self.volume_creator = OpenStackVolume(self.os_creds, volume_settings)
274 with self.assertRaises(BadRequest):
275 self.volume_creator.create(block=True)
277 def test_create_volume_bad_zone(self):
279 Tests the creation of an OpenStack volume with an availability zone
280 that does not exist to ensure it raises a BadRequest exception.
282 volume_settings = VolumeConfig(
283 name=self.__class__.__name__ + '-' + str(self.guid),
284 availability_zone='foo')
287 self.volume_creator = OpenStackVolume(self.os_creds, volume_settings)
289 with self.assertRaises(BadRequest):
290 self.volume_creator.create(block=True)
293 class CreateVolumeWithTypeTests(OSIntegrationTestCase):
295 Test cases for the CreateVolume when attempting to associate it to a
300 super(self.__class__, self).__start__()
302 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
303 self.volume_name = guid + '-vol'
304 self.volume_type_name = guid + '-vol-type'
306 self.volume_type_creator = OpenStackVolumeType(
307 self.admin_os_creds, VolumeTypeConfig(name=self.volume_type_name))
308 self.volume_type_creator.create()
309 self.volume_creator = None
312 if self.volume_creator:
313 self.volume_creator.clean()
314 if self.volume_type_creator:
315 self.volume_type_creator.clean()
317 super(self.__class__, self).__clean__()
319 def test_bad_volume_type(self):
321 Expect a NotFound to be raised when the volume type does not exist
323 self.volume_creator = OpenStackVolume(
325 VolumeConfig(name=self.volume_name, type_name='foo'))
327 with self.assertRaises(NotFound):
328 self.volume_creator.create()
330 def test_valid_volume_type(self):
332 Expect a NotFound to be raised when the volume type does not exist
334 self.volume_creator = OpenStackVolume(
335 self.admin_os_creds, VolumeConfig(
336 name=self.volume_name, type_name=self.volume_type_name))
338 created_volume = self.volume_creator.create(block=True)
339 self.assertIsNotNone(created_volume)
340 self.assertEqual(self.volume_type_name, created_volume.type)
343 class CreateVolumeWithImageTests(OSIntegrationTestCase):
345 Test cases for the CreateVolume when attempting to associate it to an Image
349 super(self.__class__, self).__start__()
351 self.cinder = cinder_utils.cinder_client(self.os_creds)
353 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
354 self.volume_name = guid + '-vol'
355 self.image_name = guid + '-image'
357 os_image_settings = openstack_tests.cirros_image_settings(
358 name=self.image_name, image_metadata=self.image_metadata)
360 self.image_creator = OpenStackImage(self.os_creds,
362 self.image_creator.create()
363 self.volume_creator = None
366 if self.volume_creator:
368 self.volume_creator.clean()
371 if self.image_creator:
373 self.image_creator.clean()
377 super(self.__class__, self).__clean__()
379 def test_bad_image_name(self):
381 Tests OpenStackVolume#create() method to ensure a volume is NOT created
382 when associating it to an invalid image name
384 self.volume_creator = OpenStackVolume(
386 VolumeConfig(name=self.volume_name, image_name='foo'))
388 with self.assertRaises(BadRequest):
389 self.volume_creator.create(block=True)
391 def test_valid_volume_image(self):
393 Tests OpenStackVolume#create() method to ensure a volume is NOT created
394 when associating it to an invalid image name
396 self.volume_creator = OpenStackVolume(
398 VolumeConfig(name=self.volume_name, image_name=self.image_name))
400 created_volume = self.volume_creator.create(block=True)
401 self.assertIsNotNone(created_volume)
403 self.volume_creator.volume_settings.name, created_volume.name)
404 self.assertTrue(self.volume_creator.volume_active())
406 retrieved_volume = cinder_utils.get_volume_by_id(
407 self.cinder, created_volume.id)
409 self.assertEqual(created_volume, retrieved_volume)