test_object_store_service.py 6.6 KB

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