Subversion Repositories Kolibri OS

Rev

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