|
| 1 | +import subprocess |
| 2 | +import time |
| 3 | +import select |
| 4 | +import sys |
| 5 | + |
| 6 | +def isWatchedStr(str_to_watch, output): |
| 7 | + if str_to_watch in output: |
| 8 | + print(f" >>>>>>>> Found the string: [{str_to_watch}] <<<<<<<<") |
| 9 | + return True |
| 10 | + return False |
| 11 | + |
| 12 | +def run_qemu_with_watch(str_to_exit_on, timeout, commands): |
| 13 | + qemu_command = [ |
| 14 | + 'qemu-system-aarch64', |
| 15 | + '-M', 'virt', |
| 16 | + '-m', '512M', |
| 17 | + '-cpu', 'cortex-a57', |
| 18 | + '-smp', '4', |
| 19 | + '-nographic', |
| 20 | + '-kernel', 'Image', |
| 21 | + '-append', 'root=/dev/ram0 init=/bin/dash console=ttyAMA0 loglevel=7', |
| 22 | + '-initrd', 'rootfs.cpio.gz_new' |
| 23 | + ] |
| 24 | + qemu_process = subprocess.Popen( |
| 25 | + qemu_command, |
| 26 | + stdin=subprocess.PIPE, |
| 27 | + stdout=subprocess.PIPE, |
| 28 | + stderr=subprocess.STDOUT, |
| 29 | + universal_newlines=True |
| 30 | + ) |
| 31 | + |
| 32 | + start_time = time.time() |
| 33 | + command_index = 0 # Track the current command to send |
| 34 | + commandReady = False |
| 35 | + loginPrompt =False |
| 36 | + outputFile = None |
| 37 | + resultStart = False |
| 38 | + while True: |
| 39 | + ready, _ , _ = select.select([qemu_process.stdout], [], [], 2) # Monitor QEMU output |
| 40 | + if qemu_process.stdout in ready: |
| 41 | + output = qemu_process.stdout.readline() |
| 42 | + if output == '': |
| 43 | + print("QEMU process has terminated.") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + isWatchedStr("Freeing unused kernel memory", output) |
| 47 | + |
| 48 | + if isWatchedStr("Welcome to Buildroot", output): |
| 49 | + loginPrompt = True |
| 50 | + |
| 51 | + |
| 52 | + if isWatchedStr("#", output): |
| 53 | + commandReady = True |
| 54 | + else: |
| 55 | + commandReady = False |
| 56 | + |
| 57 | + if isWatchedStr("buildroot login:", output): |
| 58 | + commandReady = True |
| 59 | + |
| 60 | + if isWatchedStr("Test results directory not found. Writing results to console", output): |
| 61 | + resultStart = True |
| 62 | + |
| 63 | + if str_to_exit_on in output: |
| 64 | + print(output.strip()) |
| 65 | + print(f" >>>>>>>> Found the string: [{str_to_exit_on}] <<<<<<<< SUCCESSFUL sequence of output checking") |
| 66 | + qemu_process.terminate() |
| 67 | + if outputFile: |
| 68 | + outputFile.close() |
| 69 | + resultStart = False |
| 70 | + break |
| 71 | + |
| 72 | + # Print QEMU output |
| 73 | + print(output.strip()) |
| 74 | + if(resultStart): |
| 75 | + if(outputFile is None): |
| 76 | + outputFile = open("../../../test-results-system-cross.xml", "w") |
| 77 | + outputFile.write(output) |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + # Wait for stdin to be ready before sending commands |
| 82 | + if loginPrompt: |
| 83 | + time.sleep(1) # Small delay to ensure QEMU is ready |
| 84 | + print("Stdin is ready. Sending command...") |
| 85 | + try: |
| 86 | + qemu_process.stdin.write("root" + '\n') |
| 87 | + qemu_process.stdin.flush() |
| 88 | + qemu_process.stdin.write('\n') |
| 89 | + qemu_process.stdin.flush() |
| 90 | + print(f"Sent command: root") |
| 91 | + command_index += 1 |
| 92 | + loginPrompt = False |
| 93 | + except BrokenPipeError: |
| 94 | + print("QEMU stdin is not ready. Retrying...") |
| 95 | + time.sleep(1) # Wait before retrying |
| 96 | + |
| 97 | + |
| 98 | + # Wait for stdin to be ready before sending commands |
| 99 | + if commandReady and command_index < len(commands): |
| 100 | + time.sleep(1) # Small delay to ensure QEMU is ready |
| 101 | + print("Stdin is ready. Sending command...") |
| 102 | + try: |
| 103 | + qemu_process.stdin.write(commands[command_index] + '\n') |
| 104 | + qemu_process.stdin.flush() |
| 105 | + print(f"Sent command: {commands[command_index]}") |
| 106 | + command_index += 1 |
| 107 | + except BrokenPipeError: |
| 108 | + print("QEMU stdin is not ready. Retrying...") |
| 109 | + time.sleep(1) # Wait before retrying |
| 110 | + |
| 111 | + if time.time() - start_time > timeout: |
| 112 | + print("Timeout reached. ERROR: Stopping QEMU process.") |
| 113 | + qemu_process.terminate() |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + str_to_exit_on = "Tests Completed Successfully." |
| 118 | + timeout = 300 # Timeout in seconds |
| 119 | + commands = [ |
| 120 | + "cd monitors/ ", |
| 121 | + "ls /", |
| 122 | + "pwd", |
| 123 | + "ls", |
| 124 | + "cd monitors/ && ../tests/runtests.sh" |
| 125 | + ] # List of commands to send to QEMU console |
| 126 | + run_qemu_with_watch(str_to_exit_on, timeout, commands) |
0 commit comments