test_provider_object_store_service.py 4.2 KB

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