test_object_store_service.py 5.1 KB

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