diff --git a/zun/compute/manager.py b/zun/compute/manager.py index ff17ea7ad..ae596f8d4 100644 --- a/zun/compute/manager.py +++ b/zun/compute/manager.py @@ -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): diff --git a/zun/tests/unit/compute/test_compute_manager.py b/zun/tests/unit/compute/test_compute_manager.py index 31a508e8f..ac4da74c4 100644 --- a/zun/tests/unit/compute/test_compute_manager.py +++ b/zun/tests/unit/compute/test_compute_manager.py @@ -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):