standard_interface_tests.py 3.6 KB

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