xref: /OK3568_Linux_fs/buildroot/support/testing/infra/basetest.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1import unittest
2import os
3import datetime
4
5from infra.builder import Builder
6from infra.emulator import Emulator
7
8BASIC_TOOLCHAIN_CONFIG = \
9    """
10    BR2_arm=y
11    BR2_TOOLCHAIN_EXTERNAL=y
12    BR2_TOOLCHAIN_EXTERNAL_CUSTOM=y
13    BR2_TOOLCHAIN_EXTERNAL_DOWNLOAD=y
14    BR2_TOOLCHAIN_EXTERNAL_URL="https://toolchains.bootlin.com/downloads/releases/toolchains/armv5-eabi/tarballs/armv5-eabi--uclibc--bleeding-edge-2018.11-1.tar.bz2"
15    BR2_TOOLCHAIN_EXTERNAL_GCC_8=y
16    BR2_TOOLCHAIN_EXTERNAL_HEADERS_4_14=y
17    BR2_TOOLCHAIN_EXTERNAL_LOCALE=y
18    BR2_TOOLCHAIN_HAS_THREADS_DEBUG=y
19    BR2_TOOLCHAIN_EXTERNAL_CXX=y
20    """
21
22MINIMAL_CONFIG = \
23    """
24    BR2_INIT_NONE=y
25    BR2_SYSTEM_BIN_SH_NONE=y
26    # BR2_PACKAGE_BUSYBOX is not set
27    # BR2_TARGET_ROOTFS_TAR is not set
28    """
29
30
31class BRConfigTest(unittest.TestCase):
32    config = None
33    br2_external = list()
34    downloaddir = None
35    outputdir = None
36    logtofile = True
37    keepbuilds = False
38    jlevel = 0
39    timeout_multiplier = 1
40
41    def __init__(self, names):
42        super(BRConfigTest, self).__init__(names)
43        self.testname = self.__class__.__name__
44        self.builddir = self.outputdir and os.path.join(self.outputdir, self.testname)
45        self.config += '\nBR2_DL_DIR="{}"\n'.format(self.downloaddir)
46        self.config += "\nBR2_JLEVEL={}\n".format(self.jlevel)
47
48    def show_msg(self, msg):
49        print("{} {:40s} {}".format(datetime.datetime.now().strftime("%H:%M:%S"),
50                                    self.testname, msg))
51
52    def setUp(self):
53        self.show_msg("Starting")
54        self.b = Builder(self.config, self.builddir, self.logtofile)
55
56        if not self.keepbuilds:
57            self.b.delete()
58
59        if not self.b.is_finished():
60            self.b.configure(make_extra_opts=["BR2_EXTERNAL={}".format(":".join(self.br2_external))])
61
62    def tearDown(self):
63        self.show_msg("Cleaning up")
64        if self.b and not self.keepbuilds:
65            self.b.delete()
66
67
68class BRTest(BRConfigTest):
69    def __init__(self, names):
70        super(BRTest, self).__init__(names)
71        self.emulator = None
72
73    def setUp(self):
74        super(BRTest, self).setUp()
75        if not self.b.is_finished():
76            self.show_msg("Building")
77            self.b.build()
78            self.show_msg("Building done")
79
80        self.emulator = Emulator(self.builddir, self.downloaddir,
81                                 self.logtofile, self.timeout_multiplier)
82
83    def tearDown(self):
84        if self.emulator:
85            self.emulator.stop()
86        super(BRTest, self).tearDown()
87
88    # Run the given 'cmd' with a 'timeout' on the target and
89    # assert that the command succeeded
90    def assertRunOk(self, cmd, timeout=-1):
91        _, exit_code = self.emulator.run(cmd, timeout)
92        self.assertEqual(exit_code, 0)
93