test_object_store_service.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from datetime import datetime
  2. from io import BytesIO
  3. import uuid
  4. import requests
  5. import tempfile
  6. from cloudbridge.cloud.interfaces.resources import BucketObject
  7. from test.helpers import ProviderTestBase
  8. import test.helpers as helpers
  9. class CloudObjectStoreServiceTestCase(ProviderTestBase):
  10. def __init__(self, methodName, provider):
  11. super(CloudObjectStoreServiceTestCase, self).__init__(
  12. methodName=methodName, provider=provider)
  13. @helpers.skipIfNoService(['object_store'])
  14. def test_crud_bucket(self):
  15. """
  16. Create a new bucket, check whether the expected values are set,
  17. and delete it.
  18. """
  19. name = "cbtestcreatebucket-{0}".format(uuid.uuid4())
  20. test_bucket = self.provider.object_store.create(name)
  21. with helpers.cleanup_action(lambda: test_bucket.delete()):
  22. self.assertTrue(
  23. test_bucket.id in repr(test_bucket),
  24. "repr(obj) should contain the object id so that the object"
  25. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  26. buckets = self.provider.object_store.list()
  27. list_buckets = [c for c in buckets if c.name == name]
  28. self.assertTrue(
  29. len(list_buckets) == 1,
  30. "List buckets does not return the expected bucket %s" %
  31. name)
  32. # check iteration
  33. iter_buckets = [c for c in self.provider.object_store
  34. if c.name == name]
  35. self.assertTrue(
  36. len(iter_buckets) == 1,
  37. "Iter buckets does not return the expected bucket %s" %
  38. name)
  39. # check find
  40. find_buckets = self.provider.object_store.find(name=name)
  41. self.assertTrue(
  42. len(find_buckets) == 1,
  43. "Find buckets does not return the expected bucket %s" %
  44. name)
  45. get_bucket = self.provider.object_store.get(
  46. test_bucket.id)
  47. self.assertTrue(
  48. list_buckets[0] ==
  49. get_bucket == test_bucket,
  50. "Objects returned by list: {0} and get: {1} are not as "
  51. " expected: {2}" .format(list_buckets[0].id,
  52. get_bucket.id,
  53. test_bucket.name))
  54. buckets = self.provider.object_store.list()
  55. found_buckets = [c for c in buckets if c.name == name]
  56. self.assertTrue(
  57. len(found_buckets) == 0,
  58. "Bucket %s should have been deleted but still exists." %
  59. name)
  60. @helpers.skipIfNoService(['object_store'])
  61. def test_crud_bucket_objects(self):
  62. """
  63. Create a new bucket, upload some contents into the bucket, and
  64. check whether list properly detects the new content.
  65. Delete everything afterwards.
  66. """
  67. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  68. test_bucket = self.provider.object_store.create(name)
  69. # ensure that the bucket is empty
  70. objects = test_bucket.list()
  71. self.assertEqual([], objects)
  72. with helpers.cleanup_action(lambda: test_bucket.delete()):
  73. obj_name_prefix = "hello"
  74. obj_name = obj_name_prefix + "_world.txt"
  75. obj = test_bucket.create_object(obj_name)
  76. self.assertTrue(
  77. obj.id in repr(obj),
  78. "repr(obj) should contain the object id so that the object"
  79. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  80. with helpers.cleanup_action(lambda: obj.delete()):
  81. # TODO: This is wrong. We shouldn't have to have a separate
  82. # call to upload some content before being able to delete
  83. # the content. Maybe the create_object method should accept
  84. # the file content as a parameter.
  85. obj.upload("dummy content")
  86. objs = test_bucket.list()
  87. self.assertTrue(
  88. isinstance(objs[0].size, int),
  89. "Object size property needs to be a int, not {0}".format(
  90. type(objs[0].size)))
  91. self.assertTrue(
  92. datetime.strptime(objs[0].last_modified[:23],
  93. "%Y-%m-%dT%H:%M:%S.%f"),
  94. "Object's last_modified field format {0} not matching."
  95. .format(objs[0].last_modified))
  96. # check iteration
  97. iter_objs = list(test_bucket)
  98. self.assertListEqual(iter_objs, objs)
  99. found_objs = [o for o in objs if o.name == obj_name]
  100. self.assertTrue(
  101. len(found_objs) == 1,
  102. "List bucket objects does not return the expected"
  103. " object %s" % obj_name)
  104. get_bucket_obj = test_bucket.get(obj_name)
  105. self.assertTrue(
  106. found_objs[0] ==
  107. get_bucket_obj == obj,
  108. "Objects returned by list: {0} and get: {1} are not as "
  109. " expected: {2}" .format(found_objs[0].id,
  110. get_bucket_obj.id,
  111. obj.id))
  112. obj_too = test_bucket.get(obj_name)
  113. self.assertTrue(
  114. isinstance(obj_too, BucketObject),
  115. "Did not get object {0} of expected type.".format(obj_too))
  116. prefix_filtered_list = test_bucket.list(prefix=obj_name_prefix)
  117. self.assertTrue(
  118. len(objs) == len(prefix_filtered_list) == 1,
  119. 'The number of objects returned by list function, '
  120. 'with and without a prefix, are expected to be equal, '
  121. 'but its detected otherwise.')
  122. objs = test_bucket.list()
  123. found_objs = [o for o in objs if o.name == obj_name]
  124. self.assertTrue(
  125. len(found_objs) == 0,
  126. "Object %s should have been deleted but still exists." %
  127. obj_name)
  128. @helpers.skipIfNoService(['object_store'])
  129. def test_upload_download_bucket_content(self):
  130. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  131. test_bucket = self.provider.object_store.create(name)
  132. with helpers.cleanup_action(lambda: test_bucket.delete()):
  133. obj_name = "hello_upload_download.txt"
  134. obj = test_bucket.create_object(obj_name)
  135. with helpers.cleanup_action(lambda: obj.delete()):
  136. content = b"Hello World. Here's some content."
  137. # TODO: Upload and download methods accept different parameter
  138. # types. Need to make this consistent - possibly provider
  139. # multiple methods like upload_from_file, from_stream etc.
  140. obj.upload(content)
  141. target_stream = BytesIO()
  142. obj.save_content(target_stream)
  143. self.assertEqual(target_stream.getvalue(), content)
  144. target_stream2 = BytesIO()
  145. for data in obj.iter_content():
  146. target_stream2.write(data)
  147. self.assertEqual(target_stream2.getvalue(), content)
  148. @helpers.skipIfNoService(['object_store'])
  149. def test_generate_url(self):
  150. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  151. test_bucket = self.provider.object_store.create(name)
  152. with helpers.cleanup_action(lambda: test_bucket.delete()):
  153. obj_name = "hello_upload_download.txt"
  154. obj = test_bucket.create_object(obj_name)
  155. with helpers.cleanup_action(lambda: obj.delete()):
  156. content = b"Hello World. Generate a url."
  157. obj.upload(content)
  158. target_stream = BytesIO()
  159. obj.save_content(target_stream)
  160. url = obj.generate_url(100)
  161. self.assertEqual(requests.get(url).content, content)
  162. @helpers.skipIfNoService(['object_store'])
  163. def test_upload_download_bucket_content_from_file(self):
  164. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  165. test_bucket = self.provider.object_store.create(name)
  166. with helpers.cleanup_action(lambda: test_bucket.delete()):
  167. obj_name = "hello_upload_download.txt"
  168. obj = test_bucket.create_object(obj_name)
  169. with helpers.cleanup_action(lambda: obj.delete()):
  170. content = b"Hello World. Upload from file."
  171. with tempfile.NamedTemporaryFile() as tmpFile:
  172. tmpFile.write(content)
  173. tmpFile.flush()
  174. obj.upload_from_file(tmpFile.name)
  175. target_stream = BytesIO()
  176. obj.save_content(target_stream)
  177. self.assertEqual(target_stream.getvalue(), content)