Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/autotest/examples.py
9532 views
1
"""
2
Contains functions used to test the ArduPilot examples
3
4
AP_FLAKE8_CLEAN
5
"""
6
7
import os
8
import pexpect
9
import signal
10
import subprocess
11
import time
12
import traceback
13
14
from pysim import util
15
16
17
def run_example(name, filepath, valgrind=False, gdb=False):
18
cmd = []
19
if valgrind:
20
cmd.append("valgrind")
21
if gdb:
22
cmd.append("gdb")
23
cmd.append(filepath)
24
print("Running: (%s)" % str(cmd))
25
devnull = open("/dev/null", "w")
26
bob = subprocess.Popen(cmd, stdin=devnull, stdout=devnull, stderr=devnull, close_fds=True)
27
28
expect_exit = False
29
timeout = 10
30
if name in [
31
'RCProtocolTest',
32
'Scheduler_test',
33
'TransferFunctionCheck',
34
'XPlane',
35
]:
36
expect_exit = True
37
38
tstart = time.time()
39
while True:
40
if time.time() - tstart > timeout:
41
break
42
if not expect_exit:
43
retcode = bob.poll()
44
if retcode is not None:
45
raise ValueError("Process exited before I could kill it (%s)" % str(retcode))
46
47
if expect_exit:
48
retcode = bob.wait()
49
if retcode is None:
50
raise ValueError("Expected example to exit, it did not")
51
else:
52
bob.send_signal(signal.SIGTERM)
53
time.sleep(1)
54
retcode = bob.poll()
55
print("retcode: %s" % str(retcode))
56
if retcode is None:
57
# if we get this far then we're not going to get a gcda file
58
# out of this process for coverage analysis; it has to exit
59
# normally, and it hasn't responded to a TERM.
60
bob.kill()
61
retcode2 = bob.wait()
62
print("retcode2: %s" % str(retcode2))
63
return
64
65
if retcode == -15:
66
print("process exited with -15, indicating it didn't catch the TERM signal and exit properly")
67
elif retcode != 0:
68
# note that process could exit with code 0 and we couldn't tell...
69
raise ValueError("Process exited with non-zero exitcode %s" % str(retcode))
70
71
print("Ran: (%s)" % str(cmd))
72
73
74
def print_exception_stacktrace(e):
75
print(f"{e}\n")
76
print(''.join(traceback.format_exception(type(e),
77
e,
78
tb=e.__traceback__)))
79
80
81
def run_examples(debug=False, valgrind=False, gdb=False):
82
dirpath = util.reltopdir(os.path.join('build', 'sitl', 'examples'))
83
84
print("Running Hello")
85
# explicitly run helloworld and check for output
86
hello_path = os.path.join(dirpath, "Hello")
87
p = pexpect.spawn(hello_path, ["Hello"])
88
ex = None
89
try:
90
p.expect("hello world", timeout=5)
91
except pexpect.TIMEOUT as e:
92
ex = e
93
print("ran Hello")
94
95
p.close()
96
97
if ex is not None:
98
raise ex
99
100
# note that some of the comments on examples here are incorrect -
101
# since we are running on SITL it's not a matter of not having the
102
# hardware, rather the simulation hasn't been set up
103
# appropriately. We run with a model of "NoVehicle", which
104
# doesn't update the Aircraft base class.
105
skip = {
106
"AHRS_Test": "segfault as AP_Logger not instantiated",
107
"AP_FW_Controller_test": "exits with a status code of 1 (failure) for some reason",
108
"BARO_generic": "Most linux computers don't have baros...",
109
"DSP_test": "exits with an arithmetic exception",
110
"FlashTest": "https://github.com/ArduPilot/ardupilot/issues/14168",
111
"INS_generic": "SITL is not available, segfaults",
112
"ModuleTest": "test aborts",
113
"NMEA_Output": "segfault as AP_Logger not instantiated",
114
"RCProtocolDecoder": "This assumes specific hardware is connected",
115
"SlewLimiter": "exits with a status code of 1 (failure) for some reason",
116
"UART_chargen": "This nuke the term",
117
"AP_Logger_AllTypes": "sanity checks fail on log write as we are attempting to write LOG_FILE_MSG items out and that doesn't exist in the structure we are using in this test", # noqa:E501
118
"CompassCalibrator_index_test": "flow of control error, invalid rotation created in auto_rotation_index_test?",
119
"ReplayGyroFFT": "gyro data file /tmp/gyro0.dat (should this be a tool?)",
120
"jedec_test": "external flash not found in SITL",
121
}
122
123
failures = []
124
for afile in sorted(os.listdir(dirpath)):
125
if afile in skip:
126
print("Skipping %s: %s" % (afile, skip[afile]))
127
continue
128
filepath = os.path.join(dirpath, afile)
129
if not os.path.isfile(filepath):
130
continue
131
try:
132
run_example(afile, filepath, valgrind=valgrind, gdb=gdb)
133
except Exception as e:
134
print("Example failed with exception")
135
print_exception_stacktrace(e)
136
failures.append(afile)
137
138
if len(failures):
139
print("Failed examples:")
140
for failure in failures:
141
print(f" {failure}")
142
return False
143
144
return True
145
146