test_object_store_service.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from io import BytesIO
  2. import uuid
  3. from test.helpers import ProviderTestBase
  4. import test.helpers as helpers
  5. class CloudObjectStoreServiceTestCase(ProviderTestBase):
  6. def __init__(self, methodName, provider):
  7. super(CloudObjectStoreServiceTestCase, 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(name)
  16. with helpers.cleanup_action(lambda: test_container.delete()):
  17. self.assertTrue(
  18. test_container.id in repr(test_container),
  19. "repr(obj) should contain the object id so that the object"
  20. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  21. containers = self.provider.object_store.list()
  22. found_containers = [c for c in containers if c.name == name]
  23. self.assertTrue(
  24. len(found_containers) == 1,
  25. "List containers does not return the expected container %s" %
  26. name)
  27. # check iteration
  28. found_containers = [c for c in self.provider.object_store
  29. if c.name == name]
  30. self.assertTrue(
  31. len(found_containers) == 1,
  32. "Iter containers does not return the expected container %s" %
  33. name)
  34. get_container = self.provider.object_store.get(
  35. test_container.id)
  36. self.assertTrue(
  37. found_containers[0] ==
  38. get_container == test_container,
  39. "Objects returned by list: {0} and get: {1} are not as "
  40. " expected: {2}" .format(found_containers[0].id,
  41. get_container.id,
  42. test_container.name))
  43. containers = self.provider.object_store.list()
  44. found_containers = [c for c in containers if c.name == name]
  45. self.assertTrue(
  46. len(found_containers) == 0,
  47. "Container %s should have been deleted but still exists." %
  48. name)
  49. def test_crud_container_objects(self):
  50. """
  51. Create a new container, upload some contents into the container, and
  52. check whether list properly detects the new content.
  53. Delete everything afterwards.
  54. """
  55. name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
  56. test_container = self.provider.object_store.create(name)
  57. # ensure that the container is empty
  58. objects = test_container.list()
  59. self.assertEqual([], objects)
  60. with helpers.cleanup_action(lambda: test_container.delete()):
  61. obj_name = "hello_world.txt"
  62. obj = test_container.create_object(obj_name)
  63. self.assertTrue(
  64. obj.id in repr(obj),
  65. "repr(obj) should contain the object id so that the object"
  66. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  67. with helpers.cleanup_action(lambda: obj.delete()):
  68. # TODO: This is wrong. We shouldn't have to have a separate
  69. # call to upload some content before being able to delete
  70. # the content. Maybe the create_object method should accept
  71. # the file content as a parameter.
  72. obj.upload("dummy content")
  73. objs = test_container.list()
  74. # check iteration
  75. iter_objs = list(test_container)
  76. self.assertListEqual(iter_objs, objs)
  77. found_objs = [o for o in objs if o.name == obj_name]
  78. self.assertTrue(
  79. len(found_objs) == 1,
  80. "List container objects does not return the expected"
  81. " object %s" % obj_name)
  82. self.assertTrue(
  83. found_objs[0] == obj,
  84. "Objects returned by list: {0} are not as "
  85. " expected: {1}" .format(found_objs[0].id,
  86. obj.id))
  87. objs = test_container.list()
  88. found_objs = [o for o in objs if o.name == obj_name]
  89. self.assertTrue(
  90. len(found_objs) == 0,
  91. "Object %s should have been deleted but still exists." %
  92. obj_name)
  93. def test_upload_download_container_content(self):
  94. name = "cbtestcontainerobjs-{0}".format(uuid.uuid4())
  95. test_container = self.provider.object_store.create(name)
  96. with helpers.cleanup_action(lambda: test_container.delete()):
  97. obj_name = "hello_upload_download.txt"
  98. obj = test_container.create_object(obj_name)
  99. with helpers.cleanup_action(lambda: obj.delete()):
  100. content = b"Hello World. Here's some content"
  101. # TODO: Upload and download methods accept different parameter
  102. # types. Need to make this consistent - possibly provider
  103. # multiple methods like upload_from_file, from_stream etc.
  104. obj.upload(content)
  105. target_stream = BytesIO()
  106. obj.download(target_stream)
  107. self.assertEqual(target_stream.getvalue(), content)