From a3fb87ead35cb7750c397c686d114b3980035596 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 9 Jun 2023 17:54:16 +0200 Subject: [PATCH] add API battery test --- .../test/test_api_listener.py | 99 +++++++++++++++++++ src/api_communication/test/test_copyright.py | 23 ----- src/api_communication/test/test_flake8.py | 25 ----- src/api_communication/test/test_pep257.py | 23 ----- 4 files changed, 99 insertions(+), 71 deletions(-) create mode 100644 src/api_communication/test/test_api_listener.py delete mode 100644 src/api_communication/test/test_copyright.py delete mode 100644 src/api_communication/test/test_flake8.py delete mode 100644 src/api_communication/test/test_pep257.py diff --git a/src/api_communication/test/test_api_listener.py b/src/api_communication/test/test_api_listener.py new file mode 100644 index 00000000..54f0184d --- /dev/null +++ b/src/api_communication/test/test_api_listener.py @@ -0,0 +1,99 @@ +import os +import sys +import unittest +import time + +import launch +import launch_ros +import launch_ros.actions +import launch_testing.actions +import pytest +import rclpy + +from px4_msgs.msg import BatteryStatus +from drone_services.msg import FailsafeMsg + +@pytest.mark.rostest +def generate_test_description(): + file_path = os.path.dirname(__file__) + api_listener_node = launch_ros.actions.Node( + executable=sys.executable, + arguments=[os.path.join( + file_path, '..', 'api_communication', 'api_listener.py')], + additional_env={'PYTHONUNBUFFERED': '1'} + ) + failsafe_node = launch_ros.actions.Node( + package='failsafe', executable='failsafe') + camera_node = launch_ros.actions.Node( + package='camera', executable='camera_controller') + position_changer_node = launch_ros.actions.Node( + package='drone_controls', executable='position_changer') + px4_controller_node = launch_ros.actions.Node( + package='px4_connection', executable='px4_controller') + drone_status_node = launch_ros.actions.Node( + package='drone_status', executable='drone_status') + + return ( + launch.LaunchDescription([ + api_listener_node, + failsafe_node, + camera_node, + position_changer_node, + px4_controller_node, + drone_status_node, + launch_testing.actions.ReadyToTest(), + ]), + { + 'api_listener_node': api_listener_node, + 'failsafe_node': failsafe_node, + 'camera_node': camera_node, + 'position_changer_node': position_changer_node, + 'px4_controller_node': px4_controller_node, + 'drone_status_node': drone_status_node + } + ) + + +class ApiListenerTest(unittest.TestCase): + + @classmethod + def setUpClass(cls): + rclpy.init() + + @classmethod + def tearDownClass(cls): + rclpy.shutdown() + + def setUp(self): + self.node = rclpy.create_node('test_api_listener') + self.published_battery_status = False + self.received_failsafe_callback = False + + def tearDown(self): + self.node.destroy_node() + + def failsafe_callback(self,msg): + self.assertTrue(msg.enabled, "Failsafe was not enabled!") + self.assertTrue("Battery level too low! Failsafe enabled to prevent damage to battery" in msg.msg, "Failsafe message was not correct!") + self.received_failsafe_callback = True + + def test_api_listener_battery(self, api_listener_node, proc_output): + battery_publisher = self.node.create_publisher(BatteryStatus, '/fmu/out/battery_status') + failsafe_subscriber = self.node.create_subscription(FailsafeMsg, '/drone/failsafe', self.failsafe_callback, 10) + + end_time = time.time() + 10.0 + + try: + while time.time() < end_time: + rclpy.spin_once(self.node, timeout_sec=0.1) + if not self.published_battery_status: + self.published_battery_status = True + msg = BatteryStatus() + msg.remaining = 0.10 + battery_publisher.publish(msg) + if self.received_failsafe_callback: + break + self.assertTrue(self.received_failsafe_callback, "Failsafe was not enabled!") + finally: + self.node.destroy_subscription(failsafe_subscriber) + self.node.destroy_publisher(battery_publisher) diff --git a/src/api_communication/test/test_copyright.py b/src/api_communication/test/test_copyright.py deleted file mode 100644 index cc8ff03f..00000000 --- a/src/api_communication/test/test_copyright.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2015 Open Source Robotics Foundation, Inc. -# -# 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. - -from ament_copyright.main import main -import pytest - - -@pytest.mark.copyright -@pytest.mark.linter -def test_copyright(): - rc = main(argv=['.', 'test']) - assert rc == 0, 'Found errors' diff --git a/src/api_communication/test/test_flake8.py b/src/api_communication/test/test_flake8.py deleted file mode 100644 index 27ee1078..00000000 --- a/src/api_communication/test/test_flake8.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2017 Open Source Robotics Foundation, Inc. -# -# 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. - -from ament_flake8.main import main_with_errors -import pytest - - -@pytest.mark.flake8 -@pytest.mark.linter -def test_flake8(): - rc, errors = main_with_errors(argv=[]) - assert rc == 0, \ - 'Found %d code style errors / warnings:\n' % len(errors) + \ - '\n'.join(errors) diff --git a/src/api_communication/test/test_pep257.py b/src/api_communication/test/test_pep257.py deleted file mode 100644 index b234a384..00000000 --- a/src/api_communication/test/test_pep257.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2015 Open Source Robotics Foundation, Inc. -# -# 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. - -from ament_pep257.main import main -import pytest - - -@pytest.mark.linter -@pytest.mark.pep257 -def test_pep257(): - rc = main(argv=['.', 'test']) - assert rc == 0, 'Found code style errors / warnings'