cm_msgs_utils_test.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 from controller_manager_msgs.msg import ControllerState as CtrlState
29 from controller_manager_msgs.msg import HardwareInterfaceResources
31 
32 ctrl_list = [
33  CtrlState(name='foo_controller',
34  state='running',
35  type='foo_base/foo',
36  claimed_resources=[
37  HardwareInterfaceResources(
38  hardware_interface='hardware_interface::FooInterface',
39  resources=['one', 'two', 'three'])
40  ]),
41  CtrlState(name='bar_controller',
42  state='running',
43  type='bar_base/bar',
44  claimed_resources=[
45  HardwareInterfaceResources(
46  hardware_interface='hardware_interface::BarInterface',
47  resources=['four'])
48  ]),
49  CtrlState(name='foobar_controller',
50  state='stopped',
51  type='foobar_base/foobar',
52  claimed_resources=[
53  HardwareInterfaceResources(
54  hardware_interface='hardware_interface::FooBarInterface',
55  resources=['one'])
56  ]),
57  CtrlState(name='foobaz_controller',
58  state='running',
59  type='foobaz_base/foobaz',
60  claimed_resources=[
61  HardwareInterfaceResources(
62  hardware_interface='hardware_interface::FooInterface',
63  resources=['one']),
64  HardwareInterfaceResources(
65  hardware_interface='hardware_interface::BazInterface',
66  resources=['one', 'two'])
67  ])
68 ]
69 
70 
72  # Non-existing
73  assert(not filter_by_name(ctrl_list, 'null', match_substring=False))
74  assert(not filter_by_name(ctrl_list, 'null', match_substring=True))
75 
76  # Existing, full match
77  out = filter_by_name(ctrl_list, 'foo_controller')
78  assert(1 == len(out))
79  assert(out[0].name == 'foo_controller')
80 
81  # Existing, substring match
82  out = filter_by_name(ctrl_list, 'foo_controller', match_substring=True)
83  assert(1 == len(out))
84  assert(out[0].name == 'foo_controller')
85 
86  out = filter_by_name(ctrl_list, 'foo', match_substring=True)
87  assert(3 == len(out))
88  assert(out[0].name == 'foo_controller')
89  assert(out[1].name == 'foobar_controller')
90  assert(out[2].name == 'foobaz_controller')
91 
92 
94  # Non-existing
95  assert(not filter_by_state(ctrl_list, 'null', match_substring=False))
96  assert(not filter_by_state(ctrl_list, 'null', match_substring=True))
97 
98  # Existing, full match
99  out = filter_by_state(ctrl_list, 'stopped')
100  assert(1 == len(out))
101  assert(out[0].name == 'foobar_controller')
102 
103  # Existing, substring match
104  out = filter_by_state(ctrl_list, 'stopped', match_substring=True)
105  assert(1 == len(out))
106  assert(out[0].name == 'foobar_controller')
107 
108  out = filter_by_state(ctrl_list, 'run', match_substring=True)
109  assert(3 == len(out))
110  assert(out[0].name == 'foo_controller')
111  assert(out[1].name == 'bar_controller')
112  assert(out[2].name == 'foobaz_controller')
113 
114 
116  # Non-existing
117  assert(not filter_by_type(ctrl_list, 'null', match_substring=False))
118  assert(not filter_by_type(ctrl_list, 'null', match_substring=True))
119 
120  # Existing, full match
121  out = filter_by_type(ctrl_list, 'foo_base/foo')
122  assert(1 == len(out))
123  assert(out[0].name == 'foo_controller')
124 
125  # Existing, substring match
126  out = filter_by_type(ctrl_list, 'foo_base/foo', match_substring=True)
127  assert(1 == len(out))
128  assert(out[0].name == 'foo_controller')
129 
130  out = filter_by_type(ctrl_list, 'foo', match_substring=True)
131  assert(3 == len(out))
132  assert(out[0].name == 'foo_controller')
133  assert(out[1].name == 'foobar_controller')
134  assert(out[2].name == 'foobaz_controller')
135 
136 
138  # Non-existing
139  assert(not filter_by_hardware_interface(ctrl_list,
140  'null',
141  match_substring=False))
142  assert(not filter_by_hardware_interface(ctrl_list,
143  'null',
144  match_substring=True))
145 
146  # Existing, full match
147  out = filter_by_hardware_interface(ctrl_list,
148  'hardware_interface::FooInterface')
149  assert(2 == len(out))
150  assert(out[0].name == 'foo_controller')
151  assert(out[1].name == 'foobaz_controller')
152 
153  # Existing, substring match
154  out = filter_by_hardware_interface(ctrl_list,
155  'hardware_interface::BazInterface',
156  match_substring=True)
157  assert(1 == len(out))
158  assert(out[0].name == 'foobaz_controller')
159 
160  out = filter_by_hardware_interface(ctrl_list,
161  'BazInterface',
162  match_substring=True)
163  assert(1 == len(out))
164  assert(out[0].name == 'foobaz_controller')
165 
166 
168  # Non-existing resource. Consider all hardware interfaces
169  assert(not filter_by_resources(ctrl_list, ['null'], match_any=False))
170  assert(not filter_by_resources(ctrl_list, ['null'], match_any=True))
171 
172  # Non-existing resource. Consider only specified hardware interfaces
173  assert(not filter_by_resources(
174  ctrl_list,
175  hardware_interface='hardware_interface::FooInterface',
176  resources=['null'],
177  match_any=False))
178 
179  assert(not filter_by_resources(
180  ctrl_list,
181  hardware_interface='hardware_interface::FooInterface',
182  resources=['null'],
183  match_any=True))
184 
185  # Non-existing resource in specified interface, but existing in other
186  # interfaces
187  assert(not filter_by_resources(
188  ctrl_list,
189  hardware_interface='hardware_interface::FooInterface',
190  resources=['four'],
191  match_any=False))
192 
193  assert(not filter_by_resources(
194  ctrl_list,
195  hardware_interface='hardware_interface::FooInterface',
196  resources=['four'],
197  match_any=True))
198 
199  # Existing, full match. Consider all hardware interfaces
200  res = ctrl_list[0].claimed_resources[0].resources
201  out = filter_by_resources(ctrl_list, res)
202  assert(1 == len(out))
203  assert(out[0].name == 'foo_controller')
204 
205  # Existing, full match. Consider only specified hardware interfaces
206  out = filter_by_resources(
207  ctrl_list,
208  hardware_interface='hardware_interface::FooInterface',
209  resources=res)
210  assert(1 == len(out))
211  assert(out[0].name == 'foo_controller')
212 
213  out = filter_by_resources(
214  ctrl_list,
215  hardware_interface='hardware_interface::FooInterface',
216  resources=['one'])
217  assert(2 == len(out))
218  assert(out[0].name == 'foo_controller')
219  assert(out[1].name == 'foobaz_controller')
220 
221  # Existing, partial match. Consider all hardware interfaces
222  out = filter_by_resources(ctrl_list, res, match_any=True)
223  assert(3 == len(out))
224  assert(out[0].name == 'foo_controller')
225  assert(out[1].name == 'foobar_controller')
226  assert(out[2].name == 'foobaz_controller')
227 
228  # Existing, partial match. Consider only specified hardware interfaces
229  out = filter_by_resources(
230  ctrl_list,
231  hardware_interface='hardware_interface::BazInterface',
232  resources=['one'],
233  match_any=True)
234  assert(1 == len(out))
235  assert(out[0].name == 'foobaz_controller')


controller_manager_tests
Author(s): Vijay Pradeep, Adolfo Rodriguez Tsouroukdissian
autogenerated on Fri Jun 7 2019 22:00:10