$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2009, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id$ 00035 00036 PKG = 'test_roslaunch' 00037 NAME = 'test_roslaunch_node_args' 00038 00039 import roslib; roslib.load_manifest(PKG) 00040 00041 import os 00042 import sys 00043 import unittest 00044 00045 ## Test roslaunch.node_args 00046 class TestNodeArgs(unittest.TestCase): 00047 00048 def test_create_local_process_env(self): 00049 00050 # this is almost entirely identical to the setup_env test in test_core, with some additional 00051 # higher level checks 00052 00053 from roslaunch.core import Node, Machine 00054 from roslaunch.node_args import create_local_process_env 00055 ros_root = '/ros/root1' 00056 rpp = '/rpp1' 00057 master_uri = 'http://masteruri:1234' 00058 00059 n = Node('nodepkg','nodetype') 00060 m = Machine('name1', ros_root, rpp, '1.2.3.4') 00061 d = create_local_process_env(n, m, master_uri) 00062 self.assertEquals(d['ROS_MASTER_URI'], master_uri) 00063 self.assertEquals(d['ROS_ROOT'], ros_root) 00064 self.assertEquals(d['PYTHONPATH'], os.path.join(ros_root, 'core', 'roslib', 'src')) 00065 self.assertEquals(d['ROS_PACKAGE_PATH'], rpp) 00066 for k in ['ROS_IP', 'ROS_NAMESPACE']: 00067 if k in d: 00068 self.fail('%s should not be set: %s'%(k,d[k])) 00069 00070 # test that it inherits local environment 00071 env = os.environ.copy() 00072 env['PATH'] = '/some/path' 00073 env['FOO'] = 'foo' 00074 d = create_local_process_env(n, m, master_uri, env=env) 00075 self.assertEquals(d['FOO'], 'foo') 00076 00077 # test that it defaults to os.environ 00078 os.environ['FOO'] = 'bar' 00079 d = create_local_process_env(n, m, master_uri) 00080 self.assertEquals(d['FOO'], 'bar') 00081 00082 # test that our ROS_ROOT and PYTHONPATH override env 00083 env = os.environ.copy() 00084 env['ROS_ROOT'] = '/not/ros/root/' 00085 env['PYTHONPATH'] = '/some/path' 00086 d = create_local_process_env(n, m, master_uri, env=env) 00087 self.assertEquals(d['ROS_ROOT'], ros_root) 00088 self.assertEquals(d['PYTHONPATH'], os.path.join(ros_root, 'core', 'roslib', 'src')) 00089 00090 # - make sure it didn't pollute original env 00091 self.assertEquals(env['ROS_ROOT'], '/not/ros/root/') 00092 self.assertEquals(env['PYTHONPATH'], '/some/path') 00093 00094 # don't set ROS_ROOT and ROS_PACKAGE_PATH. ROS_ROOT should default, ROS_PACKAGE_PATH does not 00095 m = Machine('name1', '', '', '1.2.3.4') 00096 d = create_local_process_env(n, m, master_uri) 00097 val = os.environ['ROS_ROOT'] 00098 self.assertEquals(d['ROS_ROOT'], val) 00099 self.assertEquals(d['PYTHONPATH'], os.path.join(val, 'core', 'roslib', 'src')) 00100 self.failIf('ROS_PACKAGE_PATH' in d, 'ROS_PACKAGE_PATH should not be set: %s'%d) 00101 00102 # test ROS_IP 00103 m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7") 00104 n = Node('nodepkg','nodetype', namespace="/ns1") 00105 d = create_local_process_env(n, m, master_uri) 00106 self.assertEquals(d['ROS_NAMESPACE'], "/ns1") 00107 # test stripping 00108 n = Node('nodepkg','nodetype', namespace="/ns2/") 00109 d = create_local_process_env(n, m, master_uri) 00110 self.assertEquals(d['ROS_NAMESPACE'], "/ns2") 00111 00112 # test ROS_NAMESPACE 00113 m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7") 00114 d = create_local_process_env(n, m, master_uri) 00115 self.assertEquals(d['ROS_IP'], "4.5.6.7") 00116 00117 # test node.env_args 00118 n = Node('nodepkg','nodetype', env_args=[('NENV1', 'val1'), ('NENV2', 'val2'), ('ROS_ROOT', '/new/root')]) 00119 d = create_local_process_env(n, m, master_uri) 00120 self.assertEquals(d['ROS_ROOT'], "/new/root") 00121 self.assertEquals(d['NENV1'], "val1") 00122 self.assertEquals(d['NENV2'], "val2") 00123 00124 # test machine.env_args 00125 m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7", env_args=[('MENV1', 'val1'), ('MENV2', 'val2'), ('ROS_ROOT', '/new/root2')]) 00126 n = Node('nodepkg','nodetype') 00127 d = create_local_process_env(n, m, master_uri) 00128 self.assertEquals(d['ROS_ROOT'], "/new/root2") 00129 self.assertEquals(d['MENV1'], "val1") 00130 self.assertEquals(d['MENV2'], "val2") 00131 00132 # test node.env_args precedence 00133 m = Machine('name1', ros_root, rpp, '1.2.3.4', ros_ip="4.5.6.7", env_args=[('MENV1', 'val1'), ('MENV2', 'val2')]) 00134 n = Node('nodepkg','nodetype', env_args=[('MENV1', 'nodeval1')]) 00135 d = create_local_process_env(n, m, master_uri) 00136 self.assertEquals(d['MENV1'], "nodeval1") 00137 self.assertEquals(d['MENV2'], "val2") 00138 00139 if __name__ == '__main__': 00140 import rostest 00141 rostest.unitrun('test_roslaunch', NAME, TestNodeArgs, coverage_packages=['roslaunch.node_args']) 00142