Subversion Repositories Kolibri OS

Rev

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

  1. # Copyright 2021 KolibriOS Team
  2. # Copyright 2021 Nekos Team
  3. # Published under MIT License
  4.  
  5. import io
  6. import os
  7. import subprocess
  8. import timeit
  9. import time
  10. import shlex
  11. import signal
  12.  
  13. class TestTimeoutException(Exception):
  14.     pass
  15.  
  16. class TestFailureException(Exception):
  17.     pass
  18.  
  19. class Qemu:
  20.     def __init__(self, popen):
  21.         self.popen = popen
  22.         # Qemu needs time to create debug.log file
  23.         while not os.path.exists("debug.log"):
  24.             self.wait()
  25.  
  26.     def wait_for_debug_log(self, needle, timeout = 1):
  27.         needle = bytes(needle, "utf-8")
  28.         start = timeit.default_timer()
  29.         stdout = open("debug.log", "rb")
  30.         log = b""
  31.  
  32.         # While no timeout, read and search logs
  33.         while timeit.default_timer() - start < timeout:
  34.             log += stdout.read(1)
  35.             if needle in log:
  36.                 return
  37.  
  38.             # We don't have to read whole logs to find the neddle
  39.             # If we read len(needle) * 2 bytes of log then we
  40.             # already can say that if there's no needle in the data
  41.             # then it can't be in first len(needle) bytes of the data
  42.             # so first len(needle) bytes of saved logs may be thrown away
  43.             #
  44.             # So we consume lessser memory and don't search all the previous
  45.             # logs every single time
  46.             if len(log) > len(needle) * 2:
  47.                 log = log[len(needle):]
  48.  
  49.         self.timeout()
  50.  
  51.     def kill(self):
  52.         os.killpg(os.getpgid(self.popen.pid), signal.SIGTERM)
  53.  
  54.     def failure(self):
  55.         self.kill()
  56.         raise TestFailureException()
  57.  
  58.     def timeout(self):
  59.         self.kill()
  60.         raise TestTimeoutException()
  61.  
  62.     def wait(self, seconds = 0.25):
  63.         time.sleep(seconds)
  64.  
  65. def run():
  66.     os.remove("debug.log")
  67.     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"
  68.     a = shlex.split(s)
  69.     popen = subprocess.Popen(a, bufsize = 0, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL, stdin = subprocess.DEVNULL, start_new_session = True)
  70.     return Qemu(popen)
  71.  
  72.