Python oslo_utils.encodeutils.exception_to_unicode() Examples

The following are 30 code examples of oslo_utils.encodeutils.exception_to_unicode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_utils.encodeutils , or try the search function .
Example #1
Source File: tests_encodeutils.py    From oslo.utils with Apache License 2.0 6 votes vote down vote up
def test_unicode_only_exception(self):
        # Exception with a __unicode__() method and a __str__() which
        # raises an exception (similar to the Message class of oslo_i18n)
        class UnicodeOnlyException(Exception):
            def __init__(self, value):
                Exception.__init__(self)
                self.value = value

            def __unicode__(self):
                return self.value

            def __str__(self):
                raise UnicodeError("use unicode()")

        # __unicode__() returns unicode
        exc = UnicodeOnlyException(u'unicode \xe9\u20ac')
        self.assertEqual(encodeutils.exception_to_unicode(exc),
                         u'unicode \xe9\u20ac')

        # __unicode__() returns bytes
        exc = UnicodeOnlyException(b'utf-8 \xc3\xa9\xe2\x82\xac')
        self.assertEqual(encodeutils.exception_to_unicode(exc),
                         u'utf-8 \xe9\u20ac') 
Example #2
Source File: utils.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def alias_error_cleanup(indexes):
    """While trying to re-index, we ran into some error. In this case, the
       new index creation/alias updating is incorrect. We will need to clean
       up by rolling back all of the changes. ElasticSearch must stay
       uncluttered. We will delete the indexes explicitly here. ElasticSearch
       will implicitly take care of removing deleted indexes from the aliases.
    """

    es_engine = searchlight.elasticsearch.get_api()

    for index in indexes.values():
        try:
            es_engine.indices.delete(index=index, ignore=404)
        except Exception as e:
            msg = {'index': index}
            LOG.error("Index [%(index)s] clean-up failed." % msg)
            LOG.error(encodeutils.exception_to_unicode(e)) 
Example #3
Source File: utils.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def set_index_refresh_interval(index_name, refresh_interval):
    """Set refresh_interval of a given index, basically it is used in the
       reindexing phase. By setting refresh_interval to -1 we disable the
       refresh of offline index to gain a performance boost for the bulk
       updates. After reindexing is done, we will restore refresh_interval
       and put the index online.
    """

    es_engine = searchlight.elasticsearch.get_api()

    body = {
        'index': {
            'refresh_interval': refresh_interval
        }
    }

    try:
        es_engine.indices.put_settings(body, index_name)
    except Exception as e:
        LOG.error(encodeutils.exception_to_unicode(e))
        raise 
Example #4
Source File: utils.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def get_index_refresh_interval(index_name):
    """Get the refresh_interval of a given index, if refresh_interval isn't
       set, return default 1s.
    """

    es_engine = searchlight.elasticsearch.get_api()
    try:
        result = es_engine.indices.get_settings(index_name,
                                                'index.refresh_interval')
    except Exception as e:
        # If we cannot get index setting, something must be wrong,
        # no need to continue, log the error message and raise.
        LOG.error(encodeutils.exception_to_unicode(e))
        raise

    if result:
        return result[index_name]['settings']['index']['refresh_interval']
    else:
        return '1s' 
Example #5
Source File: search.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def facets(self, req, index_name=None, doc_type=None,
               all_projects=False, limit_terms=0, include_fields=True,
               exclude_options=False):
        try:
            search_repo = self.gateway.get_catalog_search_repo(req.context)
            return search_repo.facets(index_name, doc_type,
                                      all_projects, limit_terms,
                                      include_fields=include_fields,
                                      exclude_options=exclude_options)
        except exception.Forbidden as e:
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError() 
Example #6
Source File: basic_and_keystone_auth.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def _basic_authenticate(self, auth_info, req):
        try:
            project_domain_id, project_name, user_domain_id = \
                self._get_auth_params()
            auth = password.Password(auth_url=self.auth_url,
                                     username=auth_info.username,
                                     password=auth_info.password,
                                     user_domain_id=user_domain_id,
                                     project_domain_id=project_domain_id,
                                     project_name=project_name)
            sess = session.Session(auth=auth)
            token = sess.get_token()
            project_id = str(auth.get_project_id(sess))
            roles = str(auth.get_auth_ref(sess).role_names[0])
            self._set_req_headers(req, token, project_id, roles)
        except Exception as e:
            to_unicode = encodeutils.exception_to_unicode(e)
            message = 'Authorization exception: %s' % to_unicode
            self._unauthorized(message) 
