mirror of https://github.com/ArduPilot/ardupilot
autotest: add test for scripting
This commit is contained in:
parent
c2b0bab762
commit
9128913ab8
|
@ -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"),
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue