selftest.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. Self testing module
  3. ===================
  4. The selftest() function runs a small test suite of relatively fast tests that are meant to discover issues
  5. with the way Borg was compiled or packaged and also bugs in Borg itself.
  6. Theses tests are a subset of the borg/testsuite and are run with Pythons built-in unittest, hence none of
  7. the tests used for this can or should be ported to py.test currently.
  8. To assert that self test discovery works correctly the number of tests is kept in the SELFTEST_COUNT
  9. variable. SELFTEST_COUNT must be updated if new tests are added or removed to or from any of the tests
  10. used here.
  11. """
  12. import sys
  13. import time
  14. from unittest import TestResult, TestSuite, defaultTestLoader
  15. from .testsuite.hashindex import HashIndexDataTestCase, HashIndexRefcountingTestCase, HashIndexTestCase
  16. from .testsuite.crypto import CryptoTestCase
  17. from .testsuite.chunker import ChunkerTestCase
  18. SELFTEST_CASES = [
  19. HashIndexDataTestCase,
  20. HashIndexRefcountingTestCase,
  21. HashIndexTestCase,
  22. CryptoTestCase,
  23. ChunkerTestCase,
  24. ]
  25. SELFTEST_COUNT = 27
  26. class SelfTestResult(TestResult):
  27. def __init__(self):
  28. super().__init__()
  29. self.successes = []
  30. def addSuccess(self, test):
  31. super().addSuccess(test)
  32. self.successes.append(test)
  33. def test_name(self, test):
  34. return test.shortDescription() or str(test)
  35. def log_results(self, logger):
  36. for test, failure in self.errors + self.failures + self.unexpectedSuccesses:
  37. logger.error('self test %s FAILED:\n%s', self.test_name(test), failure)
  38. for test, reason in self.skipped:
  39. logger.warning('self test %s skipped: %s', self.test_name(test), reason)
  40. def successful_test_count(self):
  41. return len(self.successes)
  42. def selftest(logger):
  43. selftest_started = time.perf_counter()
  44. result = SelfTestResult()
  45. test_suite = TestSuite()
  46. for test_case in SELFTEST_CASES:
  47. test_suite.addTest(defaultTestLoader.loadTestsFromTestCase(test_case))
  48. test_suite.run(result)
  49. result.log_results(logger)
  50. successful_tests = result.successful_test_count()
  51. count_mismatch = successful_tests != SELFTEST_COUNT
  52. if result.wasSuccessful() and count_mismatch:
  53. # only print this if all tests succeeded
  54. logger.error("self test count (%d != %d) mismatch, either test discovery is broken or a test was added "
  55. "without updating borg.selftest",
  56. successful_tests, SELFTEST_COUNT)
  57. if not result.wasSuccessful() or count_mismatch:
  58. logger.error("self test failed\n"
  59. "This is a bug either in Borg or in the package / distribution you use.")
  60. sys.exit(2)
  61. assert False, "sanity assertion failed: ran beyond sys.exit()"
  62. selftest_elapsed = time.perf_counter() - selftest_started
  63. logger.debug("%d self tests completed in %.2f seconds", successful_tests, selftest_elapsed)