test_object_store_service.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import filecmp
  2. import os
  3. import tempfile
  4. import uuid
  5. from datetime import datetime
  6. from io import BytesIO
  7. from test import helpers
  8. from test.helpers import ProviderTestBase
  9. from unittest import skip
  10. from cloudbridge.cloud.interfaces.resources import BucketObject
  11. import requests
  12. class CloudObjectStoreServiceTestCase(ProviderTestBase):
  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. @skip("Skip until OpenStack implementation is provided")
  149. @helpers.skipIfNoService(['object_store'])
  150. def test_generate_url(self):
  151. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  152. test_bucket = self.provider.object_store.create(name)
  153. with helpers.cleanup_action(lambda: test_bucket.delete()):
  154. obj_name = "hello_upload_download.txt"
  155. obj = test_bucket.create_object(obj_name)
  156. with helpers.cleanup_action(lambda: obj.delete()):
  157. content = b"Hello World. Generate a url."
  158. obj.upload(content)
  159. target_stream = BytesIO()
  160. obj.save_content(target_stream)
  161. url = obj.generate_url(100)
  162. self.assertEqual(requests.get(url).content, content)
  163. @helpers.skipIfNoService(['object_store'])
  164. def test_upload_download_bucket_content_from_file(self):
  165. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  166. test_bucket = self.provider.object_store.create(name)
  167. with helpers.cleanup_action(lambda: test_bucket.delete()):
  168. obj_name = "hello_upload_download.txt"
  169. obj = test_bucket.create_object(obj_name)
  170. with helpers.cleanup_action(lambda: obj.delete()):
  171. test_file = os.path.join(
  172. helpers.get_test_fixtures_folder(), 'logo.jpg')
  173. obj.upload_from_file(test_file)
  174. target_stream = BytesIO()
  175. obj.save_content(target_stream)
  176. with open(test_file, 'rb') as f:
  177. self.assertEqual(target_stream.getvalue(), f.read())
  178. @skip("Skip unless you want to test swift objects bigger than 5 Gig")
  179. @helpers.skipIfNoService(['object_store'])
  180. def test_upload_download_bucket_content_with_large_file(self):
  181. """
  182. Creates a 6 Gig file in the temp directory, then uploads it to
  183. Swift. Once uploaded, then downloads to a new file in the temp
  184. directory and compares the two files to see if they match.
  185. """
  186. temp_dir = tempfile.gettempdir()
  187. file_name = '6GigTest.tmp'
  188. six_gig_file = os.path.join(temp_dir, file_name)
  189. with open(six_gig_file, "wb") as out:
  190. out.truncate(6 * 1024 * 1024 * 1024) # 6 Gig...
  191. with helpers.cleanup_action(lambda: os.remove(six_gig_file)):
  192. download_file = "{0}/cbtestfile-{1}".format(temp_dir, file_name)
  193. bucket_name = "cbtestbucketlargeobjs-{0}".format(uuid.uuid4())
  194. test_bucket = self.provider.object_store.create(bucket_name)
  195. with helpers.cleanup_action(lambda: test_bucket.delete()):
  196. test_obj = test_bucket.create_object(file_name)
  197. with helpers.cleanup_action(lambda: test_obj.delete()):
  198. file_uploaded = test_obj.upload_from_file(six_gig_file)
  199. self.assertTrue(file_uploaded, "Could not upload object?")
  200. with helpers.cleanup_action(
  201. lambda: os.remove(download_file)):
  202. with open(download_file, 'wb') as f:
  203. test_obj.save_content(f)
  204. self.assertTrue(
  205. filecmp.cmp(six_gig_file, download_file),
  206. "Uploaded file != downloaded")