standard_interface_tests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_json(test, obj):
  16. val = obj.to_json()
  17. test.assertEqual(val.get('id'), obj.id)
  18. test.assertEqual(val.get('name'), obj.name)
  19. def check_obj_properties(test, obj):
  20. test.assertEqual(obj, obj, "Object should be equal to itself")
  21. test.assertFalse(obj != obj, "Object inequality should be false")
  22. def check_list(test, service, obj):
  23. list_objs = service.list()
  24. test.assertIsInstance(list_objs, ResultList)
  25. all_records = list_objs
  26. while list_objs.is_truncated:
  27. list_objs = service.list(marker=list_objs.marker)
  28. all_records += list_objs
  29. match_objs = [o for o in all_records if o.id == obj.id]
  30. test.assertTrue(
  31. len(match_objs) == 1,
  32. "List objects for %s does not return the expected object id %s. Got %s"
  33. % (type(obj).__name__, obj.id, match_objs))
  34. return match_objs
  35. def check_iter(test, service, obj):
  36. # check iteration
  37. iter_objs = list(service)
  38. iter_ids = [o.id for o in service]
  39. test.assertEqual(len(set(iter_ids)), len(iter_ids),
  40. "Iteration should not return duplicates")
  41. match_objs = [o for o in iter_objs if o.id == obj.id]
  42. test.assertTrue(
  43. len(match_objs) == 1,
  44. "Iter objects for %s does not return the expected object id %s. Got %s"
  45. % (type(obj).__name__, obj.id, match_objs))
  46. return match_objs
  47. def check_find(test, service, obj):
  48. # check find
  49. find_objs = service.find(name=obj.name)
  50. test.assertTrue(
  51. len(find_objs) == 1,
  52. "Find objects for %s does not return the expected object: %s. Got %s"
  53. % (type(obj).__name__, obj.name, find_objs))
  54. return find_objs
  55. def check_find_non_existent(test, service):
  56. # check find
  57. find_objs = service.find(name="random_imagined_obj_name")
  58. test.assertTrue(
  59. len(find_objs) == 0,
  60. "Find non-existent object for %s returned unexpected objects: %s"
  61. % (type(service).__name__, find_objs))
  62. def check_get(test, service, obj):
  63. get_obj = service.get(obj.id)
  64. test.assertEqual(get_obj, obj)
  65. test.assertIsInstance(get_obj, type(obj))
  66. return get_obj
  67. def check_get_non_existent(test, service):
  68. # check get
  69. get_objs = service.get("random_imagined_obj_id")
  70. test.assertIsNone(
  71. get_objs,
  72. "Get non-existent object for %s returned unexpected objects: %s"
  73. % (type(service).__name__, get_objs))
  74. def check_delete(test, service, obj, perform_delete=False):
  75. if perform_delete:
  76. obj.delete()
  77. objs = service.list()
  78. found_objs = [o for o in objs if o.id == obj.id]
  79. test.assertTrue(
  80. len(found_objs) == 0,
  81. "Object %s in service %s should have been deleted but still exists."
  82. % (found_objs, type(service).__name__))
  83. def check_standard_behaviour(test, service, obj):
  84. check_repr(test, obj)
  85. check_json(test, obj)
  86. check_obj_properties(test, obj)
  87. objs_list = check_list(test, service, obj)
  88. objs_iter = check_iter(test, service, obj)
  89. objs_find = check_find(test, service, obj)
  90. check_find_non_existent(test, service)
  91. obj_get = check_get(test, service, obj)
  92. check_get_non_existent(test, service)
  93. test.assertTrue(
  94. obj == objs_list[0] == objs_iter[0] == objs_find[0] == obj_get,
  95. "Objects returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  96. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  97. objs_find[0].id, obj_get.id,
  98. obj.id))
  99. test.assertTrue(
  100. obj.id == objs_list[0].id == objs_iter[0].id ==
  101. objs_find[0].id == obj_get.id,
  102. "Object Ids returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  103. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  104. objs_find[0].id, obj_get.id,
  105. obj.id))
  106. test.assertTrue(
  107. obj.name == objs_list[0].name == objs_iter[0].name ==
  108. objs_find[0].name == obj_get.name,
  109. "Names returned by list: {0}, iter: {1}, find: {2} and get: {3} "
  110. " are not as expected: {4}" .format(objs_list[0].id, objs_iter[0].id,
  111. objs_find[0].id, obj_get.id,
  112. obj.id))