Improve error handling of commit

This patch improves error handling of commit, adds some cleaning-up.

Change-Id: Iec545589693de0b818ef000237f663f0a06739fe
Closes-Bug: #1696156
Depends-on: I868eaf9b7245ffabcbbaa61e62ea853844785d02
This commit is contained in:
miaohb 2017-10-28 00:25:18 -07:00
parent f480a90e4e
commit a685651f51
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)
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:
image_driver.upload_image_data(context, snapshot_image,
tag, data, glance.GlanceDriver())
except Exception as e:
LOG.exception("Unexpected exception while uploading image: %s",
six.text_type(e))
image_driver.delete_image(context, snapshot_image.id,
glance.GlanceDriver())
self.driver.delete_image(container_image_id)
raise
def _do_container_commit(self, context, snapshot_image, container,
repository, tag=None):
container_image_id = None
LOG.debug('Creating image...')
if tag is None:
tag = 'latest'
@ -702,9 +707,12 @@ class Manager(periodic_task.PeriodicTasks):
except exception.DockerError as e:
LOG.error("Error occurred while calling docker commit API: %s",
six.text_type(e))
image_driver.delete_image(context, snapshot_image.id,
glance.GlanceDriver())
raise
LOG.debug('Upload image %s to glance', container_image_id)
self._do_container_image_upload(context, snapshot_image,
container_image_id,
container_image, tag)
def image_pull(self, context, image):

View File

@ -792,13 +792,15 @@ class TestManager(base.TestCase):
mock_commit.assert_called_once_with(
self.context, container, 'repo', 'tag')
@mock.patch('zun.image.driver.delete_image')
@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())
mock_commit.side_effect = exception.DockerError
self.assertRaises(exception.DockerError,
self.compute_manager._do_container_commit,
self.context, container, 'repo', 'tag')
self.assertTrue(mock_delete.called)
@mock.patch.object(fake_driver, 'network_detach')
def test_container_network_detach(self, mock_detach):