standard_interface_tests.py 14 KB

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