2021-11-05 02:52:56 +03:00
|
|
|
#!/usr/bin/python3
|
2021-11-25 23:53:33 +03:00
|
|
|
# Copyright 2021 Magomed Kostoev
|
2021-11-05 02:52:56 +03:00
|
|
|
# Published under MIT License
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2021-11-26 00:54:29 +03:00
|
|
|
import urllib
|
2021-11-05 02:52:56 +03:00
|
|
|
from importlib.machinery import SourceFileLoader
|
2021-11-25 23:26:22 +03:00
|
|
|
from shutil import which
|
2021-11-05 02:52:56 +03:00
|
|
|
import timeit
|
|
|
|
import urllib.request
|
|
|
|
import subprocess
|
2021-11-27 00:38:46 +03:00
|
|
|
from threading import Thread
|
2021-11-27 23:07:11 +03:00
|
|
|
import filecmp
|
2021-11-05 02:52:56 +03:00
|
|
|
|
|
|
|
sys.path.append('test')
|
|
|
|
import common
|
|
|
|
|
2021-12-05 16:48:32 +03:00
|
|
|
use_umka = False
|
|
|
|
|
2021-11-05 02:52:56 +03:00
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
def log(s, end="\n"):
|
|
|
|
print(s, end=end, flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
def execute(s, mute=False):
|
2021-11-05 02:52:56 +03:00
|
|
|
mute = ">/dev/null" if mute else ""
|
|
|
|
code = os.system(f"{s}{mute}")
|
|
|
|
if code:
|
|
|
|
print(f"Command returned {code}: \"{s}\"")
|
|
|
|
exit(-1)
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
|
|
|
def stage(name, command, mute=False):
|
|
|
|
print(f"{name}... ", end="")
|
|
|
|
execute(command, mute=mute)
|
2021-11-05 02:52:56 +03:00
|
|
|
print("Done.")
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-26 00:54:29 +03:00
|
|
|
def download(link, path):
|
2021-12-07 23:02:15 +03:00
|
|
|
log(f"Downloading {path}... ", end="")
|
2021-11-26 00:57:16 +03:00
|
|
|
urllib.request.urlretrieve(link, path)
|
2021-11-26 00:54:29 +03:00
|
|
|
log("Done.")
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-25 23:26:22 +03:00
|
|
|
def tool_exists(name):
|
|
|
|
assert(type(name) == str)
|
2021-12-07 23:02:15 +03:00
|
|
|
return which(name) is not None
|
|
|
|
|
2021-11-25 23:26:22 +03:00
|
|
|
|
|
|
|
def check_tools(tools):
|
|
|
|
assert(type(tools) == tuple)
|
|
|
|
for name_package_pair in tools:
|
|
|
|
assert(type(name_package_pair) == tuple)
|
|
|
|
assert(len(name_package_pair) == 2)
|
|
|
|
assert(type(name_package_pair[0]) == str)
|
|
|
|
assert(type(name_package_pair[1]) == str)
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-25 23:26:22 +03:00
|
|
|
not_exists = []
|
|
|
|
for name, package in tools:
|
|
|
|
if not tool_exists(name):
|
|
|
|
not_exists.append((name, package))
|
|
|
|
if len(not_exists) != 0:
|
|
|
|
log("Sorry, I can't find some tools:")
|
2021-11-25 23:32:59 +03:00
|
|
|
|
|
|
|
header_name = "Name"
|
|
|
|
header_package = "Package (probably)"
|
|
|
|
|
|
|
|
max_name_len = len(header_name)
|
|
|
|
max_package_name_len = len(header_package)
|
2021-11-25 23:26:22 +03:00
|
|
|
for name, package in not_exists:
|
|
|
|
if len(package) > max_package_name_len:
|
|
|
|
max_package_name_len = len(package)
|
|
|
|
if len(name) > max_name_len:
|
|
|
|
max_name_len = len(name)
|
|
|
|
|
|
|
|
def draw_row(name, package):
|
2021-12-07 23:02:15 +03:00
|
|
|
log((f" | {name.ljust(max_name_len)}" +
|
|
|
|
f" | {package.ljust(max_package_name_len)} |"))
|
2021-11-25 23:26:22 +03:00
|
|
|
|
|
|
|
def draw_line():
|
|
|
|
log(f" +-{'-' * max_name_len}-+-{'-' * max_package_name_len}-+")
|
|
|
|
|
|
|
|
draw_line()
|
2021-11-25 23:32:59 +03:00
|
|
|
draw_row(header_name, header_package)
|
2021-11-25 23:26:22 +03:00
|
|
|
draw_line()
|
|
|
|
for name, package in not_exists:
|
|
|
|
draw_row(name, package)
|
|
|
|
draw_line()
|
|
|
|
exit()
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-27 00:30:41 +03:00
|
|
|
def prepare_test_img():
|
|
|
|
# TODO: Always recompile the kernel (after build system is done?)
|
2021-11-26 00:04:13 +03:00
|
|
|
# Get IMG
|
|
|
|
if not os.path.exists("kolibri_test.img"):
|
|
|
|
if len(sys.argv) == 1:
|
2021-12-07 23:02:15 +03:00
|
|
|
download("http://builds.kolibrios.org/eng/data/data/kolibri.img",
|
|
|
|
"kolibri_test.img")
|
2021-11-26 00:04:13 +03:00
|
|
|
else:
|
|
|
|
builds_eng = sys.argv[1]
|
|
|
|
execute(f"cp {builds_eng}/data/data/kolibri.img kolibri_test.img")
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-26 01:00:43 +03:00
|
|
|
# Open the IMG
|
|
|
|
with open("kolibri_test.img", "rb") as img:
|
|
|
|
img_data = img.read()
|
|
|
|
img = common.Floppy(img_data)
|
2021-11-27 00:30:41 +03:00
|
|
|
|
2021-11-26 01:00:43 +03:00
|
|
|
# Remove unuseful folders
|
|
|
|
img.delete_path("GAMES")
|
|
|
|
img.delete_path("DEMOS")
|
|
|
|
img.delete_path("3D")
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
# Get test kernel
|
|
|
|
if not os.path.exists("kernel.mnt.pretest"):
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
with open("lang.inc", "w") as lang_inc:
|
|
|
|
lang_inc.write("lang fix en\n")
|
|
|
|
execute("fasm bootbios.asm bootbios.bin.pretest -dpretest_build=1")
|
2021-12-07 23:02:15 +03:00
|
|
|
command = "fasm "
|
|
|
|
command += "-dpretest_build=1 -ddebug_com_base=0xe9 "
|
|
|
|
command += "-m 65536 "
|
|
|
|
command += "kernel.asm kernel.mnt.pretest"
|
|
|
|
execute(command)
|
2021-11-26 00:04:13 +03:00
|
|
|
else:
|
|
|
|
builds_eng = sys.argv[1]
|
2021-12-07 23:02:15 +03:00
|
|
|
kernel_mnt_pretest_subpath = "data/kernel/trunk/kernel.mnt.pretest"
|
|
|
|
kernel_mnt_pretest = f"{builds_eng}/{kernel_mnt_pretest_subpath}"
|
|
|
|
execute(f"cp {kernel_mnt_pretest} kernel.mnt.pretest", mute=True)
|
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
# Put the kernel into IMG
|
2021-11-26 01:00:43 +03:00
|
|
|
with open("kernel.mnt.pretest", "rb") as kernel_mnt_pretest:
|
|
|
|
kernel_mnt_pretest_data = kernel_mnt_pretest.read()
|
2021-11-26 02:04:42 +03:00
|
|
|
img.add_file_path("KERNEL.MNT", kernel_mnt_pretest_data)
|
2021-11-26 01:00:43 +03:00
|
|
|
img.save("kolibri_test.img")
|
2021-11-27 00:30:41 +03:00
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-27 00:30:41 +03:00
|
|
|
def collect_tests():
|
|
|
|
tests = []
|
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
# 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"
|
2021-11-27 00:30:41 +03:00
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
if not os.path.isdir(test_folder_path):
|
|
|
|
continue
|
2021-11-27 00:30:41 +03:00
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
if os.path.exists(test_file):
|
|
|
|
tests.append(test_folder_path)
|
2021-11-27 00:30:41 +03:00
|
|
|
return tests
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-27 00:56:18 +03:00
|
|
|
def run_tests_serially_thread(test, root_dir):
|
2021-11-26 00:04:13 +03:00
|
|
|
test_number = 1
|
|
|
|
for test in tests:
|
|
|
|
test_dir = f"{root_dir}/{test}"
|
2021-12-07 23:02:15 +03:00
|
|
|
|
|
|
|
print(f"[{test_number}/{len(tests)}] {test}... ", end="", flush=True)
|
2021-11-26 00:04:13 +03:00
|
|
|
start = timeit.default_timer()
|
|
|
|
try:
|
2021-12-07 23:02:15 +03:00
|
|
|
loader = SourceFileLoader("test", f"{test_dir}/test.py")
|
|
|
|
loader.load_module().run(root_dir, test_dir)
|
2021-11-26 00:04:13 +03:00
|
|
|
except common.TestTimeoutException:
|
|
|
|
result = "TIMEOUT"
|
|
|
|
except common.TestFailureException:
|
|
|
|
result = "FAILURE"
|
|
|
|
else:
|
|
|
|
result = "SUCCESS"
|
|
|
|
finish = timeit.default_timer()
|
|
|
|
print(f"{result} ({finish - start:.2f} seconds)")
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-26 00:04:13 +03:00
|
|
|
test_number += 1
|
2021-11-27 00:38:46 +03:00
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-27 00:56:18 +03:00
|
|
|
def run_tests_serially(tests, root_dir):
|
2021-12-07 23:02:15 +03:00
|
|
|
thread = Thread(target=run_tests_serially_thread, args=(tests, root_dir))
|
2021-11-27 00:56:18 +03:00
|
|
|
thread.start()
|
|
|
|
return thread
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-12-05 16:48:32 +03:00
|
|
|
def build_umka_asm(object_output_dir):
|
|
|
|
umka_o = f"{object_output_dir}/umka.o"
|
2021-12-07 23:02:15 +03:00
|
|
|
kolibri_kernel_trunk_runtests_py = os.path.abspath(__file__)
|
|
|
|
kolibri_kernel_trunk = os.path.dirname(kolibri_kernel_trunk_runtests_py)
|
|
|
|
kolibri_kernel = os.path.dirname(kolibri_kernel_trunk)
|
|
|
|
kolibrios_folder = os.path.dirname(kolibri_kernel)
|
2021-12-05 16:48:32 +03:00
|
|
|
env = os.environ
|
2021-12-07 23:02:15 +03:00
|
|
|
libcrash = "programs/develop/libraries/libcrash/hash"
|
2021-12-05 16:48:32 +03:00
|
|
|
env["INCLUDE"] = ""
|
|
|
|
env["INCLUDE"] += f"{kolibrios_folder}/kernel/trunk;"
|
2021-12-07 23:02:15 +03:00
|
|
|
env["INCLUDE"] += f"{kolibrios_folder}/{libcrash}"
|
|
|
|
command = "fasm "
|
2021-12-10 21:06:40 +03:00
|
|
|
command += "-dWIN32=1 " if sys.platform == "win32" else ""
|
2021-12-07 23:02:15 +03:00
|
|
|
command += "-dUEFI=1 -dextended_primary_loader=1 -dUMKA=1 "
|
|
|
|
command += "umka/umka.asm umka/build/umka.o -s umka/build/umka.fas "
|
|
|
|
command += "-m 2000000 "
|
2021-12-10 21:06:40 +03:00
|
|
|
print(command)
|
2021-12-07 23:02:15 +03:00
|
|
|
stdout = subprocess.check_output(command, shell=True, env=env)
|
2021-12-10 21:06:40 +03:00
|
|
|
print(stdout.decode("ascii"))
|
2021-12-05 16:48:32 +03:00
|
|
|
return umka_o
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-12-05 16:48:32 +03:00
|
|
|
def cc(src, obj, include_path):
|
|
|
|
command = "clang "
|
2021-12-07 23:02:15 +03:00
|
|
|
command += "-Wno-everything -std=c11 -g -O0 -fno-pie -m32 -masm=intel -c "
|
|
|
|
command += "-D_FILE_OFFSET_BITS=64 -DNDEBUG -D_POSIX_C_SOURCE=200809L "
|
2021-12-05 16:48:32 +03:00
|
|
|
command += f"-I {include_path} {src} -o {obj}"
|
2021-12-10 21:06:40 +03:00
|
|
|
print(command)
|
2021-12-05 16:48:32 +03:00
|
|
|
if os.system(command) != 0:
|
|
|
|
exit()
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-12-05 16:48:32 +03:00
|
|
|
def link(objects):
|
2021-12-10 21:06:40 +03:00
|
|
|
if sys.platform == "linux" or sys.platform == "linux2":
|
|
|
|
linker_script = "-T umka/umka.ld"
|
|
|
|
else:
|
|
|
|
linker_script = "-Wl,/ALIGN:65536 -Wl,/MAP:umka.map "
|
2021-12-05 16:48:32 +03:00
|
|
|
command = "clang "
|
2021-12-07 23:02:15 +03:00
|
|
|
command += "-Wno-everything "
|
2021-12-10 21:06:40 +03:00
|
|
|
command += f"-no-pie -m32 -o umka_shell.exe -static {linker_script} "
|
2021-12-05 16:48:32 +03:00
|
|
|
command += " ".join(objects)
|
2021-12-10 21:06:40 +03:00
|
|
|
print(command)
|
2021-12-05 16:48:32 +03:00
|
|
|
if os.system(command) != 0:
|
|
|
|
exit()
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-12-05 16:48:32 +03:00
|
|
|
def build_umka():
|
|
|
|
if not use_umka:
|
|
|
|
return
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
os.makedirs("umka/build", exist_ok=True)
|
2021-12-05 16:48:32 +03:00
|
|
|
|
2021-12-10 21:06:40 +03:00
|
|
|
platform = "win32" if sys.platform == "win32" else "linux"
|
|
|
|
|
|
|
|
os.makedirs(f"umka/build/{platform}", exist_ok=True)
|
|
|
|
|
2021-12-05 16:48:32 +03:00
|
|
|
c_sources = [
|
|
|
|
"umka_shell.c",
|
|
|
|
"shell.c",
|
|
|
|
"trace.c",
|
|
|
|
"trace_lbr.c",
|
|
|
|
"vdisk.c",
|
|
|
|
"vnet.c",
|
2021-12-10 21:06:40 +03:00
|
|
|
"getopt.c",
|
|
|
|
"isatty.c",
|
2021-12-05 16:48:32 +03:00
|
|
|
"lodepng.c",
|
2021-12-10 21:06:40 +03:00
|
|
|
f"{platform}/pci.c",
|
|
|
|
f"{platform}/thread.c",
|
2021-12-05 16:48:32 +03:00
|
|
|
"util.c",
|
|
|
|
]
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
src_obj_pairs = [
|
2021-12-10 21:06:40 +03:00
|
|
|
(f"umka/{source}", f"umka/build/{source}.o") for source in c_sources
|
2021-12-07 23:02:15 +03:00
|
|
|
]
|
2021-12-05 16:48:32 +03:00
|
|
|
|
|
|
|
for src, obj in src_obj_pairs:
|
|
|
|
cc(src, obj, "umka/linux")
|
|
|
|
|
|
|
|
umka_o = build_umka_asm("umka/build")
|
|
|
|
|
2021-12-07 23:02:15 +03:00
|
|
|
objects = [obj for src, obj in src_obj_pairs] + [umka_o]
|
2021-12-05 16:48:32 +03:00
|
|
|
link(objects)
|
|
|
|
|
|
|
|
os.chdir("umka/test")
|
2021-12-07 23:02:15 +03:00
|
|
|
for test in [t for t in os.listdir(".") if t.endswith(".t")]:
|
2021-12-05 16:48:32 +03:00
|
|
|
out_log = f"{test[:-2]}.out.log"
|
|
|
|
ref_log = f"{test[:-2]}.ref.log"
|
2021-12-10 21:06:40 +03:00
|
|
|
cmd_umka = f"..{os.sep}..{os.sep}umka_shell.exe < {test} > {out_log}"
|
2021-12-05 16:48:32 +03:00
|
|
|
print(cmd_umka)
|
|
|
|
os.system(cmd_umka)
|
2021-12-10 21:06:40 +03:00
|
|
|
with open(out_log, "rb") as f:
|
|
|
|
crlf = bytes([0x0D, 0x0A])
|
|
|
|
lf = bytes([0x0A])
|
|
|
|
out_log_contents = f.read().replace(crlf, lf)
|
|
|
|
with open(out_log, "wb") as f:
|
|
|
|
f.write(out_log_contents)
|
|
|
|
with open(ref_log, "rb") as f:
|
|
|
|
ref_log_contents = f.read()
|
|
|
|
if out_log_contents != ref_log_contents:
|
2021-12-05 16:48:32 +03:00
|
|
|
print("FAILURE")
|
|
|
|
exit()
|
|
|
|
os.chdir("../../")
|
|
|
|
print("SUCCESS")
|
|
|
|
exit()
|
|
|
|
|
2021-11-27 00:38:46 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
root_dir = os.getcwd()
|
|
|
|
|
|
|
|
# Check available tools
|
|
|
|
tools = (("qemu-system-i386", "qemu-system-x86"),
|
|
|
|
("fasm", "fasm"))
|
|
|
|
check_tools(tools)
|
2021-12-07 23:02:15 +03:00
|
|
|
|
2021-11-27 00:38:46 +03:00
|
|
|
prepare_test_img()
|
2021-12-05 16:48:32 +03:00
|
|
|
build_umka()
|
2021-11-27 00:38:46 +03:00
|
|
|
tests = collect_tests()
|
2021-11-27 00:56:18 +03:00
|
|
|
serial_executor_thread = run_tests_serially(tests, root_dir)
|
2021-11-27 13:58:06 +03:00
|
|
|
serial_executor_thread.join()
|