standard_interface_tests.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 = "helloWorld"
  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. def check_standard_behaviour(test, service, obj):
  122. """
  123. Checks standard behaviour in a given cloudbridge resource
  124. of a given service.
  125. """
  126. check_repr(test, obj)
  127. check_json(test, obj)
  128. check_obj_properties(test, obj)
  129. objs_list = check_list(test, service, obj)
  130. objs_iter = check_iter(test, service, obj)
  131. objs_find = check_find(test, service, obj)
  132. check_find_non_existent(test, service)
  133. obj_get = check_get(test, service, obj)
  134. check_get_non_existent(test, service)
  135. test.assertTrue(
  136. obj == objs_list[0] == objs_iter[0] == objs_find[0] == obj_get,
  137. "Objects returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  138. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  139. objs_find[0].id, obj_get.id,
  140. obj.id))
  141. test.assertTrue(
  142. obj.id == objs_list[0].id == objs_iter[0].id ==
  143. objs_find[0].id == obj_get.id,
  144. "Object Ids returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  145. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  146. objs_find[0].id, obj_get.id,
  147. obj.id))
  148. test.assertTrue(
  149. obj.name == objs_list[0].name == objs_iter[0].name ==
  150. objs_find[0].name == obj_get.name,
  151. "Names returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  152. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  153. objs_find[0].id, obj_get.id,
  154. obj.id))
  155. def check_create(test, service, iface, name_prefix,
  156. create_func, cleanup_func):
  157. # check create with invalid name
  158. with test.assertRaises(InvalidNameException):
  159. # spaces should raise an exception
  160. create_func("hello world")
  161. # check create with invalid name
  162. with test.assertRaises(InvalidNameException):
  163. # uppercase characters should raise an exception
  164. create_func("helloWorld")
  165. # setting special characters should raise an exception
  166. with test.assertRaises(InvalidNameException):
  167. create_func("hello.world:how_goes_it")
  168. # setting a length > 63 should result in an exception
  169. with test.assertRaises(InvalidNameException,
  170. msg="Name of length > 64 should be disallowed"):
  171. create_func("a" * 64)
  172. def check_crud(test, service, iface, name_prefix,
  173. create_func, cleanup_func, extra_test_func=None,
  174. custom_check_delete=None, skip_name_check=False):
  175. """
  176. Checks crud behaviour of a given cloudbridge service. The create_func will
  177. be used as a factory function to create a service object and the
  178. cleanup_func will be used to destroy the object. Once an object is created
  179. using the create_func, all other standard behavioural tests can be run
  180. against that object.
  181. :type test: ``TestCase``
  182. :param test: The TestCase object to use
  183. :type service: ``CloudService``
  184. :param service: The CloudService object under test. For example,
  185. a VolumeService object.
  186. :type iface: ``type``
  187. :param iface: The type to test behaviour against. This type must be a
  188. subclass of ``CloudResource``.
  189. :type name_prefix: ``str``
  190. :param name_prefix: The name to prefix all created objects with. This
  191. function will generated a new name with the
  192. specified name_prefix for each test object created
  193. and pass that name into the create_func
  194. :type create_func: ``func``
  195. :param create_func: The create_func must accept the name of the object to
  196. create as a parameter and return the constructed
  197. object.
  198. :type cleanup_func: ``func``
  199. :param cleanup_func: The cleanup_func must accept the created object
  200. and perform all cleanup tasks required to delete the
  201. object.
  202. :type extra_test_func: ``func``
  203. :param extra_test_func: This function will be called to perform additional
  204. tests after object construction and initialization,
  205. but before object cleanup. It will receive the
  206. created object as a parameter.
  207. :type custom_check_delete: ``func``
  208. :param custom_check_delete: If provided, this function will be called
  209. instead of the standard check_delete function
  210. to make sure that the object has been deleted.
  211. :type skip_name_check: ``boolean``
  212. :param skip_name_check: If True, the invalid name checking will be
  213. skipped.
  214. """
  215. obj = None
  216. with helpers.cleanup_action(lambda: cleanup_func(obj)):
  217. if not skip_name_check:
  218. check_create(test, service, iface, name_prefix,
  219. create_func, cleanup_func)
  220. name = "{0}-{1}".format(name_prefix, helpers.get_uuid())
  221. obj = create_func(name)
  222. if issubclass(iface, ObjectLifeCycleMixin):
  223. obj.wait_till_ready()
  224. check_standard_behaviour(test, service, obj)
  225. if extra_test_func:
  226. extra_test_func(obj)
  227. if custom_check_delete:
  228. custom_check_delete(obj)
  229. else:
  230. check_delete(test, service, obj)