Example #7
Source File: conductor_server.py    From tacker with Apache License 2.0 6 votes vote down vote up
def get_vnf_package_vnfd(self, context, vnf_package):
        csar_path = os.path.join(CONF.vnf_package.vnf_package_csar_path,
                                 vnf_package.id)
        if not os.path.isdir(csar_path):
            location = vnf_package.location_glance_store
            try:
                zip_path = glance_store.load_csar(vnf_package.id, location)
                csar_utils.extract_csar_zip_file(zip_path, csar_path)
            except (store_exceptions.GlanceStoreException) as e:
                exc_msg = encodeutils.exception_to_unicode(e)
                msg = (_("Exception raised from glance store can be "
                         "unrecoverable if it is not related to connection"
                         " error. Error: %s.") % exc_msg)
                raise exceptions.FailedToGetVnfdData(error=msg)
        try:
            return self._read_vnfd_files(csar_path)
        except Exception as e:
            exc_msg = encodeutils.exception_to_unicode(e)
            msg = (_("Exception raised while reading csar file"
                     " Error: %s.") % exc_msg)
            raise exceptions.FailedToGetVnfdData(error=msg) 
Example #8
Source File: driver.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def configure(self, re_raise_bsc=False):
        """
        Configure the store to use the stored configuration options
        and initialize capabilities based on current configuration.

        Any store that needs special configuration should implement
        this method.
        """

        try:
            self.configure_add()
        except exceptions.BadStoreConfiguration as e:
            self.unset_capabilities(capabilities.BitMasks.WRITE_ACCESS)
            msg = (_(u"Failed to configure store correctly: %s "
                     "Disabling add method.")
                   % encodeutils.exception_to_unicode(e))
            LOG.warning(msg)
            if re_raise_bsc:
                raise
        finally:
            self.update_capabilities() 
Example #9
Source File: csar_utils.py    From tacker with Apache License 2.0 6 votes vote down vote up
def load_csar_data(context, package_uuid, zip_path):

    extract_zip_path = os.path.join(CONF.vnf_package.vnf_package_csar_path,
                                    package_uuid)
    extract_csar_zip_file(zip_path, extract_zip_path)

    try:
        tosca = ToscaTemplate(zip_path, None, True)
        return _get_data_from_csar(tosca, context, package_uuid)
    except exceptions.InvalidCSAR as exp:
        with excutils.save_and_reraise_exception():
            LOG.error("Error processing CSAR file %(path)s for vnf package"
                      " %(uuid)s: Error: %(error)s. ",
                      {'path': zip_path, 'uuid': package_uuid,
                    'error': encodeutils.exception_to_unicode(exp)})
    except Exception as exp:
        with excutils.save_and_reraise_exception():
            LOG.error("Tosca parser failed for vnf package %(uuid)s: "
                      "Error: %(error)s. ", {'uuid': package_uuid,
                      'error': encodeutils.exception_to_unicode(exp)})
            exp.reraise = False
            raise exceptions.InvalidCSAR(encodeutils.exception_to_unicode
                                         (exp)) 
Example #10
Source File: http.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def get_size(self, location, context=None):
        """
        Takes a `glance_store.location.Location` object that indicates
        where to find the image file, and returns the size

        :param location: `glance_store.location.Location` object, supplied
                        from glance_store.location.get_location_from_uri()
        """
        conn = None
        try:
            conn, resp, size = self._query(location, 'HEAD')
        except requests.exceptions.ConnectionError as exc:
            err_msg = encodeutils.exception_to_unicode(exc)
            reason = _("The HTTP URL is invalid: %s") % err_msg
            LOG.info(reason)
            raise exceptions.BadStoreUri(message=reason)
        finally:
            # NOTE(sabari): Close the connection as the request was made with
            # stream=True
            if conn is not None:
                conn.close()
        return size 
