test_base_helpers.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import unittest
  2. from cloudbridge.base import helpers as cb_helpers
  3. class BaseHelpersTestCase(unittest.TestCase):
  4. _multiprocess_can_split_ = True
  5. def test_cleanup_action_body_has_no_exception(self):
  6. invoke_order = [""]
  7. def cleanup_func():
  8. invoke_order[0] += "cleanup"
  9. with cb_helpers.cleanup_action(lambda: cleanup_func()):
  10. invoke_order[0] += "body_"
  11. self.assertEqual(invoke_order[0], "body_cleanup")
  12. def test_cleanup_action_body_has_exception(self):
  13. invoke_order = [""]
  14. def cleanup_func():
  15. invoke_order[0] += "cleanup"
  16. class CustomException(Exception):
  17. pass
  18. with self.assertRaises(CustomException):
  19. with cb_helpers.cleanup_action(lambda: cleanup_func()):
  20. invoke_order[0] += "body_"
  21. raise CustomException()
  22. self.assertEqual(invoke_order[0], "body_cleanup")
  23. def test_cleanup_action_cleanup_has_exception(self):
  24. invoke_order = [""]
  25. def cleanup_func():
  26. invoke_order[0] += "cleanup"
  27. raise Exception("test")
  28. with cb_helpers.cleanup_action(lambda: cleanup_func()):
  29. invoke_order[0] += "body_"
  30. self.assertEqual(invoke_order[0], "body_cleanup")
  31. def test_cleanup_action_body_and_cleanup_has_exception(self):
  32. invoke_order = [""]
  33. def cleanup_func():
  34. invoke_order[0] += "cleanup"
  35. raise Exception("test")
  36. class CustomException(Exception):
  37. pass
  38. with self.assertRaises(CustomException):
  39. with cb_helpers.cleanup_action(lambda: cleanup_func()):
  40. invoke_order[0] += "body_"
  41. raise CustomException()
  42. self.assertEqual(invoke_order[0], "body_cleanup")