|
| 1 | +# Copyright 2025 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import contextlib |
| 16 | +import os |
| 17 | +import sys |
| 18 | +from typing import Any |
| 19 | +from typing import Generator |
| 20 | +import unittest |
| 21 | + |
| 22 | +from launch import LaunchDescription |
| 23 | +from launch import LaunchService |
| 24 | +from launch.actions import ExecuteProcess |
| 25 | +from launch.actions import SetEnvironmentVariable |
| 26 | +import launch_testing |
| 27 | +import launch_testing.actions |
| 28 | +import launch_testing.asserts |
| 29 | +import launch_testing.markers |
| 30 | +import launch_testing.tools |
| 31 | +from launch_testing_ros.actions import EnableRmwIsolation |
| 32 | + |
| 33 | +import pytest |
| 34 | + |
| 35 | +from rclpy.utilities import get_available_rmw_implementations |
| 36 | +from ros2cli.helpers import get_rmw_additional_env |
| 37 | + |
| 38 | + |
| 39 | +# Skip cli tests on Windows while they exhibit pathological behavior |
| 40 | +# https://github.com/ros2/build_farmer/issues/248 |
| 41 | +if sys.platform.startswith('win'): |
| 42 | + pytest.skip( |
| 43 | + 'CLI tests can block for a pathological amount of time on Windows.', |
| 44 | + allow_module_level=True) |
| 45 | + |
| 46 | + |
| 47 | +CYCLONEDDS_XML = '<CycloneDDS><Domain><General></General></Domain></CycloneDDS>' |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.rostest |
| 51 | +@launch_testing.parametrize('rmw_implementation', get_available_rmw_implementations()) |
| 52 | +@launch_testing.markers.keep_alive |
| 53 | +def generate_test_description(rmw_implementation: str) -> tuple[LaunchDescription, |
| 54 | + dict[str, Any]]: |
| 55 | + additional_env = get_rmw_additional_env(rmw_implementation) |
| 56 | + additional_env['PYTHONUNBUFFERED'] = '1' |
| 57 | + set_env_actions = [SetEnvironmentVariable(k, v) for k, v in additional_env.items()] |
| 58 | + |
| 59 | + return LaunchDescription([ |
| 60 | + ExecuteProcess( |
| 61 | + cmd=['ros2', 'daemon', 'stop'], |
| 62 | + name='daemon-stop', |
| 63 | + on_exit=[ |
| 64 | + *set_env_actions, |
| 65 | + EnableRmwIsolation(), |
| 66 | + ExecuteProcess( |
| 67 | + cmd=['ros2', 'daemon', 'start'], |
| 68 | + name='daemon-start', |
| 69 | + additional_env=additional_env, |
| 70 | + on_exit=[ |
| 71 | + SetEnvironmentVariable('ROS_AUTOMATIC_DISCOVERY_RANGE', 'SUBNET'), |
| 72 | + SetEnvironmentVariable('ROS_DISTRO', 'rolling'), |
| 73 | + SetEnvironmentVariable('ROS_DISABLE_LOANED_MESSAGES', '0'), |
| 74 | + SetEnvironmentVariable('RCUTILS_COLORIZED_OUTPUT', '0'), |
| 75 | + SetEnvironmentVariable('FASTDDS_BUILTIN_TRANSPORTS', 'UDPv6'), |
| 76 | + SetEnvironmentVariable('ZENOH_ROUTER_CHECK_ATTEMPTS', '10'), |
| 77 | + SetEnvironmentVariable('RMW_CONNEXT_INITIAL_PEERS', 'CONNEXTBOO'), |
| 78 | + SetEnvironmentVariable('CYCLONEDDS_URI', CYCLONEDDS_XML), |
| 79 | + launch_testing.actions.ReadyToTest() |
| 80 | + ] |
| 81 | + ) |
| 82 | + ] |
| 83 | + ) |
| 84 | + ]), locals() |
| 85 | + |
| 86 | + |
| 87 | +class TestEnvironmentReport(unittest.TestCase): |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def setUpClass( |
| 91 | + cls, |
| 92 | + launch_service: LaunchService, |
| 93 | + proc_info: launch_testing.tools.process.ActiveProcInfoHandler, |
| 94 | + proc_output: launch_testing.tools.process.ActiveIoHandler, |
| 95 | + rmw_implementation: str, |
| 96 | + ) -> None: |
| 97 | + cls.rmw_implementation = rmw_implementation |
| 98 | + |
| 99 | + @contextlib.contextmanager |
| 100 | + def launch_doctor_command( |
| 101 | + self, |
| 102 | + arguments |
| 103 | + ) -> Generator[launch_testing.tools.process.ProcessProxy, None, None]: |
| 104 | + additional_env = get_rmw_additional_env(rmw_implementation) |
| 105 | + additional_env['PYTHONUNBUFFERED'] = '1' |
| 106 | + doctor_command_action = ExecuteProcess( |
| 107 | + cmd=['ros2', 'doctor', *arguments], |
| 108 | + additional_env=additional_env, |
| 109 | + name='ros2doctor-environment-report', |
| 110 | + output='screen' |
| 111 | + ) |
| 112 | + with launch_testing.tools.launch_process( |
| 113 | + launch_service, doctor_command_action, proc_info, proc_output |
| 114 | + ) as doctor_command: |
| 115 | + yield doctor_command |
| 116 | + |
| 117 | + cls.launch_doctor_command = launch_doctor_command |
| 118 | + |
| 119 | + if rmw_implementation == 'rmw_cyclonedds_cpp': |
| 120 | + cls.expected_line = f'CYCLONEDDS_URI={CYCLONEDDS_XML}' |
| 121 | + elif rmw_implementation == 'rmw_connextdds': |
| 122 | + cls.expected_line = 'RMW_CONNEXT_INITIAL_PEERS=CONNEXTBOO' |
| 123 | + elif rmw_implementation == 'rmw_zenoh_cpp': |
| 124 | + config = os.environ['ZENOH_CONFIG_OVERRIDE'] |
| 125 | + cls.expected_line = f'RUST_LOG=z=error, ZENOH_CONFIG_OVERRIDE={config}, ' |
| 126 | + 'ZENOH_ROUTER_CHECK_ATTEMPTS=10' |
| 127 | + elif rmw_implementation == 'rmw_fastrtps_cpp': |
| 128 | + cls.expected_line = 'FASTDDS_BUILTIN_TRANSPORTS=UDPv6' |
| 129 | + else: |
| 130 | + cls.expected_line = '' |
| 131 | + |
| 132 | + @launch_testing.markers.retry_on_failure(times=5, delay=1) |
| 133 | + def test_environment_report(self) -> None: |
| 134 | + |
| 135 | + for argument in ['-r', '--report']: |
| 136 | + with self.launch_doctor_command( |
| 137 | + arguments=[argument] |
| 138 | + ) as doctor_command: |
| 139 | + assert doctor_command.wait_for_shutdown(timeout=10) |
| 140 | + assert doctor_command.exit_code == launch_testing.asserts.EXIT_OK |
| 141 | + |
| 142 | + # Due to isolation ROS_DOMAIN_ID is unique. |
| 143 | + # ROS_DOMAIN_ID Does not seem to be set for zenoh with EnableRmwIsolation. |
| 144 | + domain_id_line = '' |
| 145 | + if self.rmw_implementation == 'rmw_zenoh_cpp': |
| 146 | + domain_id_line = '' |
| 147 | + else: |
| 148 | + domain_id = os.environ['ROS_DOMAIN_ID'] |
| 149 | + domain_id_line = f', ROS_DOMAIN_ID={domain_id}' |
| 150 | + |
| 151 | + assert launch_testing.tools.expect_output( |
| 152 | + expected_lines=[ |
| 153 | + 'ROS environment variables : ROS_AUTOMATIC_DISCOVERY_RANGE=SUBNET, ' |
| 154 | + f'ROS_DISABLE_LOANED_MESSAGES=0, ROS_DISTRO=rolling{domain_id_line}', |
| 155 | + 'rcutils environment variables : RCUTILS_COLORIZED_OUTPUT=0', |
| 156 | + f'rmw environment variables : {self.expected_line}' |
| 157 | + ], |
| 158 | + text=doctor_command.output |
| 159 | + ) |
0 commit comments