|
| 1 | +# Copyright (c) 2024 AIT - Austrian Institute of Technology GmbH |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# |
| 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 | +# |
| 13 | +# * Neither the name of the {copyright_holder} nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from |
| 15 | +# this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | +# Author: Christoph Froehlich |
| 30 | + |
| 31 | +import os |
| 32 | +import pytest |
| 33 | +import unittest |
| 34 | +import subprocess |
| 35 | + |
| 36 | +from ament_index_python.packages import get_package_share_directory |
| 37 | +from launch import LaunchDescription |
| 38 | +from launch.actions import IncludeLaunchDescription |
| 39 | +from launch.launch_description_sources import PythonLaunchDescriptionSource |
| 40 | +from launch_testing.actions import ReadyToTest |
| 41 | + |
| 42 | +# import launch_testing.markers |
| 43 | +import rclpy |
| 44 | +from rclpy.node import Node |
| 45 | +from ros2_control_demo_testing.test_utils import check_controllers_running |
| 46 | + |
| 47 | + |
| 48 | +# Executes the given launch file and checks if all nodes can be started |
| 49 | +@pytest.mark.rostest |
| 50 | +def generate_test_description(): |
| 51 | + launch_include = IncludeLaunchDescription( |
| 52 | + PythonLaunchDescriptionSource( |
| 53 | + os.path.join( |
| 54 | + get_package_share_directory("ros2_control_demo_example_1"), |
| 55 | + "launch/rrbot.launch.py", |
| 56 | + ) |
| 57 | + ), |
| 58 | + launch_arguments={"gui": "False"}.items(), |
| 59 | + ) |
| 60 | + |
| 61 | + return LaunchDescription([launch_include, ReadyToTest()]) |
| 62 | + |
| 63 | + |
| 64 | +class TestFixtureCliDirect(unittest.TestCase): |
| 65 | + |
| 66 | + def setUp(self): |
| 67 | + rclpy.init() |
| 68 | + self.node = Node("test_node") |
| 69 | + |
| 70 | + def tearDown(self): |
| 71 | + self.node.destroy_node() |
| 72 | + rclpy.shutdown() |
| 73 | + |
| 74 | + def test_main(self, proc_output): |
| 75 | + |
| 76 | + # Command to run the CLI |
| 77 | + cname = "joint_trajectory_position_controller" |
| 78 | + command = [ |
| 79 | + "ros2", |
| 80 | + "control", |
| 81 | + "load_controller", |
| 82 | + "--set-state", |
| 83 | + "inactive", |
| 84 | + cname, |
| 85 | + os.path.join( |
| 86 | + get_package_share_directory("ros2_control_demo_example_1"), |
| 87 | + "config/rrbot_jtc.yaml", |
| 88 | + ), |
| 89 | + ] |
| 90 | + subprocess.run(command, check=True) |
| 91 | + check_controllers_running(self.node, [cname], state="inactive") |
| 92 | + check_controllers_running(self.node, ["forward_position_controller"], state="active") |
| 93 | + |
| 94 | + command = [ |
| 95 | + "ros2", |
| 96 | + "control", |
| 97 | + "switch_controllers", |
| 98 | + "--activate", |
| 99 | + cname, |
| 100 | + "--deactivate", |
| 101 | + "forward_position_controller", |
| 102 | + ] |
| 103 | + subprocess.run(command, check=True) |
| 104 | + check_controllers_running(self.node, ["forward_position_controller"], state="inactive") |
| 105 | + check_controllers_running(self.node, [cname], state="active") |
| 106 | + |
| 107 | + |
| 108 | +# TODO(anyone): enable this if shutdown of ros2_control_node does not fail anymore |
| 109 | +# @launch_testing.post_shutdown_test() |
| 110 | +# # These tests are run after the processes in generate_test_description() have shutdown. |
| 111 | +# class TestDescriptionCraneShutdown(unittest.TestCase): |
| 112 | + |
| 113 | +# def test_exit_codes(self, proc_info): |
| 114 | +# """Check if the processes exited normally.""" |
| 115 | +# launch_testing.asserts.assertExitCodes(proc_info) |
0 commit comments