Subversion Repositories Kolibri OS

Rev

Rev 9397 | Rev 9412 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #!/usr/bin/python3
  2. # Copyright 2021 Magomed Kostoev
  3. # Published under MIT License
  4.  
  5. import os
  6. import sys
  7. import urllib
  8. from importlib.machinery import SourceFileLoader
  9. from shutil import which
  10. import timeit
  11. import urllib.request
  12. import subprocess
  13. from threading import Thread
  14. import filecmp
  15.  
  16. sys.path.append('test')
  17. import common
  18.  
  19. use_umka = False
  20.  
  21.  
  22. def log(s, end="\n"):
  23.     print(s, end=end, flush=True)
  24.  
  25.  
  26. def execute(s, mute=False):
  27.     mute = ">/dev/null" if mute else ""
  28.     code = os.system(f"{s}{mute}")
  29.     if code:
  30.         print(f"Command returned {code}: \"{s}\"")
  31.         exit(-1)
  32.  
  33.  
  34. def stage(name, command, mute=False):
  35.     print(f"{name}... ", end="")
  36.     execute(command, mute=mute)
  37.     print("Done.")
  38.  
  39.  
  40. def download(link, path):
  41.     log(f"Downloading {path}... ", end="")
  42.     urllib.request.urlretrieve(link, path)
  43.     log("Done.")
  44.  
  45.  
  46. def tool_exists(name):
  47.     assert(type(name) == str)
  48.     return which(name) is not None
  49.  
  50.  
  51. def check_tools(tools):
  52.     assert(type(tools) == tuple)
  53.     for name_package_pair in tools:
  54.         assert(type(name_package_pair) == tuple)
  55.         assert(len(name_package_pair) == 2)
  56.         assert(type(name_package_pair[0]) == str)
  57.         assert(type(name_package_pair[1]) == str)
  58.  
  59.     not_exists = []
  60.     for name, package in tools:
  61.         if not tool_exists(name):
  62.             not_exists.append((name, package))
  63.     if len(not_exists) != 0:
  64.         log("Sorry, I can't find some tools:")
  65.  
  66.         header_name = "Name"
  67.         header_package = "Package (probably)"
  68.  
  69.         max_name_len = len(header_name)
  70.         max_package_name_len = len(header_package)
  71.         for name, package in not_exists:
  72.             if len(package) > max_package_name_len:
  73.                 max_package_name_len = len(package)
  74.             if len(name) > max_name_len:
  75.                 max_name_len = len(name)
  76.  
  77.         def draw_row(name, package):
  78.             log((f" | {name.ljust(max_name_len)}" +
  79.                  f" | {package.ljust(max_package_name_len)} |"))
  80.  
  81.         def draw_line():
  82.             log(f" +-{'-' * max_name_len}-+-{'-' * max_package_name_len}-+")
  83.  
  84.         draw_line()
  85.         draw_row(header_name, header_package)
  86.         draw_line()
  87.         for name, package in not_exists:
  88.             draw_row(name, package)
  89.         draw_line()
  90.         exit()
  91.  
  92.  
  93. def prepare_test_img():
  94.     # TODO: Always recompile the kernel (after build system is done?)
  95.     # Get IMG
  96.     if not os.path.exists("kolibri_test.img"):
  97.         if len(sys.argv) == 1:
  98.             download("http://builds.kolibrios.org/eng/data/data/kolibri.img",
  99.                      "kolibri_test.img")
  100.         else:
  101.             builds_eng = sys.argv[1]
  102.             execute(f"cp {builds_eng}/data/data/kolibri.img kolibri_test.img")
  103.  
  104.     # Open the IMG
  105.     with open("kolibri_test.img", "rb") as img:
  106.         img_data = img.read()
  107.     img = common.Floppy(img_data)
  108.  
  109.     # Remove unuseful folders
  110.     img.delete_path("GAMES")
  111.     img.delete_path("DEMOS")
  112.     img.delete_path("3D")
  113.  
  114.     # Get test kernel
  115.     if not os.path.exists("kernel.mnt.pretest"):
  116.         if len(sys.argv) == 1:
  117.             with open("lang.inc", "w") as lang_inc:
  118.                 lang_inc.write("lang fix en\n")
  119.             execute("fasm bootbios.asm bootbios.bin.pretest -dpretest_build=1")
  120.             command = "fasm "
  121.             command += "-dpretest_build=1 -ddebug_com_base=0xe9 "
  122.             command += "-m 65536 "
  123.             command += "kernel.asm kernel.mnt.pretest"
  124.             execute(command)
  125.         else:
  126.             builds_eng = sys.argv[1]
  127.             kernel_mnt_pretest_subpath = "data/kernel/trunk/kernel.mnt.pretest"
  128.             kernel_mnt_pretest = f"{builds_eng}/{kernel_mnt_pretest_subpath}"
  129.             execute(f"cp {kernel_mnt_pretest} kernel.mnt.pretest", mute=True)
  130.  
  131.     # Put the kernel into IMG
  132.     with open("kernel.mnt.pretest", "rb") as kernel_mnt_pretest:
  133.         kernel_mnt_pretest_data = kernel_mnt_pretest.read()
  134.     img.add_file_path("KERNEL.MNT", kernel_mnt_pretest_data)
  135.     img.save("kolibri_test.img")
  136.  
  137.  
  138. def collect_tests():
  139.     tests = []
  140.  
  141.     # Collect tests from test folder (not recursively yet)
  142.     for test_folder in os.listdir("test"):
  143.         test_folder_path = f"test/{test_folder}"
  144.         test_file = f"{test_folder_path}/test.py"
  145.  
  146.         if not os.path.isdir(test_folder_path):
  147.             continue
  148.  
  149.         if os.path.exists(test_file):
  150.             tests.append(test_folder_path)
  151.     return tests
  152.  
  153.  
  154. def run_tests_serially_thread(test, root_dir):
  155.     test_number = 1
  156.     for test in tests:
  157.         test_dir = f"{root_dir}/{test}"
  158.  
  159.         print(f"[{test_number}/{len(tests)}] {test}... ", end="", flush=True)
  160.         start = timeit.default_timer()
  161.         try:
  162.             loader = SourceFileLoader("test", f"{test_dir}/test.py")
  163.             loader.load_module().run(root_dir, test_dir)
  164.         except common.TestTimeoutException:
  165.             result = "TIMEOUT"
  166.         except common.TestFailureException:
  167.             result = "FAILURE"
  168.         else:
  169.             result = "SUCCESS"
  170.         finish = timeit.default_timer()
  171.         print(f"{result} ({finish - start:.2f} seconds)")
  172.  
  173.         test_number += 1
  174.  
  175.  
  176. def run_tests_serially(tests, root_dir):
  177.     thread = Thread(target=run_tests_serially_thread, args=(tests, root_dir))
  178.     thread.start()
  179.     return thread
  180.  
  181.  
  182. def build_umka_asm(object_output_dir):
  183.     umka_o = f"{object_output_dir}/umka.o"
  184.     kolibri_kernel_trunk_runtests_py = os.path.abspath(__file__)
  185.     kolibri_kernel_trunk = os.path.dirname(kolibri_kernel_trunk_runtests_py)
  186.     kolibri_kernel = os.path.dirname(kolibri_kernel_trunk)
  187.     kolibrios_folder = os.path.dirname(kolibri_kernel)
  188.     env = os.environ
  189.     libcrash = "programs/develop/libraries/libcrash/hash"
  190.     env["INCLUDE"] = ""
  191.     env["INCLUDE"] += f"{kolibrios_folder}/kernel/trunk;"
  192.     env["INCLUDE"] += f"{kolibrios_folder}/{libcrash}"
  193.     command = "fasm "
  194.     command += "-dUEFI=1 -dextended_primary_loader=1 -dUMKA=1 "
  195.     command += "umka/umka.asm umka/build/umka.o -s umka/build/umka.fas "
  196.     command += "-m 2000000 "
  197.     stdout = subprocess.check_output(command, shell=True, env=env)
  198.     print(stdout)
  199.     return umka_o
  200.  
  201.  
  202. def cc(src, obj, include_path):
  203.     command = "clang "
  204.     command += "-Wno-everything -std=c11 -g -O0 -fno-pie -m32 -masm=intel -c "
  205.     command += "-D_FILE_OFFSET_BITS=64 -DNDEBUG -D_POSIX_C_SOURCE=200809L "
  206.     command += f"-I {include_path} {src} -o {obj}"
  207.     if os.system(command) != 0:
  208.         exit()
  209.  
  210.  
  211. def link(objects):
  212.     command = "clang "
  213.     command += "-Wno-everything "
  214.     command += "-no-pie -m32 -o umka_shell -static -T umka/umka.ld "
  215.     command += " ".join(objects)
  216.     if os.system(command) != 0:
  217.         exit()
  218.  
  219.  
  220. def build_umka():
  221.     if not use_umka:
  222.         return
  223.  
  224.     os.makedirs("umka/build", exist_ok=True)
  225.  
  226.     c_sources = [
  227.         "umka_shell.c",
  228.         "shell.c",
  229.         "trace.c",
  230.         "trace_lbr.c",
  231.         "vdisk.c",
  232.         "vnet.c",
  233.         "lodepng.c",
  234.         "linux/pci.c",
  235.         "linux/thread.c",
  236.         "util.c",
  237.     ]
  238.  
  239.     src_obj_pairs = [
  240.         (f"umka/{source}", f"umka/{source}.o") for source in c_sources
  241.     ]
  242.  
  243.     for src, obj in src_obj_pairs:
  244.         cc(src, obj, "umka/linux")
  245.  
  246.     umka_o = build_umka_asm("umka/build")
  247.  
  248.     objects = [obj for src, obj in src_obj_pairs] + [umka_o]
  249.     link(objects)
  250.  
  251.     os.chdir("umka/test")
  252.     for test in [t for t in os.listdir(".") if t.endswith(".t")]:
  253.         out_log = f"{test[:-2]}.out.log"
  254.         ref_log = f"{test[:-2]}.ref.log"
  255.         cmd_umka = f"../../umka_shell < {test} > {out_log}"
  256.         print(cmd_umka)
  257.         os.system(cmd_umka)
  258.         cmd_cmp = f"cmp {out_log} {ref_log}"
  259.         print(cmd_cmp)
  260.         if os.system(cmd_cmp) != 0:
  261.             print("FAILURE")
  262.             exit()
  263.     os.chdir("../../")
  264.     print("SUCCESS")
  265.     exit()
  266.  
  267. if __name__ == "__main__":
  268.     root_dir = os.getcwd()
  269.  
  270.     # Check available tools
  271.     tools = (("qemu-system-i386", "qemu-system-x86"),
  272.              ("fasm", "fasm"))
  273.     check_tools(tools)
  274.  
  275.     prepare_test_img()
  276.     build_umka()
  277.     tests = collect_tests()
  278.     serial_executor_thread = run_tests_serially(tests, root_dir)
  279.     serial_executor_thread.join()
  280.