Example #11
Source File: connection_manager.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _get_storage_url(self):
        """Get swift endpoint from keystone

        Return endpoint for swift from service catalog. The method works only
        Keystone v3. If you are using different version (1 or 2)
        it returns None.
        :return: swift endpoint
        """
        if self.store.auth_version == '3':
            try:
                return self.client.session.get_endpoint(
                    service_type=self.store.service_type,
                    interface=self.store.endpoint_type,
                    region_name=self.store.region
                )
            except Exception as e:
                # do the same that swift driver does
                # when catching ClientException
                msg = _("Cannot find swift service endpoint : "
                        "%s") % encodeutils.exception_to_unicode(e)
                raise exceptions.BackendException(msg) 
Example #12
Source File: buffered.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def validate_buffering(buffer_dir):
    if buffer_dir is None:
        msg = _('Configuration option "swift_upload_buffer_dir" is '
                'not set. Please set it to a valid path to buffer '
                'during Swift uploads.')
        raise exceptions.BadStoreConfiguration(store_name='swift',
                                               reason=msg)

    # NOTE(dharinic): Ensure that the provided directory path for
    # buffering is valid
    try:
        _tmpfile = tempfile.TemporaryFile(dir=buffer_dir)
    except OSError as err:
        msg = (_('Unable to use buffer directory set with '
                 '"swift_upload_buffer_dir". Error: %s') %
               encodeutils.exception_to_unicode(err))
        raise exceptions.BadStoreConfiguration(store_name='swift',
                                               reason=msg)
    else:
        _tmpfile.close()
        return True 
Example #13
Source File: s3.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _object_exists(s3_client, bucket, key):
        """Check whether object exists in the specific bucket of S3.

        :param s3_client: An object with credentials to connect to S3
        :param bucket: S3 bucket name
        :param key: The image object name
        :returns: boolean value; If the value is true, the object is exist
                  if false, it is not.
        :raises: BadStoreConfiguration if cannot connect to S3 successfully
        """
        try:
            s3_client.head_object(Bucket=bucket, Key=key)
        except boto_exceptions.ClientError as e:
            error_code = e.response['Error']['Code']
            if error_code == '404':
                return False
            msg = ("Failed to get object info: %s" %
                   encodeutils.exception_to_unicode(e))
            LOG.error(msg)
            raise glance_store.BadStoreConfiguration(store_name='s3',
                                                     reason=msg)
        else:
            return True 
Example #14
Source File: s3.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _bucket_exists(s3_client, bucket):
        """Check whether bucket exists in the S3.

        :param s3_client: An object with credentials to connect to S3
        :param bucket: S3 bucket name
        :returns: boolean value; If the value is true, the bucket is exist
                  if false, it is not.
        :raises: BadStoreConfiguration if cannot connect to S3 successfully
        """
        try:
            s3_client.head_bucket(Bucket=bucket)
        except boto_exceptions.ClientError as e:
            error_code = e.response['Error']['Code']
            if error_code == '404':
                return False
            msg = ("Failed to get bucket info: %s" %
                   encodeutils.exception_to_unicode(e))
            LOG.error(msg)
            raise glance_store.BadStoreConfiguration(store_name='s3',
                                                     reason=msg)
        else:
            return True 
Example #15
Source File: s3.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _create_bucket(s3_client, s3_host, bucket):
        """Create bucket into the S3.

        :param s3_client: An object with credentials to connect to S3
        :param s3_host: S3 endpoint url
        :param bucket: S3 bucket name
        :raises: BadStoreConfiguration if cannot connect to S3 successfully
        """
        region = get_s3_location(s3_host)
        try:
            s3_client.create_bucket(
                Bucket=bucket,
            ) if region == '' else s3_client.create_bucket(
                Bucket=bucket,
                CreateBucketConfiguration={
                    'LocationConstraint': region
                }
            )
        except boto_exceptions.ClientError as e:
            msg = ("Failed to add bucket to S3: %s" %
                   encodeutils.exception_to_unicode(e))
            LOG.error(msg)
            raise glance_store.BadStoreConfiguration(store_name='s3',
                                                     reason=msg) 
Example #16
Source File: store.py    From tacker with Apache License 2.0 6 votes vote down vote up
def get_csar_data_iter(body):
    try:
        if isinstance(body, dict):
            url = body['address_information']
            req = urllib.request.Request(url)
            if body['user_name'] is not None or body['password'] is not None:
                _add_basic_auth(req, body['user_name'], body['password'])
            data_iter = urllib.request.urlopen(req)
        else:
            data_iter = body

        return data_iter
    except Exception as e:
        error = encodeutils.exception_to_unicode(e)
        LOG.warn("Failed to open csar URL: %(url)s due to error: %(error)s",
                 {"url": url, "error": error})
        raise exceptions.VNFPackageURLInvalid(url=url) 
