standard_interface_tests.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. from cloudbridge.cloud.interfaces.resources import ResultList
  9. def check_repr(test, obj):
  10. test.assertTrue(
  11. obj.id in repr(obj),
  12. "repr(obj) for %s contain the object id so that the object"
  13. " can be reconstructed, but does not. eval(repr(obj)) == obj"
  14. % (type(obj).__name__,))
  15. def check_list(test, service, obj):
  16. list_objs = service.list()
  17. test.assertIsInstance(list_objs, ResultList)
  18. all_records = list_objs
  19. while list_objs.is_truncated:
  20. list_objs = service.list(marker=list_objs.marker)
  21. all_records += list_objs
  22. match_objs = [o for o in all_records if o.id == obj.id]
  23. test.assertTrue(
  24. len(match_objs) == 1,
  25. "List objects for %s does not return the expected object id %s. Got %s"
  26. % (type(obj).__name__, obj.id, match_objs))
  27. return match_objs
  28. def check_iter(test, service, obj):
  29. # check iteration
  30. iter_objs = list(service)
  31. iter_ids = [o.id for o in service]
  32. test.assertEqual(len(set(iter_ids)), len(iter_ids),
  33. "Iteration should not return duplicates")
  34. match_objs = [o for o in iter_objs if o.id == obj.id]
  35. test.assertTrue(
  36. len(match_objs) == 1,
  37. "Iter objects for %s does not return the expected object id %s. Got %s"
  38. % (type(obj).__name__, obj.id, match_objs))
  39. return match_objs
  40. def check_find(test, service, obj):
  41. # check find
  42. find_objs = service.find(name=obj.name)
  43. test.assertTrue(
  44. len(find_objs) == 1,
  45. "Find objects for %s does not return the expected object: %s. Got %s"
  46. % (type(obj).__name__, obj.name, find_objs))
  47. return find_objs
  48. def check_find_non_existent(test, service):
  49. # check find
  50. find_objs = service.find(name="random_imagined_obj_name")
  51. test.assertTrue(
  52. len(find_objs) == 0,
  53. "Find non-existent object for %s returned unexpected objects: %s"
  54. % (type(service).__name__, find_objs))
  55. def check_get(test, service, obj):
  56. get_obj = service.get(obj.id)
  57. test.assertEqual(get_obj, obj)
  58. return get_obj
  59. def check_delete(test, service, obj, perform_delete=False):
  60. if perform_delete:
  61. obj.delete()
  62. objs = service.list()
  63. found_objs = [o for o in objs if o.id == obj.id]
  64. test.assertTrue(
  65. len(found_objs) == 0,
  66. "Object %s in service %s should have been deleted but still exists."
  67. % (found_objs, type(service).__name__))
  68. def check_standard_behaviour(test, service, obj):
  69. check_repr(test, obj)
  70. objs_list = check_list(test, service, obj)
  71. objs_iter = check_iter(test, service, obj)
  72. objs_find = check_find(test, service, obj)
  73. check_find_non_existent(test, service)
  74. obj_get = check_get(test, service, obj)
  75. test.assertTrue(
  76. obj == objs_list[0] == objs_iter[0] == objs_find[0] == obj_get,
  77. "Objects returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  78. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  79. objs_find[0].id, obj_get.id,
  80. obj.id))
  81. test.assertTrue(
  82. obj.id == objs_list[0].id == objs_iter[0].id ==
  83. objs_find[0].id == obj_get.id,
  84. "Object Ids returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  85. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  86. objs_find[0].id, obj_get.id,
  87. obj.id))
  88. test.assertTrue(
  89. obj.name == objs_list[0].name == objs_iter[0].name ==
  90. objs_find[0].name == obj_get.name,
  91. "Names returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  92. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  93. objs_find[0].id, obj_get.id,
  94. obj.id))