test_provider_object_store_service.py 4.4 KB

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