test_object_store_service.py 8.8 KB

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