standard_interface_tests.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 uuid
  9. from cloudbridge.cloud.interfaces.exceptions \
  10. import InvalidNameException
  11. from cloudbridge.cloud.interfaces.resources import ObjectLifeCycleMixin
  12. from cloudbridge.cloud.interfaces.resources import ResultList
  13. import test.helpers as helpers
  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. test.assertEqual(find_objs[0].id, obj.id)
  61. return find_objs
  62. def check_find_non_existent(test, service):
  63. # check find
  64. find_objs = service.find(name="random_imagined_obj_name")
  65. test.assertTrue(
  66. len(find_objs) == 0,
  67. "Find non-existent object for %s returned unexpected objects: %s"
  68. % (type(service).__name__, find_objs))
  69. def check_get(test, service, obj):
  70. get_obj = service.get(obj.id)
  71. test.assertEqual(get_obj.id, obj.id)
  72. test.assertIsInstance(get_obj, type(obj))
  73. return get_obj
  74. def check_get_non_existent(test, service):
  75. # check get
  76. get_objs = service.get(str(uuid.uuid4()))
  77. test.assertIsNone(
  78. get_objs,
  79. "Get non-existent object for %s returned unexpected objects: %s"
  80. % (type(service).__name__, get_objs))
  81. def check_delete(test, service, obj, perform_delete=False):
  82. if perform_delete:
  83. obj.delete()
  84. objs = service.list()
  85. found_objs = [o for o in objs if o.id == obj.id]
  86. test.assertTrue(
  87. len(found_objs) == 0,
  88. "Object %s in service %s should have been deleted but still exists."
  89. % (found_objs, type(service).__name__))
  90. def check_obj_name(test, obj):
  91. """
  92. Cloudbridge identifiers must be 1-63 characters long, and comply with
  93. RFC1035. In addition, identifiers should contain only lowercase letters,
  94. numeric characters, underscores, and dashes. International
  95. characters are allowed.
  96. """
  97. # if name has a setter, make sure invalid values cannot be set
  98. name_property = getattr(type(obj), 'name', None)
  99. if isinstance(name_property, property) and name_property.fset:
  100. # setting letters, numbers and international characters should succeed
  101. # TODO: Unicode characters trip up Moto. Add following: \u0D85\u0200
  102. VALID_NAME = u"hello_world-123"
  103. original_name = obj.name
  104. obj.name = VALID_NAME
  105. # setting spaces should raise an exception
  106. with test.assertRaises(InvalidNameException):
  107. obj.name = "hello world"
  108. # setting upper case characters should raise an exception
  109. with test.assertRaises(InvalidNameException):
  110. obj.name = "helloWorld"
  111. # setting special characters should raise an exception
  112. with test.assertRaises(InvalidNameException):
  113. obj.name = "hello.world:how_goes_it"
  114. # setting a length > 63 should result in an exception
  115. with test.assertRaises(InvalidNameException,
  116. msg="Name of length > 64 should be disallowed"):
  117. obj.name = "a" * 64
  118. # refreshing should yield the last successfully set name
  119. obj.refresh()
  120. test.assertEqual(obj.name, VALID_NAME)
  121. obj.name = original_name
  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.id == objs_list[0].id == objs_iter[0].id ==
  138. objs_find[0].id == obj_get.id,
  139. "Object Ids returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  140. " are not as expected: {4}".format(objs_list[0].id, objs_iter[0].id,
  141. objs_find[0].id, obj_get.id,
  142. obj.id))
  143. test.assertTrue(
  144. obj.name == objs_list[0].name == objs_iter[0].name ==
  145. objs_find[0].name == obj_get.name,
  146. "Names returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  147. " are not as expected: {4}".format(objs_list[0].id, objs_iter[0].id,
  148. objs_find[0].id, obj_get.id,
  149. obj.id))
  150. def check_create(test, service, iface, name_prefix,
  151. create_func, cleanup_func):
  152. # check create with invalid name
  153. with test.assertRaises(InvalidNameException):
  154. # spaces should raise an exception
  155. create_func("hello world")
  156. # check create with invalid name
  157. with test.assertRaises(InvalidNameException):
  158. # uppercase characters should raise an exception
  159. create_func("helloWorld")
  160. # setting special characters should raise an exception
  161. with test.assertRaises(InvalidNameException):
  162. create_func("hello.world:how_goes_it")
  163. # setting a length > 63 should result in an exception
  164. with test.assertRaises(InvalidNameException,
  165. msg="Name of length > 64 should be disallowed"):
  166. create_func("a" * 64)
  167. def check_crud(test, service, iface, name_prefix,
  168. create_func, cleanup_func, extra_test_func=None,
  169. custom_check_delete=None, skip_name_check=False):
  170. """
  171. Checks crud behaviour of a given cloudbridge service. The create_func will
  172. be used as a factory function to create a service object and the
  173. cleanup_func will be used to destroy the object. Once an object is created
  174. using the create_func, all other standard behavioural tests can be run
  175. against that object.
  176. :type test: ``TestCase``
  177. :param test: The TestCase object to use
  178. :type service: ``CloudService``
  179. :param service: The CloudService object under test. For example,
  180. a VolumeService object.
  181. :type iface: ``type``
  182. :param iface: The type to test behaviour against. This type must be a
  183. subclass of ``CloudResource``.
  184. :type name_prefix: ``str``
  185. :param name_prefix: The name to prefix all created objects with. This
  186. function will generated a new name with the
  187. specified name_prefix for each test object created
  188. and pass that name into the create_func
  189. :type create_func: ``func``
  190. :param create_func: The create_func must accept the name of the object to
  191. create as a parameter and return the constructed
  192. object.
  193. :type cleanup_func: ``func``
  194. :param cleanup_func: The cleanup_func must accept the created object
  195. and perform all cleanup tasks required to delete the
  196. object.
  197. :type extra_test_func: ``func``
  198. :param extra_test_func: This function will be called to perform additional
  199. tests after object construction and initialization,
  200. but before object cleanup. It will receive the
  201. created object as a parameter.
  202. :type custom_check_delete: ``func``
  203. :param custom_check_delete: If provided, this function will be called
  204. instead of the standard check_delete function
  205. to make sure that the object has been deleted.
  206. :type skip_name_check: ``boolean``
  207. :param skip_name_check: If True, the invalid name checking will be
  208. skipped.
  209. """
  210. obj = None
  211. with helpers.cleanup_action(lambda: cleanup_func(obj)):
  212. if not skip_name_check:
  213. check_create(test, service, iface, name_prefix,
  214. create_func, cleanup_func)
  215. name = "{0}-{1}".format(name_prefix, helpers.get_uuid())
  216. obj = create_func(name)
  217. if issubclass(iface, ObjectLifeCycleMixin):
  218. obj.wait_till_ready()
  219. check_standard_behaviour(test, service, obj)
  220. if extra_test_func:
  221. extra_test_func(obj)
  222. if custom_check_delete:
  223. custom_check_delete(obj)
  224. else:
  225. check_delete(test, service, obj)