Add support for extra properties in an image
[snaps.git] / snaps / openstack / tests / create_image_tests.py
index 24bf0f2..753e83f 100644 (file)
@@ -85,6 +85,19 @@ class ImageSettingsUnitTests(unittest.TestCase):
         self.assertIsNone(settings.image_file)
         self.assertIsNone(settings.nic_config_pb_loc)
 
+    def test_name_user_format_url_only_properties(self):
+        properties = {}
+        properties['hw_video_model'] = 'vga'
+        settings = ImageSettings(name='foo', image_user='bar', img_format='qcow2', url='http://foo.com', extra_properties=properties)
+        self.assertEquals('foo', settings.name)
+        self.assertEquals('bar', settings.image_user)
+        self.assertEquals('qcow2', settings.format)
+        self.assertEquals('http://foo.com', settings.url)
+        self.assertEquals(properties, settings.extra_properties)
+        self.assertIsNone(settings.image_file)
+        self.assertIsNone(settings.nic_config_pb_loc)
+
+
     def test_config_with_name_user_format_url_only(self):
         settings = ImageSettings(config={'name': 'foo', 'image_user': 'bar', 'format': 'qcow2',
                                          'download_url': 'http://foo.com'})
@@ -115,43 +128,55 @@ class ImageSettingsUnitTests(unittest.TestCase):
         self.assertIsNone(settings.nic_config_pb_loc)
 
     def test_all_url(self):
+        properties = {}
+        properties['hw_video_model'] = 'vga'
         settings = ImageSettings(name='foo', image_user='bar', img_format='qcow2', url='http://foo.com',
-                                 nic_config_pb_loc='/foo/bar')
+                                 extra_properties=properties, nic_config_pb_loc='/foo/bar')
         self.assertEquals('foo', settings.name)
         self.assertEquals('bar', settings.image_user)
         self.assertEquals('qcow2', settings.format)
         self.assertEquals('http://foo.com', settings.url)
+        self.assertEquals(properties, settings.extra_properties)
         self.assertIsNone(settings.image_file)
         self.assertEquals('/foo/bar', settings.nic_config_pb_loc)
 
     def test_config_all_url(self):
         settings = ImageSettings(config={'name': 'foo', 'image_user': 'bar', 'format': 'qcow2',
-                                         'download_url': 'http://foo.com', 'nic_config_pb_loc': '/foo/bar'})
+                                         'download_url': 'http://foo.com',
+                                         'extra_properties' : '{\'hw_video_model\': \'vga\'}',
+                                         'nic_config_pb_loc': '/foo/bar'})
         self.assertEquals('foo', settings.name)
         self.assertEquals('bar', settings.image_user)
         self.assertEquals('qcow2', settings.format)
         self.assertEquals('http://foo.com', settings.url)
+        self.assertEquals('{\'hw_video_model\': \'vga\'}', settings.extra_properties)
         self.assertIsNone(settings.image_file)
         self.assertEquals('/foo/bar', settings.nic_config_pb_loc)
 
     def test_all_file(self):
+        properties = {}
+        properties['hw_video_model'] = 'vga'
         settings = ImageSettings(name='foo', image_user='bar', img_format='qcow2', image_file='/foo/bar.qcow',
-                                 nic_config_pb_loc='/foo/bar')
+                                 extra_properties=properties, nic_config_pb_loc='/foo/bar')
         self.assertEquals('foo', settings.name)
         self.assertEquals('bar', settings.image_user)
         self.assertEquals('qcow2', settings.format)
         self.assertIsNone(settings.url)
         self.assertEquals('/foo/bar.qcow', settings.image_file)
+        self.assertEquals(properties, settings.extra_properties)
         self.assertEquals('/foo/bar', settings.nic_config_pb_loc)
 
     def test_config_all_file(self):
         settings = ImageSettings(config={'name': 'foo', 'image_user': 'bar', 'format': 'qcow2',
-                                         'image_file': '/foo/bar.qcow', 'nic_config_pb_loc': '/foo/bar'})
+                                         'image_file': '/foo/bar.qcow',
+                                         'extra_properties' : '{\'hw_video_model\' : \'vga\'}',
+                                         'nic_config_pb_loc': '/foo/bar'})
         self.assertEquals('foo', settings.name)
         self.assertEquals('bar', settings.image_user)
         self.assertEquals('qcow2', settings.format)
         self.assertIsNone(settings.url)
         self.assertEquals('/foo/bar.qcow', settings.image_file)
