test_object_store_service.py 6.1 KB

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