standard_interface_tests.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. with test.assertRaises(TypeError):
  76. service.find(notaparameter="random_imagined_obj_name")
  77. test.assertTrue(
  78. len(find_objs) == 0,
  79. "Find non-existent object for %s returned unexpected objects: %s"
  80. % (type(service).__name__, find_objs))
  81. def check_get(test, service, obj):
  82. get_obj = service.get(obj.id)
  83. test.assertEqual(get_obj.id, obj.id)
  84. test.assertIsInstance(get_obj, type(obj))
  85. return get_obj
  86. def check_get_non_existent(test, service):
  87. # check get
  88. get_objs = service.get(str(uuid.uuid4()))
  89. test.assertIsNone(
  90. get_objs,
  91. "Get non-existent object for %s returned unexpected objects: %s"
  92. % (type(service).__name__, get_objs))
  93. def check_delete(test, service, obj, perform_delete=False):
  94. if perform_delete:
  95. obj.delete()
  96. objs = service.list()
  97. found_objs = [o for o in objs if o.id == obj.id]
  98. test.assertTrue(
  99. len(found_objs) == 0,
  100. "Object %s in service %s should have been deleted but still exists."
  101. % (found_objs, type(service).__name__))
  102. def check_obj_name(test, obj):
  103. name_property = getattr(type(obj), 'name', None)
  104. test.assertIsInstance(name_property, property)
  105. test.assertIsNone(name_property.fset, "Name should not have a setter")
  106. def check_obj_label(test, obj):
  107. """
  108. Cloudbridge identifiers must be 1-63 characters long, and comply with
  109. RFC1035. In addition, identifiers should contain only lowercase letters,
  110. numeric characters, underscores, and dashes. International
  111. characters are allowed.
  112. """
  113. # if label property exists, make sure invalid values cannot be set
  114. label_property = getattr(type(obj), 'label', None)
  115. if isinstance(label_property, property):
  116. test.assertIsInstance(obj, LabeledCloudResource)
  117. original_label = obj.label
  118. VALID_LABEL = u"hello-world-123"
  119. obj.label = VALID_LABEL
  120. # A none value should not be allowed
  121. with test.assertRaises(InvalidLabelException):
  122. obj.label = None
  123. # setting spaces should raise an exception
  124. with test.assertRaises(InvalidLabelException):
  125. obj.label = "hello world"
  126. # setting upper case characters should raise an exception
  127. with test.assertRaises(InvalidLabelException):
  128. obj.label = "helloWorld"
  129. # setting special characters should raise an exception
  130. with test.assertRaises(InvalidLabelException):
  131. obj.label = "hello.world:how_goes_it"
  132. # Starting with a dash should raise an exception
  133. with test.assertRaises(InvalidLabelException):
  134. obj.label = "-hello"
  135. # Ending with a dash should raise an exception
  136. with test.assertRaises(InvalidLabelException):
  137. obj.label = "hello-"
  138. # setting a length > 63 should result in an exception
  139. with test.assertRaises(InvalidLabelException,
  140. msg="Label of length > 64 is not allowed"):
  141. obj.label = "a" * 64
  142. # refreshing should yield the last successfully set label
  143. obj.refresh()
  144. test.assertEqual(obj.label, VALID_LABEL)
  145. obj.label = original_label
  146. def check_standard_behaviour(test, service, obj):
  147. """
  148. Checks standard behaviour in a given cloudbridge resource
  149. of a given service.
  150. """
  151. check_repr(test, obj)
  152. check_json(test, obj)
  153. check_obj_properties(test, obj)
  154. objs_list = check_list(test, service, obj)
  155. objs_iter = check_iter(test, service, obj)
  156. objs_find = check_find(test, service, obj)
  157. check_find_non_existent(test, service, obj)
  158. obj_get = check_get(test, service, obj)
  159. check_get_non_existent(test, service)
  160. test.assertTrue(
  161. obj.id == objs_list[0].id == objs_iter[0].id ==
  162. objs_find[0].id == obj_get.id,
  163. "Object Ids returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  164. " are not as expected: {4}".format(objs_list[0].id, objs_iter[0].id,
  165. objs_find[0].id, obj_get.id,
  166. obj.id))
  167. test.assertTrue(
  168. obj.name == objs_list[0].name == objs_iter[0].name ==
  169. objs_find[0].name == obj_get.name,
  170. "Names returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  171. " are not as expected: {4}".format(objs_list[0].id, objs_iter[0].id,
  172. objs_find[0].id, obj_get.id,
  173. obj.id))
  174. if isinstance(obj, LabeledCloudResource):
  175. test.assertTrue(
  176. obj.label == objs_list[0].label == objs_iter[0].label ==
  177. objs_find[0].label == obj_get.label,
  178. "Labels returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  179. " are not as expected: {4}".format(objs_list[0].id,
  180. objs_iter[0].id,
  181. objs_find[0].id, obj_get.id,
  182. obj.id))
  183. def check_create(test, service, iface, name_prefix,
  184. create_func, cleanup_func, supports_labels):
  185. # check create with invalid label
  186. with test.assertRaises(InvalidLabelException):
  187. # spaces should raise an exception
  188. create_func("hello world")
  189. # check create with invalid label
  190. with test.assertRaises(InvalidLabelException):
  191. # uppercase characters should raise an exception
  192. create_func("helloWorld")
  193. # setting special characters should raise an exception
  194. with test.assertRaises(InvalidLabelException):
  195. create_func("hello.world:how_goes_it")
  196. # Starting with a dash should raise an exception
  197. with test.assertRaises(InvalidLabelException):
  198. create_func("-hello")
  199. # Ending with a dash should raise an exception
  200. with test.assertRaises(InvalidLabelException):
  201. create_func("hello-")
  202. # underscores are not allowed
  203. with test.assertRaises(InvalidLabelException):
  204. create_func("hello_bucket")
  205. # setting a length > 63 should result in an exception
  206. with test.assertRaises(InvalidLabelException,
  207. msg="Label of length > 63 should be disallowed"):
  208. create_func("a" * 64)
  209. # name cannot be an IP address
  210. with test.assertRaises(InvalidLabelException):
  211. create_func("197.10.100.42")
  212. if supports_labels:
  213. # Comment out this test for now because actually creating two
  214. # objects violates certain test assumptions
  215. pass
  216. # empty labels should be allowed
  217. # obj = None
  218. # with helpers.cleanup_action(lambda: cleanup_func(obj)):
  219. # obj = create_func(None)
  220. else: # supports name only
  221. # empty name are not allowed
  222. with test.assertRaises(InvalidLabelException):
  223. create_func(None)
  224. # names of length less than 3 should raise an exception
  225. with test.assertRaises(InvalidLabelException):
  226. create_func("cb")
  227. def check_crud(test, service, iface, label_prefix,
  228. create_func, cleanup_func, extra_test_func=None,
  229. custom_check_delete=None, supports_labels=True,
  230. skip_name_check=False):
  231. """
  232. Checks crud behaviour of a given cloudbridge service. The create_func will
  233. be used as a factory function to create a service object and the
  234. cleanup_func will be used to destroy the object. Once an object is created
  235. using the create_func, all other standard behavioural tests can be run
  236. against that object.
  237. :type test: ``TestCase``
  238. :param test: The TestCase object to use
  239. :type service: ``CloudService``
  240. :param service: The CloudService object under test. For example,
  241. a VolumeService object.
  242. :type iface: ``type``
  243. :param iface: The type to test behaviour against. This type must be a
  244. subclass of ``CloudResource``.
  245. :type label_prefix: ``str``
  246. :param label_prefix: The label to prefix all created objects with. This
  247. function will generated a new label with the
  248. specified label_prefix for each test object created
  249. and pass that label into the create_func
  250. :type create_func: ``func``
  251. :param create_func: The create_func must accept the label of the object to
  252. create as a parameter and return the constructed
  253. object.
  254. :type cleanup_func: ``func``
  255. :param cleanup_func: The cleanup_func must accept the created object
  256. and perform all cleanup tasks required to delete the
  257. object.
  258. :type extra_test_func: ``func``
  259. :param extra_test_func: This function will be called to perform additional
  260. tests after object construction and initialization,
  261. but before object cleanup. It will receive the
  262. created object as a parameter.
  263. :type custom_check_delete: ``func``
  264. :param custom_check_delete: If provided, this function will be called
  265. instead of the standard check_delete function
  266. to make sure that the object has been deleted.
  267. :type supports_labels: ``boolean``
  268. :param supports_labels: Indicates whether the resource supports labels.
  269. If so, label related tests will be run.
  270. :type skip_name_check: ``boolean``
  271. :param skip_name_check: If True, the name related checking will be
  272. skipped.
  273. """
  274. obj = None
  275. with helpers.cleanup_action(lambda: cleanup_func(obj)):
  276. label = "{0}-{1}".format(label_prefix, helpers.get_uuid())
  277. if not skip_name_check:
  278. check_create(test, service, iface, label_prefix,
  279. create_func, cleanup_func, supports_labels)
  280. obj = create_func(label)
  281. if issubclass(iface, ObjectLifeCycleMixin):
  282. obj.wait_till_ready()
  283. check_standard_behaviour(test, service, obj)
  284. if extra_test_func:
  285. extra_test_func(obj)
  286. if custom_check_delete:
  287. custom_check_delete(obj)
  288. else:
  289. check_delete(test, service, obj)