check_rosparam_command_line_online.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2009, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import os
35 import sys
36 import time
37 import unittest
38 
39 import rostest
40 
41 from subprocess import Popen, PIPE, check_call, call
42 
43 from rosgraph.names import script_resolve_name
44 
46  import rosgraph
47  return rosgraph.Master('/rosparam')
48 
49 class TestRosparamOnline(unittest.TestCase):
50 
51  def setUp(self):
52  self.vals = set()
53  self.msgs = {}
54 
55  def callback(self, msg, val):
56  self.vals.add(val)
57  self.msgs[val] = msg
58 
59  def test_rosparam(self):
60  ps = get_param_server()
61 
62  # network is initialized
63  cmd = 'rosparam'
64  names = ['/chatter', 'foo/chatter']
65 
66  # list
67  params = ['/string', '/int', '/float',
68  '/g1/string', '/g1/int', '/g1/float',
69  '/g2/string', '/g2/int', '/g2/float',
70  ]
71  # - we aren't matching against the core services as those can make the test suites brittle
72  output = Popen([cmd, 'list'], stdout=PIPE).communicate()[0]
73  l = set(output.split())
74  for t in params:
75  self.assert_(t in l)
76 
77  # get
78  # - strings
79  output = Popen([cmd, 'get', "string"], stdout=PIPE).communicate()[0]
80  self.assertEquals('foo-value', output.strip())
81  # -- pretty
82  output = Popen([cmd, 'get', '-p', "string"], stdout=PIPE).communicate()[0]
83  self.assertEquals('foo-value', output.strip())
84  output = Popen([cmd, 'get', "/string"], stdout=PIPE).communicate()[0]
85  self.assertEquals('foo-value', output.strip())
86  output = Popen([cmd, 'get', "g1/string"], stdout=PIPE).communicate()[0]
87  self.assertEquals('g1-foo-value', output.strip())
88  output = Popen([cmd, 'get', "/g1/string"], stdout=PIPE).communicate()[0]
89  self.assertEquals('g1-foo-value', output.strip())
90  output = Popen([cmd, 'get', "/g2/string"], stdout=PIPE).communicate()[0]
91  self.assertEquals('g2-foo-value', output.strip())
92  # - ints
93  output = Popen([cmd, 'get', "int"], stdout=PIPE).communicate()[0]
94  self.assertEquals('1', output.strip())
95  # -- pretty
96  output = Popen([cmd, 'get', '-p', "int"], stdout=PIPE).communicate()[0]
97  self.assertEquals('1', output.strip())
98  output = Popen([cmd, 'get', "/int"], stdout=PIPE).communicate()[0]
99  self.assertEquals('1', output.strip())
100  output = Popen([cmd, 'get', "g1/int"], stdout=PIPE).communicate()[0]
101  self.assertEquals('10', output.strip())
102  output = Popen([cmd, 'get', "/g1/int"], stdout=PIPE).communicate()[0]
103  self.assertEquals('10', output.strip())
104  output = Popen([cmd, 'get', "/g2/int"], stdout=PIPE).communicate()[0]
105  self.assertEquals('20', output.strip())
106  # - floats
107  output = Popen([cmd, 'get', "float"], stdout=PIPE).communicate()[0]
108  self.assertEquals('1.0', output.strip())
109  # -- pretty
110  output = Popen([cmd, 'get', '-p', "float"], stdout=PIPE).communicate()[0]
111  self.assertEquals('1.0', output.strip())
112  output = Popen([cmd, 'get', "/float"], stdout=PIPE).communicate()[0]
113  self.assertEquals('1.0', output.strip())
114  output = Popen([cmd, 'get', "g1/float"], stdout=PIPE).communicate()[0]
115  self.assertEquals('10.0', output.strip())
116  output = Popen([cmd, 'get', "/g1/float"], stdout=PIPE).communicate()[0]
117  self.assertEquals('10.0', output.strip())
118  output = Popen([cmd, 'get', "/g2/float"], stdout=PIPE).communicate()[0]
119  self.assertEquals('20.0', output.strip())
120  # - dictionary
121  output = Popen([cmd, 'get', "g1"], stdout=PIPE).communicate()[0]
122  import yaml
123  d = yaml.load(output)
124  self.assertEquals(d['float'], 10.0)
125  self.assertEquals(d['int'], 10.0)
126  self.assertEquals(d['string'], "g1-foo-value")
127  self.assertEquals(set(['float', 'int', 'string']), set(d.keys()))
128 
129  # -- don't bother parsing pretty output of dictionary, but check for no errors
130  check_call([cmd, 'get', '-p', "g1"])
131  # --- with verbose
132  check_call([cmd, 'get', '-pv', "g1"])
133 
134  # set
135  # - integers
136  Popen([cmd, 'set', "/set/test1", "1"], stdout=PIPE).communicate()[0]
137  self.assertEquals(1, ps.getParam('/set/test1'))
138  # -- verbose
139  Popen([cmd, 'set', '-v', "/set/test1", "1"], stdout=PIPE).communicate()[0]
140  self.assertEquals(1, ps.getParam('/set/test1'))
141  Popen([cmd, 'set', "set/test1", "2"], stdout=PIPE).communicate()[0]
142  self.assertEquals(2, ps.getParam('/set/test1'))
143  # - floats
144  Popen([cmd, 'set', "/set/test2", "1.0"], stdout=PIPE).communicate()[0]
145  self.assertEquals(1, ps.getParam('/set/test2'))
146  Popen([cmd, 'set', "set/test2", "2.0"], stdout=PIPE).communicate()[0]
147  self.assertEquals(2, ps.getParam('/set/test2'))
148  # - booleans
149  Popen([cmd, 'set', "/set/testbool", "true"], stdout=PIPE).communicate()[0]
150  self.assertEquals(True, ps.getParam('/set/testbool'))
151  Popen([cmd, 'set', "set/testbool", "false"], stdout=PIPE).communicate()[0]
152  self.assertEquals(False, ps.getParam('/set/testbool'))
153  # - strings
154  # TODO: test more interesting encodings, like multi-line
155  Popen([cmd, 'set', "/set/teststr", "hi"], stdout=PIPE).communicate()[0]
156  self.assertEquals("hi", ps.getParam('/set/teststr'))
157  Popen([cmd, 'set', "set/teststr", "hello world"], stdout=PIPE).communicate()[0]
158  self.assertEquals("hello world", ps.getParam('/set/teststr'))
159  Popen([cmd, 'set', "set/teststr", "'true'"], stdout=PIPE).communicate()[0]
160  self.assertEquals("true", ps.getParam('/set/teststr'))
161  # - list
162  Popen([cmd, 'set', "set/testlist", "[]"], stdout=PIPE).communicate()[0]
163  self.assertEquals([], ps.getParam('/set/testlist'))
164  Popen([cmd, 'set', "/set/testlist", "[1, 2, 3]"], stdout=PIPE).communicate()[0]
165  self.assertEquals([1, 2, 3], ps.getParam('/set/testlist'))
166  # - dictionary
167  Popen([cmd, 'set', "/set/testdict", "{a: b, c: d}"], stdout=PIPE).communicate()[0]
168  self.assertEquals('b', ps.getParam('/set/testdict/a'))
169  self.assertEquals('d', ps.getParam('/set/testdict/c'))
170  # - empty dictionary should be a noop
171  Popen([cmd, 'set', "set/testdict", "{}"], stdout=PIPE).communicate()[0]
172  self.assertEquals('b', ps.getParam('/set/testdict/a'))
173  self.assertEquals('d', ps.getParam('/set/testdict/c'))
174  # - this should be an update
175  Popen([cmd, 'set', "/set/testdict", "{e: f, g: h}"], stdout=PIPE).communicate()[0]
176  self.assertEquals('b', ps.getParam('/set/testdict/a'))
177  self.assertEquals('d', ps.getParam('/set/testdict/c'))
178  self.assertEquals('f', ps.getParam('/set/testdict/e'))
179  self.assertEquals('h', ps.getParam('/set/testdict/g'))
180  # -- verbose
181  check_call([cmd, 'set', '-v', "/set/testdictverbose", "{e: f, g: h}"])
182 
183  # delete
184  ps.setParam('/delete/me', True)
185  self.assert_(ps.hasParam('/delete/me'))
186  Popen([cmd, 'delete', "/delete/me"], stdout=PIPE).communicate()[0]
187  self.failIf(ps.hasParam('/delete/me'))
188 
189  # TODO: dump
190  # TODO: load
191 
192 PKG = 'test_rosparam'
193 NAME = 'test_rosparam_command_line_online'
194 if __name__ == '__main__':
195  rostest.run(PKG, NAME, TestRosparamOnline, sys.argv)


test_rosparam
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:21