test_object_store_service.py 4.6 KB

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