test_provider_object_store_service.py 4.1 KB

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