2
1

basetest.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: str
  23. br2_external: list[str] = 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_BACKUP_SITE=""\n'
  35. self.config += '\nBR2_DL_DIR="{}"\n'.format(self.downloaddir)
  36. self.config += "\nBR2_JLEVEL={}\n".format(self.jlevel)
  37. def show_msg(self, msg):
  38. print("{} {:40s} {}".format(datetime.datetime.now().strftime("%H:%M:%S"),
  39. self.testname, msg))
  40. def setUp(self):
  41. self.show_msg("Starting")
  42. self.b = Builder(self.config, self.builddir, self.logtofile, self.jlevel)
  43. if not self.keepbuilds:
  44. self.b.delete()
  45. if not self.b.is_finished():
  46. self.b.configure(make_extra_opts=["BR2_EXTERNAL={}".format(":".join(self.br2_external))])
  47. def tearDown(self):
  48. self.show_msg("Cleaning up")
  49. if self.b and not self.keepbuilds:
  50. self.b.delete()
  51. class BRTest(BRConfigTest):
  52. """Test up to the build stage and instantiate an emulator."""
  53. def __init__(self, names):
  54. super(BRTest, self).__init__(names)
  55. self.emulator = None
  56. def setUp(self):
  57. super(BRTest, self).setUp()
  58. if not self.b.is_finished():
  59. self.show_msg("Building")
  60. self.b.build()
  61. self.show_msg("Building done")
  62. self.emulator = Emulator(self.builddir, self.downloaddir,
  63. self.logtofile, self.timeout_multiplier)
  64. def tearDown(self):
  65. if self.emulator:
  66. self.emulator.stop()
  67. super(BRTest, self).tearDown()
  68. # Run the given 'cmd' with a 'timeout' on the target and
  69. # assert that the command succeeded; on error, print the
  70. # faulty command and its output
  71. def assertRunOk(self, cmd, timeout=-1):
  72. out, exit_code = self.emulator.run(cmd, timeout)
  73. self.assertEqual(
  74. exit_code,
  75. 0,
  76. "\nFailed to run: {}\noutput was:\n{}".format(cmd, ' '+'\n '.join(out))
  77. )