diff options
-rw-r--r-- | snaps/openstack/create_image.py | 75 | ||||
-rw-r--r-- | snaps/openstack/tests/create_image_tests.py | 362 | ||||
-rw-r--r-- | snaps/openstack/tests/create_instance_tests.py | 468 | ||||
-rw-r--r-- | snaps/openstack/tests/openstack_tests.py | 92 | ||||
-rw-r--r-- | snaps/openstack/tests/os_source_file_test.py | 13 | ||||
-rw-r--r-- | snaps/provisioning/tests/ansible_utils_tests.py | 48 |
6 files changed, 387 insertions, 671 deletions
diff --git a/snaps/openstack/create_image.py b/snaps/openstack/create_image.py index 6ced052..4f2bd7e 100644 --- a/snaps/openstack/create_image.py +++ b/snaps/openstack/create_image.py @@ -12,10 +12,10 @@ # 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 time from glanceclient.exc import HTTPNotFound +import logging +import time from snaps.openstack.utils import glance_utils @@ -43,6 +43,8 @@ class OpenStackImage: self.__os_creds = os_creds self.image_settings = image_settings self.__image = None + self.__kernel_image = None + self.__ramdisk_image = None self.__glance = glance_utils.glance_client(os_creds) def create(self, cleanup=False): @@ -56,6 +58,28 @@ class OpenStackImage: logger.info('Found image with name - ' + self.image_settings.name) return self.__image elif not cleanup: + extra_properties = self.image_settings.extra_properties or dict() + + if self.image_settings.kernel_image_settings: + self.__kernel_image = glance_utils.get_image( + self.__glance, self.image_settings.kernel_image_settings.name) + + if not self.__kernel_image and not cleanup: + logger.info('Creating associated kernel image') + self.__kernel_image = glance_utils.create_image( + self.__glance, self.image_settings.kernel_image_settings) + extra_properties['kernel_id'] = self.__kernel_image.id + if self.image_settings.ramdisk_image_settings: + self.__ramdisk_image = glance_utils.get_image( + self.__glance, self.image_settings.ramdisk_image_settings.name) + + if not self.__ramdisk_image and not cleanup: + logger.info('Creating associated ramdisk image') + self.__ramdisk_image = glance_utils.create_image( + self.__glance, self.image_settings.ramdisk_image_settings) + extra_properties['ramdisk_id'] = self.__ramdisk_image.id + + self.image_settings.extra_properties = extra_properties self.__image = glance_utils.create_image(self.__glance, self.image_settings) logger.info('Creating image') if self.__image and self.image_active(block=True): @@ -73,12 +97,16 @@ class OpenStackImage: Cleanse environment of all artifacts :return: void """ - if self.__image: - try: - glance_utils.delete_image(self.__glance, self.__image) - except HTTPNotFound: - pass - self.__image = None + for image in [self.__image, self.__kernel_image, self.__ramdisk_image]: + if image: + try: + glance_utils.delete_image(self.__glance, image) + except HTTPNotFound: + pass + + self.__image = None + self.__kernel_image = None + self.__ramdisk_image = None def get_image(self): """ @@ -87,6 +115,20 @@ class OpenStackImage: """ return self.__image + def get_kernel_image(self): + """ + Returns the OpenStack kernel image object as it was populated when create() was called + :return: the object + """ + return self.__kernel_image + + def get_ramdisk_image(self): + """ + Returns the OpenStack ramdisk image object as it was populated when create() was called + :return: the object + """ + return self.__ramdisk_image + def image_active(self, block=False, timeout=IMAGE_ACTIVE_TIMEOUT, poll_interval=POLL_INTERVAL): """ Returns true when the image status returns the value of expected_status_code @@ -145,7 +187,8 @@ class OpenStackImage: class ImageSettings: def __init__(self, config=None, name=None, image_user=None, img_format=None, url=None, image_file=None, - extra_properties=None, nic_config_pb_loc=None): + extra_properties=None, nic_config_pb_loc=None, kernel_image_settings=None, + ramdisk_image_settings=None): """ :param config: dict() object containing the configuration settings using the attribute names below as each @@ -158,6 +201,8 @@ class ImageSettings: :param extra_properties: dict() object containing extra parameters to pass when loading the image; can be ids of kernel and initramfs images for a 3-part image :param nic_config_pb_loc: the file location to the Ansible Playbook that can configure multiple NICs + :param kernel_image_settings: the settings for a kernel image + :param ramdisk_image_settings: the settings for a kernel image """ if config: @@ -168,6 +213,16 @@ class ImageSettings: self.image_file = config.get('image_file') self.extra_properties = config.get('extra_properties') self.nic_config_pb_loc = config.get('nic_config_pb_loc') + if config.get('kernel_image_settings'): + self.kernel_image_settings = ImageSettings(config=config['kernel_image_settings']) + else: + self.kernel_image_settings = None + + if config.get('ramdisk_image_settings'): + self.ramdisk_image_settings = ImageSettings(config=config['ramdisk_image_settings']) + else: + self.ramdisk_image_settings = None + else: self.name = name self.image_user = image_user @@ -176,6 +231,8 @@ class ImageSettings: self.image_file = image_file self.extra_properties = extra_properties self.nic_config_pb_loc = nic_config_pb_loc + self.kernel_image_settings = kernel_image_settings + self.ramdisk_image_settings = ramdisk_image_settings if not self.name or not self.image_user or not self.format: raise Exception("The attributes name, image_user, format, and url are required for ImageSettings") diff --git a/snaps/openstack/tests/create_image_tests.py b/snaps/openstack/tests/create_image_tests.py index 0abd33c..1fb4e29 100644 --- a/snaps/openstack/tests/create_image_tests.py +++ b/snaps/openstack/tests/create_image_tests.py @@ -128,8 +128,11 @@ class ImageSettingsUnitTests(unittest.TestCase): def test_all_url(self): properties = {'hw_video_model': 'vga'} + kernel_settings = ImageSettings(name='kernel', url='http://kernel.com', image_user='bar', img_format='qcow2') + ramdisk_settings = ImageSettings(name='ramdisk', url='http://ramdisk.com', image_user='bar', img_format='qcow2') settings = ImageSettings(name='foo', image_user='bar', img_format='qcow2', url='http://foo.com', - extra_properties=properties, nic_config_pb_loc='/foo/bar') + extra_properties=properties, nic_config_pb_loc='/foo/bar', + kernel_image_settings=kernel_settings, ramdisk_image_settings=ramdisk_settings) self.assertEquals('foo', settings.name) self.assertEquals('bar', settings.image_user) self.assertEquals('qcow2', settings.format) @@ -137,12 +140,25 @@ class ImageSettingsUnitTests(unittest.TestCase): self.assertEquals(properties, settings.extra_properties) self.assertIsNone(settings.image_file) self.assertEquals('/foo/bar', settings.nic_config_pb_loc) + self.assertEquals('kernel', settings.kernel_image_settings.name) + self.assertEquals('http://kernel.com', settings.kernel_image_settings.url) + self.assertEquals('bar', settings.kernel_image_settings.image_user) + self.assertEquals('qcow2', settings.kernel_image_settings.format) + self.assertEquals('ramdisk', settings.ramdisk_image_settings.name) + self.assertEquals('http://ramdisk.com', settings.ramdisk_image_settings.url) + self.assertEquals('bar', settings.ramdisk_image_settings.image_user) + self.assertEquals('qcow2', settings.ramdisk_image_settings.format) def test_config_all_url(self): - settings = ImageSettings(config={'name': 'foo', 'image_user': 'bar', 'format': 'qcow2', - 'download_url': 'http://foo.com', - 'extra_properties': '{\'hw_video_model\': \'vga\'}', - 'nic_config_pb_loc': '/foo/bar'}) + settings = ImageSettings( + config={'name': 'foo', 'image_user': 'bar', 'format': 'qcow2', + 'download_url': 'http://foo.com', + 'extra_properties': '{\'hw_video_model\': \'vga\'}', + 'nic_config_pb_loc': '/foo/bar', + 'kernel_image_settings': {'name': 'kernel', 'download_url': 'http://kernel.com', + 'image_user': 'bar', 'format': 'qcow2'}, + 'ramdisk_image_settings': {'name': 'ramdisk', 'download_url': 'http://ramdisk.com', + 'image_user': 'bar', 'format': 'qcow2'}}) self.assertEquals('foo', settings.name) self.assertEquals('bar', settings.image_user) self.assertEquals('qcow2', settings.format) @@ -150,6 +166,10 @@ class ImageSettingsUnitTests(unittest.TestCase): self.assertEquals('{\'hw_video_model\': \'vga\'}', settings.extra_properties) self.assertIsNone(settings.image_file) self.assertEquals('/foo/bar', settings.nic_config_pb_loc) + self.assertEquals('kernel', settings.kernel_image_settings.name) + self.assertEquals('http://kernel.com', settings.kernel_image_settings.url) + self.assertEquals('ramdisk', settings.ramdisk_image_settings.name) + self.assertEquals('http://ramdisk.com', settings.ramdisk_image_settings.url) def test_all_file(self): properties = {'hw_video_model': 'vga'} @@ -192,7 +212,7 @@ class CreateImageSuccessTests(OSIntegrationTestCase): guid = uuid.uuid4() self.image_name = self.__class__.__name__ + '-' + str(guid) self.glance = glance_utils.glance_client(self.os_creds) - self.image_creators = list() + self.image_creator = None self.tmp_dir = 'tmp/' + str(guid) if not os.path.exists(self.tmp_dir): @@ -202,10 +222,8 @@ class CreateImageSuccessTests(OSIntegrationTestCase): """ Cleans the image and downloaded image file """ - if self.image_creators: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + if self.image_creator: + self.image_creator.clean() if os.path.exists(self.tmp_dir) and os.path.isdir(self.tmp_dir): shutil.rmtree(self.tmp_dir) @@ -218,35 +236,10 @@ class CreateImageSuccessTests(OSIntegrationTestCase): """ # Create Image # Set the default image settings, then set any custom parameters sent from the app - os_image_settings = openstack_tests.cirros_url_image(name=self.image_name) - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - os_image_settings.extra_properties = self.image_metadata['extra_properties'] + os_image_settings = openstack_tests.cirros_url_image(name=self.image_name, image_metadata=self.image_metadata) - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.assertIsNotNone(kernel_image) - os_image_settings.extra_properties['kernel_id'] = kernel_image.id - self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - self.assertEquals(get_image_size(ramdisk_image_settings), ramdisk_image.size) - - self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) - created_image = self.image_creators[-1].create() + self.image_creator = create_image.OpenStackImage(self.os_creds, os_image_settings) + created_image = self.image_creator.create() self.assertIsNotNone(created_image) retrieved_image = glance_utils.get_image(self.glance, os_image_settings.name) @@ -262,49 +255,19 @@ class CreateImageSuccessTests(OSIntegrationTestCase): """ # Create Image # Set the default image settings, then set any custom parameters sent from the app - os_image_settings = openstack_tests.cirros_url_image(name=self.image_name) - # Set properties - os_image_settings.extra_properties = {'hw_video_model': 'vga'} + os_image_settings = openstack_tests.cirros_url_image(name=self.image_name, image_metadata=self.image_metadata) - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - os_image_settings.extra_properties = dict( - os_image_settings.extra_properties.items() - + self.image_metadata['extra_properties'].items()) - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.assertIsNotNone(kernel_image) - os_image_settings.extra_properties[str('kernel_id')] = kernel_image.id - self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - os_image_settings.extra_properties[str('ramdisk_id')] = ramdisk_image.id - self.assertEquals(get_image_size(ramdisk_image_settings), ramdisk_image.size) - - self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) - created_image = self.image_creators[-1].create() + self.image_creator = create_image.OpenStackImage(self.os_creds, os_image_settings) + created_image = self.image_creator.create() self.assertIsNotNone(created_image) retrieved_image = glance_utils.get_image(self.glance, os_image_settings.name) self.assertIsNotNone(retrieved_image) - self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) + self.assertEquals(self.image_creator.get_image().size, retrieved_image.size) self.assertEquals(get_image_size(os_image_settings), retrieved_image.size) self.assertEquals(created_image.name, retrieved_image.name) self.assertEquals(created_image.id, retrieved_image.id) - # self.assertEquals(created_image.properties, retrieved_image.properties) + self.assertEquals(created_image.properties, retrieved_image.properties) def test_create_image_clean_file(self): """ @@ -313,50 +276,21 @@ class CreateImageSuccessTests(OSIntegrationTestCase): # Create Image # Set the default image settings, then set any custom parameters sent from the app - url_image_settings = openstack_tests.cirros_url_image('foo') - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - url_image_settings.url = self.image_metadata['disk_url'] + url_image_settings = openstack_tests.cirros_url_image(self.image_name, image_metadata=self.image_metadata) # Download the file of the image image_file = file_utils.download(url_image_settings.url, self.tmp_dir) - file_image_settings = openstack_tests.file_image_test_settings(name=self.image_name, file_path=image_file.name) - - # Set extra properties sent from the app (if any) - if self.image_metadata: - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - file_image_settings.extra_properties = self.image_metadata['extra_properties'] + file_image_settings = openstack_tests.file_image_test_settings( + name=self.image_name, file_path=image_file.name, image_metadata=self.image_metadata) - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_file = file_utils.download(self.image_metadata['kernel_url'], self.tmp_dir) - kernel_image_settings = openstack_tests.file_image_test_settings( - name=self.image_name+'_kernel', file_path=kernel_image_file.name) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.assertIsNotNone(kernel_image) - file_image_settings.extra_properties['kernel_id'] = kernel_image.id - self.assertIsNotNone(kernel_image) - self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_file = file_utils.download(self.image_metadata['ramdisk_url'], self.tmp_dir) - ramdisk_image_settings = openstack_tests.file_image_test_settings( - name=self.image_name+'_ramdisk', file_path=ramdisk_image_file.name) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - file_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(create_image.OpenStackImage(self.os_creds, file_image_settings)) - created_image = self.image_creators[-1].create() + self.image_creator = create_image.OpenStackImage(self.os_creds, file_image_settings) + created_image = self.image_creator.create() self.assertIsNotNone(created_image) self.assertEqual(self.image_name, created_image.name) retrieved_image = glance_utils.get_image(self.glance, file_image_settings.name) self.assertIsNotNone(retrieved_image) - self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) + self.assertEquals(self.image_creator.get_image().size, retrieved_image.size) self.assertEquals(get_image_size(file_image_settings), retrieved_image.size) self.assertEquals(created_image.name, retrieved_image.name) @@ -368,51 +302,25 @@ class CreateImageSuccessTests(OSIntegrationTestCase): """ # Create Image # Set the default image settings, then set any custom parameters sent from the app - os_image_settings = openstack_tests.cirros_url_image(name=self.image_name) - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.assertIsNotNone(kernel_image) - self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) - os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - self.assertEquals(get_image_size(ramdisk_image_settings), ramdisk_image.size) - os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id + os_image_settings = openstack_tests.cirros_url_image(name=self.image_name, image_metadata=self.image_metadata) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) - created_image = self.image_creators[-1].create() + self.image_creator = create_image.OpenStackImage(self.os_creds, os_image_settings) + created_image = self.image_creator.create() self.assertIsNotNone(created_image) retrieved_image = glance_utils.get_image(self.glance, os_image_settings.name) self.assertIsNotNone(retrieved_image) - self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) + self.assertEquals(self.image_creator.get_image().size, retrieved_image.size) self.assertEquals(get_image_size(os_image_settings), retrieved_image.size) # Delete Image manually glance_utils.delete_image(self.glance, created_image) - self.assertIsNone(glance_utils.get_image(self.glance, self.image_creators[-1].image_settings.name)) + self.assertIsNone(glance_utils.get_image(self.glance, self.image_creator.image_settings.name)) # Must not throw an exception when attempting to cleanup non-existent image - self.image_creators[-1].clean() - self.assertIsNone(self.image_creators[-1].get_image()) - self.image_creators.pop() + self.image_creator.clean() + self.assertIsNone(self.image_creator.get_image()) def test_create_same_image(self): """ @@ -420,38 +328,14 @@ class CreateImageSuccessTests(OSIntegrationTestCase): """ # Create Image # Set the default image settings, then set any custom parameters sent from the app - os_image_settings = openstack_tests.cirros_url_image(name=self.image_name) - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.assertIsNotNone(kernel_image) - self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) - os_image_settings.extra_properties['kernel_id'] = kernel_image.id + os_image_settings = openstack_tests.cirros_url_image(name=self.image_name, image_metadata=self.image_metadata) - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) - image1 = self.image_creators[-1].create() + self.image_creator = create_image.OpenStackImage(self.os_creds, os_image_settings) + image1 = self.image_creator.create() retrieved_image = glance_utils.get_image(self.glance, os_image_settings.name) self.assertIsNotNone(retrieved_image) - self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) + self.assertEquals(self.image_creator.get_image().size, retrieved_image.size) self.assertEquals(get_image_size(os_image_settings), retrieved_image.size) self.assertEquals(image1.name, retrieved_image.name) self.assertEquals(image1.id, retrieved_image.id) @@ -566,7 +450,7 @@ class CreateImageNegativeTests(OSIntegrationTestCase): class CreateMultiPartImageTests(OSIntegrationTestCase): """ - Test for creating a 3-part image + Test different means for creating a 3-part images """ def setUp(self): """ @@ -588,9 +472,8 @@ class CreateMultiPartImageTests(OSIntegrationTestCase): """ Cleans the images and downloaded image file """ - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + for image_creator in self.image_creators: + image_creator.clean() if os.path.exists(self.tmp_dir) and os.path.isdir(self.tmp_dir): shutil.rmtree(self.tmp_dir) @@ -601,63 +484,33 @@ class CreateMultiPartImageTests(OSIntegrationTestCase): """ Tests the creation of a 3-part OpenStack image from a URL. """ - # Set properties - properties = {} - if self.image_metadata: - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - properties = self.image_metadata['extra_properties'] - # Create the kernel image - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_kernel', - url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel') - - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings.url = self.image_metadata['kernel_url'] - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.assertIsNotNone(kernel_image) - self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) - - # Create the ramdisk image - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_ramdisk', - url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs') - if self.image_metadata: - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings.url = self.image_metadata['ramdisk_url'] - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - self.assertEquals(get_image_size(ramdisk_image_settings), ramdisk_image.size) - - # Create the main image - os_image_settings = openstack_tests.cirros_url_image( + image_settings = openstack_tests.cirros_url_image( name=self.image_name, - url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img') - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] + image_metadata={'disk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img', + 'kernel_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel', + 'ramdisk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'}) - properties['kernel_id'] = kernel_image.id - properties['ramdisk_id'] = ramdisk_image.id - os_image_settings.extra_properties = properties + image_creator = create_image.OpenStackImage(self.os_creds, image_settings) + self.image_creators.append(image_creator) + image_creator.create() - self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) - created_image = self.image_creators[-1].create() - self.assertIsNotNone(created_image) - self.assertEqual(self.image_name, created_image.name) + main_image = glance_utils.get_image(self.glance, image_settings.name) + self.assertIsNotNone(main_image) + self.assertIsNotNone(image_creator.get_image()) + self.assertEquals(image_creator.get_image().id, main_image.id) - retrieved_image = glance_utils.get_image(self.glance, os_image_settings.name) - self.assertIsNotNone(retrieved_image) - self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) - self.assertEquals(get_image_size(os_image_settings), retrieved_image.size) - self.assertEquals(created_image.name, retrieved_image.name) - self.assertEquals(created_image.id, retrieved_image.id) - self.assertEquals(created_image.properties, retrieved_image.properties) + kernel_image = glance_utils.get_image(self.glance, image_settings.kernel_image_settings.name) + self.assertIsNotNone(kernel_image) + self.assertIsNotNone(image_creator.get_kernel_image()) + self.assertEquals(kernel_image.id, image_creator.get_kernel_image().id) - def test_create_three_part_image_from_file(self): + ramdisk_image = glance_utils.get_image(self.glance, image_settings.ramdisk_image_settings.name) + self.assertIsNotNone(ramdisk_image) + self.assertIsNotNone(image_creator.get_ramdisk_image()) + self.assertEquals(ramdisk_image.id, image_creator.get_ramdisk_image().id) + + def test_create_three_part_image_from_file_3_creators(self): """ Tests the creation of a 3-part OpenStack image from files. """ @@ -707,9 +560,68 @@ class CreateMultiPartImageTests(OSIntegrationTestCase): retrieved_image = glance_utils.get_image(self.glance, file_image_settings.name) self.assertIsNotNone(retrieved_image) - self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) self.assertEquals(get_image_size(file_image_settings), retrieved_image.size) + self.assertEquals(created_image.name, retrieved_image.name) + self.assertEquals(created_image.id, retrieved_image.id) + self.assertEquals(created_image.properties, retrieved_image.properties) + + def test_create_three_part_image_from_url_3_creators(self): + """ + Tests the creation of a 3-part OpenStack image from a URL. + """ + # Set properties + properties = {} + if self.image_metadata and self.image_metadata['extra_properties']: + properties = self.image_metadata['extra_properties'] + + # Create the kernel image + kernel_image_settings = openstack_tests.cirros_url_image( + name=self.image_name+'_kernel', + url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel') + + if self.image_metadata: + if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: + kernel_image_settings.url = self.image_metadata['kernel_url'] + self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) + kernel_image = self.image_creators[-1].create() + self.assertIsNotNone(kernel_image) + self.assertEquals(get_image_size(kernel_image_settings), kernel_image.size) + + # Create the ramdisk image + ramdisk_image_settings = openstack_tests.cirros_url_image( + name=self.image_name+'_ramdisk', + url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs') + if self.image_metadata: + if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: + ramdisk_image_settings.url = self.image_metadata['ramdisk_url'] + self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) + ramdisk_image = self.image_creators[-1].create() + self.assertIsNotNone(ramdisk_image) + self.assertEquals(get_image_size(ramdisk_image_settings), ramdisk_image.size) + + # Create the main image + os_image_settings = openstack_tests.cirros_url_image( + name=self.image_name, + url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img') + if self.image_metadata: + if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: + os_image_settings.url = self.image_metadata['disk_url'] + + properties['kernel_id'] = kernel_image.id + properties['ramdisk_id'] = ramdisk_image.id + os_image_settings.extra_properties = properties + + self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) + created_image = self.image_creators[-1].create() + self.assertIsNotNone(created_image) + self.assertEqual(self.image_name, created_image.name) + + retrieved_image = glance_utils.get_image(self.glance, os_image_settings.name) + self.assertIsNotNone(retrieved_image) + + self.assertEquals(self.image_creators[-1].get_image().size, retrieved_image.size) + self.assertEquals(get_image_size(os_image_settings), retrieved_image.size) self.assertEquals(created_image.name, retrieved_image.name) self.assertEquals(created_image.id, retrieved_image.id) diff --git a/snaps/openstack/tests/create_instance_tests.py b/snaps/openstack/tests/create_instance_tests.py index d8d69b3..ff24efb 100644 --- a/snaps/openstack/tests/create_instance_tests.py +++ b/snaps/openstack/tests/create_instance_tests.py @@ -243,7 +243,7 @@ class SimpleHealthCheck(OSIntegrationTestCase): self.floating_ip_name = guid + 'fip1' # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.network_creator = None self.flavor_creator = None self.inst_creator = None @@ -255,33 +255,12 @@ class SimpleHealthCheck(OSIntegrationTestCase): # Create Image # Set the default image settings, then set any custom parameters sent from the app - self.os_image_settings = openstack_tests.cirros_url_image(name=guid + '-image') - - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] + self.os_image_settings = openstack_tests.cirros_url_image( + name=guid + '-image', image_metadata=self.image_metadata) try: - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() # Create Network self.network_creator = OpenStackNetwork(self.os_creds, self.priv_net_config.network_settings) @@ -324,11 +303,9 @@ class SimpleHealthCheck(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning flavor with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -343,7 +320,7 @@ class SimpleHealthCheck(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) vm = self.inst_creator.create() ip = self.inst_creator.get_port_ip(self.port_settings.name) @@ -387,14 +364,15 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.vm_inst_name = guid + '-inst' self.nova = nova_utils.nova_client(self.os_creds) - self.os_image_settings = openstack_tests.cirros_url_image(name=guid + '-image') + self.os_image_settings = openstack_tests.cirros_url_image( + name=guid + '-image', image_metadata=self.image_metadata) net_config = openstack_tests.get_priv_net_config( net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet', router_name=guid + '-pub-router', external_net=self.ext_net_name) # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.flavor_creator = None self.network_creator = None @@ -402,31 +380,8 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): try: # Create Image - # Set any custom parameters sent from the app - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() # Create Flavor self.flavor_creator = OpenStackFlavor( @@ -467,11 +422,9 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -485,7 +438,7 @@ class CreateInstanceSimpleTests(OSIntegrationTestCase): port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings) + self.os_creds, instance_settings, self.image_creator.image_settings) vm_inst = self.inst_creator.create() self.assertEquals(1, len(nova_utils.get_servers_by_name(self.nova, instance_settings.name))) @@ -522,7 +475,7 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): self.floating_ip_name = guid + 'fip1' # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.network_creator = None self.router_creator = None self.flavor_creator = None @@ -532,35 +485,12 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): self.pub_net_config = openstack_tests.get_pub_net_config( net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet', router_name=guid + '-pub-router', external_net=self.ext_net_name) - self.os_image_settings = openstack_tests.cirros_url_image(name=guid + '-image') - + self.os_image_settings = openstack_tests.cirros_url_image( + name=guid + '-image', image_metadata=self.image_metadata) try: # Create Image - # Set any custom parameters sent from the app - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() # Create Network self.network_creator = OpenStackNetwork(self.os_creds, self.pub_net_config.network_settings) @@ -625,11 +555,9 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -652,7 +580,7 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): router_name=self.pub_net_config.router_settings.name)]) inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings, + self.os_creds, instance_settings, self.image_creator.image_settings, keypair_settings=self.keypair_creator.keypair_settings) self.inst_creators.append(inst_creator) vm_inst = inst_creator.create() @@ -675,7 +603,7 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): router_name=self.pub_net_config.router_settings.name)]) inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings, + self.os_creds, instance_settings, self.image_creator.image_settings, keypair_settings=self.keypair_creator.keypair_settings) self.inst_creators.append(inst_creator) vm_inst = inst_creator.create() @@ -700,7 +628,7 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): router_name=self.pub_net_config.router_settings.name)]) inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings, + self.os_creds, instance_settings, self.image_creator.image_settings, keypair_settings=self.keypair_creator.keypair_settings) self.inst_creators.append(inst_creator) @@ -713,90 +641,6 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): self.assertTrue(validate_ssh_client(inst_creator)) - # TODO - Determine how allowed_address_pairs is supposed to operate before continuing this test - # see http://docs.openstack.org/developer/dragonflow/specs/allowed_address_pairs.html for a functional description - # def test_allowed_address_port_access(self): - # """ - # Tests to ensure that setting allowed_address_pairs on a port functions as designed - # """ - # port_settings_1 = PortSettings( - # name=self.port_1_name + '-1', network_name=self.pub_net_config.network_settings.name) - # - # instance_settings_1 = VmInstanceSettings( - # name=self.vm_inst_name + '-1', flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings_1], - # floating_ip_settings=[FloatingIpSettings( - # name=self.floating_ip_name + '-1', port_name=port_settings_1.name, - # router_name=self.pub_net_config.router_settings.name)]) - # - # inst_creator_1 = OpenStackVmInstance( - # self.os_creds, instance_settings_1, self.image_creators[-1].image_settings, - # keypair_settings=self.keypair_creator.keypair_settings) - # self.inst_creators.append(inst_creator_1) - # - # # block=True will force the create() method to block until the - # vm_inst_1 = inst_creator_1.create(block=True) - # self.assertIsNotNone(vm_inst_1) - # - # port_settings_1 = PortSettings( - # name=self.port_1_name + '-1', network_name=self.pub_net_config.network_settings.name) - # - # instance_settings_1 = VmInstanceSettings( - # name=self.vm_inst_name + '-1', flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings_1], - # floating_ip_settings=[FloatingIpSettings( - # name=self.floating_ip_name + '-1', port_name=port_settings_1.name, - # router_name=self.pub_net_config.router_settings.name)]) - # - # inst_creator_1 = OpenStackVmInstance( - # self.os_creds, instance_settings_1, self.image_creators[-1].image_settings, - # keypair_settings=self.keypair_creator.keypair_settings) - # self.inst_creators.append(inst_creator_1) - # inst_creator_1.create(block=True) - # - # ip = inst_creator_1.get_port_ip(port_settings_1.name, - # subnet_name=self.pub_net_config.network_settings.subnet_settings[0].name) - # self.assertIsNotNone(ip) - # mac_addr = inst_creator_1.get_port_mac(port_settings_1.name) - # self.assertIsNotNone(mac_addr) - # - # allowed_address_pairs = [{'ip_address': ip, 'mac_address': mac_addr}] - # - # # Create VM that can be accessed by vm_inst_1 - # port_settings_2 = PortSettings( - # name=self.port_1_name + '-2', network_name=self.pub_net_config.network_settings.name, - # allowed_address_pairs=allowed_address_pairs) - # - # instance_settings_2 = VmInstanceSettings( - # name=self.vm_inst_name + '-2', flavor=self.flavor_creator.flavor_settings.name, - # port_settings=[port_settings_2]) - # - # inst_creator_2 = OpenStackVmInstance( - # self.os_creds, instance_settings_2, self.image_creators[-1].image_settings) - # self.inst_creators.append(inst_creator_2) - # inst_creator_2.create(block=True) - # - # # Create VM that cannot be accessed by vm_inst_1 - # ip = '10.55.0.101' - # mac_addr = '0a:1b:2c:3d:4e:5f' - # invalid_address_pairs = [{'ip_address': ip, 'mac_address': mac_addr}] - # - # port_settings_3 = PortSettings( - # name=self.port_1_name + '-3', network_name=self.pub_net_config.network_settings.name, - # allowed_address_pairs=invalid_address_pairs) - # - # instance_settings_3 = VmInstanceSettings( - # name=self.vm_inst_name + '-3', flavor=self.flavor_creator.flavor_settings.name, - # port_settings=[port_settings_3]) - # - # inst_creator_3 = OpenStackVmInstance( - # self.os_creds, instance_settings_3, self.image_creators[-1].image_settings) - # self.inst_creators.append(inst_creator_3) - # inst_creator_3.create(block=True) - # - # print 'foo' - # I expected that this feature would block/allow traffic from specific endpoints (VMs). In this case, I would - # expect inst_1 to be able to access inst_2 but not inst_3; however, they all can access each other. - # TODO - Add validation - class CreateInstancePortManipulationTests(OSIntegrationTestCase): """ @@ -817,7 +661,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): self.floating_ip_name = guid + 'fip1' # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.network_creator = None self.flavor_creator = None self.inst_creator = None @@ -825,35 +669,13 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): self.net_config = openstack_tests.get_priv_net_config( net_name=guid + '-pub-net', subnet_name=guid + '-pub-subnet', router_name=guid + '-pub-router', external_net=self.ext_net_name) - self.os_image_settings = openstack_tests.cirros_url_image(name=guid + '-image') + self.os_image_settings = openstack_tests.cirros_url_image( + name=guid + '-image', image_metadata=self.image_metadata) try: # Create Image - # Set any custom parameters sent from the app - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() # Create Network self.network_creator = OpenStackNetwork(self.os_creds, self.net_config.network_settings) @@ -890,11 +712,9 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -913,7 +733,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) self.inst_creator.create(block=True) self.assertEquals(ip, self.inst_creator.get_port_ip( @@ -932,7 +752,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) with self.assertRaises(Exception): self.inst_creator.create() @@ -949,7 +769,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) self.inst_creator.create(block=True) self.assertEquals(mac_addr, self.inst_creator.get_port_mac(self.port_1_name)) @@ -966,7 +786,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings) + self.os_creds, instance_settings, self.image_creator.image_settings) with self.assertRaises(Exception): self.inst_creator.create() @@ -985,7 +805,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) self.inst_creator.create(block=True) self.assertEquals(ip, self.inst_creator.get_port_ip( @@ -1006,7 +826,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) self.inst_creator.create(block=True) port = self.inst_creator.get_port_by_name(port_settings.name) @@ -1032,7 +852,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) with self.assertRaises(Exception): self.inst_creator.create() @@ -1053,7 +873,7 @@ class CreateInstancePortManipulationTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) with self.assertRaises(Exception): self.inst_creator.create() @@ -1075,7 +895,7 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): self.port_base_name = guid + 'port' # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.flavor_creator = None self.network_creator = None self.inst_creators = list() @@ -1083,7 +903,8 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): self.priv_net_config = openstack_tests.get_priv_net_config( net_name=guid + '-priv-net', subnet_name=guid + '-priv-subnet') - self.os_image_settings = openstack_tests.cirros_url_image(name=guid + '-image') + self.os_image_settings = openstack_tests.cirros_url_image( + name=guid + '-image', image_metadata=self.image_metadata) try: # Create Network @@ -1097,31 +918,8 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): self.flavor_creator.create() # Create Image - # Set any custom parameters sent from the app - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() except Exception as e: self.tearDown() @@ -1149,11 +947,9 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -1179,7 +975,7 @@ class CreateInstanceOnComputeHost(OSIntegrationTestCase): name=inst_name, flavor=self.flavor_creator.flavor_settings.name, availability_zone=zone, port_settings=[port_settings]) inst_creator = OpenStackVmInstance( - self.admin_os_creds, instance_settings, self.image_creators[-1].image_settings) + self.admin_os_creds, instance_settings, self.image_creator.image_settings) self.inst_creators.append(inst_creator) inst_creator.create() @@ -1209,7 +1005,7 @@ class CreateInstancePubPrivNetTests(OSIntegrationTestCase): super(self.__class__, self).__start__() # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.network_creators = list() self.router_creators = list() self.flavor_creator = None @@ -1230,36 +1026,14 @@ class CreateInstancePubPrivNetTests(OSIntegrationTestCase): self.pub_net_config = openstack_tests.get_pub_net_config( net_name=self.guid + '-pub-net', subnet_name=self.guid + '-pub-subnet', router_name=self.guid + '-pub-router', external_net=self.ext_net_name) + image_name = self.__class__.__name__ + '-' + str(uuid.uuid4()) - self.os_image_settings = openstack_tests.centos_url_image(name=image_name) + self.os_image_settings = openstack_tests.centos_url_image(name=image_name, image_metadata=self.image_metadata) try: # Create Image - # Set any custom parameters sent from the app - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() # First network is public self.network_creators.append(OpenStackNetwork(self.os_creds, self.pub_net_config.network_settings)) @@ -1332,11 +1106,9 @@ class CreateInstancePubPrivNetTests(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -1368,7 +1140,7 @@ class CreateInstancePubPrivNetTests(OSIntegrationTestCase): router_name=self.pub_net_config.router_settings.name)]) self.inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings, + self.os_creds, instance_settings, self.image_creator.image_settings, keypair_settings=self.keypair_creator.keypair_settings) vm_inst = self.inst_creator.create(block=True) @@ -1403,7 +1175,8 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4()) self.vm_inst_name = self.guid + '-inst' self.nova = nova_utils.nova_client(self.os_creds) - self.os_image_settings = openstack_tests.cirros_url_image(name=self.guid + '-image') + self.os_image_settings = openstack_tests.cirros_url_image( + name=self.guid + '-image', image_metadata=self.image_metadata) self.vm_inst_name = self.guid + '-inst' self.port_1_name = self.guid + 'port-1' @@ -1415,7 +1188,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): router_name=self.guid + '-pub-router', external_net=self.ext_net_name) # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.flavor_creator = None self.network_creator = None self.router_creator = None @@ -1424,31 +1197,8 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): try: # Create Image - # Set any custom parameters sent from the app - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - self.os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - self.os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(OpenStackImage(self.os_creds, self.os_image_settings)) - self.image_creators[-1].create() + self.image_creator = OpenStackImage(self.os_creds, self.os_image_settings) + self.image_creator.create() # Create Network self.network_creator = OpenStackNetwork(self.os_creds, net_config.network_settings) @@ -1495,11 +1245,9 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) @@ -1513,7 +1261,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): instance_settings = VmInstanceSettings( name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) vm_inst = self.inst_creator.create(block=True) self.assertIsNotNone(vm_inst) @@ -1540,7 +1288,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): instance_settings = VmInstanceSettings( name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) vm_inst = self.inst_creator.create(block=True) self.assertIsNotNone(vm_inst) @@ -1575,7 +1323,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, security_group_names=[sec_grp_settings.name], port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) vm_inst = self.inst_creator.create(block=True) self.assertIsNotNone(vm_inst) @@ -1602,7 +1350,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): instance_settings = VmInstanceSettings( name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) vm_inst = self.inst_creator.create(block=True) self.assertIsNotNone(vm_inst) @@ -1630,7 +1378,7 @@ class InstanceSecurityGroupTests(OSIntegrationTestCase): name=self.vm_inst_name, flavor=self.flavor_creator.flavor_settings.name, security_group_names=[sec_grp_settings.name], port_settings=[self.port_settings]) self.inst_creator = OpenStackVmInstance(self.os_creds, instance_settings, - self.image_creators[-1].image_settings) + self.image_creator.image_settings) vm_inst = self.inst_creator.create(block=True) self.assertIsNotNone(vm_inst) @@ -1707,58 +1455,20 @@ class CreateInstanceFromThreePartImage(OSIntegrationTestCase): router_name=guid + '-pub-router', external_net=self.ext_net_name) # Initialize for tearDown() - self.image_creators = list() + self.image_creator = None self.network_creator = None self.flavor_creator = None self.inst_creator = None try: - # Create Images - # Set properties - properties = {} - if self.image_metadata: - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - properties = self.image_metadata['extra_properties'] - - # Create the kernel image - kernel_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_kernel', - url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel') - - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings.url = self.image_metadata['kernel_url'] - - self.image_creators.append(OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - - # Create the ramdisk image - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=self.image_name+'_ramdisk', - url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs') - - if self.image_metadata: - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings.url = self.image_metadata['ramdisk_url'] - - self.image_creators.append(OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - self.assertIsNotNone(ramdisk_image) - - # Create the main image - os_image_settings = openstack_tests.cirros_url_image( + image_settings = openstack_tests.cirros_url_image( name=self.image_name, - url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img') - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] - - properties['kernel_id'] = kernel_image.id - properties['ramdisk_id'] = ramdisk_image.id - os_image_settings.extra_properties = properties - self.image_creators.append(OpenStackImage(self.os_creds, os_image_settings)) - created_image = self.image_creators[-1].create() - self.assertIsNotNone(created_image) + image_metadata={'disk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img', + 'kernel_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel', + 'ramdisk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'}) + + self.image_creator = OpenStackImage(self.os_creds, image_settings) + self.image_creator.create() # Create Flavor self.flavor_creator = OpenStackFlavor( @@ -1798,17 +1508,15 @@ class CreateInstanceFromThreePartImage(OSIntegrationTestCase): except Exception as e: logger.error('Unexpected exception cleaning network with message - ' + e.message) - if self.image_creators: + if self.image_creator: try: - while self.image_creators: - self.image_creators[0].clean() - self.image_creators.pop(0) + self.image_creator.clean() except Exception as e: logger.error('Unexpected exception cleaning image with message - ' + e.message) super(self.__class__, self).__clean__() - def test_create_delete_instance_from_three_part_image(self): + def test_create_instance_from_three_part_image(self): """ Tests the creation of an OpenStack instance from a 3-part image. """ @@ -1817,18 +1525,8 @@ class CreateInstanceFromThreePartImage(OSIntegrationTestCase): # The last created image is the main image from which we create the instance self.inst_creator = OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings) + self.os_creds, instance_settings, self.image_creator.image_settings) vm_inst = self.inst_creator.create() - self.assertEquals(1, len(nova_utils.get_servers_by_name(self.nova, instance_settings.name))) - - self.assertTrue(self.inst_creator.vm_active(block=True)) - - # Delete instance - nova_utils.delete_vm_instance(self.nova, vm_inst) - - self.assertTrue(self.inst_creator.vm_deleted(block=True)) - self.assertEquals(0, len(nova_utils.get_servers_by_name(self.nova, instance_settings.name))) - - # Exception should not be thrown - self.inst_creator.clean() + self.assertIsNotNone(vm_inst) + self.assertTrue(self.inst_creator.vm_active(block=True))
\ No newline at end of file diff --git a/snaps/openstack/tests/openstack_tests.py b/snaps/openstack/tests/openstack_tests.py index c4becc8..81e49ee 100644 --- a/snaps/openstack/tests/openstack_tests.py +++ b/snaps/openstack/tests/openstack_tests.py @@ -97,31 +97,101 @@ def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=Non return os_creds -def cirros_url_image(name, url=None): +def cirros_url_image(name, url=None, image_metadata=None, kernel_settings=None, ramdisk_settings=None): + if image_metadata and 'disk_url' in image_metadata: + url = image_metadata['disk_url'] if not url: url = 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img' - return ImageSettings(name=name, image_user='cirros', img_format='qcow2', url=url) + if image_metadata and 'kernel_url' in image_metadata and kernel_settings is None: + kernel_image_settings = ImageSettings(name=name + '-kernel', image_user='cirros', img_format='qcow2', + url=image_metadata['kernel_url']) + else: + kernel_image_settings = kernel_settings + + if image_metadata and 'ramdisk_url' in image_metadata and ramdisk_settings is None: + ramdisk_image_settings = ImageSettings(name=name + '-ramdisk', image_user='cirros', img_format='qcow2', + url=image_metadata['ramdisk_url']) + else: + ramdisk_image_settings = ramdisk_settings + + extra_properties = None + if image_metadata and 'extra_properties' in image_metadata: + extra_properties = image_metadata['extra_properties'] + + return ImageSettings(name=name, image_user='cirros', img_format='qcow2', url=url, + extra_properties=extra_properties, + kernel_image_settings=kernel_image_settings, + ramdisk_image_settings=ramdisk_image_settings) + + +def file_image_test_settings(name, file_path, image_user='cirros', image_metadata=None): + kernel_image_settings = None + if image_metadata and 'kernel_url' in image_metadata: + kernel_image_settings = ImageSettings(name=name + '-kernel', url=image_metadata['kernel_url']) + ramdisk_image_settings = None + if image_metadata and 'ramdisk_url' in image_metadata: + ramdisk_image_settings = ImageSettings(name=name + '-ramdisk', url=image_metadata['ramdisk_url']) -def file_image_test_settings(name, file_path): - return ImageSettings(name=name, image_user='cirros', img_format='qcow2', - image_file=file_path) + extra_properties = None + if image_metadata and 'extra_properties' in image_metadata: + extra_properties = image_metadata['extra_properties'] + return ImageSettings(name=name, image_user=image_user, img_format='qcow2', + extra_properties=extra_properties, + image_file=file_path, + kernel_image_settings=kernel_image_settings, + ramdisk_image_settings=ramdisk_image_settings) -def centos_url_image(name, url=None): + +def centos_url_image(name, url=None, image_metadata=None): + if image_metadata and 'disk_url' in image_metadata: + url = image_metadata['disk_url'] if not url: url = 'http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2' + + kernel_image_settings = None + if image_metadata and 'kernel_url' in image_metadata: + kernel_image_settings = ImageSettings(name=name + '-kernel', url=image_metadata['kernel_url']) + ramdisk_image_settings = None + if image_metadata and 'ramdisk_url' in image_metadata: + ramdisk_image_settings = ImageSettings(name=name + '-ramdisk', url=image_metadata['ramdisk_url']) + + extra_properties = None + if image_metadata and 'extra_properties' in image_metadata: + extra_properties = image_metadata['extra_properties'] + return ImageSettings( - name=name, image_user='centos', img_format='qcow2', url=url, - nic_config_pb_loc='./provisioning/ansible/centos-network-setup/playbooks/configure_host.yml') + name=name, image_user='centos', img_format='qcow2', + extra_properties=extra_properties, url=url, + nic_config_pb_loc='./provisioning/ansible/centos-network-setup/playbooks/configure_host.yml', + kernel_image_settings=kernel_image_settings, + ramdisk_image_settings=ramdisk_image_settings) -def ubuntu_url_image(name, url=None): +def ubuntu_url_image(name, url=None, image_metadata=None): + if image_metadata and 'disk_url' in image_metadata: + url = image_metadata['disk_url'] if not url: url = 'http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img' + + kernel_image_settings = None + if image_metadata and 'kernel_url' in image_metadata: + kernel_image_settings = ImageSettings(name=name + '-kernel', url=image_metadata['kernel_url']) + ramdisk_image_settings = None + if image_metadata and 'ramdisk_url' in image_metadata: + ramdisk_image_settings = ImageSettings(name=name + '-ramdisk', url=image_metadata['ramdisk_url']) + + extra_properties = None + if image_metadata and 'extra_properties' in image_metadata: + extra_properties = image_metadata['extra_properties'] + return ImageSettings( - name=name, image_user='ubuntu', img_format='qcow2', url=url, - nic_config_pb_loc='./provisioning/ansible/ubuntu-network-setup/playbooks/configure_host.yml') + name=name, image_user='ubuntu', img_format='qcow2', + extra_properties=extra_properties, url=url, + nic_config_pb_loc='./provisioning/ansible/ubuntu-network-setup/playbooks/configure_host.yml', + kernel_image_settings=kernel_image_settings, + ramdisk_image_settings=ramdisk_image_settings) def get_priv_net_config(net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None): diff --git a/snaps/openstack/tests/os_source_file_test.py b/snaps/openstack/tests/os_source_file_test.py index 0c9f1b1..af75381 100644 --- a/snaps/openstack/tests/os_source_file_test.py +++ b/snaps/openstack/tests/os_source_file_test.py @@ -12,15 +12,14 @@ # 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 logging import uuid +import unittest from snaps import file_utils -import openstack_tests -import logging - from snaps.openstack.create_project import ProjectSettings from snaps.openstack.create_user import UserSettings +from snaps.openstack.tests import openstack_tests from snaps.openstack.utils import deploy_utils, keystone_utils dev_os_env_file = 'openstack/tests/conf/os_env.yaml' @@ -78,8 +77,12 @@ class OSIntegrationTestCase(OSComponentTestCase): :param os_creds: the OSCreds object, when null it searches for the file {cwd}/openstack/tests/conf/os_env.yaml :param ext_net_name: the name of the external network that is used for creating routers for floating IPs :param use_keystone: when true, these tests will create a new user/project under which to run the test + :param image_metadata: dict() containing the URLs for the disk, kernel, and ramdisk images when multi-part + images are required. + image_metadata={'disk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img', + 'kernel_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel', + 'ramdisk_url': 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'}) :param flavor_metadata: dict() to be sent directly into the Nova client generally used for page sizes - :param image_metadata: dict() to be sent directly into the Nova client generally used for multi-part images :param log_level: the logging level of your test run (default DEBUG) """ super(OSIntegrationTestCase, self).__init__(method_name=method_name, os_creds=os_creds, diff --git a/snaps/provisioning/tests/ansible_utils_tests.py b/snaps/provisioning/tests/ansible_utils_tests.py index a26c497..4cd17c7 100644 --- a/snaps/provisioning/tests/ansible_utils_tests.py +++ b/snaps/provisioning/tests/ansible_utils_tests.py @@ -15,18 +15,17 @@ import os import uuid +from scp import SCPClient +from snaps.openstack import create_flavor from snaps.openstack import create_instance +from snaps.openstack import create_image from snaps.openstack import create_keypairs from snaps.openstack import create_network from snaps.openstack import create_router -from snaps.openstack import create_image -from snaps.openstack import create_flavor -from scp import SCPClient - -from snaps.provisioning import ansible_utils from snaps.openstack.tests import openstack_tests from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase +from snaps.provisioning import ansible_utils VM_BOOT_TIMEOUT = 600 @@ -62,35 +61,14 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): self.flavor_creator = None self.router_creator = None self.network_creator = None - self.image_creators = list() + self.image_creator = None try: # Create Image - os_image_settings = openstack_tests.ubuntu_url_image(name=guid + '-' + '-image') - if self.image_metadata: - if 'disk_url' in self.image_metadata and self.image_metadata['disk_url']: - os_image_settings.url = self.image_metadata['disk_url'] - if 'extra_properties' in self.image_metadata and self.image_metadata['extra_properties']: - os_image_settings.extra_properties = self.image_metadata['extra_properties'] - - # If this is a 3-part image create the kernel and ramdisk images first - if self.image_metadata: - if 'kernel_url' in self.image_metadata and self.image_metadata['kernel_url']: - kernel_image_settings = openstack_tests.cirros_url_image( - name=os_image_settings.name+'_kernel', url=self.image_metadata['kernel_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings)) - kernel_image = self.image_creators[-1].create() - os_image_settings.extra_properties['kernel_id'] = kernel_image.id - - if 'ramdisk_url' in self.image_metadata and self.image_metadata['ramdisk_url']: - ramdisk_image_settings = openstack_tests.cirros_url_image( - name=os_image_settings.name+'_ramdisk', url=self.image_metadata['ramdisk_url']) - self.image_creators.append(create_image.OpenStackImage(self.os_creds, ramdisk_image_settings)) - ramdisk_image = self.image_creators[-1].create() - os_image_settings.extra_properties['ramdisk_id'] = ramdisk_image.id - - self.image_creators.append(create_image.OpenStackImage(self.os_creds, os_image_settings)) - self.image_creators[-1].create() + os_image_settings = openstack_tests.ubuntu_url_image(name=guid + '-' + '-image', + image_metadata=self.image_metadata) + self.image_creator = create_image.OpenStackImage(self.os_creds, os_image_settings) + self.image_creator.create() # First network is public self.pub_net_config = openstack_tests.get_pub_net_config( @@ -131,7 +109,7 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): router_name=self.pub_net_config.router_settings.name)]) self.inst_creator = create_instance.OpenStackVmInstance( - self.os_creds, instance_settings, self.image_creators[-1].image_settings, + self.os_creds, instance_settings, self.image_creator.image_settings, keypair_settings=self.keypair_creator.keypair_settings) except Exception as e: self.tearDown() @@ -162,10 +140,8 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): if self.network_creator: self.network_creator.clean() - if self.image_creators: - while self.image_creators: - self.image_creators[-1].clean() - self.image_creators.pop() + if self.image_creator: + self.image_creator.clean() if os.path.isfile(self.test_file_local_path): os.remove(self.test_file_local_path) |