From 0b73fe657f6ba4be2315f81726e253daade9ba00 Mon Sep 17 00:00:00 2001 From: "Magomed Kostoev (mkostoevr)" Date: Thu, 4 Nov 2021 23:52:56 +0000 Subject: [PATCH] [KERNEL] Add test framework git-svn-id: svn://kolibrios.org@9249 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/boot/preboot.inc | 9 ++- kernel/trunk/bootbios.asm | 6 +- kernel/trunk/kernel.asm | 8 +- kernel/trunk/runtests.py | 95 +++++++++++++++++++++++ kernel/trunk/test/common/__init__.py | 68 ++++++++++++++++ kernel/trunk/test/kernel_revision/test.py | 11 +++ 6 files changed, 192 insertions(+), 5 deletions(-) create mode 100755 kernel/trunk/runtests.py create mode 100644 kernel/trunk/test/common/__init__.py create mode 100644 kernel/trunk/test/kernel_revision/test.py diff --git a/kernel/trunk/boot/preboot.inc b/kernel/trunk/boot/preboot.inc index 1492e181f8..eff3072be4 100644 --- a/kernel/trunk/boot/preboot.inc +++ b/kernel/trunk/boot/preboot.inc @@ -30,7 +30,14 @@ preboot_device db 0 ; device to load ramdisk from ; 1-floppy 2-harddisk 3-kernel restart ; 4-format ram disk 5-don't use ramdisk ; !!!! 0 - autodetect !!!! -preboot_biosdisk db 0 ; use V86 to access disks through BIOS (1-yes, 2-no) + +; use V86 to access disks through BIOS (1-yes, 2-no) +if defined pretest_build + preboot_biosdisk db 1 +else + preboot_biosdisk db 0 +end if + preboot_syspath db '/RD/1',0 ; path to /sys dir rb 20-($-preboot_syspath) if defined extended_primary_loader diff --git a/kernel/trunk/bootbios.asm b/kernel/trunk/bootbios.asm index 7039a6769f..ec5d944b80 100644 --- a/kernel/trunk/bootbios.asm +++ b/kernel/trunk/bootbios.asm @@ -11,7 +11,11 @@ include 'encoding.inc' include 'const.inc' os_code = code_l - tmp_gdt -PREBOOT_TIMEOUT = 5 ; seconds +if defined pretest_build + PREBOOT_TIMEOUT = 0 ; seconds +else + PREBOOT_TIMEOUT = 5 +end if use16 org 0x0 diff --git a/kernel/trunk/kernel.asm b/kernel/trunk/kernel.asm index ffe99bb925..9db1dccf09 100644 --- a/kernel/trunk/kernel.asm +++ b/kernel/trunk/kernel.asm @@ -144,10 +144,12 @@ pci_data_sel = pci_data_32-gdts ; places revision number there. if ~ defined UEFI bootbios: - if ~ defined extended_primary_loader - file 'bootbios.bin' - else + if defined extended_primary_loader file 'bootbios.bin.ext_loader' + else if defined pretest_build + file 'bootbios.bin.pretest' + else + file 'bootbios.bin' end if if __REV__ > 0 cur_pos = 0 diff --git a/kernel/trunk/runtests.py b/kernel/trunk/runtests.py new file mode 100755 index 0000000000..9c3846395b --- /dev/null +++ b/kernel/trunk/runtests.py @@ -0,0 +1,95 @@ +#!/usr/bin/python3 +# Copyright 2021 KolibriOS Team +# Copyright 2021 Nekos Team +# Published under MIT License + +import os +import sys +from importlib.machinery import SourceFileLoader +import timeit +import urllib.request +import subprocess + +sys.path.append('test') +import common + +root_dir = os.getcwd() +tests = [] + +def log(s, end = "\n"): + print(s, end = end, flush = True) + +def execute(s, mute = False): + mute = ">/dev/null" if mute else "" + code = os.system(f"{s}{mute}") + if code: + print(f"Command returned {code}: \"{s}\"") + exit(-1) + +def stage(name, command, mute = False): + print(f"{name}... ", end = "") + execute(command, mute = mute) + print("Done.") + +if not os.path.exists("kolibri_test.img"): + if len(sys.argv) == 1: + execute("wget -q --show-progress http://builds.kolibrios.org/eng/data/data/kolibri.img -O kolibri_test.img") + else: + builds = sys.argv[1] + execute(f"cp {builds}/data/data/kolibri.img kolibri_test.img") + +# Remove old kernel (may fail if we removed it before so no check here) +os.system("mdel -i kolibri_test.img ::kernel.mnt > /dev/null") + +# Check free space after kernel remove +free_clusters = int(subprocess.check_output("mdu -i kolibri_test.img :: -s", shell=True).split()[-1]) +floppy_image_clusters = 2880 +if floppy_image_clusters - free_clusters < 500: + # Remove unuseful files from IMG if lesser than 500 sectors + execute("mdeltree -i kolibri_test.img ::GAMES", mute = True) + execute("mdeltree -i kolibri_test.img ::DEMOS", mute = True) + execute("mdeltree -i kolibri_test.img ::3D", mute = True) + +# Build kernel with debug output +stage("Building bootbios.bin.pretest", + "fasm -m 65536 -dpretest_build=1 bootbios.asm bootbios.bin.pretest", mute = True) + +stage("Building kernel.mnt.pretest", + "fasm -m 65536 -dpretest_build=1 -ddebug_com_base=0xe9 kernel.asm kernel.mnt.pretest", mute = True) + +# Put the kernel into IMG +execute("mcopy -D o -i kolibri_test.img kernel.mnt.pretest ::kernel.mnt", mute = True) + +# Collect tests from test folder (not recursively yet) +for test_folder in os.listdir("test"): + test_folder_path = f"test/{test_folder}" + test_file = f"{test_folder_path}/test.py" + + if not os.path.isdir(test_folder_path): + continue + + if os.path.exists(test_file): + tests.append(test_folder_path) + +# Execute each test +test_number = 1 +for test in tests: + test_dir = f"{root_dir}/{test}" + + os.chdir(test_dir) + print(f"[{test_number}/{len(tests)}] {test}... ", end = "", flush=True) + start = timeit.default_timer() + try: + SourceFileLoader("test", "test.py").load_module().run() + except common.TestTimeoutException: + result = "TIMEOUT" + except common.TestFailureException: + result = "FAILURE" + else: + result = "SUCCESS" + finish = timeit.default_timer() + print(f"{result} ({finish - start:.2f} seconds)") + os.chdir(root_dir) + + test_number += 1 + diff --git a/kernel/trunk/test/common/__init__.py b/kernel/trunk/test/common/__init__.py new file mode 100644 index 0000000000..c4dea74b83 --- /dev/null +++ b/kernel/trunk/test/common/__init__.py @@ -0,0 +1,68 @@ +# Copyright 2021 KolibriOS Team +# Copyright 2021 Nekos Team +# Published under MIT License + +import io +import os +import subprocess +import timeit +import time +import shlex +import signal + +class TestTimeoutException(Exception): + pass + +class TestFailureException(Exception): + pass + +class Qemu: + def __init__(self, popen): + self.popen = popen + self.wait() # qemu needs time to create debug.log file + + def wait_for_debug_log(self, needle, timeout = 1): + needle = bytes(needle, "utf-8") + start = timeit.default_timer() + stdout = open("debug.log", "rb") + log = b"" + + # While no timeout, read and search logs + while timeit.default_timer() - start < timeout: + log += stdout.read(1) + if needle in log: + return + + # We don't have to read whole logs to find the neddle + # If we read len(needle) * 2 bytes of log then we + # already can say that if there's no needle in the data + # then it can't be in first len(needle) bytes of the data + # so first len(needle) bytes of saved logs may be thrown away + # + # So we consume lessser memory and don't search all the previous + # logs every single time + if len(log) > len(needle) * 2: + log = log[len(needle):] + + self.timeout() + + def kill(self): + os.killpg(os.getpgid(self.popen.pid), signal.SIGTERM) + + def failure(self): + self.kill() + raise TestFailureException() + + def timeout(self): + self.kill() + raise TestTimeoutException() + + def wait(self, seconds = 0.25): + time.sleep(seconds) + +def run(): + s = f"qemu-system-i386 -nographic -L . -m 128 -drive format=raw,file=../../kolibri_test.img,index=0,if=floppy -boot a -vga vmware -net nic,model=rtl8139 -net user -soundhw ac97 -debugcon file:debug.log" + a = shlex.split(s) + popen = subprocess.Popen(a, bufsize = 0, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL, start_new_session = True) + return Qemu(popen) + diff --git a/kernel/trunk/test/kernel_revision/test.py b/kernel/trunk/test/kernel_revision/test.py new file mode 100644 index 0000000000..a4f6a1609a --- /dev/null +++ b/kernel/trunk/test/kernel_revision/test.py @@ -0,0 +1,11 @@ +#!/usr/bin/python3 +import sys + +sys.path.append('../') +import common + +def run(): + os = common.run() + os.wait_for_debug_log("K : kernel SVN", timeout = 10) + os.kill() +