diff options
author | Thomas Petazzoni <thomas.petazzoni@free-electrons.com> | 2017-03-20 21:36:50 +0100 |
---|---|---|
committer | Thomas Petazzoni <thomas.petazzoni@free-electrons.com> | 2017-05-07 22:04:54 +0200 |
commit | a732fb222b8a60076a93d490834d459b0a1c1d6b (patch) | |
tree | 8c79a7249134bb17016dc71335c61f6d4351ab64 /support/testing/infra/emulator.py | |
parent | 04829295c78bd49f4d6238f2ffdb03d744202932 (diff) | |
download | buildroot-a732fb222b8a60076a93d490834d459b0a1c1d6b.tar.gz buildroot-a732fb222b8a60076a93d490834d459b0a1c1d6b.zip |
support/testing: core testing infrastructure
This commit adds the core of a new testing infrastructure that allows to
perform runtime testing of Buildroot generated systems. This
infrastructure uses the Python unittest logic as its foundation.
This core infrastructure commit includes the following aspects:
- A base test class, called BRTest, defined in
support/testing/infra/basetest.py. This base test class inherited
from the Python provided unittest.TestCase, and must be subclassed by
all Buildroot test cases.
Its main purpose is to provide the Python unittest setUp() and
tearDown() methods. In our case, setUp() takes care of building the
Buildroot system described in the test case, and instantiate the
Emulator object in case runtime testing is needed. The tearDown()
method simply cleans things up (stop the emulator, remove the output
directory).
- A Builder class, defined in support/testing/infra/builder.py, simply
responsible for building the Buildroot system in each test case.
- An Emulator class, defined in support/testing/infra/emulator.py,
responsible for running the generated system under Qemu, allowing
each test case to run arbitrary commands inside the emulated system.
- A run-tests script, which is the entry point to start the tests.
Even though I wrote the original version of this small infrastructure, a
huge amount of rework and improvement has been done by Maxime
Hadjinlian, and squashed into this patch. So many thanks to Maxime for
cleaning up and improving my Python code!
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Diffstat (limited to 'support/testing/infra/emulator.py')
-rw-r--r-- | support/testing/infra/emulator.py | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/support/testing/infra/emulator.py b/support/testing/infra/emulator.py new file mode 100644 index 0000000000..7c476cb0e4 --- /dev/null +++ b/support/testing/infra/emulator.py @@ -0,0 +1,135 @@ +import socket +import subprocess +import telnetlib + +import infra +import infra.basetest + +# TODO: Most of the telnet stuff need to be replaced by stdio/pexpect to discuss +# with the qemu machine. +class Emulator(object): + + def __init__(self, builddir, downloaddir, logtofile): + self.qemu = None + self.__tn = None + self.downloaddir = downloaddir + self.log = "" + self.log_file = "{}-run.log".format(builddir) + if logtofile is None: + self.log_file = None + + # Start Qemu to boot the system + # + # arch: Qemu architecture to use + # + # kernel: path to the kernel image, or the special string + # 'builtin'. 'builtin' means a pre-built kernel image will be + # downloaded from ARTEFACTS_URL and suitable options are + # automatically passed to qemu and added to the kernel cmdline. So + # far only armv5, armv7 and i386 builtin kernels are available. + # If None, then no kernel is used, and we assume a bootable device + # will be specified. + # + # kernel_cmdline: array of kernel arguments to pass to Qemu -append option + # + # options: array of command line options to pass to Qemu + # + def boot(self, arch, kernel=None, kernel_cmdline=None, options=None): + if arch in ["armv7", "armv5"]: + qemu_arch = "arm" + else: + qemu_arch = arch + + qemu_cmd = ["qemu-system-{}".format(qemu_arch), + "-serial", "telnet::1234,server", + "-display", "none"] + + if options: + qemu_cmd += options + + if kernel_cmdline is None: + kernel_cmdline = [] + + if kernel: + if kernel == "builtin": + if arch in ["armv7", "armv5"]: + kernel_cmdline.append("console=ttyAMA0") + + if arch == "armv7": + kernel = infra.download(self.downloaddir, + "kernel-vexpress") + dtb = infra.download(self.downloaddir, + "vexpress-v2p-ca9.dtb") + qemu_cmd += ["-dtb", dtb] + qemu_cmd += ["-M", "vexpress-a9"] + elif arch == "armv5": + kernel = infra.download(self.downloaddir, + "kernel-versatile") + qemu_cmd += ["-M", "versatilepb"] + + qemu_cmd += ["-kernel", kernel] + + if kernel_cmdline: + qemu_cmd += ["-append", " ".join(kernel_cmdline)] + + with infra.smart_open(self.log_file) as lfh: + lfh.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd)) + self.qemu = subprocess.Popen(qemu_cmd, stdout=lfh, stderr=lfh) + + # Wait for the telnet port to appear and connect to it. + while True: + try: + self.__tn = telnetlib.Telnet("localhost", 1234) + if self.__tn: + break + except socket.error: + continue + + def __read_until(self, waitstr, timeout=5): + data = self.__tn.read_until(waitstr, timeout) + self.log += data + with infra.smart_open(self.log_file) as lfh: + lfh.write(data) + return data + + def __write(self, wstr): + self.__tn.write(wstr) + + # Wait for the login prompt to appear, and then login as root with + # the provided password, or no password if not specified. + def login(self, password=None): + self.__read_until("buildroot login:", 10) + if "buildroot login:" not in self.log: + with infra.smart_open(self.log_file) as lfh: + lfh.write("==> System does not boot") + raise SystemError("System does not boot") + + self.__write("root\n") + if password: + self.__read_until("Password:") + self.__write(password + "\n") + self.__read_until("# ") + if "# " not in self.log: + raise SystemError("Cannot login") + self.run("dmesg -n 1") + + # Run the given 'cmd' on the target + # return a tuple (output, exit_code) + def run(self, cmd): + self.__write(cmd + "\n") + output = self.__read_until("# ") + output = output.strip().splitlines() + output = output[1:len(output)-1] + + self.__write("echo $?\n") + exit_code = self.__read_until("# ") + exit_code = exit_code.strip().splitlines()[1] + exit_code = int(exit_code) + + return output, exit_code + + def stop(self): + if self.qemu is None: + return + self.qemu.terminate() + self.qemu.kill() |