Skip to content

Commit

Permalink
Clean up rostest (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvukov authored Dec 24, 2024
1 parent 6c024d6 commit eba1148
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 70 deletions.
1 change: 1 addition & 0 deletions ros/test.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from third_party.ros.rostest import rostest_main
test_outputs_dir = os.environ['TEST_UNDECLARED_OUTPUTS_DIR']
os.environ['ROS_LOG_DIR'] = test_outputs_dir
os.environ['ROS_TEST_RESULTS_DIR'] = test_outputs_dir
os.environ['ROS_HOSTNAME'] = 'localhost'

LAUNCH_FILE = {launch_file}

Expand Down
22 changes: 1 addition & 21 deletions third_party/ros/rostest/rostest_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,8 @@ def rostestmain():
dest='results_filename',
default=None,
help='results_filename')
parser.add_option(
'-r',
'--reuse-master',
action='store_true',
help=
'Connect to an existing ROS master instead of spawning a new ROS master on a custom port'
)
parser.add_option(
'-c',
'--clear',
action='store_true',
help=
'Clear all parameters when connecting to an existing ROS master (only works with --reuse-master)'
)
(options, args) = parser.parse_args()

if options.clear and not options.reuse_master:
print('The --clear option is only valid with --reuse-master',
file=sys.stderr)
sys.exit(1)

try:
args = roslaunch.rlutil.resolve_launch_arguments(args)
except roslaunch.core.RLException as e:
Expand Down Expand Up @@ -149,8 +130,7 @@ def rostestmain():
results_file)

try:
testCase = runner.createUnitTest(test_file, options.reuse_master,
options.clear, results_dir)
testCase = runner.createUnitTest(test_file, results_dir)
suite = unittest.TestLoader().loadTestsFromTestCase(testCase)

if options.text_mode:
Expand Down
45 changes: 5 additions & 40 deletions third_party/ros/rostest/rostest_parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
# Revision $Id$
# pylint: disable=raise-missing-from,consider-using-f-string,invalid-name
# pylint: disable=line-too-long
import rosgraph
from rosmaster.master import Master

from third_party.ros.roslaunch import core
Expand All @@ -42,50 +41,17 @@

class ROSTestLaunchParent(parent.ROSLaunchParent):

def __init__(self,
config,
roslaunch_files,
port=0,
reuse_master=False,
clear=False):
def __init__(self, config, roslaunch_files, port=0):
if config is None:
raise Exception('config not initialized')
# we generate a run_id for each test
if reuse_master:
param_server = rosgraph.Master('/roslaunch')
try:
run_id = param_server.getParam('/run_id')
except Exception as e:
# The user asked us to connect to an existing ROS master, and
# we can't. Throw an exception and die
raise Exception('Could not connect to existing ROS master. ' +
'Original exception was: %s' % str(e))
except:
# oh boy; we got something that wasn't an exception.
# Throw an exception and die
raise Exception('Could not connect to existing ROS master.')

if clear:
params = param_server.getParamNames()
# whitelist of parameters to keep
whitelist = ['/run_id', '/rosversion', '/rosdistro']
for i in reversed(range(len(params))):
param = params[i]
if param in whitelist:
del params[i]
elif param.startswith('/roslaunch/'):
del params[i]
for param in params:
param_server.deleteParam(param)
else:
run_id = core.generate_run_id()
run_id = core.generate_run_id()
super(ROSTestLaunchParent, self).__init__(run_id,
roslaunch_files,
is_core=False,
is_rostest=True)
self.config = config
self.port = port
self.reuse_master = reuse_master
self.master = None

def _load_config(self):
Expand All @@ -97,10 +63,9 @@ def setUp(self):
initializes self.config and xmlrpc infrastructure
"""
self._start_infrastructure()
if not self.reuse_master:
self.master = Master(port=self.port)
self.master.start()
self.config.master.uri = self.master.uri
self.master = Master(port=self.port)
self.master.start()
self.config.master.uri = self.master.uri
self._init_runner()

def tearDown(self):
Expand Down
11 changes: 2 additions & 9 deletions third_party/ros/rostest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ def setUp(self):
# new test_parent for each run. we are a bit inefficient as it would be possible to
# reuse the roslaunch base infrastructure for each test, but the roslaunch code
# is not abstracted well enough yet
self.test_parent = ROSTestLaunchParent(self.config, [self.test_file],
reuse_master=self.reuse_master,
clear=self.clear)
self.test_parent = ROSTestLaunchParent(self.config, [self.test_file])

printlog('setup[%s] run_id[%s] starting', self.test_file,
self.test_parent.run_id)
Expand All @@ -239,10 +237,7 @@ def tearDown(self):
printlog('rostest teardown %s complete', self.test_file)


def createUnitTest(test_file,
reuse_master=False,
clear=False,
results_base_dir=None):
def createUnitTest(test_file, results_base_dir=None):
"""
Unit test factory. Constructs a unittest class based on the roslaunch
Expand All @@ -259,8 +254,6 @@ def createUnitTest(test_file,
'config': config,
'test_parent': None,
'test_file': test_file,
'reuse_master': reuse_master,
'clear': clear
}

# add in the tests
Expand Down

0 comments on commit eba1148

Please sign in to comment.