test_provider_object_store_service.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. containers = self.provider.object_store.list_containers()
  25. found_containers = [c for c in containers if c.name == name]
  26. self.assertTrue(
  27. len(found_containers) == 0,
  28. "Container %s should have been deleted but still exists." %
  29. name)
  30. def test_crud_container_objects(self):
  31. """
  32. Create a new container, upload some contents into the container, and
  33. check whether list properly detects the new content.
  34. Delete everything afterwards.
  35. """
  36. name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
  37. test_container = self.provider.object_store.create_container(name)
  38. # ensure that the container is empty
  39. objects = test_container.list()
  40. self.assertEqual([], objects)
  41. with helpers.exception_action(lambda x: test_container.delete()):
  42. obj_name = "hello_world.txt"
  43. obj = test_container.create_object(obj_name)
  44. with helpers.exception_action(lambda x: obj.delete()):
  45. # TODO: This is wrong. We shouldn't have to have a separate
  46. # call to upload some content before being able to delete
  47. # the content. Maybe the create_object method should accept
  48. # the file content as a parameter.
  49. obj.upload("dummy content")
  50. objs = test_container.list()
  51. found_objs = [o for o in objs if o.name == obj_name]
  52. self.assertTrue(
  53. len(found_objs) == 1,
  54. "List container objects does not return the expected"
  55. " object %s" % obj_name)
  56. obj.delete()
  57. objs = test_container.list()
  58. found_objs = [o for o in objs if o.name == obj_name]
  59. self.assertTrue(
  60. len(found_objs) == 0,
  61. "Object %s should have been deleted but still exists." %
  62. obj_name)
  63. test_container.delete()
  64. def test_upload_download_container_content(self):
  65. name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
  66. test_container = self.provider.object_store.create_container(name)
  67. with helpers.exception_action(lambda x: test_container.delete()):
  68. obj_name = "hello_upload_download.txt"
  69. obj = test_container.create_object(obj_name)
  70. with helpers.exception_action(lambda x: obj.delete()):
  71. content = "Hello World. Here's some content"
  72. # TODO: Upload and download methods accept different parameter
  73. # types. Need to make this consistent - possibly provider
  74. # multiple methods like upload_from_file, from_stream etc.
  75. obj.upload(content)
  76. target_stream = StringIO.StringIO()
  77. obj.download(target_stream)
  78. self.assertEqual(target_stream.getvalue(), content)
  79. obj.delete()
  80. test_container.delete()