2020-11-10 23:45:50 -04:00
|
|
|
#! /usr/bin/env python3
|
2018-08-04 17:48:47 -03:00
|
|
|
|
|
|
|
import serial, time
|
|
|
|
import subprocess
|
|
|
|
from subprocess import call, Popen
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import re
|
2020-02-25 04:58:02 -04:00
|
|
|
import unittest
|
|
|
|
import os
|
2020-11-10 23:45:50 -04:00
|
|
|
import sys
|
2021-07-17 10:33:39 -03:00
|
|
|
import datetime
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2021-07-16 22:08:08 -03:00
|
|
|
COLOR_RED = "\x1b[31m"
|
2021-07-16 11:46:44 -03:00
|
|
|
COLOR_GREEN = "\x1b[32m"
|
2021-07-16 22:08:08 -03:00
|
|
|
COLOR_YELLOW = "\x1b[33m"
|
2021-07-16 11:46:44 -03:00
|
|
|
COLOR_WHITE = "\x1b[37m"
|
|
|
|
COLOR_RESET = "\x1b[0m"
|
|
|
|
|
|
|
|
def print_line(line):
|
|
|
|
if "WARNING" in line:
|
|
|
|
line = line.replace("WARNING", f"{COLOR_YELLOW}WARNING{COLOR_RESET}", 1)
|
|
|
|
elif "WARN" in line:
|
|
|
|
line = line.replace("WARN", f"{COLOR_YELLOW}WARN{COLOR_RESET}", 1)
|
|
|
|
elif "ERROR" in line:
|
|
|
|
line = line.replace("ERROR", f"{COLOR_RED}ERROR{COLOR_RESET}", 1)
|
|
|
|
elif "INFO" in line:
|
|
|
|
line = line.replace("INFO", f"{COLOR_WHITE}INFO{COLOR_RESET}", 1)
|
|
|
|
|
|
|
|
if "PASSED" in line:
|
|
|
|
line = line.replace("PASSED", f"{COLOR_GREEN}PASSED{COLOR_RESET}", 1)
|
|
|
|
|
|
|
|
if "FAILED" in line:
|
|
|
|
line = line.replace("FAILED", f"{COLOR_RED}FAILED{COLOR_RESET}", 1)
|
|
|
|
|
2021-08-24 10:15:48 -03:00
|
|
|
if "\n" in line:
|
|
|
|
current_time = datetime.datetime.now()
|
|
|
|
print('[{0}] {1}'.format(current_time.isoformat(timespec='milliseconds'), line), end='')
|
|
|
|
else:
|
|
|
|
print('{0}'.format(line), end='')
|
2021-07-16 11:46:44 -03:00
|
|
|
|
2019-06-15 12:09:21 -03:00
|
|
|
def do_test(port, baudrate, test_name):
|
2021-07-17 10:33:39 -03:00
|
|
|
ser = serial.Serial(port, baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=0.2, xonxoff=True, rtscts=False, dsrdtr=False)
|
2021-07-15 20:17:48 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-08-23 17:45:46 -03:00
|
|
|
timeout = 30 # 30 seconds
|
2021-07-15 20:17:48 -03:00
|
|
|
|
|
|
|
# wait for nsh prompt
|
|
|
|
while True:
|
|
|
|
ser.write("\n".encode("ascii"))
|
|
|
|
ser.flush()
|
|
|
|
|
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
|
|
|
|
|
|
|
if "nsh>" in serial_line:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
|
|
|
print(serial_line, end='')
|
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
2021-07-15 20:17:48 -03:00
|
|
|
print("Error, timeout waiting for prompt")
|
|
|
|
return False
|
2019-06-15 12:09:21 -03:00
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
# clear
|
|
|
|
ser.readlines()
|
|
|
|
|
2019-06-15 13:03:37 -03:00
|
|
|
success = False
|
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
# run test cmd
|
|
|
|
print('\n|======================================================================')
|
|
|
|
cmd = 'tests ' + test_name
|
|
|
|
print("| Running:", cmd)
|
|
|
|
print('|======================================================================')
|
2021-07-15 20:17:48 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-07-17 10:33:39 -03:00
|
|
|
timeout = 2 # 2 seconds
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
# wait for command echo
|
2021-12-11 14:41:06 -04:00
|
|
|
print("Running command: \'{0}\'".format(cmd))
|
2020-11-13 16:17:31 -04:00
|
|
|
serial_cmd = '{0}\n'.format(cmd)
|
|
|
|
ser.write(serial_cmd.encode("ascii"))
|
|
|
|
ser.flush()
|
2021-07-15 20:17:48 -03:00
|
|
|
while True:
|
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
2020-11-13 16:17:31 -04:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
if cmd in serial_line:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
2021-07-16 11:46:44 -03:00
|
|
|
print_line(serial_line)
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
2021-07-15 20:17:48 -03:00
|
|
|
print("Error, timeout waiting for command echo")
|
|
|
|
break
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2020-09-28 17:41:28 -03:00
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
# print results, wait for final result (PASSED or FAILED)
|
2021-07-18 12:09:49 -03:00
|
|
|
timeout = 300 # 5 minutes
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2020-09-28 17:41:28 -03:00
|
|
|
timeout_newline = timeout_start
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2020-09-28 17:41:28 -03:00
|
|
|
while True:
|
2020-11-13 16:17:31 -04:00
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
2021-07-16 11:46:44 -03:00
|
|
|
|
|
|
|
if len(serial_line) > 0:
|
|
|
|
print_line(serial_line)
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
if test_name + " PASSED" in serial_line:
|
2019-06-15 12:09:21 -03:00
|
|
|
success = True
|
2020-09-28 17:41:28 -03:00
|
|
|
break
|
2019-06-15 12:09:21 -03:00
|
|
|
elif test_name + " FAILED" in serial_line:
|
|
|
|
success = False
|
2020-09-28 17:41:28 -03:00
|
|
|
break
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
2019-06-15 13:03:37 -03:00
|
|
|
print("Error, timeout")
|
2021-07-16 11:46:44 -03:00
|
|
|
print(test_name + f" {COLOR_RED}FAILED{COLOR_RESET}")
|
2019-06-15 13:03:37 -03:00
|
|
|
success = False
|
|
|
|
break
|
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
# newline every 10 seconds if still running
|
2021-12-11 14:41:06 -04:00
|
|
|
if (len(serial_line) <= 0) and (time.monotonic() - timeout_newline > 10):
|
2020-11-13 16:17:31 -04:00
|
|
|
ser.write("\n".encode("ascii"))
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_newline = time.monotonic()
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2019-06-15 12:09:21 -03:00
|
|
|
ser.close()
|
|
|
|
|
|
|
|
return success
|
2018-08-04 17:48:47 -03:00
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
class TestHardwareMethods(unittest.TestCase):
|
2020-02-25 04:58:02 -04:00
|
|
|
TEST_DEVICE = 0
|
|
|
|
TEST_BAUDRATE = 0
|
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
def test_atomic_bitset(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "atomic_bitset"))
|
|
|
|
|
2020-02-25 04:58:02 -04:00
|
|
|
def test_bezier(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "bezier"))
|
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
def test_bitset(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "bitset"))
|
|
|
|
|
2020-02-25 04:58:02 -04:00
|
|
|
def test_bson(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "bson"))
|
|
|
|
|
2021-10-04 15:21:33 -03:00
|
|
|
def test_dataman(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "dataman"))
|
2021-07-18 12:09:49 -03:00
|
|
|
|
|
|
|
# def test_file(self):
|
|
|
|
# self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "file"))
|
|
|
|
|
|
|
|
def test_file2(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "file2"))
|
|
|
|
|
|
|
|
def test_float(self):
|
2020-02-25 04:58:02 -04:00
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "float"))
|
|
|
|
|
|
|
|
def test_hrt(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "hrt"))
|
|
|
|
|
2021-07-18 12:09:49 -03:00
|
|
|
def test_int(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "int"))
|
|
|
|
|
|
|
|
def test_i2c_spi_cli(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "i2c_spi_cli"))
|
|
|
|
|
2020-02-25 04:58:02 -04:00
|
|
|
def test_IntrusiveQueue(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "IntrusiveQueue"))
|
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
def test_IntrusiveSortedList(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "IntrusiveSortedList"))
|
|
|
|
|
2020-02-25 04:58:02 -04:00
|
|
|
def test_List(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "List"))
|
|
|
|
|
|
|
|
def test_mathlib(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "mathlib"))
|
|
|
|
|
|
|
|
def test_matrix(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "matrix"))
|
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
# def test_mixer(self):
|
|
|
|
# self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "mixer"))
|
2020-02-25 04:58:02 -04:00
|
|
|
|
|
|
|
def test_param(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "param"))
|
|
|
|
|
|
|
|
def test_parameters(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "parameters"))
|
|
|
|
|
|
|
|
def test_perf(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "perf"))
|
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
# def test_rc(self):
|
|
|
|
# self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "rc"))
|
|
|
|
|
|
|
|
def test_search_min(self):
|
2020-02-25 04:58:02 -04:00
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "search_min"))
|
|
|
|
|
|
|
|
def test_sleep(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "sleep"))
|
|
|
|
|
|
|
|
def test_time(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "time"))
|
|
|
|
|
|
|
|
def test_versioning(self):
|
|
|
|
self.assertTrue(do_test(self.TEST_DEVICE, self.TEST_BAUDRATE, "versioning"))
|
|
|
|
|
2018-08-04 17:48:47 -03:00
|
|
|
def main():
|
|
|
|
parser = ArgumentParser(description=__doc__)
|
2020-11-10 23:45:50 -04:00
|
|
|
parser.add_argument('--device', "-d", nargs='?', default=None, help='', required=True)
|
2018-08-04 17:48:47 -03:00
|
|
|
parser.add_argument("--baudrate", "-b", dest="baudrate", type=int, help="Mavlink port baud rate (default=57600)", default=57600)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-11-10 17:41:13 -04:00
|
|
|
TestHardwareMethods.TEST_DEVICE = args.device
|
|
|
|
TestHardwareMethods.TEST_BAUDRATE = args.baudrate
|
2019-06-15 12:09:21 -03:00
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
unittest.main(__name__, failfast=True, verbosity=0, argv=['main'])
|
2018-08-04 17:48:47 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-06-15 12:09:21 -03:00
|
|
|
main()
|