test_provider_object_store_service.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import StringIO
  2. import uuid
  3. from test.helpers import ProviderTestBase
  4. import test.helpers as helpers
  5. class ProviderObjectStoreServiceTestCase(ProviderTestBase):
  6. def __init__(self, methodName, provider):
  7. super(ProviderObjectStoreServiceTestCase, self).__init__(
  8. methodName=methodName, provider=provider)
  9. def test_crud_container(self):
  10. """
  11. Create a new container, check whether the expected values are set,
  12. and delete it
  13. """
  14. name = "cbtestcreatecontainer-{0}".format(uuid.uuid4())
  15. test_container = self.provider.object_store.create_container(name)
  16. with helpers.exception_action(lambda x: test_container.delete()):
  17. containers = self.provider.object_store.list_containers()
  18. found_containers = [c for c in containers if c.name == name]
  19. self.assertTrue(
  20. len(found_containers) == 1,
  21. "List containers does not return the expected container %s" %
  22. name)
  23. test_container.delete()
  24. def test_crud_container_objects(self):
  25. """
  26. Create a new container, upload some contents into the container, and
  27. check whether list properly detects the new content.
  28. Delete everything afterwards.
  29. """
  30. name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
  31. test_container = self.provider.object_store.create_container(name)
  32. # ensure that the container is empty
  33. objects = test_container.list()
  34. self.assertEqual([], objects)
  35. with helpers.exception_action(lambda x: test_container.delete()):
  36. obj_name = "hello_world.txt"
  37. obj = test_container.create_object(obj_name)
  38. with helpers.exception_action(lambda x: obj.delete()):
  39. # TODO: This is wrong. We shouldn't have to have a separate
  40. # call to upload some content before being able to delete
  41. # the content. Maybe the create_object method should accept
  42. # the file content as a parameter.
  43. obj.upload("dummy content")
  44. objs = test_container.list()
  45. found_objs = [o for o in objs if o.name == obj_name]
  46. print "FOUND: ", found_objs
  47. self.assertTrue(
  48. len(found_objs) == 1,
  49. "List container objects does not return the expected"
  50. " object %s" % obj_name)
  51. obj.delete()
  52. test_container.delete()
  53. def test_upload_download_container_content(self):
  54. name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
  55. test_container = self.provider.object_store.create_container(name)
  56. with helpers.exception_action(lambda x: test_container.delete()):
  57. obj_name = "hello_upload_download.txt"
  58. obj = test_container.create_object(obj_name)
  59. with helpers.exception_action(lambda x: obj.delete()):
  60. content = "Hello World. Here's some content"
  61. # TODO: Upload and download methods accept different parameter
  62. # types. Need to make this consistent - possibly provider
  63. # multiple methods like upload_from_file, from_stream etc.
  64. obj.upload(content)
  65. target_stream = StringIO.StringIO()
  66. obj.download(target_stream)
  67. self.assertEqual(target_stream.getvalue(), content)
  68. obj.delete()
  69. test_container.delete()