From 7842d07b123270bf7a6078302c42f17f53dcd55a Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 22 Oct 2019 02:25:46 +1100 Subject: [PATCH] autotest: add test for scripting --- Tools/autotest/apmrover2.py | 92 +++++++++++++++++++++++++++++++++++++ Tools/autotest/common.py | 14 ++++++ 2 files changed, 106 insertions(+) diff --git a/Tools/autotest/apmrover2.py b/Tools/autotest/apmrover2.py index 768954f218..aec4f1ab1b 100644 --- a/Tools/autotest/apmrover2.py +++ b/Tools/autotest/apmrover2.py @@ -5,6 +5,7 @@ from __future__ import print_function import copy import os +import shutil import sys import time @@ -4621,6 +4622,93 @@ Brakes have negligible effect (with=%0.2fm without=%0.2fm delta=%0.2fm) target_component=target_component, ) + def script_source_path(self, scriptname): + return os.path.join(self.rootdir(), "libraries", "AP_Scripting", "examples", scriptname) + + def installed_script_path(self, scriptname): + return os.path.join(self.rootdir(), "scripts", scriptname) + + def install_example_script(self, scriptname): + source = self.script_source_path(scriptname) + dest = self.installed_script_path(scriptname) + destdir = os.path.dirname(dest) + if not os.path.exists(destdir): + os.mkdir(destdir) + shutil.copy(source, dest) + + def remove_example_script(self, scriptname): + dest = self.installed_script_path(scriptname) + try: + os.unlink(dest) + except IOError: + pass + except OSError: + pass + + def test_scripting_simple_loop(self): + ex = None + example_script = "simple_loop.lua" + messages = [] + def my_message_hook(mav, m): + if m.get_type() != 'STATUSTEXT': + return + messages.append(m) + self.install_message_hook(my_message_hook) + try: + self.set_parameter("SCR_ENABLE", 1) + self.install_example_script(example_script) + self.reboot_sitl() + except Exception as e: + ex = e + self.remove_example_script(example_script) + self.reboot_sitl() + + self.remove_message_hook(my_message_hook) + + if ex is not None: + raise ex + + # check all messages to see if we got our message + count = 0 + for m in messages: + if "hello, world" in m.text: + count += 1 + self.progress("Got %u hellos" % count) + if count < 3: + raise NotAchievedException("Expected at least three hellos") + + def test_scripting_hello_world(self): + ex = None + example_script = "hello_world.lua" + messages = [] + def my_message_hook(mav, m): + if m.get_type() != 'STATUSTEXT': + return + messages.append(m) + self.install_message_hook(my_message_hook) + try: + self.set_parameter("SCR_ENABLE", 1) + self.install_example_script(example_script) + self.reboot_sitl() + except Exception as e: + ex = e + self.remove_example_script(example_script) + self.reboot_sitl() + + self.remove_message_hook(my_message_hook) + + if ex is not None: + raise ex + + # check all messages to see if we got our message + for m in messages: + if "hello, world" in m.text: + return # success! + raise NotAchievedException("Did not get expected text") + + def test_scripting(self): + self.test_scripting_hello_world() + self.test_scripting_simple_loop() def tests(self): '''return list of all tests''' @@ -4776,6 +4864,10 @@ Brakes have negligible effect (with=%0.2fm without=%0.2fm delta=%0.2fm) "PolyFence object avoidance tests - easier bendy ruler test", self.test_poly_fence_object_avoidance_bendy_ruler_easier), + ("Scripting", + "Scripting test", + self.test_scripting), + ("DownLoadLogs", "Download logs", lambda: self.log_download( self.buildlogs_path("APMrover2-log.bin"), diff --git a/Tools/autotest/common.py b/Tools/autotest/common.py index 49c0b8d017..00dcba39a2 100644 --- a/Tools/autotest/common.py +++ b/Tools/autotest/common.py @@ -647,6 +647,20 @@ class AutoTest(ABC): wploader.load(filename) return wploader.count() + def install_message_hook(self, hook): + self.mav.message_hooks.append(hook) + + def remove_message_hook(self, hook): + oldlen = len(self.mav.message_hooks) + self.mav.message_hooks = list(filter(lambda x : x != hook, + self.mav.message_hooks)) + if len(self.mav.message_hooks) == oldlen: + raise NotAchievedException("Failed to remove hook") + + def rootdir(self): + this_dir = os.path.dirname(__file__) + return os.path.realpath(os.path.join(this_dir, "../..")) + def mission_directory(self): return testdir