test_object_store_service.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 test.helpers import standard_interface_tests as sit
  10. from unittest import skip
  11. from cloudbridge.cloud.interfaces.exceptions import InvalidNameException
  12. from cloudbridge.cloud.interfaces.resources import Bucket
  13. from cloudbridge.cloud.interfaces.resources import BucketObject
  14. import requests
  15. class CloudObjectStoreServiceTestCase(ProviderTestBase):
  16. @helpers.skipIfNoService(['object_store'])
  17. def test_crud_bucket(self):
  18. """
  19. Create a new bucket, check whether the expected values are set,
  20. and delete it.
  21. """
  22. def create_bucket(name):
  23. return self.provider.object_store.create(name)
  24. def cleanup_bucket(bucket):
  25. bucket.delete()
  26. with self.assertRaises(InvalidNameException):
  27. # underscores are not allowed in bucket names
  28. create_bucket("cb_bucket")
  29. with self.assertRaises(InvalidNameException):
  30. # names of length less than 3 should raise an exception
  31. create_bucket("cb")
  32. with self.assertRaises(InvalidNameException):
  33. # names of length greater than 63 should raise an exception
  34. create_bucket("a" * 64)
  35. with self.assertRaises(InvalidNameException):
  36. # bucket name cannot be an IP address
  37. create_bucket("197.10.100.42")
  38. sit.check_crud(self, self.provider.object_store, Bucket,
  39. "cb-crudbucket", create_bucket, cleanup_bucket,
  40. skip_name_check=True)
  41. @helpers.skipIfNoService(['object_store'])
  42. def test_crud_bucket_object(self):
  43. test_bucket = None
  44. def create_bucket_obj(name):
  45. obj = test_bucket.create_object(name)
  46. # TODO: This is wrong. We shouldn't have to have a separate
  47. # call to upload some content before being able to delete
  48. # the content. Maybe the create_object method should accept
  49. # the file content as a parameter.
  50. obj.upload("dummy content")
  51. return obj
  52. def cleanup_bucket_obj(bucket_obj):
  53. bucket_obj.delete()
  54. with helpers.cleanup_action(lambda: test_bucket.delete()):
  55. name = "cb-crudbucketobj-{0}".format(uuid.uuid4())
  56. test_bucket = self.provider.object_store.create(name)
  57. sit.check_crud(self, test_bucket, BucketObject,
  58. "cb_bucketobj", create_bucket_obj,
  59. cleanup_bucket_obj, skip_name_check=True)
  60. @helpers.skipIfNoService(['object_store'])
  61. def test_crud_bucket_object_properties(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. with helpers.cleanup_action(lambda: obj.delete()):
  77. # TODO: This is wrong. We shouldn't have to have a separate
  78. # call to upload some content before being able to delete
  79. # the content. Maybe the create_object method should accept
  80. # the file content as a parameter.
  81. obj.upload("dummy content")
  82. objs = test_bucket.list()
  83. self.assertTrue(
  84. isinstance(objs[0].size, int),
  85. "Object size property needs to be a int, not {0}".format(
  86. type(objs[0].size)))
  87. self.assertTrue(
  88. datetime.strptime(objs[0].last_modified[:23],
  89. "%Y-%m-%dT%H:%M:%S.%f"),
  90. "Object's last_modified field format {0} not matching."
  91. .format(objs[0].last_modified))
  92. # check iteration
  93. iter_objs = list(test_bucket)
  94. self.assertListEqual(iter_objs, objs)
  95. obj_too = test_bucket.get(obj_name)
  96. self.assertTrue(
  97. isinstance(obj_too, BucketObject),
  98. "Did not get object {0} of expected type.".format(obj_too))
  99. prefix_filtered_list = test_bucket.list(prefix=obj_name_prefix)
  100. self.assertTrue(
  101. len(objs) == len(prefix_filtered_list) == 1,
  102. 'The number of objects returned by list function, '
  103. 'with and without a prefix, are expected to be equal, '
  104. 'but its detected otherwise.')
  105. sit.check_delete(self, test_bucket, obj)
  106. @helpers.skipIfNoService(['object_store'])
  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)
  126. @skip("Skip until OpenStack implementation is provided")
  127. @helpers.skipIfNoService(['object_store'])
  128. def test_generate_url(self):
  129. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  130. test_bucket = self.provider.object_store.create(name)
  131. with helpers.cleanup_action(lambda: test_bucket.delete()):
  132. obj_name = "hello_upload_download.txt"
  133. obj = test_bucket.create_object(obj_name)
  134. with helpers.cleanup_action(lambda: obj.delete()):
  135. content = b"Hello World. Generate a url."
  136. obj.upload(content)
  137. target_stream = BytesIO()
  138. obj.save_content(target_stream)
  139. url = obj.generate_url(100)
  140. self.assertEqual(requests.get(url).content, content)
  141. @helpers.skipIfNoService(['object_store'])
  142. def test_upload_download_bucket_content_from_file(self):
  143. name = "cbtestbucketobjs-{0}".format(uuid.uuid4())
  144. test_bucket = self.provider.object_store.create(name)
  145. with helpers.cleanup_action(lambda: test_bucket.delete()):
  146. obj_name = "hello_upload_download.txt"
  147. obj = test_bucket.create_object(obj_name)
  148. with helpers.cleanup_action(lambda: obj.delete()):
  149. test_file = os.path.join(
  150. helpers.get_test_fixtures_folder(), 'logo.jpg')
  151. obj.upload_from_file(test_file)
  152. target_stream = BytesIO()
  153. obj.save_content(target_stream)
  154. with open(test_file, 'rb') as f:
  155. self.assertEqual(target_stream.getvalue(), f.read())
  156. @skip("Skip unless you want to test swift objects bigger than 5 Gig")
  157. @helpers.skipIfNoService(['object_store'])
  158. def test_upload_download_bucket_content_with_large_file(self):
  159. """
  160. Creates a 6 Gig file in the temp directory, then uploads it to
  161. Swift. Once uploaded, then downloads to a new file in the temp
  162. directory and compares the two files to see if they match.
  163. """
  164. temp_dir = tempfile.gettempdir()
  165. file_name = '6GigTest.tmp'
  166. six_gig_file = os.path.join(temp_dir, file_name)
  167. with open(six_gig_file, "wb") as out:
  168. out.truncate(6 * 1024 * 1024 * 1024) # 6 Gig...
  169. with helpers.cleanup_action(lambda: os.remove(six_gig_file)):
  170. download_file = "{0}/cbtestfile-{1}".format(temp_dir, file_name)
  171. bucket_name = "cbtestbucketlargeobjs-{0}".format(uuid.uuid4())
  172. test_bucket = self.provider.object_store.create(bucket_name)
  173. with helpers.cleanup_action(lambda: test_bucket.delete()):
  174. test_obj = test_bucket.create_object(file_name)
  175. with helpers.cleanup_action(lambda: test_obj.delete()):
  176. file_uploaded = test_obj.upload_from_file(six_gig_file)
  177. self.assertTrue(file_uploaded, "Could not upload object?")
  178. with helpers.cleanup_action(
  179. lambda: os.remove(download_file)):
  180. with open(download_file, 'wb') as f:
  181. test_obj.save_content(f)
  182. self.assertTrue(
  183. filecmp.cmp(six_gig_file, download_file),
  184. "Uploaded file != downloaded")