def get_data(self, offset=0, chunk_size=None): if not self.image.locations: # NOTE(mclaren): This is the only set of arguments # which work with this exception currently, see: # https://bugs.launchpad.net/glance-store/+bug/1501443 # When the above glance_store bug is fixed we can # add a msg as usual. raise store.NotFound(image=None) err = None for loc in self.image.locations: try: #自backend中取数据 data, size = self.store_api.get_from_backend( loc['url'], offset=offset, chunk_size=chunk_size, context=self.context) return data except Exception as e: LOG.warn( _LW('Get image %(id)s data failed: ' '%(err)s.') % { 'id': self.image.image_id, 'err': encodeutils.exception_to_unicode(e) }) err = e # tried all locations LOG.error( _LE('Glance tried all active locations to get data for ' 'image %s but all have failed.') % self.image.image_id) raise err
def get_data(self, offset=0, chunk_size=None): if not self.image.locations: raise store.NotFound(_("No image data could be found")) err = None for loc in self.image.locations: try: data, size = self.store_api.get_from_backend( loc['url'], offset=offset, chunk_size=chunk_size, context=self.context) return data except Exception as e: LOG.warn( _('Get image %(id)s data failed: ' '%(err)s.') % { 'id': self.image.image_id, 'err': encodeutils.exception_to_unicode(e) }) err = e # tried all locations LOG.error( _LE('Glance tried all active locations to get data for ' 'image %s but all have failed.') % self.image.image_id) raise err
def get_from_backend(self, location, offset=0, chunk_size=None, context=None): try: scheme = location[:location.find('/') - 1] if scheme == 'unknown': raise store.UnknownScheme(scheme=scheme) return self.data[location] except KeyError: raise store.NotFound(image=location)
def test_store_delete_notfound_exception(self, mock_image_get): # While scrubbing image data, NotFound exception is ignored and image # scrubbing succeeds uri = 'file://some/path/%s' % uuid.uuid4() id = 'helloworldid' ex = glance_store.NotFound(message='random') scrub = scrubber.Scrubber(glance_store) with patch.object(glance_store, "delete_from_backend") as _mock_delete: _mock_delete.side_effect = ex scrub._scrub_image(id, [(id, '-', uri)])
def test_download_failure_with_valid_content_range(self): with mock.patch.object(glance.api.policy.ImageProxy, 'get_data') as mock_get_data: mock_get_data.side_effect = glance_store.NotFound(image="image") request = wsgi.Request.blank('/') request.environ = {} request.headers['Content-Range'] = 'bytes %s-%s/3' % (1, 2) response = webob.Response() response.request = request image = FakeImage(size=3, data=[b'Z', b'Z', b'Z']) image.get_data = mock_get_data self.assertRaises(webob.exc.HTTPNoContent, self.serializer.download, response, image)
def test_store_delete_notfound_exception(self, mock_image_get): # While scrubbing image data, NotFound exception is ignored and image # scrubbing succeeds uri = 'file://some/path/%s' % uuid.uuid4() id = 'helloworldid' ex = glance_store.NotFound(message='random') scrub = scrubber.Scrubber(glance_store) self.mox.StubOutWithMock(glance_store, "delete_from_backend") glance_store.delete_from_backend(uri, mox.IgnoreArg()).AndRaise(ex) self.mox.ReplayAll() scrub._scrub_image(id, [(id, '-', uri)]) self.mox.VerifyAll()
def test_store_delete_notfound_exception(self): # While scrubbing image data, NotFound exception is ignored and image # scrubbing succeeds uri = 'file://some/path/%s' % uuid.uuid4() id = 'helloworldid' ex = glance_store.NotFound(message='random') scrub = scrubber.Scrubber(glance_store) scrub.registry = self.mox.CreateMockAnything() scrub.registry.get_image(id).AndReturn({'status': 'pending_delete'}) scrub.registry.update_image(id, {'status': 'deleted'}) self.mox.StubOutWithMock(glance_store, "delete_from_backend") glance_store.delete_from_backend(uri, mox.IgnoreArg()).AndRaise(ex) self.mox.ReplayAll() scrub._scrub_image(id, [(id, '-', uri)]) self.mox.VerifyAll()
def test_download_no_content(self): """Test image download returns HTTPNoContent Make sure that serializer returns 204 no content error in case of image data is not available at specified location. """ with mock.patch.object(xmonitor.api.policy.ImageProxy, 'get_data') as mock_get_data: mock_get_data.side_effect = glance_store.NotFound(image="image") request = wsgi.Request.blank('/') response = webob.Response() response.request = request image = FakeImage(size=3, data=iter('ZZZ')) image.get_data = mock_get_data self.assertRaises(webob.exc.HTTPNoContent, self.serializer.download, response, image)
def fake_save(self): raise glance_store.NotFound()
def fake_save(self, from_state=None): raise glance_store.NotFound()
def test_non_unicode_error_msg(self): exc = glance_store.NotFound(str('test')) self.assertIsInstance(encodeutils.exception_to_unicode(exc), six.text_type)
def test_exception_with_kwargs(self): msg = glance_store.NotFound('Message: %(foo)s', foo='bar') self.assertIn('Message: bar', encodeutils.exception_to_unicode(msg))
def test_exception_with_message(self): msg = glance_store.NotFound('Some message') self.assertIn('Some message', encodeutils.exception_to_unicode(msg))
def test_exception_not_found_with_image(self): msg = glance_store.NotFound(image='123') self.assertIn('Image 123 not found', encodeutils.exception_to_unicode(msg))
def test_exception_no_message(self): msg = glance_store.NotFound() self.assertIn('Image %(image)s not found', encodeutils.exception_to_unicode(msg))