test_provider_object_store_service.py 4.6 KB

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