import math
+from eventlet import greenthread
import mock
from cinder import test
+from cinder.volume.drivers.vmware import error_util
from cinder.volume.drivers.vmware import io_util
max_transfer_size = 30
queue = io_util.ThreadSafePipe(10, 30)
self.assertEqual(max_transfer_size, queue.tell())
+
+
+class GlanceWriteThreadTest(test.TestCase):
+ """Tests for GlanceWriteThread class."""
+
+ def _create_image_writer(self):
+ self._image_service = mock.Mock()
+ self._context = mock.Mock()
+ self._input_file = mock.Mock()
+ self._image_id = mock.Mock()
+ return io_util.GlanceWriteThread(self._context, self._input_file,
+ self._image_service, self._image_id)
+
+ @mock.patch.object(greenthread, 'sleep')
+ def test_start(self, mock_sleep):
+ writer = self._create_image_writer()
+ status_list = ['queued', 'saving', 'active']
+
+ def image_service_show_side_effect(context, image_id):
+ status = status_list.pop(0)
+ return {'status': status}
+
+ self._image_service.show.side_effect = image_service_show_side_effect
+ exp_calls = [mock.call(self._context,
+ self._image_id)] * len(status_list)
+
+ writer.start()
+ self.assertTrue(writer.wait())
+ self._image_service.update.assert_called_once_with(
+ self._context, self._image_id, {}, data=self._input_file)
+ self.assertEqual(exp_calls, self._image_service.show.call_args_list)
+
+ def test_start_with_killed_status(self):
+ writer = self._create_image_writer()
+
+ def image_service_show_side_effect(_context, _image_id):
+ return {'status': 'killed'}
+
+ self._image_service.show.side_effect = image_service_show_side_effect
+
+ writer.start()
+ self.assertRaises(error_util.ImageTransferException, writer.wait)
+ self._image_service.update.assert_called_once_with(
+ self._context, self._image_id, {}, data=self._input_file)
+ self._image_service.show.assert_called_once_with(self._context,
+ self._image_id)
+
+ def test_start_with_unknown_status(self):
+ writer = self._create_image_writer()
+
+ def image_service_show_side_effect(_context, _image_id):
+ return {'status': 'unknown'}
+
+ self._image_service.show.side_effect = image_service_show_side_effect
+
+ writer.start()
+ self.assertRaises(error_util.ImageTransferException, writer.wait)
+ self._image_service.update.assert_called_once_with(
+ self._context, self._image_id, {}, data=self._input_file)
+ self._image_service.show.assert_called_once_with(self._context,
+ self._image_id)
+
+ def test_start_with_image_service_update_exception(self):
+ writer = self._create_image_writer()
+ self._image_service.update.side_effect = Exception
+
+ writer.start()
+ self.assertRaises(error_util.ImageTransferException, writer.wait)
+ self._image_service.update.assert_called_once_with(
+ self._context, self._image_id, {}, data=self._input_file)
+ self.assertFalse(self._image_service.show.called)
+
+ def test_start_with_image_service_show_exception(self):
+ writer = self._create_image_writer()
+ self._image_service.show.side_effect = Exception
+
+ writer.start()
+ self.assertRaises(error_util.ImageTransferException, writer.wait)
+ self._image_service.update.assert_called_once_with(
+ self._context, self._image_id, {}, data=self._input_file)
+ self._image_service.show.assert_called_once_with(self._context,
+ self._image_id)
from eventlet import greenthread
from eventlet import queue
-from cinder import exception
from cinder.i18n import _
from cinder.openstack.common import log as logging
+from cinder.volume.drivers.vmware import error_util
LOG = logging.getLogger(__name__)
IO_THREAD_SLEEP_TIME = .01
LOG.debug("Initiating image service update on image: %(image)s "
"with meta: %(meta)s" % {'image': self.image_id,
'meta': self.image_meta})
- self.image_service.update(self.context,
- self.image_id,
- self.image_meta,
- data=self.input_file)
- self._running = True
- while self._running:
- try:
+
+ try:
+ self.image_service.update(self.context,
+ self.image_id,
+ self.image_meta,
+ data=self.input_file)
+
+ self._running = True
+ while self._running:
image_meta = self.image_service.show(self.context,
self.image_id)
image_status = image_meta.get('status')
msg = (_("Glance image: %s is in killed state.") %
self.image_id)
LOG.error(msg)
- excep = exception.CinderException(msg)
+ excep = error_util.ImageTransferException(msg)
self.done.send_exception(excep)
elif image_status in ['saving', 'queued']:
greenthread.sleep(GLANCE_POLL_INTERVAL)
"- %(state)s") % {'id': self.image_id,
'state': image_status}
LOG.error(msg)
- excep = exception.CinderException(msg)
+ excep = error_util.ImageTransferException(msg)
self.done.send_exception(excep)
- except Exception as exc:
- self.stop()
- self.done.send_exception(exc)
+ except Exception as ex:
+ self.stop()
+ msg = (_("Error occurred while writing to image: %s") %
+ self.image_id)
+ LOG.exception(msg)
+ excep = error_util.ImageTransferException(ex)
+ self.done.send_exception(excep)
greenthread.spawn(_inner)
return self.done