|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2024, FZI Forschungszentrum Informatik |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# * Neither the name of the {copyright_holder} nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from |
| 16 | +# this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +# POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +import logging |
| 31 | +import os |
| 32 | +import pytest |
| 33 | +import sys |
| 34 | +import time |
| 35 | +import unittest |
| 36 | + |
| 37 | +import rclpy |
| 38 | +from rclpy.node import Node |
| 39 | + |
| 40 | + |
| 41 | +from launch import LaunchDescription |
| 42 | +from launch.actions import ( |
| 43 | + ExecuteProcess, |
| 44 | + IncludeLaunchDescription, |
| 45 | + RegisterEventHandler, |
| 46 | +) |
| 47 | +from launch.event_handlers import OnProcessExit |
| 48 | +from launch.launch_description_sources import PythonLaunchDescriptionSource |
| 49 | +from launch.substitutions import LaunchConfiguration, PathJoinSubstitution |
| 50 | +from launch_ros.substitutions import FindPackagePrefix, FindPackageShare |
| 51 | + |
| 52 | +import launch_testing |
| 53 | +from launch_testing.actions import ReadyToTest |
| 54 | + |
| 55 | +sys.path.append(os.path.dirname(__file__)) |
| 56 | +from test_common import ( # noqa: E402 |
| 57 | + ControllerManagerInterface, |
| 58 | + _declare_launch_arguments, |
| 59 | + _ursim_action, |
| 60 | +) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.launch_test |
| 64 | +@launch_testing.parametrize( |
| 65 | + "launch_dashboard_client", |
| 66 | + [("true"), ("false")], |
| 67 | +) |
| 68 | +def generate_test_description(launch_dashboard_client): |
| 69 | + ur_type = LaunchConfiguration("ur_type") |
| 70 | + launch_arguments = { |
| 71 | + "robot_ip": "192.168.56.101", |
| 72 | + "ur_type": ur_type, |
| 73 | + "launch_rviz": "false", |
| 74 | + "controller_spawner_timeout": str(120), |
| 75 | + "initial_joint_controller": "scaled_joint_trajectory_controller", |
| 76 | + "headless_mode": "true", |
| 77 | + "launch_dashboard_client": launch_dashboard_client, |
| 78 | + "start_joint_controller": "false", |
| 79 | + } |
| 80 | + |
| 81 | + robot_driver = IncludeLaunchDescription( |
| 82 | + PythonLaunchDescriptionSource( |
| 83 | + PathJoinSubstitution( |
| 84 | + [FindPackageShare("ur_robot_driver"), "launch", "ur_control.launch.py"] |
| 85 | + ) |
| 86 | + ), |
| 87 | + launch_arguments=launch_arguments.items(), |
| 88 | + ) |
| 89 | + wait_dashboard_server = ExecuteProcess( |
| 90 | + cmd=[ |
| 91 | + PathJoinSubstitution( |
| 92 | + [ |
| 93 | + FindPackagePrefix("ur_robot_driver"), |
| 94 | + "bin", |
| 95 | + "wait_dashboard_server.sh", |
| 96 | + ] |
| 97 | + ) |
| 98 | + ], |
| 99 | + name="wait_dashboard_server", |
| 100 | + output="screen", |
| 101 | + ) |
| 102 | + driver_starter = RegisterEventHandler( |
| 103 | + OnProcessExit(target_action=wait_dashboard_server, on_exit=robot_driver) |
| 104 | + ) |
| 105 | + |
| 106 | + return LaunchDescription( |
| 107 | + _declare_launch_arguments() |
| 108 | + + [ReadyToTest(), wait_dashboard_server, _ursim_action(), driver_starter] |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +class DashboardClientTest(unittest.TestCase): |
| 113 | + @classmethod |
| 114 | + def setUpClass(cls): |
| 115 | + # Initialize the ROS context |
| 116 | + rclpy.init() |
| 117 | + cls.node = Node("robot_driver_launch_test") |
| 118 | + time.sleep(1) |
| 119 | + cls.init_robot(cls) |
| 120 | + |
| 121 | + @classmethod |
| 122 | + def tearDownClass(cls): |
| 123 | + # Shutdown the ROS context |
| 124 | + cls.node.destroy_node() |
| 125 | + rclpy.shutdown() |
| 126 | + |
| 127 | + def init_robot(self): |
| 128 | + # This waits for the controller_manager to be available |
| 129 | + self._controller_manager_interface = ControllerManagerInterface(self.node) |
| 130 | + |
| 131 | + def setUp(self): |
| 132 | + pass |
| 133 | + |
| 134 | + def test_dashboard_client_exists(self, launch_dashboard_client): |
| 135 | + # Use the get_node_names_and_namespaces() method to get the list of nodes and their namespaces |
| 136 | + nodes_with_ns = self.node.get_node_names_and_namespaces() |
| 137 | + |
| 138 | + nodes = [namespace + name for (name, namespace) in nodes_with_ns] |
| 139 | + |
| 140 | + for node in nodes: |
| 141 | + print(node) |
| 142 | + |
| 143 | + # Print out the nodes and their namespaces |
| 144 | + logging.info("Discovered ROS nodes:") |
| 145 | + if launch_dashboard_client == "true": |
| 146 | + self.assertIn("/dashboard_client", nodes) |
| 147 | + else: |
| 148 | + self.assertNotIn("/dashboard_client", nodes) |
0 commit comments