Example #17
Source File: store.py    From tacker with Apache License 2.0 6 votes vote down vote up
def load_csar(package_uuid, location):
    zip_path = os.path.join(CONF.vnf_package.vnf_package_csar_path,
                            package_uuid + ".zip")
    resp, size = _get_csar_chunks(
        package_uuid, location, offset=0, chunk_size=None)

    try:
        temp_data = open(zip_path, 'wb')
        for chunk in resp:
            temp_data.write(chunk)
        temp_data.close()
    except Exception as exp:
        LOG.exception("Exception encountered while tee'ing "
                      "csar '%(package_uuid)s' into csar path %(zip_path)s:"
                      "%(error)s. ", {'package_uuid': package_uuid,
                       'zip_path': zip_path,
                       'error': encodeutils.exception_to_unicode(exp)})

    return zip_path 
Example #18
Source File: vnflcm_driver.py    From tacker with Apache License 2.0 6 votes vote down vote up
def terminate_vnf(self, context, vnf_instance, terminate_vnf_req):

        vim_info = vnflcm_utils._get_vim(context,
            vnf_instance.vim_connection_info)

        vim_connection_info = objects.VimConnectionInfo.obj_from_primitive(
            vim_info, context)

        LOG.info("Terminating vnf %s", vnf_instance.id)
        try:
            self._delete_vnf_instance_resources(context, vnf_instance,
                    vim_connection_info, terminate_vnf_req=terminate_vnf_req)

            vnf_instance.instantiated_vnf_info.reinitialize()
            self._vnf_instance_update(context, vnf_instance,
                vim_connection_info=[], task_state=None)

            LOG.info("Vnf terminated %s successfully", vnf_instance.id)
        except Exception as exp:
            with excutils.save_and_reraise_exception():
                LOG.error("Unable to terminate vnf '%s' instance. "
                          "Error: %s", vnf_instance.id,
                          encodeutils.exception_to_unicode(exp)) 
Example #19
Source File: tests_encodeutils.py    From oslo.utils with Apache License 2.0 6 votes vote down vote up
def test_unicode_or_str_exception(self):
        # Exception with __str__() and __unicode__() methods
        class UnicodeOrStrException(Exception):
            def __init__(self, unicode_value, str_value):
                Exception.__init__(self)
                self.unicode_value = unicode_value
                self.str_value = str_value

            def __unicode__(self):
                return self.unicode_value

            def __str__(self):
                return self.str_value

        # __unicode__() returns unicode
        exc = UnicodeOrStrException(u'unicode \xe9\u20ac', b'str')
        self.assertEqual(encodeutils.exception_to_unicode(exc),
                         u'unicode \xe9\u20ac')

        # __unicode__() returns bytes (does this case really happen in the
        # wild?)
        exc = UnicodeOrStrException(b'utf-8 \xc3\xa9\xe2\x82\xac', b'str')
        self.assertEqual(encodeutils.exception_to_unicode(exc),
                         u'utf-8 \xe9\u20ac') 
Example #20
Source File: tests_encodeutils.py    From oslo.utils with Apache License 2.0 6 votes vote down vote up
def test_unicode_exception(self):
        # Exception with a __unicode__() method, but no __str__()
        class UnicodeException(Exception):
            def __init__(self, value):
                Exception.__init__(self)
                self.value = value

            def __unicode__(self):
                return self.value

        # __unicode__() returns unicode
        exc = UnicodeException(u'unicode \xe9\u20ac')
        self.assertEqual(encodeutils.exception_to_unicode(exc),
                         u'unicode \xe9\u20ac')

        # __unicode__() returns bytes (does this case really happen in the
        # wild?)
        exc = UnicodeException(b'utf-8 \xc3\xa9\xe2\x82\xac')
        self.assertEqual(encodeutils.exception_to_unicode(exc),
                         u'utf-8 \xe9\u20ac') 
Example #21
Source File: api.py    From searchlight with Apache License 2.0 5 votes vote down vote up
def fail(e):
    global KNOWN_EXCEPTIONS
    return_code = KNOWN_EXCEPTIONS.index(type(e)) + 1
    sys.stderr.write("ERROR: %s\n" % encodeutils.exception_to_unicode(e))
    sys.exit(return_code) 
