2
0

standard_interface_tests.py 13 KB

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