|
| 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 | + |
| 35 | +from ament_index_python.packages import get_package_share_directory |
| 36 | +from launch import LaunchDescription |
| 37 | +from launch.actions import IncludeLaunchDescription |
| 38 | +from launch.launch_description_sources import PythonLaunchDescriptionSource |
| 39 | +from launch_testing.actions import ReadyToTest |
| 40 | + |
| 41 | +import launch_testing |
| 42 | +import rclpy |
| 43 | +from rclpy.node import Node |
| 44 | +from ros2_control_demo_testing.test_utils import ( |
| 45 | + check_controllers_running, |
| 46 | + check_if_js_published, |
| 47 | + check_node_running, |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +# This function specifies the processes to be run for our test |
| 52 | +# The ReadyToTest action waits for 15 second by default for the processes to |
| 53 | +# start, if processes take more time an error is thrown. We use decorator here |
| 54 | +# to provide timeout duration of 20 second so that processes that take longer than |
| 55 | +# 15 seconds can start up. |
| 56 | +@pytest.mark.launch_test |
| 57 | +@launch_testing.ready_to_test_action_timeout(20) |
| 58 | +def generate_test_description(): |
| 59 | + launch_include = IncludeLaunchDescription( |
| 60 | + PythonLaunchDescriptionSource( |
| 61 | + os.path.join( |
| 62 | + get_package_share_directory("ros2_control_demo_example_10"), |
| 63 | + "launch/rrbot.launch.py", |
| 64 | + ) |
| 65 | + ), |
| 66 | + launch_arguments={"gui": "false"}.items(), |
| 67 | + ) |
| 68 | + |
| 69 | + return LaunchDescription([launch_include, ReadyToTest()]) |
| 70 | + |
| 71 | + |
| 72 | +# This is our test fixture. Each method is a test case. |
| 73 | +# These run alongside the processes specified in generate_test_description() |
| 74 | +class TestFixture(unittest.TestCase): |
| 75 | + |
| 76 | + def setUp(self): |
| 77 | + rclpy.init() |
| 78 | + self.node = Node("test_node") |
| 79 | + |
| 80 | + def tearDown(self): |
| 81 | + self.node.destroy_node() |
| 82 | + rclpy.shutdown() |
| 83 | + |
| 84 | + def test_node_start(self, proc_output): |
| 85 | + check_node_running(self.node, "robot_state_publisher") |
| 86 | + |
| 87 | + def test_controller_running(self, proc_output): |
| 88 | + |
| 89 | + cnames = ["forward_position_controller", "gpio_controller", "joint_state_broadcaster"] |
| 90 | + |
| 91 | + check_controllers_running(self.node, cnames) |
| 92 | + |
| 93 | + def test_check_if_msgs_published(self): |
| 94 | + check_if_js_published("/joint_states", ["joint1", "joint2"]) |
| 95 | + |
| 96 | + |
| 97 | +# TODO(anyone): enable this if shutdown of ros2_control_node does not fail anymore |
| 98 | +# @launch_testing.post_shutdown_test() |
| 99 | +# # These tests are run after the processes in generate_test_description() have shutdown. |
| 100 | +# class TestDescriptionCraneShutdown(unittest.TestCase): |
| 101 | + |
| 102 | +# def test_exit_codes(self, proc_info): |
| 103 | +# """Check if the processes exited normally.""" |
| 104 | +# launch_testing.asserts.assertExitCodes(proc_info) |
0 commit comments