run.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. import os
  3. import argparse
  4. import logging
  5. import datetime
  6. from run_utils import Err, CMakeCache, log, execute
  7. from run_suite import TestSuite
  8. from run_android import AndroidTestSuite
  9. epilog = '''
  10. NOTE:
  11. Additional options starting with "--gtest_" and "--perf_" will be passed directly to the test executables.
  12. '''
  13. if __name__ == "__main__":
  14. # log.basicConfig(format='[%(levelname)s] %(message)s', level = log.DEBUG)
  15. # log.basicConfig(format='[%(levelname)s] %(message)s', level = log.INFO)
  16. parser = argparse.ArgumentParser(
  17. description='OpenCV test runner script',
  18. epilog=epilog,
  19. formatter_class=argparse.RawDescriptionHelpFormatter)
  20. parser.add_argument("build_path", nargs='?', default=".", help="Path to build directory (should contain CMakeCache.txt, default is current) or to directory with tests (all platform checks will be disabled in this case)")
  21. parser.add_argument("-t", "--tests", metavar="MODULES", default="", help="Comma-separated list of modules to test (example: -t core,imgproc,java)")
  22. parser.add_argument("-b", "--blacklist", metavar="MODULES", default="", help="Comma-separated list of modules to exclude from test (example: -b java)")
  23. parser.add_argument("-a", "--accuracy", action="store_true", default=False, help="Look for accuracy tests instead of performance tests")
  24. parser.add_argument("--check", action="store_true", default=False, help="Shortcut for '--perf_min_samples=1 --perf_force_samples=1'")
  25. parser.add_argument("-w", "--cwd", metavar="PATH", default=".", help="Working directory for tests (default is current)")
  26. parser.add_argument("--list", action="store_true", default=False, help="List available tests (executables)")
  27. parser.add_argument("--list_short", action="store_true", default=False, help="List available tests (aliases)")
  28. parser.add_argument("--list_short_main", action="store_true", default=False, help="List available tests (main repository, aliases)")
  29. parser.add_argument("--configuration", metavar="CFG", default=None, help="Force Debug or Release configuration (for Visual Studio and Java tests build)")
  30. parser.add_argument("-n", "--dry_run", action="store_true", help="Do not run the tests")
  31. parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Print more debug information")
  32. # Valgrind
  33. parser.add_argument("--valgrind", action="store_true", default=False, help="Run C++ tests in valgrind")
  34. parser.add_argument("--valgrind_supp", metavar="FILE", action='append', help="Path to valgrind suppression file (example: --valgrind_supp opencv/platforms/scripts/valgrind.supp)")
  35. parser.add_argument("--valgrind_opt", metavar="OPT", action="append", default=[], help="Add command line option to valgrind (example: --valgrind_opt=--leak-check=full)")
  36. # QEMU
  37. parser.add_argument("--qemu", default="", help="Specify qemu binary and base parameters")
  38. # Android
  39. parser.add_argument("--android", action="store_true", default=False, help="Android: force all tests to run on device")
  40. parser.add_argument("--android_sdk", metavar="PATH", help="Android: path to SDK to use adb and aapt tools")
  41. parser.add_argument("--android_test_data_path", metavar="PATH", default="/sdcard/opencv_testdata/", help="Android: path to testdata on device")
  42. parser.add_argument("--android_env", action='append', help="Android: add environment variable (NAME=VALUE)")
  43. parser.add_argument("--android_propagate_opencv_env", action="store_true", default=False, help="Android: propagate OPENCV* environment variables")
  44. parser.add_argument("--serial", metavar="serial number", default="", help="Android: directs command to the USB device or emulator with the given serial number")
  45. parser.add_argument("--package", metavar="package", default="", help="Java: run JUnit tests for specified module or Android package")
  46. parser.add_argument("--java_test_exclude", metavar="java_test_exclude", default="", help="Java: Filter out specific JUnit tests")
  47. parser.add_argument("--trace", action="store_true", default=False, help="Trace: enable OpenCV tracing")
  48. parser.add_argument("--trace_dump", metavar="trace_dump", default=-1, help="Trace: dump highlight calls (specify max entries count, 0 - dump all)")
  49. args, other_args = parser.parse_known_args()
  50. log.setLevel(logging.DEBUG if args.verbose else logging.INFO)
  51. test_args = [a for a in other_args if a.startswith("--perf_") or a.startswith("--test_") or a.startswith("--gtest_")]
  52. bad_args = [a for a in other_args if a not in test_args]
  53. if len(bad_args) > 0:
  54. log.error("Error: Bad arguments: %s", bad_args)
  55. exit(1)
  56. args.mode = "test" if args.accuracy else "perf"
  57. android_env = []
  58. if args.android_env:
  59. android_env.extend([entry.split("=", 1) for entry in args.android_env])
  60. if args.android_propagate_opencv_env:
  61. android_env.extend([entry for entry in os.environ.items() if entry[0].startswith('OPENCV')])
  62. android_env = dict(android_env)
  63. if args.android_test_data_path:
  64. android_env['OPENCV_TEST_DATA_PATH'] = args.android_test_data_path
  65. if args.valgrind:
  66. try:
  67. ver = execute(["valgrind", "--version"], silent=True)
  68. log.debug("Using %s", ver)
  69. except OSError as e:
  70. log.error("Failed to run valgrind: %s", e)
  71. exit(1)
  72. if len(args.build_path) != 1:
  73. test_args = [a for a in test_args if not a.startswith("--gtest_output=")]
  74. if args.check:
  75. if not [a for a in test_args if a.startswith("--perf_min_samples=")]:
  76. test_args.extend(["--perf_min_samples=1"])
  77. if not [a for a in test_args if a.startswith("--perf_force_samples=")]:
  78. test_args.extend(["--perf_force_samples=1"])
  79. if not [a for a in test_args if a.startswith("--perf_verify_sanity")]:
  80. test_args.extend(["--perf_verify_sanity"])
  81. if bool(os.environ.get('BUILD_PRECOMMIT', None)):
  82. test_args.extend(["--skip_unstable=1"])
  83. ret = 0
  84. logs = []
  85. stamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
  86. path = args.build_path
  87. try:
  88. if not os.path.isdir(path):
  89. raise Err("Not a directory (should contain CMakeCache.txt to test executables)")
  90. cache = CMakeCache(args.configuration)
  91. fname = os.path.join(path, "CMakeCache.txt")
  92. if os.path.isfile(fname):
  93. log.debug("Reading cmake cache file: %s", fname)
  94. cache.read(path, fname)
  95. else:
  96. log.debug("Assuming folder contains tests: %s", path)
  97. cache.setDummy(path)
  98. if args.android or cache.getOS() == "android":
  99. log.debug("Creating Android test runner")
  100. suite = AndroidTestSuite(args, cache, stamp, android_env)
  101. else:
  102. log.debug("Creating native test runner")
  103. suite = TestSuite(args, cache, stamp)
  104. if args.list or args.list_short or args.list_short_main:
  105. suite.listTests(args.list_short or args.list_short_main, args.list_short_main)
  106. else:
  107. log.debug("Running tests in '%s', working dir: '%s'", path, args.cwd)
  108. def parseTests(s):
  109. return [o.strip() for o in s.split(",") if o]
  110. logs, ret = suite.runTests(parseTests(args.tests), parseTests(args.blacklist), args.cwd, test_args)
  111. except Err as e:
  112. log.error("ERROR: test path '%s' ==> %s", path, e.msg)
  113. ret = -1
  114. if logs:
  115. log.warning("Collected: %s", logs)
  116. if ret != 0:
  117. log.error("ERROR: some tests have failed")
  118. exit(ret)