test_start_fail.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2011, 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 PKG = 'app_manager'
35 
36 import os
37 import sys
38 import time
39 import unittest
40 
41 import rospy
42 import rostest
43 import rosunit
44 
45 from app_manager.msg import *
46 from app_manager.srv import *
47 
48 from std_msgs.msg import String
49 
50 class StartFailTest(unittest.TestCase):
51 
52  def __init__(self, *args):
53  super(StartFailTest, self).__init__(*args)
54  rospy.init_node('start_fail_test')
55 
56  def cb(self, msg):
57  rospy.logwarn("{} received".format(msg))
58  self.msg = msg
59  self.msg_received = self.msg_received + 1
60 
61  def setUp(self):
62  rospy.wait_for_service('/robot/list_apps')
63  rospy.wait_for_service('/robot/stop_app')
64  rospy.wait_for_service('/robot/start_app')
65  self.list = rospy.ServiceProxy('/robot/list_apps', ListApps)
66  self.stop = rospy.ServiceProxy('/robot/stop_app', StopApp)
67  self.start = rospy.ServiceProxy('/robot/start_app', StartApp)
68  self.msg = None
69  self.msg_received = 0
70  rospy.Subscriber('/chatter', String, self.cb)
71 
73  # wait for application
74  rospy.logwarn("Wait for application")
75  list_req = ListAppsRequest()
76  list_res = ListAppsResponse()
77  while not 'app_manager/appA' in list(map(lambda x: x.name, list_res.available_apps)):
78  list_res = self.list.call(list_req)
79  # rospy.logwarn("received 'list_apps' {}".format(list_res))
80  time.sleep(1)
81 
82  # intentionally failed to start
83  rospy.logwarn("Start application with wrong arguments")
84  start_req = StartAppRequest(name='app_manager/appA', args=[KeyValue('launch_prefix', 'no_command')])
85  start_res = self.start.call(start_req)
86  rospy.logwarn("start 'started_app' {}".format(start_res))
87  self.assertEqual(start_res.started, False)
88 
89  # confirm if application is failed to start
90  rospy.logwarn("Check running application")
91  list_req = ListAppsRequest()
92  list_res = ListAppsResponse(running_apps=[App(name='app_manager/appA')])
93  while 'app_manager/appA' in list(map(lambda x: x.name, list_res.running_apps)):
94  list_res = self.list.call(list_req)
95  # rospy.logwarn("received 'list_apps' {}".format(list_res))
96  time.sleep(1)
97  break
98 
99  # start app and check if actually started
100  rospy.logwarn("Start application")
101  start_req = StartAppRequest(name='app_manager/appA', args=[KeyValue('launch_prefix', 'python{}'.format(os.environ['ROS_PYTHON_VERSION']))])
102  start_res = self.start.call(start_req)
103  rospy.logwarn("received 'started_app' {}".format(start_res))
104  self.assertEqual(start_res.started, True)
105 
106  # check if msg received
107  while (not rospy.is_shutdown()) and self.msg == None:
108  rospy.logwarn('Wait for /chatter message received..')
109  rospy.sleep(1)
110 
111  # stop plugin
112  stop_req = StopAppRequest(name='app_manager/appA')
113  stop_res = self.stop.call(stop_req)
114  rospy.logwarn('stop app {}'.format(stop_res))
115  self.assertEqual(stop_res.stopped, True)
116 
117 if __name__ == '__main__':
118  try:
119  rostest.run('start_fail_test', PKG, StartFailTest, sys.argv)
120  except KeyboardInterrupt:
121  pass
122  print("{} exiting".format(PKG))


app_manager
Author(s): Jeremy Leibs, Ken Conley, Yuki Furuta
autogenerated on Thu Oct 13 2022 02:59:17