Skip to content

Commit d109eef

Browse files
committed
Update to automate tests in emulated environment.
Signed-off-by: Paul Mani <paulmani88@yahoo.com>
1 parent 3f6c7fb commit d109eef

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

demos/copilot/src/monitors/Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ RUN_PREFIX := $(if $(shell mount | grep -q 'xfs' && echo 1),,docker run --rm $(C
99
RUN_PREFIX_TEST := $(if $(shell mount | grep -q 'xfs' && echo 1),,docker run --rm $(CONTAINER_TEST_ARGS) $(CONTAINER_IMG))
1010
RUN_IT_PREFIX := $(if $(shell mount | grep -q 'xfs' && echo 1),,docker run --rm -it $(CONTAINER_ARGS) $(CONTAINER_IMG))
1111
DEMO_CHDIR := $(if $(shell mount | grep -q 'xfs' && echo 1),,cd /demo/monitors &&)
12+
DEMO_CHDIR_TEST := $(if $(shell mount | grep -q 'xfs' && echo 1),,cd /demo/copilot/src/monitors &&)
13+
1214
CMD_PREFIX := $(if $(shell mount | grep -q 'xfs' && echo 1),,$(DEMO_CHDIR) export "PATH=$$PATH:/opt/ghc/8.6.5/bin/" &&)
15+
CMD_PREFIX_TEST := $(if $(shell mount | grep -q 'xfs' && echo 1),,$(DEMO_CHDIR_TEST) export "PATH=$$PATH:/opt/ghc/8.6.5/bin/" &&)
1316

1417
# TODO add CICD adjustments to prefix and when detecting that we're running inside a container (for the `make dev` case)
1518

@@ -98,11 +101,14 @@ run: main_syslog_time
98101
&& tmux attach-session -t my_session"
99102
# To kill session, `Ctrl-b` then type `:kill-session` <enter>
100103

101-
test: test_monitors systemtest
104+
test: test_monitors systemtest main_syslog_time_cross_test
102105

103106
test_monitors: main_syslog_test
104107
./test.sh
105108

106109
systemtest: main_syslog_time
107110
$(RUN_PREFIX_TEST) sudo sh -c "cd /demo/copilot/src/monitors \
108-
&& ../tests/runtests.sh"
111+
&& ../tests/runtests.sh"
112+
113+
main_syslog_time_cross_test: main_syslog_time_cross
114+
$(RUN_PREFIX_TEST) sh -c '$(CMD_PREFIX_TEST) python3 ../tests/wrap-qemu.py'

demos/copilot/src/tests/runtests.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@ if [ -d "$SCRIPT_DIR/../../../test-results/" ]; then
5555
mv "$REPORT_FILE" "$SCRIPT_DIR/../../../test-results/"
5656
echo "JUnit Report moved to: $SCRIPT_DIR/../../../test-results/"
5757
else
58-
echo "Test results directory not found. Leaving report in current directory."
59-
fi
58+
echo "Test results directory not found. Writing results to console"
59+
cat "$REPORT_FILE"
60+
fi
61+
62+
echo "Tests Completed Successfully."
63+
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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

Comments
 (0)