Example #22
Source File: test_exceptions.py    From glance_store with Apache License 2.0 5 votes vote down vote up
def test_unsupported_backend_exception(self):
        msg = glance_store.UnsupportedBackend()
        self.assertIn(u'', encodeutils.exception_to_unicode(msg)) 
Example #23
Source File: common.py    From manila with Apache License 2.0 5 votes vote down vote up
def validate_ip(access_to, enable_ipv6):
    try:
        if enable_ipv6:
            validator = ipaddress.ip_network
        else:
            validator = ipaddress.IPv4Network
        validator(six.text_type(access_to))
    except ValueError as error:
        err_msg = encodeutils.exception_to_unicode(error)
        raise webob.exc.HTTPBadRequest(explanation=err_msg) 
Example #24
Source File: nova.py    From masakari with Apache License 2.0 5 votes vote down vote up
def translate_nova_exception(method):
    """Transforms a cinder exception but keeps its traceback intact."""
    @functools.wraps(method)
    def wrapper(self, ctx, *args, **kwargs):
        try:
            res = method(self, ctx, *args, **kwargs)
        except (request_exceptions.Timeout,
                nova_exception.CommandError,
                keystone_exception.ConnectionError) as exc:
            err_msg = encodeutils.exception_to_unicode(exc)
            _reraise(exception.MasakariException(reason=err_msg))
        except (keystone_exception.BadRequest,
                nova_exception.BadRequest) as exc:
            err_msg = encodeutils.exception_to_unicode(exc)
            _reraise(exception.InvalidInput(reason=err_msg))
        except (keystone_exception.Forbidden,
                nova_exception.Forbidden) as exc:
            err_msg = encodeutils.exception_to_unicode(exc)
            _reraise(exception.Forbidden(err_msg))
        except (nova_exception.NotFound) as exc:
            err_msg = encodeutils.exception_to_unicode(exc)
            _reraise(exception.NotFound(reason=err_msg))
        except nova_exception.Conflict as exc:
            err_msg = encodeutils.exception_to_unicode(exc)
            _reraise(exception.Conflict(reason=err_msg))
        return res
    return wrapper 
Example #25
Source File: test_failure.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def test_wrapped_failure_non_ascii_unicode(self):
        hi_cn = u'嗨'
        fail = ValueError(hi_cn)
        self.assertEqual(hi_cn, encodeutils.exception_to_unicode(fail))
        fail = failure.Failure.from_exception(fail)
        wrapped_fail = exceptions.WrappedFailure([fail])
        expected_result = (u"WrappedFailure: "
                           "[Failure: ValueError: %s]" % (hi_cn))
        self.assertEqual(expected_result, six.text_type(wrapped_fail)) 
Example #26
Source File: test_failure.py    From taskflow with Apache License 2.0 5 votes vote down vote up
def test_exception_with_non_ascii_str(self):
        bad_string = chr(200)
        excp = ValueError(bad_string)
        fail = failure.Failure.from_exception(excp)
        self.assertEqual(encodeutils.exception_to_unicode(excp),
                         fail.exception_str)
        # This is slightly different on py2 vs py3... due to how
        # __str__ or __unicode__ is called and what is expected from
        # both...
        if six.PY2:
            msg = encodeutils.exception_to_unicode(excp)
            expected = 'Failure: ValueError: %s' % msg.encode('utf-8')
        else:
            expected = u'Failure: ValueError: \xc8'
        self.assertEqual(expected, str(fail)) 
