def test_delete_client_failure(self): client = mock.MagicMock() client.call.side_effect = glanceclient.exc.NotFound ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) self.assertRaises(exception.ImageNotFound, service.delete, ctx, mock.sentinel.image_id)
def test_create_success(self, trans_to_mock, trans_from_mock): translated = {'image_id': mock.sentinel.image_id} trans_to_mock.return_value = translated trans_from_mock.return_value = mock.sentinel.trans_from image_mock = mock.MagicMock(spec=dict) client = mock.MagicMock() client.call.return_value = mock.sentinel.image_meta ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) image_meta = service.create(ctx, image_mock) trans_to_mock.assert_called_once_with(image_mock) client.call.assert_called_once_with(ctx, 1, 'create', image_id=mock.sentinel.image_id) trans_from_mock.assert_called_once_with(mock.sentinel.image_meta) self.assertEqual(mock.sentinel.trans_from, image_meta) # Now verify that if we supply image data to the call, # that the client is also called with the data kwarg client.reset_mock() image_meta = service.create(ctx, image_mock, data=mock.sentinel.data) client.call.assert_called_once_with(ctx, 1, 'create', image_id=mock.sentinel.image_id, data=mock.sentinel.data)
def test_download_data_dest_path(self, show_mock, open_mock): # NOTE(jaypipes): This really shouldn't be allowed, but because of the # horrible design of the download() method in GlanceImageService, no # error is raised, and the dst_path is ignored... # #TODO(jaypipes): Fix the aforementioned horrible design of # the download() method. client = mock.MagicMock() client.call.return_value = [1, 2, 3] ctx = mock.sentinel.ctx data = mock.MagicMock() service = glance.GlanceImageService(client) res = service.download(ctx, mock.sentinel.image_id, data=data) self.assertFalse(show_mock.called) self.assertFalse(open_mock.called) client.call.assert_called_once_with(ctx, 1, 'data', mock.sentinel.image_id) self.assertIsNone(res) data.write.assert_has_calls( [ mock.call(1), mock.call(2), mock.call(3) ] ) self.assertFalse(data.close.called)
def test_download_direct_file_uri(self, show_mock, get_tran_mock): self.flags(allowed_direct_url_schemes=['file'], group='glance') show_mock.return_value = { 'locations': [{ 'url': 'file:///files/image', 'metadata': mock.sentinel.loc_meta }] } tran_mod = mock.MagicMock() get_tran_mock.return_value = tran_mod client = mock.MagicMock() ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) res = service.download(ctx, mock.sentinel.image_id, dst_path=mock.sentinel.dst_path) self.assertIsNone(res) self.assertFalse(client.call.called) show_mock.assert_called_once_with(ctx, mock.sentinel.image_id, include_locations=True) get_tran_mock.assert_called_once_with('file') tran_mod.download.assert_called_once_with(ctx, mock.ANY, mock.sentinel.dst_path, mock.sentinel.loc_meta)
def test_show_queued_image_without_some_attrs(self, is_avail_mock): is_avail_mock.return_value = True client = mock.MagicMock() # fake image cls without disk_format, container_format, name attributes class fake_image_cls(dict): id = 'b31aa5dd-f07a-4748-8f15-398346887584' deleted = False protected = False min_disk = 0 created_at = '2014-05-20T08:16:48' size = 0 status = 'queued' is_public = False min_ram = 0 owner = '980ec4870033453ead65c0470a78b8a8' updated_at = '2014-05-20T08:16:48' glance_image = fake_image_cls() client.call.return_value = glance_image ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) image_info = service.show(ctx, glance_image.id) client.call.assert_called_once_with(ctx, 1, 'get', glance_image.id) NOVA_IMAGE_ATTRIBUTES = set([ 'size', 'disk_format', 'owner', 'container_format', 'status', 'id', 'name', 'created_at', 'updated_at', 'deleted', 'deleted_at', 'checksum', 'min_disk', 'min_ram', 'is_public', 'properties' ]) self.assertEqual(NOVA_IMAGE_ATTRIBUTES, set(image_info.keys()))
def setUp(self): super(TestGlanceImageService, self).setUp() fakes.stub_out_compute_api_snapshot(self.stubs) client = glance_stubs.StubGlanceClient() self.service = glance.GlanceImageService(client=client) self.context = context.RequestContext('fake', 'fake', auth_token=True) self.service.delete_all()
def test_get_with_retries(self): tries = [0] class GlanceBusyException(Exception): pass class MyGlanceStubClient(glance_stubs.StubGlanceClient): """A client that fails the first time, then succeeds.""" def get_image(self, image_id): if tries[0] == 0: tries[0] = 1 raise GlanceBusyException() else: return {}, [] client = MyGlanceStubClient() service = glance.GlanceImageService(client=client) image_id = 1 # doesn't matter writer = NullWriter() # When retries are disabled, we should get an exception self.flags(glance_num_retries=0) self.assertRaises(GlanceBusyException, service.get, self.context, image_id, writer) # Now lets enable retries. No exception should happen now. self.flags(glance_num_retries=1) service.get(self.context, image_id, writer)
def __init__(self): self.site = None self.kclient = {} self.tenant_manager = {} self.user_manager = {} self._wiki_logged_in = False self.glance_service = glance.GlanceImageService()
def test_delete_success(self): client = mock.MagicMock() client.call.return_value = True ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) service.delete(ctx, mock.sentinel.image_id) client.call.assert_called_once_with(ctx, 1, 'delete', mock.sentinel.image_id)
def _create_image_service(self, client): def _fake_create_glance_client(context, host, port): return client self.stubs.Set(glance, '_create_glance_client', _fake_create_glance_client) client_wrapper = glance.GlanceClientWrapper('fake', 'fake_host', 9292) return glance.GlanceImageService(client=client_wrapper)
def test_detail_params_passed(self, is_avail_mock, _trans_from_mock, ext_query_mock): params = dict(limit=10) ext_query_mock.return_value = params client = mock.MagicMock() client.call.return_value = [mock.sentinel.images_0] ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) images = service.detail(ctx, **params) client.call.assert_called_once_with(ctx, 1, 'list', limit=10)
def test_client_raises_forbidden(self): class MyGlanceStubClient(glance_stubs.StubGlanceClient): """A client that fails the first time, then succeeds.""" def get_image(self, image_id): raise glance_exception.Forbidden() client = MyGlanceStubClient() service = glance.GlanceImageService(client=client) image_id = 1 # doesn't matter writer = NullWriter() self.assertRaises(exception.ImageNotAuthorized, service.get, self.context, image_id, writer)
def test_download_no_data_no_dest_path(self, show_mock, open_mock): client = mock.MagicMock() client.call.return_value = mock.sentinel.image_chunks ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) res = service.download(ctx, mock.sentinel.image_id) self.assertFalse(show_mock.called) self.assertFalse(open_mock.called) client.call.assert_called_once_with(ctx, 1, 'data', mock.sentinel.image_id) self.assertEqual(mock.sentinel.image_chunks, res)
def test_show_success(self, is_avail_mock, trans_from_mock): is_avail_mock.return_value = True trans_from_mock.return_value = mock.sentinel.trans_from client = mock.MagicMock() client.call.return_value = mock.sentinel.images_0 ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) info = service.show(ctx, mock.sentinel.image_id) client.call.assert_called_once_with(ctx, 1, 'get', mock.sentinel.image_id) is_avail_mock.assert_called_once_with(ctx, mock.sentinel.images_0) trans_from_mock.assert_called_once_with(mock.sentinel.images_0) self.assertEqual(mock.sentinel.trans_from, info)
def test_show_not_available(self, is_avail_mock, trans_from_mock): is_avail_mock.return_value = False client = mock.MagicMock() client.call.return_value = mock.sentinel.images_0 ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) with testtools.ExpectedException(exception.ImageNotFound): service.show(ctx, mock.sentinel.image_id) client.call.assert_called_once_with(ctx, 1, 'get', mock.sentinel.image_id) is_avail_mock.assert_called_once_with(ctx, mock.sentinel.images_0) self.assertFalse(trans_from_mock.called)
def test_detail_params_passed(self, is_avail_mock, _trans_from_mock): client = mock.MagicMock() client.call.return_value = [mock.sentinel.images_0] ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) service.detail(ctx, page_size=5, limit=10) expected_filters = {'is_public': 'none'} client.call.assert_called_once_with(ctx, 1, 'list', filters=expected_filters, page_size=5, limit=10)
def test_download_direct_exception_fallback(self, show_mock, get_tran_mock, open_mock): # Test that we fall back to downloading to the dst_path # if the download method of the transfer module raised # an exception. self.flags(allowed_direct_url_schemes=['file'], group='glance') show_mock.return_value = { 'locations': [ { 'url': 'file:///files/image', 'metadata': mock.sentinel.loc_meta } ] } tran_mod = mock.MagicMock() tran_mod.download.side_effect = Exception get_tran_mock.return_value = tran_mod client = mock.MagicMock() client.call.return_value = [1, 2, 3] ctx = mock.sentinel.ctx writer = mock.MagicMock() open_mock.return_value = writer service = glance.GlanceImageService(client) res = service.download(ctx, mock.sentinel.image_id, dst_path=mock.sentinel.dst_path) self.assertIsNone(res) show_mock.assert_called_once_with(ctx, mock.sentinel.image_id, include_locations=True) get_tran_mock.assert_called_once_with('file') tran_mod.download.assert_called_once_with(ctx, mock.ANY, mock.sentinel.dst_path, mock.sentinel.loc_meta) client.call.assert_called_once_with(ctx, 1, 'data', mock.sentinel.image_id) # NOTE(jaypipes): log messages call open() in part of the # download path, so here, we just check that the last open() # call was done for the dst_path file descriptor. open_mock.assert_called_with(mock.sentinel.dst_path, 'wb') self.assertIsNone(res) writer.write.assert_has_calls( [ mock.call(1), mock.call(2), mock.call(3) ] )
def test_download_data_no_dest_path(self, show_mock, open_mock): client = mock.MagicMock() client.call.return_value = [1, 2, 3] ctx = mock.sentinel.ctx data = mock.MagicMock() service = glance.GlanceImageService(client) res = service.download(ctx, mock.sentinel.image_id, data=data) self.assertFalse(show_mock.called) self.assertFalse(open_mock.called) client.call.assert_called_once_with(ctx, 1, 'data', mock.sentinel.image_id) self.assertIsNone(res) data.write.assert_has_calls([mock.call(1), mock.call(2), mock.call(3)]) self.assertFalse(data.close.called)
def test_show_success(self, is_avail_mock, trans_from_mock): is_avail_mock.return_value = True trans_from_mock.return_value = {'mock': mock.sentinel.trans_from} client = mock.MagicMock() client.call.return_value = {} ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) info = service.show(ctx, mock.sentinel.image_id) client.call.assert_called_once_with(ctx, 1, 'get', mock.sentinel.image_id) is_avail_mock.assert_called_once_with(ctx, {}) trans_from_mock.assert_called_once_with({}, include_locations=False) self.assertIn('mock', info) self.assertEqual(mock.sentinel.trans_from, info['mock'])
def test_create_client_failure(self, trans_to_mock, trans_from_mock, reraise_mock): translated = {} trans_to_mock.return_value = translated image_mock = mock.MagicMock(spec=dict) raised = exception.Invalid() client = mock.MagicMock() client.call.side_effect = glanceclient.exc.BadRequest ctx = mock.sentinel.ctx reraise_mock.side_effect = raised service = glance.GlanceImageService(client) self.assertRaises(exception.Invalid, service.create, ctx, image_mock) trans_to_mock.assert_called_once_with(image_mock) self.assertFalse(trans_from_mock.called)
def test_detail_success_unavailable(self, is_avail_mock, trans_from_mock, ext_query_mock): params = {} is_avail_mock.return_value = False ext_query_mock.return_value = params trans_from_mock.return_value = mock.sentinel.trans_from client = mock.MagicMock() client.call.return_value = [mock.sentinel.images_0] ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) images = service.detail(ctx, **params) client.call.assert_called_once_with(ctx, 1, 'list') is_avail_mock.assert_called_once_with(ctx, mock.sentinel.images_0) self.assertFalse(trans_from_mock.called) self.assertEqual([], images)
def test_show_client_failure(self, is_avail_mock, trans_from_mock, reraise_mock): raised = exception.ImageNotAuthorized(image_id=123) client = mock.MagicMock() client.call.side_effect = glanceclient.exc.Forbidden ctx = mock.sentinel.ctx reraise_mock.side_effect = raised service = glance.GlanceImageService(client) with testtools.ExpectedException(exception.ImageNotAuthorized): service.show(ctx, mock.sentinel.image_id) client.call.assert_called_once_with(ctx, 1, 'get', mock.sentinel.image_id) self.assertFalse(is_avail_mock.called) self.assertFalse(trans_from_mock.called) reraise_mock.assert_called_once_with(mock.sentinel.image_id)
def test_do_not_show_deleted_images(self, is_avail_mock, trans_from_mock): class fake_image_cls(dict): id = 'b31aa5dd-f07a-4748-8f15-398346887584' deleted = True glance_image = fake_image_cls() client = mock.MagicMock() client.call.return_value = glance_image ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) with testtools.ExpectedException(exception.ImageNotFound): service.show(ctx, glance_image.id, show_deleted=False) client.call.assert_called_once_with(ctx, 1, 'get', glance_image.id) self.assertFalse(is_avail_mock.called) self.assertFalse(trans_from_mock.called)
def test_detail_client_failure(self, is_avail_mock, trans_from_mock, ext_query_mock, reraise_mock): params = {} ext_query_mock.return_value = params raised = exception.Forbidden() client = mock.MagicMock() client.call.side_effect = glanceclient.exc.Forbidden ctx = mock.sentinel.ctx reraise_mock.side_effect = raised service = glance.GlanceImageService(client) with testtools.ExpectedException(exception.Forbidden): service.detail(ctx, **params) client.call.assert_called_once_with(ctx, 1, 'list') self.assertFalse(is_avail_mock.called) self.assertFalse(trans_from_mock.called) reraise_mock.assert_called_once_with()
def test_include_locations_success(self, avail_mock, trans_from_mock): locations = [mock.sentinel.loc1] avail_mock.return_value = True trans_from_mock.return_value = {'locations': locations} client = mock.Mock() client.call.return_value = mock.sentinel.image service = glance.GlanceImageService(client) ctx = mock.sentinel.ctx image_id = mock.sentinel.image_id info = service.show(ctx, image_id, include_locations=True) client.call.assert_called_once_with(ctx, 2, 'get', image_id) avail_mock.assert_called_once_with(ctx, mock.sentinel.image) trans_from_mock.assert_called_once_with(mock.sentinel.image, include_locations=True) self.assertIn('locations', info) self.assertEqual(locations, info['locations'])
def test_include_direct_uri_success(self, avail_mock, trans_from_mock): locations = [mock.sentinel.loc1] avail_mock.return_value = True trans_from_mock.return_value = {'locations': locations, 'direct_uri': mock.sentinel.duri} client = mock.Mock() client.call.return_value = mock.sentinel.image service = glance.GlanceImageService(client) ctx = mock.sentinel.ctx image_id = mock.sentinel.image_id info = service.show(ctx, image_id, include_locations=True) client.call.assert_called_once_with(ctx, 2, 'get', image_id) expected = locations expected.append({'url': mock.sentinel.duri, 'metadata': {}}) self.assertIn('locations', info) self.assertEqual(expected, info['locations'])
def test_download_no_data_dest_path(self, show_mock, open_mock): client = mock.MagicMock() client.call.return_value = [1, 2, 3] ctx = mock.sentinel.ctx writer = mock.MagicMock() open_mock.return_value = writer service = glance.GlanceImageService(client) res = service.download(ctx, mock.sentinel.image_id, dst_path=mock.sentinel.dst_path) self.assertFalse(show_mock.called) client.call.assert_called_once_with(ctx, 1, 'data', mock.sentinel.image_id) open_mock.assert_called_once_with(mock.sentinel.dst_path, 'wb') self.assertIsNone(res) writer.write.assert_has_calls( [mock.call(1), mock.call(2), mock.call(3)]) writer.close.assert_called_once_with()
def test_update_success(self, trans_to_mock, trans_from_mock): translated = {'id': mock.sentinel.image_id, 'name': mock.sentinel.name} trans_to_mock.return_value = translated trans_from_mock.return_value = mock.sentinel.trans_from image_mock = mock.MagicMock(spec=dict) client = mock.MagicMock() client.call.return_value = mock.sentinel.image_meta ctx = mock.sentinel.ctx service = glance.GlanceImageService(client) image_meta = service.update(ctx, mock.sentinel.image_id, image_mock) trans_to_mock.assert_called_once_with(image_mock) # Verify that the 'id' element has been removed as a kwarg to # the call to glanceclient's update (since the image ID is # supplied as a positional arg), and that the # purge_props default is True. client.call.assert_called_once_with(ctx, 1, 'update', mock.sentinel.image_id, name=mock.sentinel.name, purge_props=True) trans_from_mock.assert_called_once_with(mock.sentinel.image_meta) self.assertEqual(mock.sentinel.trans_from, image_meta) # Now verify that if we supply image data to the call, # that the client is also called with the data kwarg client.reset_mock() image_meta = service.update(ctx, mock.sentinel.image_id, image_mock, data=mock.sentinel.data) client.call.assert_called_once_with(ctx, 1, 'update', mock.sentinel.image_id, name=mock.sentinel.name, purge_props=True, data=mock.sentinel.data)
def test_update_client_failure(self, trans_to_mock, trans_from_mock, reraise_mock): translated = {'name': mock.sentinel.name} trans_to_mock.return_value = translated trans_from_mock.return_value = mock.sentinel.trans_from image_mock = mock.MagicMock(spec=dict) raised = exception.ImageNotAuthorized(image_id=123) client = mock.MagicMock() client.call.side_effect = glanceclient.exc.Forbidden ctx = mock.sentinel.ctx reraise_mock.side_effect = raised service = glance.GlanceImageService(client) self.assertRaises(exception.ImageNotAuthorized, service.update, ctx, mock.sentinel.image_id, image_mock) client.call.assert_called_once_with(ctx, 1, 'update', mock.sentinel.image_id, purge_props=True, name=mock.sentinel.name) self.assertFalse(trans_from_mock.called) reraise_mock.assert_called_once_with(mock.sentinel.image_id)
def setUp(self): self.client = StubGlanceClient(None) self.service = glance.GlanceImageService(self.client)