Merge "Improve error handling of commit"

This commit is contained in:
Zuul 2017-11-15 16:15:38 +00:00 committed by Gerrit Code Review
commit a03967388c
2 changed files with 12 additions and 2 deletions

View File

@ -680,17 +680,22 @@ class Manager(periodic_task.PeriodicTasks):
utils.spawn_n(do_container_commit) utils.spawn_n(do_container_commit)
return {"uuid": snapshot_image.id} return {"uuid": snapshot_image.id}
def _do_container_image_upload(self, context, snapshot_image, data, tag): def _do_container_image_upload(self, context, snapshot_image,
container_image_id, data, tag):
try: try:
image_driver.upload_image_data(context, snapshot_image, image_driver.upload_image_data(context, snapshot_image,
tag, data, glance.GlanceDriver()) tag, data, glance.GlanceDriver())
except Exception as e: except Exception as e:
LOG.exception("Unexpected exception while uploading image: %s", LOG.exception("Unexpected exception while uploading image: %s",
six.text_type(e)) six.text_type(e))
image_driver.delete_image(context, snapshot_image.id,
glance.GlanceDriver())
self.driver.delete_image(container_image_id)
raise raise
def _do_container_commit(self, context, snapshot_image, container, def _do_container_commit(self, context, snapshot_image, container,
repository, tag=None): repository, tag=None):
container_image_id = None
LOG.debug('Creating image...') LOG.debug('Creating image...')
if tag is None: if tag is None:
tag = 'latest' tag = 'latest'
@ -702,9 +707,12 @@ class Manager(periodic_task.PeriodicTasks):
except exception.DockerError as e: except exception.DockerError as e:
LOG.error("Error occurred while calling docker commit API: %s", LOG.error("Error occurred while calling docker commit API: %s",
six.text_type(e)) six.text_type(e))
image_driver.delete_image(context, snapshot_image.id,
glance.GlanceDriver())
raise raise
LOG.debug('Upload image %s to glance', container_image_id) LOG.debug('Upload image %s to glance', container_image_id)
self._do_container_image_upload(context, snapshot_image, self._do_container_image_upload(context, snapshot_image,
container_image_id,
container_image, tag) container_image, tag)
def image_pull(self, context, image): def image_pull(self, context, image):

View File

@ -792,13 +792,15 @@ class TestManager(base.TestCase):
mock_commit.assert_called_once_with( mock_commit.assert_called_once_with(
self.context, container, 'repo', 'tag') self.context, container, 'repo', 'tag')
@mock.patch('zun.image.driver.delete_image')
@mock.patch.object(fake_driver, 'commit') @mock.patch.object(fake_driver, 'commit')
def test_container_commit_failed(self, mock_commit): def test_container_commit_failed(self, mock_commit, mock_delete):
container = Container(self.context, **utils.get_test_container()) container = Container(self.context, **utils.get_test_container())
mock_commit.side_effect = exception.DockerError mock_commit.side_effect = exception.DockerError
self.assertRaises(exception.DockerError, self.assertRaises(exception.DockerError,
self.compute_manager._do_container_commit, self.compute_manager._do_container_commit,
self.context, container, 'repo', 'tag') self.context, container, 'repo', 'tag')
self.assertTrue(mock_delete.called)
@mock.patch.object(fake_driver, 'network_detach') @mock.patch.object(fake_driver, 'network_detach')
def test_container_network_detach(self, mock_detach): def test_container_network_detach(self, mock_detach):