def test_image_member_find(self): TENANT1 = utils.generate_uuid() TENANT2 = utils.generate_uuid() fixtures = [ {'member': TENANT1, 'image_id': UUID1}, {'member': TENANT1, 'image_id': UUID2}, {'member': TENANT2, 'image_id': UUID1}, ] for f in fixtures: self.db_api.image_member_create(self.context, copy.deepcopy(f)) def _simplify(output): return def _assertMemberListMatch(list1, list2): _simple = lambda x: set([(o['member'], o['image_id']) for o in x]) self.assertEqual(_simple(list1), _simple(list2)) output = self.db_api.image_member_find(self.context, member=TENANT1) _assertMemberListMatch([fixtures[0], fixtures[1]], output) output = self.db_api.image_member_find(self.context, image_id=UUID1) _assertMemberListMatch([fixtures[0], fixtures[2]], output) output = self.db_api.image_member_find(self.context, member=TENANT2, image_id=UUID1) _assertMemberListMatch([fixtures[2]], output) output = self.db_api.image_member_find(self.context, member=TENANT2, image_id=utils.generate_uuid()) _assertMemberListMatch([], output)
def test_image_get_all_owned(self): TENANT1 = utils.generate_uuid() ctxt1 = context.RequestContext(is_admin=False, tenant=TENANT1) UUIDX = utils.generate_uuid() self.db_api.image_create(ctxt1, { 'id': UUIDX, 'status': 'queued', 'owner': TENANT1 }) TENANT2 = utils.generate_uuid() ctxt2 = context.RequestContext(is_admin=False, tenant=TENANT2) UUIDY = utils.generate_uuid() self.db_api.image_create(ctxt2, { 'id': UUIDY, 'status': 'queued', 'owner': TENANT2 }) # NOTE(bcwaldon): the is_public=True flag indicates that you want # to get all images that are public AND those that are owned by the # calling context images = self.db_api.image_get_all(ctxt1, filters={'is_public': True}) image_ids = [image['id'] for image in images] expected = [UUIDX, UUID3, UUID2, UUID1] self.assertEqual(sorted(expected), sorted(image_ids))
def test_image_get_not_owned(self): TENANT1 = utils.generate_uuid() TENANT2 = utils.generate_uuid() ctxt1 = context.RequestContext(is_admin=False, tenant=TENANT1) ctxt2 = context.RequestContext(is_admin=False, tenant=TENANT2) image = self.db_api.image_create( ctxt1, {'status': 'queued', 'owner': TENANT1}) self.assertRaises(exception.Forbidden, self.db_api.image_get, ctxt2, image['id'])
def test_multitenant(self): """Ensure an image is properly configured when using multitenancy.""" fake_swift_admin = 'd2f68325-8e2c-4fb1-8c8b-89de2f3d9c4a' self.config( swift_store_multi_tenant=True, ) swift_store_user = self.swift_config['swift_store_user'] tenant_name, username = swift_store_user.split(':') tenant_id, auth_token, service_catalog = keystone_authenticate( self.swift_config['swift_store_auth_address'], self.swift_config['swift_store_auth_version'], tenant_name, username, self.swift_config['swift_store_key']) context = glance.context.RequestContext( tenant=tenant_id, service_catalog=service_catalog, auth_tok=auth_token) store = self.get_store(context=context) image_id = utils.generate_uuid() image_data = StringIO.StringIO('XXX') uri, _, _ = store.add(image_id, image_data, 3) location = glance.store.location.Location( self.store_name, store.get_store_location_class(), uri=uri, image_id=image_id) read_tenant = utils.generate_uuid() write_tenant = utils.generate_uuid() store.set_acls(location, public=False, read_tenants=[read_tenant], write_tenants=[write_tenant]) container_name = location.store_location.container container, _ = swift_get_container(self.swift_client, container_name) self.assertEqual(read_tenant, container.get('x-container-read')) self.assertEqual(write_tenant, container.get('x-container-write')) store.set_acls(location, public=True, read_tenants=[read_tenant]) container_name = location.store_location.container container, _ = swift_get_container(self.swift_client, container_name) self.assertEqual('.r:*', container.get('x-container-read')) self.assertEqual('', container.get('x-container-write', '')) (get_iter, get_size) = store.get(location) self.assertEqual('3', get_size) self.assertEqual('XXX', ''.join(get_iter)) store.delete(location)
def test_multitenant(self): """Ensure an image is properly configured when using multitenancy.""" fake_swift_admin = 'd2f68325-8e2c-4fb1-8c8b-89de2f3d9c4a' self.config(swift_store_multi_tenant=True, ) swift_store_user = self.swift_config['swift_store_user'] tenant_name, username = swift_store_user.split(':') tenant_id, auth_token, service_catalog = keystone_authenticate( self.swift_config['swift_store_auth_address'], self.swift_config['swift_store_auth_version'], tenant_name, username, self.swift_config['swift_store_key']) context = glance.context.RequestContext( tenant=tenant_id, service_catalog=service_catalog, auth_tok=auth_token) store = self.get_store(context=context) image_id = utils.generate_uuid() image_data = StringIO.StringIO('XXX') uri, _, _ = store.add(image_id, image_data, 3) location = glance.store.location.Location( self.store_name, store.get_store_location_class(), uri=uri, image_id=image_id) read_tenant = utils.generate_uuid() write_tenant = utils.generate_uuid() store.set_acls(location, public=False, read_tenants=[read_tenant], write_tenants=[write_tenant]) container_name = location.store_location.container container, _ = swift_get_container(self.swift_client, container_name) self.assertEqual(read_tenant, container.get('x-container-read')) self.assertEqual(write_tenant, container.get('x-container-write')) store.set_acls(location, public=True, read_tenants=[read_tenant]) container_name = location.store_location.container container, _ = swift_get_container(self.swift_client, container_name) self.assertEqual('.r:*', container.get('x-container-read')) self.assertEqual('', container.get('x-container-write', '')) (get_iter, get_size) = store.get(location) self.assertEqual('3', get_size) self.assertEqual('XXX', ''.join(get_iter)) store.delete(location)
def test_image_member_delete(self): TENANT1 = utils.generate_uuid() fixture = {'member': TENANT1, 'image_id': UUID1, 'can_share': True} member = self.db_api.image_member_create(self.context, fixture) self.assertEqual(1, len(self.db_api.image_member_find(self.context))) member = self.db_api.image_member_delete(self.context, member['id']) self.assertEqual(0, len(self.db_api.image_member_find(self.context)))
def test_get_remote_image(self): """Get an image that was created externally to Glance Rather than depending on local configuration to build a connetion to Swift, we depend on the URI of the image. """ image_id = utils.generate_uuid() container_name = self.swift_config['swift_store_container'] swift_put_object(self.swift_client, container_name, image_id, 'XXX') #NOTE(bcwaldon): This is a hack until we find a better way to # build this URL auth_url = self.swift_config['swift_store_auth_address'] auth_url = urlparse.urlparse(auth_url) user = urllib.quote(self.swift_config['swift_store_user']) key = self.swift_config['swift_store_key'] netloc = ''.join(('%s:%s' % (user, key), '@', auth_url.netloc)) path = os.path.join(auth_url.path, container_name, image_id) # This is an auth url with /<CONTAINER>/<OBJECT> on the end faux_obj_url = 'swift+http://%s%s' % (netloc, path) store = self.get_store() location = glance.store.location.Location( self.store_name, store.get_store_location_class(), uri=faux_obj_url) (get_iter, get_size) = store.get(location) self.assertEqual('3', get_size) self.assertEqual('XXX', ''.join(get_iter))
def test_create_with_id(self): request = test_utils.FakeRequest() image_id = utils.generate_uuid() request.body = json.dumps({'id': image_id}) output = self.deserializer.create(request) expected = {'image': {'id': image_id}} self.assertEqual(expected, output)
def test_get(self): """Test a "normal" retrieval of an image in chunks""" # First add an image... image_id = utils.generate_uuid() file_contents = "chunk00000remainder" location = "file://%s/%s" % (self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) location, size, checksum = self.store.add(image_id, image_file, len(file_contents)) # Now read it back... uri = "file:///%s/%s" % (self.test_dir, image_id) loc = get_location_from_uri(uri) (image_file, image_size) = self.store.get(loc) expected_data = "chunk00000remainder" expected_num_chunks = 2 data = "" num_chunks = 0 for chunk in image_file: num_chunks += 1 data += chunk self.assertEqual(expected_data, data) self.assertEqual(expected_num_chunks, num_chunks)
def test_add(self): """Test that we can add an image via the filesystem backend""" ChunkedFile.CHUNKSIZE = 1024 expected_image_id = utils.generate_uuid() expected_file_size = 1024 * 5 # 5K expected_file_contents = "*" * expected_file_size expected_checksum = hashlib.md5(expected_file_contents).hexdigest() expected_location = "file://%s/%s" % (self.test_dir, expected_image_id) image_file = StringIO.StringIO(expected_file_contents) location, size, checksum = self.store.add(expected_image_id, image_file, expected_file_size) self.assertEquals(expected_location, location) self.assertEquals(expected_file_size, size) self.assertEquals(expected_checksum, checksum) uri = "file:///%s/%s" % (self.test_dir, expected_image_id) loc = get_location_from_uri(uri) (new_image_file, new_image_size) = self.store.get(loc) new_image_contents = "" new_image_file_size = 0 for chunk in new_image_file: new_image_file_size += len(chunk) new_image_contents += chunk self.assertEquals(expected_file_contents, new_image_contents) self.assertEquals(expected_file_size, new_image_file_size)
def test_create_with_id(self): request = unit_test_utils.get_fake_request() image_id = utils.generate_uuid() request.body = json.dumps({'id': image_id}) output = self.deserializer.create(request) expected = {'image': {'id': image_id, 'properties': {}}} self.assertEqual(expected, output)
def _image_member_format(image_id, tenant_id, can_share): return { 'id': utils.generate_uuid(), 'image_id': image_id, 'member': tenant_id, 'can_share': can_share, }
def test_add(self): """Test that we can add an image via the s3 backend""" expected_image_id = utils.generate_uuid() expected_s3_size = FIVE_KB expected_s3_contents = "*" * expected_s3_size expected_checksum = hashlib.md5(expected_s3_contents).hexdigest() expected_location = format_s3_location(S3_CONF['s3_store_access_key'], S3_CONF['s3_store_secret_key'], S3_CONF['s3_store_host'], S3_CONF['s3_store_bucket'], expected_image_id) image_s3 = StringIO.StringIO(expected_s3_contents) location, size, checksum = self.store.add(expected_image_id, image_s3, expected_s3_size) self.assertEquals(expected_location, location) self.assertEquals(expected_s3_size, size) self.assertEquals(expected_checksum, checksum) loc = get_location_from_uri(expected_location) (new_image_s3, new_image_size) = self.store.get(loc) new_image_contents = StringIO.StringIO() for chunk in new_image_s3: new_image_contents.write(chunk) new_image_s3_size = new_image_contents.len self.assertEquals(expected_s3_contents, new_image_contents.getvalue()) self.assertEquals(expected_s3_size, new_image_s3_size)
def test_add(self): """Test that we can add an image via the s3 backend""" expected_image_id = utils.generate_uuid() expected_s3_size = FIVE_KB expected_s3_contents = "*" * expected_s3_size expected_checksum = hashlib.md5(expected_s3_contents).hexdigest() expected_location = format_s3_location( S3_CONF['s3_store_access_key'], S3_CONF['s3_store_secret_key'], S3_CONF['s3_store_host'], S3_CONF['s3_store_bucket'], expected_image_id) image_s3 = StringIO.StringIO(expected_s3_contents) location, size, checksum = self.store.add(expected_image_id, image_s3, expected_s3_size) self.assertEquals(expected_location, location) self.assertEquals(expected_s3_size, size) self.assertEquals(expected_checksum, checksum) loc = get_location_from_uri(expected_location) (new_image_s3, new_image_size) = self.store.get(loc) new_image_contents = StringIO.StringIO() for chunk in new_image_s3: new_image_contents.write(chunk) new_image_s3_size = new_image_contents.len self.assertEquals(expected_s3_contents, new_image_contents.getvalue()) self.assertEquals(expected_s3_size, new_image_s3_size)
def test_generate_uuid_format(self): """Check the format of a uuid""" uuid = utils.generate_uuid() self.assertTrue(isinstance(uuid, basestring)) self.assertTrue(len(uuid), 36) # make sure there are 4 dashes self.assertTrue(len(uuid.replace('-', '')), 36)
def test_lifecycle(self): """Add, get and delete an image""" store = self.get_store() image_id = utils.generate_uuid() image_data = StringIO.StringIO('XXX') image_checksum = 'bc9189406be84ec297464a514221406d' uri, add_size, add_checksum = store.add(image_id, image_data, 3) self.assertEqual(3, add_size) self.assertEqual(image_checksum, add_checksum) store = self.get_store() location = glance.store.location.Location( self.store_name, store.get_store_location_class(), uri=uri, image_id=image_id) (get_iter, get_size) = store.get(location) self.assertEqual('3', get_size) self.assertEqual('XXX', ''.join(get_iter)) store.delete(location) self.assertRaises(exception.NotFound, store.get, location)
def test_add(self): """Test that we can add an image via the filesystem backend""" ChunkedFile.CHUNKSIZE = 1024 expected_image_id = utils.generate_uuid() expected_file_size = 1024 * 5 # 5K expected_file_contents = "*" * expected_file_size expected_checksum = hashlib.md5(expected_file_contents).hexdigest() expected_location = "file://%s/%s" % (stubs.FAKE_FILESYSTEM_ROOTDIR, expected_image_id) image_file = StringIO.StringIO(expected_file_contents) location, size, checksum = self.store.add(expected_image_id, image_file, expected_file_size) self.assertEquals(expected_location, location) self.assertEquals(expected_file_size, size) self.assertEquals(expected_checksum, checksum) uri = "file:///tmp/glance-tests/%s" % expected_image_id loc = get_location_from_uri(uri) (new_image_file, new_image_size) = self.store.get(loc) new_image_contents = "" new_image_file_size = 0 for chunk in new_image_file: new_image_file_size += len(chunk) new_image_contents += chunk self.assertEquals(expected_file_contents, new_image_contents) self.assertEquals(expected_file_size, new_image_file_size)
def test_index_with_invalid_marker(self): fake_uuid = utils.generate_uuid() request = unit_test_utils.get_fake_request() self.assertRaises(webob.exc.HTTPBadRequest, self.controller.index, request, marker=fake_uuid)
def test_index(self): marker = utils.generate_uuid() path = "/images?limit=1&marker=%s" % marker request = unit_test_utils.get_fake_request(path) expected = {"limit": 1, "marker": marker, "sort_key": "created_at", "sort_dir": "desc", "filters": {}} output = self.deserializer.index(request) self.assertEqual(output, expected)
def test_index_with_non_default_is_public_filter(self): image = {"id": utils.generate_uuid(), "owner": TENANT3, "name": "3", "is_public": False} self.db.image_create(None, image) path = "/images?visibility=private" request = unit_test_utils.get_fake_request(path) output = self.controller.index(request, filters={"is_public": False}) self.assertEqual(2, len(output["images"]))
def test_create_with_id(self): request = test_utils.FakeRequest() image_id = utils.generate_uuid() request.body = json.dumps({'id': image_id}) output = self.deserializer.create(request) expected = {'image': {'id': image_id, 'properties': {}}} self.assertEqual(expected, output)
def test_create_with_id(self): request = test_utils.FakeRequest() image_id = utils.generate_uuid() request.body = json.dumps({'id': image_id, 'name': 'image-1'}) output = self.deserializer.create(request) self.assertEqual(output, {'image': {'id': image_id, 'name': 'image-1'}})
def test_index_with_marker_not_found(self): fake_uuid = utils.generate_uuid() path = '/images' request = unit_test_utils.get_fake_request(path) self.assertRaises(webob.exc.HTTPBadRequest, self.controller.index, request, marker=fake_uuid)
def test_index_with_many_filter(self): name = "My Little Image" instance_id = utils.generate_uuid() path = "/images?name=%(name)s&id=%(instance_id)s" % locals() request = unit_test_utils.get_fake_request(path) output = self.deserializer.index(request) self.assertEqual(output["filters"]["name"], name) self.assertEqual(output["filters"]["id"], instance_id)
def test_image_member_delete(self): TENANT1 = utils.generate_uuid() fixture = {'member': TENANT1, 'image_id': UUID1} member = self.db_api.image_member_create(self.context, fixture) member = self.db_api.image_member_delete(self.context, member) self.assertNotEqual(None, member['deleted_at']) self.assertTrue(isinstance(member['deleted_at'], datetime.datetime)) self.assertTrue(member['deleted'])
def test_index_with_many_filter(self): name = 'My Little Image' instance_id = utils.generate_uuid() path = '/images?name=%(name)s&id=%(instance_id)s' % locals() request = unit_test_utils.get_fake_request(path) output = self.deserializer.index(request) self.assertEqual(output['filters']['name'], name) self.assertEqual(output['filters']['id'], instance_id)
def test_create(self): fixture = { 'image_id': test_utils.UUID1, 'member': utils.generate_uuid(), 'can_share': True, } req = test_utils.FakeRequest() output = self.controller.create(req, fixture) self.assertEqual(fixture, output)
def test_index(self): marker = utils.generate_uuid() path = '/images?limit=1&marker=%s' % marker request = unit_test_utils.get_fake_request(path) expected = {'limit': 1, 'marker': marker, 'sort_key': 'created_at', 'sort_dir': 'desc'} output = self.deserializer.index(request) self.assertEqual(output, expected)
def test_image_get_all_owned(self): TENANT1 = utils.generate_uuid() ctxt1 = context.RequestContext(is_admin=False, tenant=TENANT1) UUIDX = utils.generate_uuid() self.db_api.image_create(ctxt1, {'id': UUIDX, 'status': 'queued', 'owner': TENANT1}) TENANT2 = utils.generate_uuid() ctxt2 = context.RequestContext(is_admin=False, tenant=TENANT2) UUIDY = utils.generate_uuid() self.db_api.image_create(ctxt2, {'id': UUIDY, 'status': 'queued', 'owner': TENANT2}) images = self.db_api.image_get_all(ctxt1) image_ids = [image['id'] for image in images] expected = [UUIDX, UUID3, UUID2, UUID1] self.assertEqual(sorted(expected), sorted(image_ids))
def test_get_remote_image(self): """Get an image that was created externally to Glance""" image_id = utils.generate_uuid() image_uri = self.stash_image(image_id, 'XXX') store = self.get_store() location = glance.store.location.Location( self.store_name, store.get_store_location_class(), uri=image_uri) (get_iter, get_size) = store.get(location) self.assertEqual('3', get_size) self.assertEqual('XXX', ''.join(get_iter))
def test_image_get_all_owned(self): TENANT1 = utils.generate_uuid() ctxt1 = context.RequestContext(is_admin=False, tenant=TENANT1) UUIDX = utils.generate_uuid() self.db_api.image_create(ctxt1, {'id': UUIDX, 'status': 'queued', 'owner': TENANT1}) TENANT2 = utils.generate_uuid() ctxt2 = context.RequestContext(is_admin=False, tenant=TENANT2) UUIDY = utils.generate_uuid() self.db_api.image_create(ctxt2, {'id': UUIDY, 'status': 'queued', 'owner': TENANT2}) # NOTE(bcwaldon): the is_public=True flag indicates that you want # to get all images that are public AND those that are owned by the # calling context images = self.db_api.image_get_all(ctxt1, filters={'is_public': True}) image_ids = [image['id'] for image in images] expected = [UUIDX, UUID3, UUID2, UUID1] self.assertEqual(sorted(expected), sorted(image_ids))
def test_show_deleted_properties(self): """ Ensure that the api filters out deleted image properties. """ # get the image properties into the odd state image = {"id": utils.generate_uuid(), "status": "active", "properties": {"poo": "bear"}} self.db.image_create(None, image) self.db.image_update(None, image["id"], {"properties": {"yin": "yang"}}, purge_props=True) request = unit_test_utils.get_fake_request() output = self.controller.show(request, image["id"]) self.assertEqual(output["properties"], {"yin": "yang"})
def test_index_with_non_default_is_public_filter(self): image = { 'id': utils.generate_uuid(), 'owner': TENANT3, 'name': '3', 'is_public': False } self.db.image_create(None, image) path = '/images?visibility=private' request = unit_test_utils.get_fake_request(path) output = self.controller.index(request, filters={'is_public': False}) self.assertEqual(2, len(output['images']))
def test_index(self): marker = utils.generate_uuid() path = '/images?limit=1&marker=%s' % marker request = unit_test_utils.get_fake_request(path) expected = { 'limit': 1, 'marker': marker, 'sort_key': 'created_at', 'sort_dir': 'desc', 'filters': {} } output = self.deserializer.index(request) self.assertEqual(output, expected)
def test_image_member_create(self): memberships = self.db_api.image_member_find(self.context) #NOTE(bcwaldon): we do this magic to translate sqlalchemy models # into something usable. memberships = [(m['member'], m['image_id']) for m in memberships] self.assertEqual([], memberships) TENANT1 = utils.generate_uuid() self.db_api.image_member_create(self.context, {'member': TENANT1, 'image_id': UUID1}) memberships = self.db_api.image_member_find(self.context) memberships = [(m['member'], m['image_id']) for m in memberships] self.assertEqual([(TENANT1, UUID1)], memberships)
def test_create(self): member = utils.generate_uuid() fixture = { 'member': member, 'can_share': True, } expected = { 'image_id': test_utils.UUID1, 'member': member, 'can_share': True, 'deleted': False, } req = test_utils.FakeRequest() output = self.controller.create(req, test_utils.UUID1, fixture) self.assertEqual(expected, output)
def _do_test_add_failure(self, errno, exception): ChunkedFile.CHUNKSIZE = 1024 image_id = utils.generate_uuid() file_size = 1024 * 5 # 5K file_contents = "*" * file_size location = "file://%s/%s" % (self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) def fake_IO_Error(size): e = IOError() e.errno = errno raise e self.stubs.Set(image_file, 'read', fake_IO_Error) self.assertRaises(exception, self.store.add, image_id, image_file, 0)
def _do_test_add_failure(self, errno, exception): ChunkedFile.CHUNKSIZE = 1024 image_id = utils.generate_uuid() file_size = 1024 * 5 # 5K file_contents = "*" * file_size location = "file://%s/%s" % (self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) def fake_IO_Error(size): e = IOError() e.errno = errno raise e self.stubs.Set(image_file, "read", fake_IO_Error) self.assertRaises(exception, self.store.add, image_id, image_file, 0)
def test_add_already_existing(self): """ Tests that adding an image with an existing identifier raises an appropriate exception """ ChunkedFile.CHUNKSIZE = 1024 image_id = utils.generate_uuid() file_size = 1024 * 5 # 5K file_contents = "*" * file_size location = "file://%s/%s" % (self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) location, size, checksum = self.store.add(image_id, image_file, file_size) image_file = StringIO.StringIO("nevergonnamakeit") self.assertRaises(exception.Duplicate, self.store.add, image_id, image_file, 0)
def test_image_member_create(self): memberships = self.db_api.image_member_find(self.context) #NOTE(bcwaldon): we do this magic to translate sqlalchemy models # into something usable. memberships = [(m['member'], m['image_id']) for m in memberships] self.assertEqual([], memberships) TENANT1 = utils.generate_uuid() self.db_api.image_member_create(self.context, { 'member': TENANT1, 'image_id': UUID1 }) memberships = self.db_api.image_member_find(self.context) memberships = [(m['member'], m['image_id']) for m in memberships] self.assertEqual([(TENANT1, UUID1)], memberships)
def test_image_paginate(self): """Paginate through a list of images using limit and marker""" extra_uuids = [utils.generate_uuid() for i in range(2)] extra_images = [build_image_fixture(id=_id) for _id in extra_uuids] self.create_images(extra_images) # Reverse uuids to match default sort of created_at extra_uuids.reverse() page = self.db_api.image_get_all(self.context, limit=2) self.assertEquals(extra_uuids, [i['id'] for i in page]) last = page[-1]['id'] page = self.db_api.image_get_all(self.context, limit=2, marker=last) self.assertEquals([UUID3, UUID2], [i['id'] for i in page]) page = self.db_api.image_get_all(self.context, limit=2, marker=UUID2) self.assertEquals([UUID1], [i['id'] for i in page])
def test_add_storage_full(self): """ Tests that adding an image without enough space on disk raises an appropriate exception """ ChunkedFile.CHUNKSIZE = 1024 image_id = utils.generate_uuid() file_size = 1024 * 5 # 5K file_contents = "*" * file_size location = "file://%s/%s" % (self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) def fake_IO_Error(size): raise IOError self.stubs.Set(image_file, 'read', fake_IO_Error) self.assertRaises(exception.StorageFull, self.store.add, image_id, image_file, 0)
def test_image_member_create(self): memberships = self.db_api.image_member_find(self.context) self.assertEqual([], memberships) TENANT1 = utils.generate_uuid() self.db_api.image_member_create(self.context, { 'member': TENANT1, 'image_id': UUID1 }) memberships = self.db_api.image_member_find(self.context) self.assertEqual(1, len(memberships)) actual = memberships[0] actual.pop('id') expected = { 'member': TENANT1, 'image_id': UUID1, 'can_share': False, } self.assertEqual(expected, actual)
def test_delete(self): """ Test we can delete an existing image in the filesystem store """ # First add an image image_id = utils.generate_uuid() file_size = 1024 * 5 # 5K file_contents = "*" * file_size location = "file://%s/%s" % (self.test_dir, image_id) image_file = StringIO.StringIO(file_contents) location, size, checksum = self.store.add(image_id, image_file, file_size) # Now check that we can delete it uri = "file:///%s/%s" % (self.test_dir, image_id) loc = get_location_from_uri(uri) self.store.delete(loc) self.assertRaises(exception.NotFound, self.store.get, loc)
def test_add_host_variations(self): """ Test that having http(s):// in the s3serviceurl in config options works as expected. """ variations = [ 'http://localhost:80', 'http://localhost', 'http://localhost/v1', 'http://localhost/v1/', 'https://localhost', 'https://localhost:8080', 'https://localhost/v1', 'https://localhost/v1/', 'localhost', 'localhost:8080/v1' ] for variation in variations: expected_image_id = utils.generate_uuid() expected_s3_size = FIVE_KB expected_s3_contents = "*" * expected_s3_size expected_checksum = \ hashlib.md5(expected_s3_contents).hexdigest() new_conf = S3_CONF.copy() new_conf['s3_store_host'] = variation expected_location = format_s3_location( new_conf['s3_store_access_key'], new_conf['s3_store_secret_key'], new_conf['s3_store_host'], new_conf['s3_store_bucket'], expected_image_id) image_s3 = StringIO.StringIO(expected_s3_contents) self.store = Store(test_utils.TestConfigOpts(new_conf)) location, size, checksum = self.store.add(expected_image_id, image_s3, expected_s3_size) self.assertEquals(expected_location, location) self.assertEquals(expected_s3_size, size) self.assertEquals(expected_checksum, checksum) loc = get_location_from_uri(expected_location) (new_image_s3, new_image_size) = self.store.get(loc) new_image_contents = new_image_s3.getvalue() new_image_s3_size = len(new_image_s3) self.assertEquals(expected_s3_contents, new_image_contents) self.assertEquals(expected_s3_size, new_image_s3_size)
def build_image_fixture(**kwargs): default_datetime = datetime.datetime.now() image = { 'id': utils.generate_uuid(), 'name': 'fake image #2', 'status': 'active', 'disk_format': 'vhd', 'container_format': 'ovf', 'is_public': True, 'created_at': default_datetime, 'updated_at': default_datetime, 'deleted_at': None, 'deleted': False, 'checksum': None, 'min_disk': 5, 'min_ram': 256, 'size': 19, 'location': "file:///tmp/glance-tests/2", 'properties': {}, } image.update(kwargs) return image
def test_image_member_update(self): TENANT1 = utils.generate_uuid() member = self.db_api.image_member_create(self.context, { 'member': TENANT1, 'image_id': UUID1 }) member_id = member.pop('id') expected = {'member': TENANT1, 'image_id': UUID1, 'can_share': False} self.assertEqual(expected, member) member = self.db_api.image_member_update(self.context, member_id, {'can_share': True}) member.pop('id') expected = {'member': TENANT1, 'image_id': UUID1, 'can_share': True} self.assertEqual(expected, member) members = self.db_api.image_member_find(self.context, member=TENANT1, image_id=UUID1) member = members[0] member.pop('id') self.assertEqual(expected, member)