Skip to content

Commit

Permalink
reworked as per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kadiredd committed Apr 24, 2024
1 parent 47315ed commit ab9eaf3
Showing 1 changed file with 40 additions and 50 deletions.
90 changes: 40 additions & 50 deletions realsense2_camera/test/live_camera/rosci.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,42 @@
# limitations under the License.

import sys, os, subprocess, re, platform, getopt, time
sys.path.append( os.path.join( os.environ['WORKSPACE'], 'lrs/unit-tests/py' ))
from rspy import log, file, repo, libci
start_time = time.time()
current_dir = os.path.dirname( os.path.abspath( __file__ ) )
print(f'{current_dir}')
root = os.path.dirname( os.path.dirname( os.path.dirname( os.path.dirname( os.path.dirname( os.path.abspath( __file__ ))))))
dir_live_tests = os.path.join( os.environ['WORKSPACE'], 'ros2/realsense2_camera/test/live_camera' )
running_on_ci = False
if 'WORKSPACE' in os.environ:
#Path for ROS-CI on Jenkins
ws_rosci = os.environ['WORKSPACE']
sys.path.append( os.path.join( ws_rosci, 'lrs/unit-tests/py' ))
running_on_ci = True
else:
#For running this script locally
#Extract the root where both realsense-ros and librealsense are cloned
ws_local = '/'.join(os.path.abspath( __file__ ).split( os.path.sep )[0:-5])
#expected to have 'librealsense' repo in parallel to 'realsense-ros'
assert os.path.exists( os.path.join(ws_local, 'librealsense')), f" 'librealsense' doesn't exist at {ws_local} "
sys.path.append( os.path.join( ws_local, 'librealsense/unit-tests/py' ))

dir_live_tests = os.path.dirname(__file__)
from rspy import log, libci
hub_reset = False
logdir = None
handle = None
test_ran = False

def usage():
ourname = os.path.basename( sys.argv[0] )
print( 'Syntax: ' + ourname + ' [options] [dir]' )
print( ' dir: location of executable tests to run' )
print( 'Syntax: ' + ourname + ' [options] ' )
print( 'Options:' )
print( ' --debug Turn on debugging information (does not include LibRS debug logs; see --rslog)' )
print( ' -v, --verbose Errors will dump the log to stdout' )
print( ' -q, --quiet Suppress output; rely on exit status (0=no failures)' )
print( ' -s, --stdout Do not redirect stdout to logs' )
print( ' -r, --regex Run all tests whose name matches the following regular expression' )
print( ' --list-tests Print out all available tests. This option will not run any tests' )
print( ' --repeat <#> Repeat each test <#> times' )
print( ' --config <> Ignore test configurations; use the one provided' )
print( ' --device <> Run only on the specified devices; ignore any test that does not match (implies --live)' )
print( ' --no-reset Do not try to reset any devices, with or without a hub' )
print( ' --hub-reset If a hub is available, reset the hub itself' )
print( ' --skip-disconnected Skip live test if required device is disconnected (only applies w/o a hub)' )
print( 'Examples:' )
print( 'Running: python3.10 rosci.py -s' )
print( ' Runs all tests, but direct their output to the console rather than log files' )
print( 'Running: python3.10 rosci.py --list-tests' )
print( " Will find all tests and print" )

print( ' -h, --help Usage help' )
print( ' --debug Turn on debugging information (does not include LibRS debug logs; see --rslog)' )

sys.exit( 2 )

def command(dev_name):
cmd = ['pytest-3']
cmd += ['-s']
cmd += ['-m', ''.join(dev_name)]
cmd += ['-k', 'test_camera_imu_tests']
#cmd += ['-m', 'd415']
cmd += [''.join(dir_live_tests)]
cmd += ['--debug']
return cmd
Expand Down Expand Up @@ -90,57 +84,53 @@ def run_test(cmd, dev_name, stdout=None, append =False):
if handle:
handle.close()

device_set = None
try:
opts, args = getopt.getopt( sys.argv[1:], 'hvqr:st:',
longopts=['help', 'verbose', 'debug', 'quiet', 'regex=', 'stdout', 'tag=', 'list-tags',
'list-tests', 'no-exceptions', 'context=', 'repeat=', 'config=', 'no-reset', 'hub-reset',
'rslog', 'skip-disconnected', 'live', 'not-live', 'device='] )
opts, args = getopt.getopt( sys.argv[1:], 'h', longopts=['help', 'debug' ] )
except getopt.GetoptError as err:
log.e( err ) # something like "option -a not recognized"
usage()

for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
elif opt == '--device':
device_set = arg.split()
for dev in device_set:
print(dev)
#Get into action

#Find device, Run test
try:
logdir = os.path.join( os.path.dirname( os.path.dirname( os.path.dirname( os.path.abspath( __file__ )))), 'log')
#logs are stored @ ./realsense2_camera/test
logdir = os.path.join( '/'.join(os.path.abspath( __file__ ).split( os.path.sep )[0:-2]), 'logs')
os.makedirs( logdir, exist_ok=True )
print(logdir)



#Import '_device_by_sn' from devices.py module of librealsense repo
from rspy import devices
if not devices.hub:
assert False, 'No hub available'
else:
devices.hub.connect()

devices.query( hub_reset = hub_reset )
#devices.map_unknown_ports()
global _device_by_sn

if not devices._device_by_sn:
assert False, 'No Camera device detected!'
else:
#Loop in for all devices and run tests
for device in devices._device_by_sn.values():
print(f' Device found: {device.name} ')
for device in devices._device_by_sn.values():
#Loop in for all devices and run tests
for device in devices._device_by_sn.values():
log.i(f'Device found: {device.name} ')
if device.name == 'D455':
print('Running test on device:', device.name)
log.i('Running test on device:', device.name)
cmd = command(str(device.name).lower())
run_test(cmd, device.name, stdout=logdir, append =False)
elif device.name != 'D455':
print('Skipping test on device:', device.name)
test_ran = True
else:
log.i('Skipping test on device:', device.name)

finally:
devices.hub.disconnect()
log.i("Log path- \"Build Artifacts\":/ros2/realsense_camera/log ")
if running_on_ci:
log.i("Log path- \"Build Artifacts\":/ros2/realsense_camera/test/logs ")
else:
log.i("log path:", logdir)
assert test_ran, "No test cases ran"
run_time = time.time() - start_time
log.d( "server took", run_time, "seconds" )

Expand Down

0 comments on commit ab9eaf3

Please sign in to comment.