Example #27
Source File: vnflcm_driver.py    From tacker with Apache License 2.0 5 votes vote down vote up
def _respawn_vnf(self, context, vnf_instance, vim_connection_info,
            heal_vnf_request):
        try:
            self._delete_vnf_instance_resources(context, vnf_instance,
                vim_connection_info, update_instantiated_state=False)
        except Exception as exc:
            with excutils.save_and_reraise_exception() as exc_ctxt:
                exc_ctxt.reraise = False
                err_msg = ("Failed to delete vnf resources for vnf instance "
                          "%(id)s before respawning. The vnf is in "
                          "inconsistent state. Error: %(error)s")
                LOG.error(err_msg % {"id": vnf_instance.id,
                          "error": six.text_type(exc)})
                raise exceptions.VnfHealFailed(id=vnf_instance.id,
                    error=encodeutils.exception_to_unicode(exc))

        # InstantiateVnfRequest is not stored in the db as it's mapped
        # to InstantiatedVnfInfo version object. Convert InstantiatedVnfInfo
        # version object to InstantiateVnfRequest so that vnf can be
        # instantiated.

        instantiate_vnf_request = objects.InstantiateVnfRequest.\
            from_vnf_instance(vnf_instance)

        try:
            self._instantiate_vnf(context, vnf_instance, vim_connection_info,
                                  instantiate_vnf_request)
        except Exception as exc:
            with excutils.save_and_reraise_exception() as exc_ctxt:
                exc_ctxt.reraise = False
                err_msg = ("Failed to instantiate vnf instance "
                          "%(id)s after termination. The vnf is in "
                          "inconsistent state. Error: %(error)s")
                LOG.error(err_msg % {"id": vnf_instance.id,
                          "error": six.text_type(exc)})
                raise exceptions.VnfHealFailed(id=vnf_instance.id,
                    error=encodeutils.exception_to_unicode(exc))

        self._vnf_instance_update(context, vnf_instance,
                    instantiation_state=fields.VnfInstanceState.INSTANTIATED,
                    task_state=None) 
Example #28
Source File: csar_utils.py    From tacker with Apache License 2.0 5 votes vote down vote up
def extract_csar_zip_file(file_path, extract_path):
    try:
        with zipfile.ZipFile(file_path, 'r') as zf:
            zf.extractall(extract_path)
    except (RuntimeError, zipfile.BadZipfile) as exp:
        with excutils.save_and_reraise_exception():
            LOG.error("Error encountered while extracting "
                      "csar zip file %(path)s. Error: %(error)s.",
                      {'path': file_path,
                      'error': encodeutils.exception_to_unicode(exp)})
            exp.reraise = False
            raise exceptions.InvalidZipFile(path=file_path) 
Example #29
Source File: csar_utils.py    From tacker with Apache License 2.0 5 votes vote down vote up
def delete_csar_data(package_uuid):
    # Remove zip and folder from the vnf_package_csar_path
    csar_zip_temp_path = os.path.join(CONF.vnf_package.vnf_package_csar_path,
                                      package_uuid)
    csar_path = os.path.join(CONF.vnf_package.vnf_package_csar_path,
                 package_uuid + ".zip")

    try:
        shutil.rmtree(csar_zip_temp_path)
        os.remove(csar_path)
    except OSError as exc:
        exc_message = encodeutils.exception_to_unicode(exc)
        msg = _('Failed to delete csar folder: '
                '%(csar_path)s, Error: %(exc)s')
        LOG.warning(msg, {'csar_path': csar_path, 'exc': exc_message}) 
Example #30
Source File: test_swift_store_multibackend.py    From glance_store with Apache License 2.0 5 votes vote down vote up
def test_add_no_container_and_multiple_containers_no_create(self):
        """
        Tests that adding an image with a non-existing container while using
        multiple containers raises an appropriate exception
        """
        conf = copy.deepcopy(SWIFT_CONF)
        conf['swift_store_user'] = 'tenant:user'
        conf['swift_store_create_container_on_put'] = False
        conf['swift_store_container'] = 'randomname'
        conf['swift_store_multiple_containers_seed'] = 2
        self.config(group="swift1", **conf)
        moves.reload_module(swift)
        self.mock_keystone_client()

        expected_image_id = str(uuid.uuid4())
        expected_container = 'randomname_' + expected_image_id[:2]

        self.store = Store(self.conf, backend="swift1")
        self.store.configure()

        image_swift = six.BytesIO(b"nevergonnamakeit")

        global SWIFT_PUT_OBJECT_CALLS
        SWIFT_PUT_OBJECT_CALLS = 0

        # We check the exception text to ensure the container
        # missing text is found in it, otherwise, we would have
        # simply used self.assertRaises here
        exception_caught = False
        try:
            self.store.add(expected_image_id, image_swift, 0)
        except exceptions.BackendException as e:
            exception_caught = True
            expected_msg = "container %s does not exist in Swift"
            expected_msg = expected_msg % expected_container
            self.assertIn(expected_msg, encodeutils.exception_to_unicode(e))
        self.assertTrue(exception_caught)
        self.assertEqual(0, SWIFT_PUT_OBJECT_CALLS)