-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #408 from husarion/ros2-lights-integration-tests
Ros2 lights integration tests
- Loading branch information
Showing
15 changed files
with
254 additions
and
13 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
panther_diagnostics/test/integration/system_monitor_node.test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Husarion sp. z o.o. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import unittest | ||
|
||
import launch | ||
import launch_testing | ||
import panther_utils.integration_test_utils as test_utils | ||
import rclpy | ||
import rclpy.qos | ||
from diagnostic_msgs.msg import DiagnosticArray | ||
from launch import LaunchDescription | ||
from launch.substitutions import PathJoinSubstitution | ||
from launch_ros.actions import Node | ||
from launch_ros.substitutions import FindPackageShare | ||
from launch_testing_ros import WaitForTopics | ||
from panther_msgs.msg import LEDAnimation | ||
from panther_msgs.srv import SetLEDAnimation | ||
from sensor_msgs.msg import Image | ||
from std_srvs.srv import SetBool | ||
|
||
|
||
def generate_test_description(): | ||
|
||
led_config_file = ( | ||
PathJoinSubstitution([FindPackageShare("panther_lights"), "config", "led_config.yaml"]), | ||
) | ||
|
||
lights_controller_node = Node( | ||
package="panther_lights", | ||
executable="lights_controller_node", | ||
parameters=[{"led_config_file": led_config_file}], | ||
) | ||
|
||
lights_driver_node = Node( | ||
package="panther_lights", | ||
executable="lights_driver_node", | ||
) | ||
|
||
# Start test after 1s | ||
delay_timer = launch.actions.TimerAction( | ||
period=1.0, actions=[launch_testing.actions.ReadyToTest()] | ||
) | ||
|
||
actions = [lights_controller_node, lights_driver_node, delay_timer] | ||
|
||
context = {} | ||
|
||
return ( | ||
LaunchDescription(actions), | ||
context, | ||
) | ||
|
||
|
||
class TestNodesIntegration(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
rclpy.init() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
rclpy.shutdown() | ||
|
||
def setUp(self): | ||
self._test_node = rclpy.create_node("test_node") | ||
self._led_control_requested = None | ||
|
||
self._led_control_enable_srv = self._test_node.create_service( | ||
srv_type=SetBool, | ||
srv_name="hardware/led_control_enable", | ||
callback=self._led_control_enable_cb, | ||
qos_profile=rclpy.qos.qos_profile_services_default, | ||
) | ||
|
||
self._set_led_animation_client = self._test_node.create_client( | ||
srv_type=SetLEDAnimation, | ||
srv_name="lights/set_animation", | ||
qos_profile=rclpy.qos.qos_profile_services_default, | ||
) | ||
|
||
def tearDown(self): | ||
self._test_node.destroy_node() | ||
|
||
def _led_control_enable_cb(self, request, response): | ||
self._led_control_requested = request.data | ||
response.success = True | ||
response.message = "LED control enabled" | ||
return response | ||
|
||
def test_initialization(self, proc_output): | ||
rclpy.spin_until_future_complete(self._test_node, rclpy.task.Future(), timeout_sec=2.0) | ||
|
||
self.assertTrue(self._led_control_requested) | ||
|
||
# Controller initialization | ||
proc_output.assertWaitFor("[lights_controller]: Loaded default animations.") | ||
proc_output.assertWaitFor("[lights_controller]: Initialized successfully.") | ||
# Driver initialization | ||
proc_output.assertWaitFor("[lights_driver]: Node constructed successfully.") | ||
proc_output.assertWaitFor("[lights_driver]: LED control granted.") | ||
|
||
def _request_error_animation(self): | ||
request = SetLEDAnimation.Request() | ||
request.animation = LEDAnimation(id=LEDAnimation.ERROR) | ||
request.repeating = False | ||
|
||
self._set_led_animation_client.wait_for_service(timeout_sec=1.0) | ||
self._set_led_animation_client.call_async(request) | ||
|
||
def test_msg_publishers(self): | ||
self._request_error_animation() | ||
|
||
topic_list = [ | ||
("lights/channel_1_frame", Image), | ||
("lights/channel_2_frame", Image), | ||
("diagnostics", DiagnosticArray), | ||
] | ||
|
||
with WaitForTopics(topic_list, timeout=5.0) as wait_for_topics: | ||
received_topics_str = ", ".join(wait_for_topics.topics_received()) | ||
print("Received messages from the following topics: [" + received_topics_str + "]") | ||
|
||
def test_msg_subscribers(self): | ||
node_info = test_utils.get_node_info("/lights_driver") | ||
|
||
self.assertTrue("/lights/channel_1_frame" in node_info.subscribers) | ||
self.assertTrue("/lights/channel_2_frame" in node_info.subscribers) | ||
|
||
|
||
@launch_testing.post_shutdown_test() | ||
class TestProcessOutput(unittest.TestCase): | ||
|
||
def test_exit_code(self, proc_info): | ||
# Check that process exits with code 0: no error | ||
launch_testing.asserts.assertExitCodes(proc_info) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Husarion sp. z o.o. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import subprocess | ||
from typing import List | ||
|
||
|
||
class ROSNodeInfo: | ||
""" | ||
Class representing the ROS node info. | ||
""" | ||
|
||
def __init__(self): | ||
self.subscribers: List[str] = [] | ||
self.publishers: List[str] = [] | ||
self.service_servers: List[str] = [] | ||
self.service_clients: List[str] = [] | ||
self.action_servers: List[str] = [] | ||
self.action_clients: List[str] = [] | ||
|
||
|
||
def get_node_info(node_name: str) -> ROSNodeInfo: | ||
""" | ||
Executes the command 'ros2 node info <node_name>' and returns the ROSNodeInfo object. | ||
Args: | ||
node_name (str): The name of the ROS 2 node to get information about. | ||
Returns: | ||
ROSNodeInfo: An object representing a complete node info. | ||
Raises: | ||
RuntimeError: If the command execution fails. | ||
""" | ||
node_info = ROSNodeInfo() | ||
|
||
section_map = { | ||
"Subscribers:": node_info.subscribers, | ||
"Publishers:": node_info.publishers, | ||
"Service Servers:": node_info.service_servers, | ||
"Service Clients:": node_info.service_clients, | ||
"Action Servers:": node_info.action_servers, | ||
"Action Clients:": node_info.action_clients, | ||
} | ||
|
||
try: | ||
# Execute the `ros2 node info` command | ||
result = subprocess.run( | ||
["ros2", "node", "info", node_name], capture_output=True, text=True, check=True | ||
) | ||
|
||
# Parse the output | ||
lines = result.stdout.splitlines() | ||
current_section = None | ||
|
||
for line in lines: | ||
line = line.strip() | ||
if line in section_map: | ||
current_section = section_map[line] | ||
elif line and current_section is not None: | ||
current_section.append(line.split(":")[0].strip()) | ||
else: | ||
current_section = None | ||
|
||
except subprocess.CalledProcessError as e: | ||
raise RuntimeError(f"Error executing command: {e}. stderr: {e.stderr}") from e | ||
|
||
return node_info |