standard_interface_tests.py 11 KB

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