Example #1
0
def add_image(image_url, glance_client=None, image_name=None, tags=[],
              properties=None):
    """Retrieve image from ``image_url`` and add it to glance.

    :param image_url: Retrievable URL with image data
    :type image_url: str
    :param glance: Authenticated glanceclient
    :type glance: glanceclient.Client
    :param image_name: Label for the image in glance
    :type image_name: str
    :param tags: List of tags to add to image
    :type tags: list of str
    :param properties: Properties to add to image
    :type properties: dict
    """
    if not glance_client:
        keystone_session = openstack_utils.get_overcloud_keystone_session()
        glance_client = openstack_utils.get_glance_session_client(
            keystone_session)
    if image_name:
        image = openstack_utils.get_images_by_name(
            glance_client, image_name)

    if image:
        logging.warning('Using existing glance image "{}" ({})'
                        .format(image_name, image[0].id))
    else:
        logging.info('Downloading image {}'.format(image_name or image_url))
        openstack_utils.create_image(
            glance_client,
            image_url,
            image_name,
            tags=tags,
            properties=properties)
Example #2
0
    def test_100_create_image(self):
        """Create an image and do a simple validation of it.

        The OpenStack Swift API is used to do the validation, since the Ceph
        Rados Gateway serves an API which is compatible with that.
        """
        image_name = 'zaza-ceph-rgw-image'
        openstack_utils.create_image(
            glance=self.glance_client,
            image_url=openstack_utils.find_cirros_image(arch='x86_64'),
            image_name=image_name,
            backend='swift')
        headers, containers = self.swift.get_account()
        self.assertEqual(len(containers), 1)
        container_name = containers[0].get('name')
        headers, objects = self.swift.get_container(container_name)
        images = openstack_utils.get_images_by_name(self.glance_client,
                                                    image_name)
        self.assertEqual(len(images), 1)
        image = images[0]
        total_bytes = 0
        for ob in objects:
            if '{}-'.format(image['id']) in ob['name']:
                total_bytes = total_bytes + int(ob['bytes'])
        logging.info('Checking glance image size {} matches swift '
                     'image size {}'.format(image['size'], total_bytes))
        self.assertEqual(image['size'], total_bytes)
        openstack_utils.delete_image(self.glance_client, image['id'])
Example #3
0
    def test_100_create_delete_image(self):
        """Create an image and do a simple validation of it.

        Validate the size of the image in both Glance API and actual S3 bucket.
        """
        image_name = "zaza-s3-test-image"
        openstack_utils.create_image(
            glance=self.glance_client,
            image_url=openstack_utils.find_cirros_image(arch="x86_64"),
            image_name=image_name,
            backend="s3",
        )
        images = openstack_utils.get_images_by_name(self.glance_client,
                                                    image_name)
        self.assertEqual(len(images), 1)
        image = images[0]

        s3_client = boto3.client(
            "s3",
            endpoint_url=self.s3_store_host,
            aws_access_key_id=self.s3_store_access_key,
            aws_secret_access_key=self.s3_store_secret_key,
        )
        response = s3_client.head_object(Bucket=self.s3_store_bucket,
                                         Key=image["id"])
        logging.info(
            "Checking glance image size {} matches S3 object's ContentLength "
            "{}".format(image["size"], response["ContentLength"]))
        self.assertEqual(image["size"], response["ContentLength"])
        openstack_utils.delete_image(self.glance_client, image["id"])
Example #4
0
    def test_100_create_delete_image(self):
        """Create an image and do a simple validation of it.

        Validate the size of the image in both Glance API and Cinder API.
        """
        image_name = "zaza-cinder-test-image"
        openstack_utils.create_image(
            glance=self.glance_client,
            image_url=openstack_utils.find_cirros_image(arch="x86_64"),
            image_name=image_name,
            backend="cinder",
        )
        images = openstack_utils.get_images_by_name(self.glance_client,
                                                    image_name)
        self.assertEqual(len(images), 1)
        image = images[0]

        volume_name = 'image-' + image["id"]
        volumes = openstack_utils.get_volumes_by_name(self.cinder_client,
                                                      volume_name)
        self.assertEqual(len(volumes), 1)
        volume = volumes[0]

        logging.info("Checking glance image size {} matches volume size {} "
                     "GB".format(image["size"], volume.size))
        image_size_in_gb = int(math.ceil(float(image["size"]) / 1024**3))
        self.assertEqual(image_size_in_gb, volume.size)
        openstack_utils.delete_image(self.glance_client, image["id"])
