$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, 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 ## Unit test of rospy.client APIs for parameter server access 00037 00038 PKG = 'test_rospy' 00039 NAME = 'test_rospy_client_param_server' 00040 import roslib; roslib.load_manifest(PKG) 00041 00042 import sys, time 00043 import math 00044 import unittest 00045 00046 import rospy 00047 import rostest 00048 from test_rospy.srv import * 00049 00050 def test_param_filter(params): 00051 return [p for p in params if not p.startswith('/roslaunch') and p not in ['/run_id', '/rosdistro', '/rosversion']] 00052 00053 ## Test the rospy.client parameter server API: 00054 ##- get_param 00055 ##- set_param 00056 ##- delete_param 00057 ##- has_param 00058 ##- get_param_names 00059 class TestClientParamServer(unittest.TestCase): 00060 00061 ## test get/has param against state initialized from rostest script 00062 def test_get_has_param(self): 00063 #test error conditions 00064 try: rospy.get_param(None); self.fail("get_param(None) succeeded") 00065 except: pass 00066 try: rospy.get_param(''); self.fail("get_param('') succeeded") 00067 except: pass 00068 # should be keyerror 00069 try: rospy.get_param('non-existent'); self.fail("get_param('non-existent') succeeded") 00070 except: pass 00071 try: rospy.has_param(None, 'foo'); self.fail("has_param(None) succeeded") 00072 except: pass 00073 try: rospy.has_param(''); self.fail("has_param('') succeeded") 00074 except: pass 00075 00076 self.failIf(rospy.has_param('non-existent'), "has_param('non-existent') succeeded") 00077 00078 #validate get_param against values on the param server 00079 rostest_tests = { 00080 'string': "string", 00081 'int0': 0, 00082 'int10': 10, 00083 'float0': 0.0, 00084 'float10': 10.0, 00085 "namespace/string": "namespaced string", 00086 } 00087 param_names = [rospy.resolve_name(k) for k in rostest_tests.keys()] 00088 00089 # test get parameter names 00090 diff = set(param_names) ^ set(test_param_filter(rospy.get_param_names())) 00091 self.failIf(diff, diff) 00092 00093 # test for existing and value 00094 for k, v in rostest_tests.iteritems(): 00095 self.assert_(rospy.has_param(k)) 00096 self.assert_(rospy.has_param(rospy.resolve_name(k))) 00097 if not type(v) == float: 00098 self.assertEqual(v, rospy.get_param(k)) 00099 self.assertEqual(v, rospy.get_param(rospy.resolve_name(k))) 00100 else: 00101 self.assertAlmostEqual(v, rospy.get_param(k), 1) 00102 self.assertAlmostEqual(v, rospy.get_param(rospy.resolve_name(k)), 1) 00103 00104 def test_search_param(self): 00105 try: 00106 orig_caller_id = rospy.names.get_caller_id() 00107 00108 rospy.names._set_caller_id('/global_node') 00109 00110 self.assertEquals(None, rospy.search_param('search_param')) 00111 rospy.set_param('/search_param', 1) 00112 self.assertEquals('/search_param', rospy.search_param('search_param')) 00113 00114 rospy.names._set_caller_id('/level1/level2/relative_node') 00115 self.assertEquals('/search_param', rospy.search_param('search_param')) 00116 rospy.set_param('/level1/search_param', 2) 00117 self.assertEquals('/level1/search_param', rospy.search_param('search_param')) 00118 rospy.set_param('~search_param', 3) 00119 # make sure that search starts in our private namespace first 00120 self.assertEquals('/level1/level2/relative_node/search_param', rospy.search_param('search_param')) 00121 00122 finally: 00123 rospy.names._set_caller_id(orig_caller_id) 00124 00125 def test_delete_param(self): 00126 try: rospy.delete_param(None); self.fail("delete_param(None) succeeded") 00127 except: pass 00128 try: rospy.delete_param(''); self.fail("delete_param('') succeeded") 00129 except: pass 00130 00131 rostest_tests = { 00132 'dpstring': "string", 00133 'dpint0': 0, 00134 'dpint10': 10, 00135 'dpfloat0': 0.0, 00136 'dpfloat10': 10.0, 00137 "dpnamespace/string": "namespaced string", 00138 } 00139 initial_params = rospy.get_param_names() 00140 # implicitly depends on set param 00141 for k, v in rostest_tests.iteritems(): 00142 rospy.set_param(k, v) 00143 00144 # test delete 00145 for k, v in rostest_tests.iteritems(): 00146 self.assert_(rospy.has_param(k)) 00147 rospy.delete_param(k) 00148 self.failIf(rospy.has_param(k)) 00149 self.failIf(rospy.has_param(rospy.resolve_name(k))) 00150 try: 00151 rospy.get_param(k) 00152 self.fail("get_param should fail on deleted key") 00153 except KeyError: pass 00154 00155 # make sure no new state 00156 self.failIf(set(initial_params) ^ set(rospy.get_param_names())) 00157 00158 ## test set_param separately 00159 def test_set_param(self): 00160 try: rospy.set_param(None, 'foo'); self.fail("set_param(None) succeeded") 00161 except: pass 00162 try: rospy.set_param('', 'foo'); self.fail("set_param('') succeeded") 00163 except: pass 00164 00165 rostest_tests = { 00166 'spstring': "string", 00167 'spint0': 0, 00168 'spint10': 10, 00169 'spfloat0': 0.0, 00170 'spfloat10': 10.0, 00171 "spnamespace/string": "namespaced string", 00172 } 00173 initial_param_names = rospy.get_param_names() 00174 param_names = [rospy.resolve_name(k) for k in rostest_tests.keys()] 00175 for k, v in rostest_tests.iteritems(): 00176 self.failIf(rospy.has_param(k)) 00177 self.failIf(rospy.has_param(rospy.resolve_name(k))) 00178 rospy.set_param(k, v) 00179 self.assert_(rospy.has_param(k)) 00180 self.assert_(rospy.has_param(rospy.resolve_name(k))) 00181 self.assertEquals(v, rospy.get_param(k)) 00182 self.assertEquals(v, rospy.get_param(rospy.resolve_name(k))) 00183 correct_state = set(initial_param_names + param_names) 00184 self.failIf(correct_state ^ set(rospy.get_param_names())) 00185 00186 if __name__ == '__main__': 00187 rospy.init_node(NAME) 00188 rostest.run(PKG, NAME, TestClientParamServer, sys.argv)