test_object_store_service.py 5.4 KB

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