standard_interface_tests.py 14 KB

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