Example #5
0
 def test_create_image_use_tempdir(self):
     glance_mock = mock.MagicMock()
     self.patch_object(openstack_utils.os.path, "exists")
     self.patch_object(openstack_utils, "download_image")
     self.patch_object(openstack_utils, "upload_image_to_glance")
     self.patch_object(openstack_utils.tempfile, "gettempdir")
     self.gettempdir.return_value = "wibbly"
     openstack_utils.create_image(glance_mock, 'http://cirros/c.img', 'bob')
     self.exists.return_value = False
     self.download_image.assert_called_once_with('http://cirros/c.img',
                                                 'wibbly/c.img')
     self.upload_image_to_glance.assert_called_once_with(
         glance_mock, 'wibbly/c.img', 'bob')
Example #6
0
def add_image(image_url,
              glance_client=None,
              image_name=None,
              tags=[],
              properties=None,
              backend=None,
              disk_format='qcow2',
              visibility='public',
              container_format='bare'):
    """Retrieve image from ``image_url`` and add it to glance.

    :param image_url: Retrievable URL with image data
    :type image_url: str
    :param glance: Authenticated glanceclient
    :type glance: glanceclient.Client
    :param image_name: Label for the image in glance
    :type image_name: str
    :param tags: List of tags to add to image
    :type tags: list of str
    :param properties: Properties to add to image
    :type properties: dict
    """
    glance_client = glance_client or _get_default_glance_client()
    if backend is not None:
        stores = get_store_ids(glance_client)
        if backend not in stores:
            raise ValueError("Invalid backend: %(backend)s "
                             "(available: %(available)s)" % {
                                 "backend": backend,
                                 "available": ", ".join(stores)
                             })
    if image_name:
        image = openstack_utils.get_images_by_name(glance_client, image_name)

    if image:
        logging.warning('Using existing glance image "{}" ({})'.format(
            image_name, image[0].id))
    else:
        logging.info('Downloading image {}'.format(image_name or image_url))
        openstack_utils.create_image(glance_client,
                                     image_url,
                                     image_name,
                                     tags=tags,
                                     properties=properties,
                                     backend=backend,
                                     disk_format=disk_format,
                                     visibility=visibility,
                                     container_format=container_format)
Example #7
0
    def test_412_image_conversion(self):
        """Check image-conversion config.

        When image-conversion config is enabled glance will convert images
        to raw format, this is only performed for interoperable image import
        docs.openstack.org/glance/train/admin/interoperable-image-import.html
        image conversion is done at server-side for better image handling
        """
        current_release = openstack_utils.get_os_release()
        bionic_stein = openstack_utils.get_os_release('bionic_stein')
        if current_release < bionic_stein:
            self.skipTest('image-conversion config is supported since '
                          'bionic_stein or newer versions')

        with self.config_change({'image-conversion': 'false'},
                                {'image-conversion': 'true'}):
            image_url = openstack_utils.find_cirros_image(arch='x86_64')
            image = openstack_utils.create_image(self.glance_client,
                                                 image_url,
                                                 'cirros-test-import',
                                                 force_import=True)

            disk_format = self.glance_client.images.get(image.id).disk_format
            self.assertEqual('raw', disk_format)
Example #8
0
 def test_410_glance_image_create_delete(self):
     """Create an image and then delete it."""
     image_url = openstack_utils.find_cirros_image(arch='x86_64')
     image = openstack_utils.create_image(self.glance_client, image_url,
                                          'cirrosimage')
     openstack_utils.delete_image(self.glance_client, image.id)