test_object_store_service.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from io import BytesIO
  2. import uuid
  3. from test.helpers import ProviderTestBase
  4. import test.helpers as helpers
  5. class CloudObjectStoreServiceTestCase(ProviderTestBase):
  6. def __init__(self, methodName, provider):
  7. super(CloudObjectStoreServiceTestCase, self).__init__(
  8. methodName=methodName, provider=provider)
  9. def test_crud_bucket(self):
  10. """
  11. Create a new bucket, check whether the expected values are set,
  12. and delete it.
  13. """
  14. name = "cbtestcreatebucket-{0}".format(uuid.uuid4())
  15. test_bucket = self.provider.object_store.create(name)
  16. with helpers.cleanup_action(lambda: test_bucket.delete()):
  17. self.assertTrue(
  18. test_bucket.id in repr(test_bucket),
  19. "repr(obj) should contain the object id so that the object"
  20. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  21. buckets = self.provider.object_store.list()
  22. found_buckets = [c for c in buckets if c.name == name]
  23. self.assertTrue(
  24. len(found_buckets) == 1,
  25. "List buckets does not return the expected bucket %s" %
  26. name)
  27. # check iteration
  28. found_buckets = [c for c in self.provider.object_store
  29. if c.name == name]
  30. self.assertTrue(
  31. len(found_buckets) == 1,
  32. "Iter buckets does not return the expected bucket %s" %
  33. name)
  34. get_bucket = self.provider.object_store.get(
  35. test_bucket.id)
  36. self.assertTrue(
  37. found_buckets[0] ==
  38. get_bucket == test_bucket,
  39. "Objects returned by list: {0} and get: {1} are not as "
  40. " expected: {2}" .format(found_buckets[0].id,
  41. get_bucket.id,
  42. test_bucket.name))
  43. buckets = self.provider.object_store.list()
  44. found_buckets = [c for c in buckets if c.name == name]
  45. self.assertTrue(
  46. len(found_buckets) == 0,
  47. "Bucket %s should have been deleted but still exists." %
  48. name)
  49. def test_crud_bucket_objects(self):
  50. """
  51. Create a new bucket, upload some contents into the bucket, and
  52. check whether list properly detects the new content.
  53. Delete everything afterwards.
  54. """
  55. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  56. test_bucket = self.provider.object_store.create(name)
  57. # ensure that the bucket is empty
  58. objects = test_bucket.list()
  59. self.assertEqual([], objects)
  60. with helpers.cleanup_action(lambda: test_bucket.delete()):
  61. obj_name = "hello_world.txt"
  62. obj = test_bucket.create_object(obj_name)
  63. self.assertTrue(
  64. obj.id in repr(obj),
  65. "repr(obj) should contain the object id so that the object"
  66. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  67. with helpers.cleanup_action(lambda: obj.delete()):
  68. # TODO: This is wrong. We shouldn't have to have a separate
  69. # call to upload some content before being able to delete
  70. # the content. Maybe the create_object method should accept
  71. # the file content as a parameter.
  72. obj.upload("dummy content")
  73. objs = test_bucket.list()
  74. # check iteration
  75. iter_objs = list(test_bucket)
  76. self.assertListEqual(iter_objs, objs)
  77. found_objs = [o for o in objs if o.name == obj_name]
  78. self.assertTrue(
  79. len(found_objs) == 1,
  80. "List bucket objects does not return the expected"
  81. " object %s" % obj_name)
  82. self.assertTrue(
  83. found_objs[0] == obj,
  84. "Objects returned by list: {0} are not as "
  85. " expected: {1}" .format(found_objs[0].id,
  86. obj.id))
  87. objs = test_bucket.list()
  88. found_objs = [o for o in objs if o.name == obj_name]
  89. self.assertTrue(
  90. len(found_objs) == 0,
  91. "Object %s should have been deleted but still exists." %
  92. obj_name)
  93. def test_upload_download_bucket_content(self):
  94. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  95. test_bucket = self.provider.object_store.create(name)
  96. with helpers.cleanup_action(lambda: test_bucket.delete()):
  97. obj_name = "hello_upload_download.txt"
  98. obj = test_bucket.create_object(obj_name)
  99. with helpers.cleanup_action(lambda: obj.delete()):
  100. content = b"Hello World. Here's some content."
  101. # TODO: Upload and download methods accept different parameter
  102. # types. Need to make this consistent - possibly provider
  103. # multiple methods like upload_from_file, from_stream etc.
  104. obj.upload(content)
  105. target_stream = BytesIO()
  106. obj.download(target_stream)
  107. self.assertEqual(target_stream.getvalue(), content)