Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ env:
install:
- 'a() { set -e; }'
- "z() { E=$?; test $E -eq 0 && return 0; printf \"\\n\\033[1;31mThe command failed with exit code $?.\\033[0m\"; set -e; return $E; }"
- (a; sudo pip2 install nose );z
- (a; sudo pip2 install nose psutil );z
- '(a; sudo pip2 install git+https://github.com/3drobotics/dronekit-sitl-runner.git );z'
script:
- (a; sudo python setup.py install );z
- |-
(a; cd tests
nosetests
);z
- (a; nosetests tests/web );z
- (a; cd tests; python -m sitl );z
git:
depth: 10
language: objective-c
notifications:
email: false
matrix:
fast_finish: true
branches:
only:
- master
6 changes: 4 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ environment:
init: []
install:
- cmd: 'setlocal & python -m pip install pip -U & endlocal'
- cmd: "setlocal & SET PATH=%PYTHON%;c:\\Python27\\Scripts;%PATH% & pip2 install nose numpy & endlocal"
- cmd: "setlocal & SET PATH=%PYTHON%;c:\\Python27\\Scripts;%PATH% & pip2 install nose numpy psutil & endlocal"
- cmd: "setlocal & SET PATH=%PYTHON%;c:\\Python27\\Scripts;%PATH% & pip2 install git+https://github.com/3drobotics/dronekit-sitl-runner.git & endlocal"
build_script:
- cmd: 'setlocal & python setup.py install & endlocal'
- cmd: "setlocal & SET PATH=%PYTHON%;c:\\Python27\\Scripts;%PATH% & cd tests & nosetests & endlocal"
- cmd: "setlocal & SET PATH=%PYTHON%;c:\\Python27\\Scripts;%PATH% & nosetests tests\\web & endlocal"
- cmd: "setlocal & SET PATH=%PYTHON%;c:\\Python27\\Scripts;%PATH% & cd tests & python -m sitl & endlocal"
clone_depth: 10
cache:
- '%CYG_CACHE%'
Expand Down
10 changes: 7 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ machine:
environment: {}
dependencies:
override:
- sudo python setup.py install:
- python setup.py install:
environment: {}
- "cd tests\nnosetests\n":
- nosetests tests/web:
environment: {}
- cd tests; python -m sitl:
environment: {}
pre:
- sudo pip2 install nose:
- pip2 install nose psutil:
environment: {}
- 'pip2 install git+https://github.com/3drobotics/dronekit-sitl-runner.git':
environment: {}
test:
override:
Expand Down
24 changes: 13 additions & 11 deletions incisor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ builds:
stages:
setup:
- python -m pip install pip -U
- SET PATH=%PYTHON%;c:\Python27\Scripts;%PATH% & pip2 install nose numpy
- SET PATH=%PYTHON%;c:\Python27\Scripts;%PATH% & pip2 install nose numpy psutil
- SET PATH=%PYTHON%;c:\Python27\Scripts;%PATH% & pip2 install git+https://github.com/3drobotics/dronekit-sitl-runner.git
build:
- python setup.py install
- SET PATH=%PYTHON%;c:\Python27\Scripts;%PATH% & cd tests & nosetests
- SET PATH=%PYTHON%;c:\Python27\Scripts;%PATH% & nosetests tests\web
- SET PATH=%PYTHON%;c:\Python27\Scripts;%PATH% & cd tests & python -m sitl

- os: linux
stages:
setup:
- sudo pip2 install nose
- pip2 install nose psutil
- pip2 install git+https://github.com/3drobotics/dronekit-sitl-runner.git
build:
- sudo python setup.py install
- |
cd tests
nosetests
- python setup.py install
- nosetests tests/web
- cd tests; python -m sitl

- os: osx
stages:
setup:
- sudo pip2 install nose
- sudo pip2 install nose psutil
- sudo pip2 install git+https://github.com/3drobotics/dronekit-sitl-runner.git
build:
- sudo python setup.py install
- |
cd tests
nosetests
- nosetests tests/web
- cd tests; python -m sitl
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 added tests/sitl/__init__.py
Empty file.
138 changes: 138 additions & 0 deletions tests/sitl/__main__.py
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)

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

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

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()
23 changes: 23 additions & 0 deletions tests/sitl/test_12.py
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,)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the relevant Asserts here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #12 (my last comment)

12 changes: 12 additions & 0 deletions tests/sitl/test_simple.py
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)
94 changes: 94 additions & 0 deletions tests/sitl/testlib.py
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.