standard_interface_tests.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. """
  2. Standard tests for behaviour common across the whole of cloudbridge.
  3. This includes:
  4. 1. Checking that every resource has an id property
  5. 2. Checking for object equality and repr
  6. 3. Checking standard behaviour for list, iter, find, get, delete
  7. """
  8. import test.helpers as helpers
  9. import uuid
  10. from cloudbridge.cloud.interfaces.exceptions \
  11. import InvalidNameException
  12. from cloudbridge.cloud.interfaces.resources import ObjectLifeCycleMixin
  13. from cloudbridge.cloud.interfaces.resources import ResultList
  14. def check_repr(test, obj):
  15. test.assertTrue(
  16. obj.id in repr(obj),
  17. "repr(obj) for %s contain the object id so that the object"
  18. " can be reconstructed, but does not. eval(repr(obj)) == obj"
  19. % (type(obj).__name__,))
  20. def check_json(test, obj):
  21. val = obj.to_json()
  22. test.assertEqual(val.get('id'), obj.id)
  23. test.assertEqual(val.get('name'), obj.name)
  24. def check_obj_properties(test, obj):
  25. test.assertEqual(obj, obj, "Object should be equal to itself")
  26. test.assertFalse(obj != obj, "Object inequality should be false")
  27. check_obj_name(test, obj)
  28. def check_list(test, service, obj):
  29. list_objs = service.list()
  30. test.assertIsInstance(list_objs, ResultList)
  31. all_records = list_objs
  32. while list_objs.is_truncated:
  33. list_objs = service.list(marker=list_objs.marker)
  34. all_records += list_objs
  35. match_objs = [o for o in all_records if o.id == obj.id]
  36. test.assertTrue(
  37. len(match_objs) == 1,
  38. "List objects for %s does not return the expected object id %s. Got %s"
  39. % (type(obj).__name__, obj.id, match_objs))
  40. return match_objs
  41. def check_iter(test, service, obj):
  42. # check iteration
  43. iter_objs = list(service)
  44. iter_ids = [o.id for o in service]
  45. test.assertEqual(len(set(iter_ids)), len(iter_ids),
  46. "Iteration should not return duplicates")
  47. match_objs = [o for o in iter_objs if o.id == obj.id]
  48. test.assertTrue(
  49. len(match_objs) == 1,
  50. "Iter objects for %s does not return the expected object id %s. Got %s"
  51. % (type(obj).__name__, obj.id, match_objs))
  52. return match_objs
  53. def check_find(test, service, obj):
  54. # check find
  55. find_objs = service.find(name=obj.name)
  56. test.assertTrue(
  57. len(find_objs) == 1,
  58. "Find objects for %s does not return the expected object: %s. Got %s"
  59. % (type(obj).__name__, obj.name, find_objs))
  60. return find_objs
  61. def check_find_non_existent(test, service):
  62. # check find
  63. find_objs = service.find(name="random_imagined_obj_name")
  64. test.assertTrue(
  65. len(find_objs) == 0,
  66. "Find non-existent object for %s returned unexpected objects: %s"
  67. % (type(service).__name__, find_objs))
  68. def check_get(test, service, obj):
  69. get_obj = service.get(obj.id)
  70. test.assertEqual(get_obj, obj)
  71. test.assertIsInstance(get_obj, type(obj))
  72. return get_obj
  73. def check_get_non_existent(test, service):
  74. # check get
  75. get_objs = service.get(str(uuid.uuid4()))
  76. test.assertIsNone(
  77. get_objs,
  78. "Get non-existent object for %s returned unexpected objects: %s"
  79. % (type(service).__name__, get_objs))
  80. def check_delete(test, service, obj, perform_delete=False):
  81. if perform_delete:
  82. obj.delete()
  83. objs = service.list()
  84. found_objs = [o for o in objs if o.id == obj.id]
  85. test.assertTrue(
  86. len(found_objs) == 0,
  87. "Object %s in service %s should have been deleted but still exists."
  88. % (found_objs, type(service).__name__))
  89. def check_obj_name(test, obj):
  90. """
  91. Cloudbridge identifiers must be 1-63 characters long, and comply with
  92. RFC1035. In addition, identifiers should contain only lowercase letters,
  93. numeric characters, underscores, and dashes. International
  94. characters are allowed.
  95. """
  96. # if name has a setter, make sure invalid values cannot be set
  97. name_property = getattr(type(obj), 'name', None)
  98. if isinstance(name_property, property) and name_property.fset:
  99. # setting letters, numbers and international characters should succeed
  100. # TODO: Unicode characters trip up Moto. Add following: \u0D85\u0200
  101. VALID_NAME = u"hello_world-123"
  102. original_name = obj.name
  103. obj.name = VALID_NAME
  104. # setting spaces should raise an exception
  105. with test.assertRaises(InvalidNameException):
  106. obj.name = "hello world"
  107. # setting upper case characters should raise an exception
  108. with test.assertRaises(InvalidNameException):
  109. obj.name = "hello World"
  110. # setting special characters should raise an exception
  111. with test.assertRaises(InvalidNameException):
  112. obj.name = "hello.world:how_goes_it"
  113. # setting a length > 63 should result in an exception
  114. with test.assertRaises(InvalidNameException,
  115. msg="Name of length > 64 should be disallowed"):
  116. obj.name = "a" * 64
  117. # refreshing should yield the last successfully set name
  118. obj.refresh()
  119. test.assertEqual(obj.name, VALID_NAME)
  120. obj.name = original_name
  121. pass
  122. def check_standard_behaviour(test, service, obj):
  123. """
  124. Checks standard behaviour in a given cloudbridge resource
  125. of a given service.
  126. """
  127. check_repr(test, obj)
  128. check_json(test, obj)
  129. check_obj_properties(test, obj)
  130. objs_list = check_list(test, service, obj)
  131. objs_iter = check_iter(test, service, obj)
  132. objs_find = check_find(test, service, obj)
  133. check_find_non_existent(test, service)
  134. obj_get = check_get(test, service, obj)
  135. check_get_non_existent(test, service)
  136. test.assertTrue(
  137. obj == objs_list[0] == objs_iter[0] == objs_find[0] == obj_get,
  138. "Objects returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  139. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  140. objs_find[0].id, obj_get.id,
  141. obj.id))
  142. test.assertTrue(
  143. obj.id == objs_list[0].id == objs_iter[0].id ==
  144. objs_find[0].id == obj_get.id,
  145. "Object Ids returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  146. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  147. objs_find[0].id, obj_get.id,
  148. obj.id))
  149. test.assertTrue(
  150. obj.name == objs_list[0].name == objs_iter[0].name ==
  151. objs_find[0].name == obj_get.name,
  152. "Names returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  153. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  154. objs_find[0].id, obj_get.id,
  155. obj.id))
  156. def check_crud(test, service, iface, name_prefix,
  157. create_func, cleanup_func, extra_test_func=None,
  158. custom_check_delete=None):
  159. """
  160. Checks crud behaviour of a given cloudbridge service. The create_func will
  161. be used as a factory function to create a service object and the
  162. cleanup_func will be used to destroy the object. Once an object is created
  163. using the create_func, all other standard behavioural tests can be run
  164. against that object.
  165. :type test: ``TestCase``
  166. :param test: The TestCase object to use
  167. :type service: ``CloudService``
  168. :param service: The CloudService object under test. For example,
  169. a VolumeService object.
  170. :type iface: ``type``
  171. :param iface: The type to test behaviour against. This type must be a
  172. subclass of ``CloudResource``.
  173. :type name_prefix: ``str``
  174. :param name_prefix: The name to prefix all created objects with. This
  175. function will generated a new name with the
  176. specified name_prefix for each test object created
  177. and pass that name into the create_func
  178. :type create_func: ``func``
  179. :param create_func: The create_func must accept the name of the object to
  180. create as a parameter and return the constructed
  181. object.
  182. :type cleanup_func: ``func``
  183. :param cleanup_func: The cleanup_func must accept the created object
  184. and perform all cleanup tasks required to delete the
  185. object.
  186. :type extra_test_func: ``func``
  187. :param extra_test_func: This function will be called to perform additional
  188. tests after object construction and initialization,
  189. but before object cleanup. It will receive the
  190. created object as a parameter.
  191. :type custom_check_delete: ``func``
  192. :param custom_check_delete: If provided, this function will be called
  193. instead of the standard check_delete function
  194. to make sure that the object has been deleted.
  195. """
  196. name = "{0}-{1}".format(name_prefix, helpers.get_uuid())
  197. obj = None
  198. with helpers.cleanup_action(lambda: cleanup_func(obj)):
  199. obj = create_func(name)
  200. if issubclass(iface, ObjectLifeCycleMixin):
  201. obj.wait_till_ready()
  202. check_standard_behaviour(test, service, obj)
  203. if extra_test_func:
  204. extra_test_func(obj)
  205. if custom_check_delete:
  206. custom_check_delete(obj)
  207. else:
  208. check_delete(test, service, obj)