test_object_store_service.py 8.7 KB

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