standard_interface_tests.py 3.8 KB

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