standard_interface_tests.py 10 KB

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