oe-selftest 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. # Copyright (c) 2013 Intel Corporation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along
  14. # with this program; if not, write to the Free Software Foundation, Inc.,
  15. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. # DESCRIPTION
  17. # This script runs tests defined in meta/lib/selftest/
  18. # It's purpose is to automate the testing of different bitbake tools.
  19. # To use it you just need to source your build environment setup script and
  20. # add the meta-selftest layer to your BBLAYERS.
  21. # Call the script as: "oe-selftest" to run all the tests in in meta/lib/selftest/
  22. # Call the script as: "oe-selftest <module>.<Class>.<method>" to run just a single test
  23. # E.g: "oe-selftest bboutput.BitbakeLayers" will run just the BitbakeLayers class from meta/lib/selftest/bboutput.py
  24. import os
  25. import sys
  26. import unittest
  27. import logging
  28. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'meta/lib')))
  29. import oeqa.selftest
  30. import oeqa.utils.ftools as ftools
  31. from oeqa.utils.commands import runCmd, get_bb_var, get_test_layer
  32. from oeqa.selftest.base import oeSelfTest
  33. def logger_create():
  34. log = logging.getLogger("selftest")
  35. log.setLevel(logging.DEBUG)
  36. fh = logging.FileHandler(filename='oe-selftest.log', mode='w')
  37. fh.setLevel(logging.DEBUG)
  38. ch = logging.StreamHandler(sys.stdout)
  39. ch.setLevel(logging.INFO)
  40. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  41. fh.setFormatter(formatter)
  42. ch.setFormatter(formatter)
  43. log.addHandler(fh)
  44. log.addHandler(ch)
  45. return log
  46. log = logger_create()
  47. def preflight_check():
  48. log.info("Checking that everything is in order before running the tests")
  49. if not os.environ.get("BUILDDIR"):
  50. log.error("BUILDDIR isn't set. Did you forget to source your build environment setup script?")
  51. return False
  52. builddir = os.environ.get("BUILDDIR")
  53. if os.getcwd() != builddir:
  54. log.info("Changing cwd to %s" % builddir)
  55. os.chdir(builddir)
  56. if not "meta-selftest" in get_bb_var("BBLAYERS"):
  57. log.error("You don't seem to have the meta-selftest layer in BBLAYERS")
  58. return False
  59. log.info("Running bitbake -p")
  60. runCmd("bitbake -p")
  61. return True
  62. def add_include():
  63. builddir = os.environ.get("BUILDDIR")
  64. if "#include added by oe-selftest.py" \
  65. not in ftools.read_file(os.path.join(builddir, "conf/local.conf")):
  66. log.info("Adding: \"include selftest.inc\" in local.conf")
  67. ftools.append_file(os.path.join(builddir, "conf/local.conf"), \
  68. "\n#include added by oe-selftest.py\ninclude selftest.inc")
  69. def remove_include():
  70. builddir = os.environ.get("BUILDDIR")
  71. if "#include added by oe-selftest.py" \
  72. in ftools.read_file(os.path.join(builddir, "conf/local.conf")):
  73. log.info("Removing the include from local.conf")
  74. ftools.remove_from_file(os.path.join(builddir, "conf/local.conf"), \
  75. "#include added by oe-selftest.py\ninclude selftest.inc")
  76. def get_tests():
  77. testslist = []
  78. for x in sys.argv[1:]:
  79. testslist.append('oeqa.selftest.' + x)
  80. if not testslist:
  81. testpath = os.path.abspath(os.path.dirname(oeqa.selftest.__file__))
  82. files = sorted([f for f in os.listdir(testpath) if f.endswith('.py') and not f.startswith('_') and f != 'base.py'])
  83. for f in files:
  84. module = 'oeqa.selftest.' + f[:-3]
  85. testslist.append(module)
  86. return testslist
  87. def main():
  88. if not preflight_check():
  89. return 1
  90. testslist = get_tests()
  91. suite = unittest.TestSuite()
  92. loader = unittest.TestLoader()
  93. loader.sortTestMethodsUsing = None
  94. runner = unittest.TextTestRunner(verbosity=2)
  95. # we need to do this here, otherwise just loading the tests
  96. # will take 2 minutes (bitbake -e calls)
  97. oeSelfTest.testlayer_path = get_test_layer()
  98. for test in testslist:
  99. log.info("Loading tests from: %s" % test)
  100. try:
  101. suite.addTests(loader.loadTestsFromName(test))
  102. except AttributeError as e:
  103. log.error("Failed to import %s" % test)
  104. log.error(e)
  105. return 1
  106. add_include()
  107. result = runner.run(suite)
  108. log.info("Finished")
  109. return 0
  110. if __name__ == "__main__":
  111. try:
  112. ret = main()
  113. except Exception:
  114. ret = 1
  115. import traceback
  116. traceback.print_exc(5)
  117. finally:
  118. remove_include()
  119. sys.exit(ret)