Repository navigation
Adds SITL test infrastructure. #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ pymavlink>=1.1.50 | |
| sphinx-3dr-theme>=0.0.6 | ||
| MAVProxy>=1.4.13 | ||
| nose>=1.3.7 | ||
| psutil>=3.0.0 | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| #!/usr/bin/env python -u | ||
|
|
||
| from __future__ import print_function | ||
| import time | ||
| import os | ||
| from subprocess import Popen, PIPE | ||
| import atexit | ||
| import sys | ||
| import tempfile | ||
| from functools import wraps | ||
| import errno | ||
| import os | ||
| import signal | ||
| import time | ||
| import psutil | ||
|
|
||
| def wait_timeout(proc, seconds): | ||
| """Wait for a process to finish, or raise exception after timeout""" | ||
| start = time.time() | ||
| end = start + seconds | ||
| interval = min(seconds / 1000.0, .25) | ||
|
|
||
| while True: | ||
| result = proc.poll() | ||
| if result is not None: | ||
| return result | ||
| if time.time() >= end: | ||
| raise RuntimeError("Process timed out") | ||
| time.sleep(interval) | ||
|
|
||
| def kill(proc_pid): | ||
| process = psutil.Process(proc_pid) | ||
| for proc in process.children(recursive=True): | ||
| try: | ||
| proc.kill() | ||
| except psutil.NoSuchProcess: | ||
| pass | ||
| try: | ||
| process.kill() | ||
| except psutil.NoSuchProcess: | ||
| pass | ||
|
|
||
| if sys.platform == "win32": | ||
| import msvcrt | ||
| import _subprocess | ||
| else: | ||
| import fcntl | ||
|
|
||
| bg = [] | ||
| def cleanup_processes(): | ||
| for p in bg: | ||
| kill(p.pid) | ||
| atexit.register(cleanup_processes) | ||
|
|
||
| testpath = os.path.dirname(__file__) | ||
|
|
||
|
|
||
| def wrap_fd(pipeout): | ||
| # Prepare to pass to child process | ||
| if sys.platform == "win32": | ||
| curproc = _subprocess.GetCurrentProcess() | ||
| pipeouth = msvcrt.get_osfhandle(pipeout) | ||
| pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1, _subprocess.DUPLICATE_SAME_ACCESS) | ||
| return (str(int(pipeoutih)), pipeoutih) | ||
| else: | ||
| return (str(pipeout), None) | ||
|
|
||
| def lets_run_a_test(name): | ||
| sitl = Popen(['dronekit-sitl', 'copter-3.3-rc5', '-I0', '-S', '--model', 'quad', '--home=-35.363261,149.165230,584,353'], stdout=PIPE, stderr=PIPE) | ||
| bg.append(sitl) | ||
|
|
||
| while sitl.poll() == None: | ||
| line = sitl.stdout.readline() | ||
| if 'Waiting for connection' in line: | ||
| break | ||
| if sitl.poll() != None and sitl.returncode != 0: | ||
| print('[runner] ...aborting with SITL error code ' + str(sitl.returncode)) | ||
| sys.stdout.flush() | ||
| sys.stderr.flush() | ||
| sys.exit(sitl.returncode) | ||
|
|
||
| newenv = os.environ.copy() | ||
| newenv['PYTHONUNBUFFERED'] = '1' | ||
|
|
||
| if sys.platform == 'win32': | ||
| out_fd = 1 | ||
| err_fd = 2 | ||
| else: | ||
| out_fd = os.dup(1) | ||
| err_fd = os.dup(2) | ||
|
|
||
| (out_fd, out_h) = wrap_fd(out_fd) | ||
| (err_fd, err_h) = wrap_fd(err_fd) | ||
|
|
||
| newenv['TEST_WRITE_OUT'] = out_fd | ||
| newenv['TEST_WRITE_ERR'] = err_fd | ||
| newenv['TEST_NAME'] = name | ||
|
|
||
| print('[runner] ' + name, file=sys.stderr) | ||
| sys.stdout.flush() | ||
| sys.stderr.flush() | ||
|
|
||
| timeout = 300 | ||
| try: | ||
| p = Popen([sys.executable, '-m', 'MAVProxy.mavproxy', '--logfile=' + tempfile.mkstemp()[1], '--master=tcp:127.0.0.1:5760', '--cmd=module load droneapi.module.api; ; api start testlib.py'], cwd=testpath, env=newenv, stdout=PIPE, stderr=PIPE) | ||
| bg.append(p) | ||
| wait_timeout(p, timeout) | ||
| except TimeoutExpired: | ||
| kill(p.pid) | ||
| p.returncode = 143 | ||
| print('Error: Timeout after ' + str(timeout) + ' seconds.') | ||
| bg.remove(p) | ||
|
|
||
| if sys.platform == 'win32': | ||
| out_h.Close() | ||
| err_h.Close() | ||
|
|
||
| kill(sitl.pid) | ||
| bg.remove(sitl) | ||
|
|
||
| if p.returncode != 0: | ||
| print('[runner] ...aborting with dronekit error code ' + str(p.returncode)) | ||
| sys.stdout.flush() | ||
| sys.stderr.flush() | ||
| sys.exit(p.returncode) | ||
|
|
||
| print('[runner] ...success.') | ||
| time.sleep(5) | ||
|
|
||
| for i in os.listdir(testpath): | ||
| if i.startswith('test_') and i.endswith('.py'): | ||
| lets_run_a_test(i[:-3]) | ||
|
|
||
| print('[runner] finished.') | ||
| sys.stdout.flush() | ||
| sys.stdout.close() | ||
| sys.stderr.flush() | ||
| sys.stderr.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from droneapi.lib import VehicleMode | ||
| from pymavlink import mavutil | ||
| import time | ||
| import sys | ||
| import os | ||
|
|
||
| def current_milli_time(): | ||
| return int(round(time.time() * 1000)) | ||
|
|
||
| def test_timeout(local_connect): | ||
| v = local_connect().get_vehicles()[0] | ||
|
|
||
| print "Read vehicle param 'THR_MIN': %s" % v.parameters['THR_MIN'] | ||
|
|
||
| print "Write vehicle param 'THR_MIN' : 50" | ||
| start = current_milli_time() | ||
| v.parameters['THR_MIN']=50 | ||
| end = current_milli_time() | ||
|
|
||
| print "Read new value of param 'THR_MIN': %s" % v.parameters['THR_MIN'] | ||
|
|
||
| # TODO | ||
| # assert end - start < 1000, 'time to set parameter was %s, over 1s' % (end - start,) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the relevant Asserts here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #12 (my last comment) |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from droneapi.lib import VehicleMode | ||
| from pymavlink import mavutil | ||
| import time | ||
| import sys | ||
| import os | ||
| from testlib import assert_equals | ||
|
|
||
| def test_parameters(local_connect): | ||
| v = local_connect().get_vehicles()[0] | ||
|
|
||
| # Simple parameter checks | ||
| assert_equals(type(v.parameters['THR_MIN']), float) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """ | ||
| We can't use nose right now, so we recreate some of | ||
| nose's features in a compatible way, but run it in-process. | ||
| """ | ||
|
|
||
| import inspect | ||
| import traceback | ||
| import os | ||
| import sys | ||
| import importlib | ||
|
|
||
| def assert_equals(a, b, msg=None): | ||
| if not a == b: | ||
| raise AssertionError(msg or "%r != %r" % (a, b)) | ||
|
|
||
| def override_io(): | ||
| old_out = sys.stdout | ||
| old_err = sys.stderr | ||
|
|
||
| fd_out = int(os.environ['TEST_WRITE_OUT']) | ||
| fd_err = int(os.environ['TEST_WRITE_ERR']) | ||
| if sys.platform == 'win32': | ||
| import msvcrt | ||
| fd_out = msvcrt.open_osfhandle(fd_out, 0) | ||
| fd_err = msvcrt.open_osfhandle(fd_err, 0) | ||
| api_out = os.fdopen(fd_out, 'w') | ||
| api_err = os.fdopen(fd_err, 'w') | ||
|
|
||
| class Intercept: | ||
| def __init__(self, api, old): | ||
| self.api = api | ||
| self.old = old | ||
|
|
||
| def write(self, data): | ||
| import threading | ||
| if threading.current_thread().name.startswith('APIThread'): | ||
| self.api.write(data) | ||
| else: | ||
| self.old.write(data) | ||
|
|
||
| def flush(self): | ||
| self.api.flush() | ||
| self.old.flush() | ||
|
|
||
| sys.stdout = Intercept(api_out, old_out) | ||
| sys.stderr = Intercept(api_err, old_err) | ||
|
|
||
| def unoverride_io(): | ||
| sys.stdout.api.close() | ||
| sys.stdout = sys.stdout.old | ||
| sys.stderr.api.close() | ||
| sys.stderr = sys.stderr.old | ||
|
|
||
| def run_tests(lib): | ||
| # Work around weird test reloading issues. | ||
| try: | ||
| sys.local_connect = local_connect | ||
| except: | ||
| pass | ||
|
|
||
| try: | ||
| sys.path.append(os.path.dirname('__file__')) | ||
| mod = importlib.import_module(lib) | ||
|
|
||
| tests = [] | ||
| for (name, m) in inspect.getmembers(mod, inspect.isfunction): | ||
| if name.startswith('test_'): | ||
| tests.append(m) | ||
|
|
||
| if len(tests) == 0: | ||
| return | ||
|
|
||
| override_io() | ||
|
|
||
| for test in tests: | ||
| test(sys.local_connect) | ||
| sys.stdout.write('.') | ||
| except: | ||
| print '\nERROR' | ||
| traceback.print_exc() | ||
|
|
||
| sys.stdout.flush() | ||
| sys.stderr.flush() | ||
| unoverride_io() | ||
| os._exit(1) | ||
|
|
||
| print '\nOK' | ||
|
|
||
| sys.stdout.flush() | ||
| sys.stderr.flush() | ||
| unoverride_io() | ||
| os._exit(0) | ||
|
|
||
| run_tests(os.environ['TEST_NAME']) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe handle this on a config?, not clear to me why we are testing to a release candidate instead of a stable version, handling this on a config would help bumping the version once we are tied to an Ardupilot version, also people are probably going to ask about testing for Plane and Rover
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.3-rc5 was picked because 3.2 doesn't have the relevant options (which ArduPlane 3.3 stable already does). It's possible we could toggle the options it doesn't support though.
Opened this issue as #202