2
1

basetest.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import unittest
  2. import os
  3. import datetime
  4. from infra.builder import Builder
  5. from infra.emulator import Emulator
  6. BASIC_TOOLCHAIN_CONFIG = \
  7. """
  8. BR2_arm=y
  9. BR2_TOOLCHAIN_EXTERNAL=y
  10. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN=y
  11. BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_ARMV5_EABI_GLIBC_STABLE=y
  12. """
  13. MINIMAL_CONFIG = \
  14. """
  15. BR2_INIT_NONE=y
  16. BR2_SYSTEM_BIN_SH_NONE=y
  17. # BR2_PACKAGE_BUSYBOX is not set
  18. # BR2_TARGET_ROOTFS_TAR is not set
  19. """
  20. class BRConfigTest(unittest.TestCase):
  21. """Test up to the configure stage."""
  22. config = None
  23. br2_external = list()
  24. downloaddir = None
  25. outputdir = None
  26. logtofile = True
  27. keepbuilds = False
  28. jlevel = 0
  29. timeout_multiplier = 1
  30. def __init__(self, names):
  31. super(BRConfigTest, self).__init__(names)
  32. self.testname = self.__class__.__name__
  33. self.builddir = self.outputdir and os.path.join(self.outputdir, self.testname)
  34. self.config += '\nBR2_DL_DIR="{}"\n'.format(self.downloaddir)
  35. self.config += "\nBR2_JLEVEL={}\n".format(self.jlevel)
  36. def show_msg(self, msg):
  37. print("{} {:40s} {}".format(datetime.datetime.now().strftime("%H:%M:%S"),
  38. self.testname, msg))
  39. def setUp(self):
  40. self.show_msg("Starting")
  41. self.b = Builder(self.config, self.builddir, self.logtofile, self.jlevel)
  42. if not self.keepbuilds:
  43. self.b.delete()
  44. if not self.b.is_finished():
  45. self.b.configure(make_extra_opts=["BR2_EXTERNAL={}".format(":".join(self.br2_external))])
  46. def tearDown(self):
  47. self.show_msg("Cleaning up")
  48. if self.b and not self.keepbuilds:
  49. self.b.delete()
  50. class BRTest(BRConfigTest):
  51. """Test up to the build stage and instantiate an emulator."""
  52. def __init__(self, names):
  53. super(BRTest, self).__init__(names)
  54. self.emulator = None
  55. def setUp(self):
  56. super(BRTest, self).setUp()
  57. if not self.b.is_finished():
  58. self.show_msg("Building")
  59. self.b.build()
  60. self.show_msg("Building done")
  61. self.emulator = Emulator(self.builddir, self.downloaddir,
  62. self.logtofile, self.timeout_multiplier)
  63. def tearDown(self):
  64. if self.emulator:
  65. self.emulator.stop()
  66. super(BRTest, self).tearDown()
  67. # Run the given 'cmd' with a 'timeout' on the target and
  68. # assert that the command succeeded; on error, print the
  69. # faulty command and its output
  70. def assertRunOk(self, cmd, timeout=-1):
  71. out, exit_code = self.emulator.run(cmd, timeout)
  72. self.assertEqual(
  73. exit_code,
  74. 0,
  75. "\nFailed to run: {}\noutput was:\n{}".format(cmd, ' '+'\n '.join(out))
  76. )