|
| 1 | +# Copyright 2026 Sony Corporation |
| 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 io |
| 16 | +import os |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from ros2cli.helpers import check_discovery_configuration |
| 20 | + |
| 21 | + |
| 22 | +def test_check_discovery_configuration_off_without_peers(): |
| 23 | + """Test warning is shown when ROS_AUTOMATIC_DISCOVERY_RANGE=OFF without ROS_STATIC_PEERS.""" |
| 24 | + env = { |
| 25 | + 'ROS_AUTOMATIC_DISCOVERY_RANGE': 'OFF', |
| 26 | + } |
| 27 | + # Make sure ROS_STATIC_PEERS is not set |
| 28 | + env_without_peers = {k: v for k, v in os.environ.items() if k != 'ROS_STATIC_PEERS'} |
| 29 | + env_without_peers.update(env) |
| 30 | + |
| 31 | + # Capture stderr |
| 32 | + stderr_capture = io.StringIO() |
| 33 | + with patch.dict(os.environ, env_without_peers, clear=True): |
| 34 | + with patch('sys.stderr', stderr_capture): |
| 35 | + check_discovery_configuration() |
| 36 | + |
| 37 | + output = stderr_capture.getvalue() |
| 38 | + assert 'Warning: ROS_AUTOMATIC_DISCOVERY_RANGE=OFF' in output |
| 39 | + assert 'No discovery mechanism is available' in output |
| 40 | + assert 'ROS_STATIC_PEERS' in output |
| 41 | + assert 'LOCALHOST or SUBNET' in output |
| 42 | + |
| 43 | + |
| 44 | +def test_check_discovery_configuration_off_with_peers(): |
| 45 | + """Test no warning when ROS_AUTOMATIC_DISCOVERY_RANGE=OFF with ROS_STATIC_PEERS set.""" |
| 46 | + env = { |
| 47 | + 'ROS_AUTOMATIC_DISCOVERY_RANGE': 'OFF', |
| 48 | + 'ROS_STATIC_PEERS': '192.168.1.10;192.168.1.11', |
| 49 | + } |
| 50 | + |
| 51 | + # Capture stderr |
| 52 | + stderr_capture = io.StringIO() |
| 53 | + with patch.dict(os.environ, env, clear=True): |
| 54 | + with patch('sys.stderr', stderr_capture): |
| 55 | + check_discovery_configuration() |
| 56 | + |
| 57 | + output = stderr_capture.getvalue() |
| 58 | + assert output == '', 'Expected no warning output' |
| 59 | + |
| 60 | + |
| 61 | +def test_check_discovery_configuration_not_set(): |
| 62 | + """Test no warning when ROS_AUTOMATIC_DISCOVERY_RANGE is not set (default behavior).""" |
| 63 | + # Make sure neither env var is set |
| 64 | + env_clean = { |
| 65 | + k: v for k, v in os.environ.items() |
| 66 | + if k not in ('ROS_AUTOMATIC_DISCOVERY_RANGE', 'ROS_STATIC_PEERS') |
| 67 | + } |
| 68 | + |
| 69 | + # Capture stderr |
| 70 | + stderr_capture = io.StringIO() |
| 71 | + with patch.dict(os.environ, env_clean, clear=True): |
| 72 | + with patch('sys.stderr', stderr_capture): |
| 73 | + check_discovery_configuration() |
| 74 | + |
| 75 | + output = stderr_capture.getvalue() |
| 76 | + assert output == '', 'Expected no warning output' |
0 commit comments