cm_msgs_utils_rostest.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2014, PAL Robotics S.L.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of PAL Robotics S.L. nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27 
28 import unittest
29 import rospy
31 
32 valid_cm = [
33  '/',
34  '/controller_manager',
35  '/foo/robot/controller_manager1',
36  '/foo/robot/controller_manager2'
37 ]
38 
39 invalid_cm = [
40  'non_existent',
41  '/incomplete',
42  '/bad_type'
43 ]
44 
45 
46 class TestUtils(unittest.TestCase):
47  def _ready(self):
48  try:
49  rospy.wait_for_service("/controller_manager/list_controllers", 2.0)
50  except ROSException:
51  return False
52  return True
53 
55  self.assertTrue(self._ready())
56 
57  for cm in valid_cm:
58  self.assertTrue(is_controller_manager(cm))
59  for cm in invalid_cm:
60  self.assertFalse(is_controller_manager(cm))
61 
63  self.assertTrue(self._ready())
64 
65  # Root namespace
66  self.assertEqual(valid_cm, get_controller_managers())
67  self.assertEqual(valid_cm, get_controller_managers('/'))
68 
69  # Nested namespace
70  nested_cm = [valid_cm[2], valid_cm[3]]
71  self.assertEqual(nested_cm, get_controller_managers('/foo'))
72  self.assertEqual(nested_cm, get_controller_managers('/foo/robot'))
73  self.assertEqual(['/controller_manager'],
74  get_controller_managers('/controller_manager'))
75 
76  # Initial guess: Recommended usage pattern
77  prev_cm = get_controller_managers()
78  self.assertEqual(valid_cm,
79  get_controller_managers(initial_guess=prev_cm))
80 
81  # Initial guess: Partial guess
82  self.assertEqual(valid_cm,
83  get_controller_managers(initial_guess=nested_cm))
84 
85  # Misuse of initial guess. Specifying entries that have not gone
86  # through a full API check can yield incorrect results.
87  # You have been warned!
88  wrong_cm = get_controller_managers(initial_guess=invalid_cm)
89  self.assertNotEqual(valid_cm, wrong_cm)
90  diff = list(set(wrong_cm) - set(valid_cm))
91  self.assertEqual(2, len(diff))
92  self.assertIn('/incomplete', diff)
93  self.assertIn('/bad_type', diff)
94 
96  self.assertTrue(self._ready())
97 
98  # Root namespace
99  list_cm = ControllerManagerLister()
100  self.assertEqual(valid_cm, list_cm())
101 
102  # Nested namespace
103  list_cm_foo = ControllerManagerLister('/foo')
104  nested_cm = [valid_cm[2], valid_cm[3]]
105  self.assertEqual(nested_cm, list_cm_foo())
106 
108  self.assertTrue(self._ready())
109 
110  # Default namespace
111  list_controllers = ControllerLister()
112  controllers = list_controllers()
113  self.assertEqual(2, len(controllers))
114  self.assertEqual('foo_controller', controllers[0].name)
115  self.assertEqual('bar_controller', controllers[1].name)
116 
117  # Custom namespace
118  list_controllers_ns = ControllerLister('/foo')
119  self.assertEqual(0, len(list_controllers_ns()))
120 
122  # Default namespace
123  names_def = get_rosparam_controller_names()
124  self.assertEqual(2, len(names_def))
125  self.assertIn('foo_controller', names_def)
126  self.assertIn('bar_controller', names_def)
127 
128  # Root namespace
129  names_root = get_rosparam_controller_names('/')
130  self.assertEqual(2, len(names_root))
131  self.assertIn('foo_controller', names_root)
132  self.assertIn('bar_controller', names_root)
133 
134  # Empty namespace
135  names_empty = get_rosparam_controller_names('')
136  self.assertEqual(2, len(names_empty))
137  self.assertIn('foo_controller', names_empty)
138  self.assertIn('bar_controller', names_empty)
139 
140  # Custom namespace
141  names_ns = get_rosparam_controller_names('/ns')
142  self.assertEqual(2, len(names_ns))
143  self.assertIn('baz_controller', names_ns)
144  self.assertIn('qux_controller', names_ns)
145 
146  # Custom namespace, trailing slash
147  names_nss = get_rosparam_controller_names('/ns/')
148  self.assertEqual(2, len(names_nss))
149  self.assertIn('baz_controller', names_nss)
150  self.assertIn('qux_controller', names_nss)
151 
152 if __name__ == '__main__':
153  import rostest
154  rostest.rosrun('controller_manager_msgs',
155  'cm_msgs_utils_rostest',
156  TestUtils)


controller_manager_tests
Author(s): Vijay Pradeep, Adolfo Rodriguez Tsouroukdissian
autogenerated on Mon Apr 20 2020 03:52:10