+        self.assertEquals('{\'hw_video_model\' : \'vga\'}', settings.extra_properties)
         self.assertEquals('/foo/bar', settings.nic_config_pb_loc)
 
 
@@ -206,6 +231,29 @@ class CreateImageSuccessTests(OSIntegrationTestCase):
         self.assertEquals(created_image.name, retrieved_image.name)
         self.assertEquals(created_image.id, retrieved_image.id)
 
+    def test_create_image_clean_url_properties(self):
+        """
+        Tests the creation of an OpenStack image from a URL and set properties.
+        """
+        # Set properties
+        properties = {}
+        properties['hw_video_model'] = 'vga'
+
+        # Create Image
+        os_image_settings = openstack_tests.cirros_url_image(name=self.image_name)
+        os_image_settings.extra_properties = properties
+        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.nova, self.glance, os_image_settings.name)
+        self.assertIsNotNone(retrieved_image)
+
+        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_image_clean_file(self):
         """
         Tests the creation of an OpenStack image from a file.
@@ -360,3 +408,126 @@ class CreateImageNegativeTests(OSIntegrationTestCase):
                                        self.os_creds.project_name,
                                        proxy_settings=self.os_creds.proxy_settings),
                 os_image_settings)
+
+
+class CreateMultiPartImageTests(OSIntegrationTestCase):
+    """
+    Test for creating a 3-part image
+    """
+    def setUp(self):
+        """
+        Instantiates the CreateImage object that is responsible for
+        downloading and creating an OS image file within OpenStack
+        """
+        super(self.__class__, self).__start__()
+
+        guid = uuid.uuid4()
+        self.image_creators = list()
+        self.image_name = self.__class__.__name__ + '-' + str(guid)
+
+        self.nova = nova_utils.nova_client(self.os_creds)
+        self.glance = glance_utils.glance_client(self.os_creds)
+
+        self.tmp_dir = 'tmp/' + str(guid)
+        if not os.path.exists(self.tmp_dir):
+            os.makedirs(self.tmp_dir)
+
+    def tearDown(self):
+        """
+        Cleans the images and downloaded image file
+        """
+        while self.image_creators:
+            self.image_creators[0].clean()
+            self.image_creators.pop(0)
+
+        if os.path.exists(self.tmp_dir) and os.path.isdir(self.tmp_dir):
+            shutil.rmtree(self.tmp_dir)
+
+        super(self.__class__, self).__clean__()
+
+    def test_create_three_part_image_from_url(self):
+        """
+        Tests the creation of a 3-part OpenStack image from a URL.
+        """
+        # Set properties
+        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')
+        self.image_creators.append(create_image.OpenStackImage(self.os_creds, kernel_image_settings))
+        kernel_image = self.image_creators[-1].create()
+        self.assertIsNotNone(kernel_image)
+
+        # 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')
+        self.image_creators.append(create_image.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(name=self.image_name,
+            url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img')
+        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.nova, self.glance, os_image_settings.name)
+        self.assertIsNotNone(retrieved_image)
+
+        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_file(self):
+        """
+        Tests the creation of a 3-part OpenStack image from files.
+        """
+        # Set properties
+        properties = {}
+
+        # Create the kernel image
+        url_image_settings = openstack_tests.cirros_url_image('foo_kernel',
+            url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel')
+        kernel_image_file = file_utils.download(url_image_settings.url, self.tmp_dir)
+        kernel_file_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_file_image_settings))
+        kernel_image = self.image_creators[-1].create()
+        self.assertIsNotNone(kernel_image)
+
+        # Create the ramdisk image
+        url_image_settings = openstack_tests.cirros_url_image('foo_ramdisk',
+            url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs')
+        ramdisk_image_file = file_utils.download(url_image_settings.url, self.tmp_dir)
+        ramdisk_file_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_file_image_settings))
+        ramdisk_image = self.image_creators[-1].create()
+        self.assertIsNotNone(ramdisk_image)
+
+        # Create the main image
+        url_image_settings = openstack_tests.cirros_url_image('foo',
+            url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img')
+        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)
+        properties['kernel_id'] = kernel_image.id
+        properties['ramdisk_id'] = ramdisk_image.id
+        file_image_settings.extra_properties = properties
+        self.image_creators.append(create_image.OpenStackImage(self.os_creds, file_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.nova, self.glance, file_image_settings.name)
+        self.assertIsNotNone(retrieved_image)
+
+        self.assertEquals(created_image.name, retrieved_image.name)
+        self.assertEquals(created_image.id, retrieved_image.id)
+        self.assertEquals(created_image.properties, retrieved_image.properties)