-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move test_utils module from demos repo #1955
Merged
+215
−57
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63168b5
Make a valid urdf but move it to ros2_control_test_assets
christophfroehlich 98cd242
Add test_utils from ros2_control_demo repo
christophfroehlich d5d3ae7
Update ros2_control_node tests to use test_utils
christophfroehlich 0dce82a
Add docstrings
christophfroehlich 0a6ee0d
Add release_notes
christophfroehlich 0993099
Remove xacro and load URDF directly
christophfroehlich 65eec39
Remove wrong dependency
christophfroehlich 18f8371
Add missing dependency
christophfroehlich 246114c
Cleanup double dependency
christophfroehlich df209eb
Use pytest again, does coverage work?
christophfroehlich 7bd97e2
Merge branch 'master' into add/test_utils
saikishor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,129 @@ | ||
# Copyright (c) 2024 AIT - Austrian Institute of Technology GmbH | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# | ||
# * Neither the name of the {copyright_holder} nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
# Author: Christoph Froehlich | ||
|
||
import time | ||
|
||
from launch_testing_ros import WaitForTopics | ||
from sensor_msgs.msg import JointState | ||
from controller_manager.controller_manager_services import list_controllers | ||
|
||
|
||
def check_node_running(node, node_name, timeout=5.0): | ||
|
||
start = time.time() | ||
found = False | ||
while time.time() - start < timeout and not found: | ||
found = node_name in node.get_node_names() | ||
time.sleep(0.1) | ||
assert found, f"{node_name} not found!" | ||
|
||
|
||
def check_controllers_running(node, cnames, namespace="", state="active"): | ||
""" | ||
Check if the specified controllers are running on the given node. | ||
|
||
Args: | ||
node (Node): The ROS2 node instance to check for controllers. | ||
cnames (list of str): List of controller names to check. | ||
namespace (str, optional): The namespace in which to look for controllers. Defaults to "". | ||
state (str, optional): The desired state of the controllers. Defaults to "active". | ||
|
||
Raises: | ||
AssertionError: If any of the specified controllers are not found or not in the desired state within the timeout period. | ||
""" | ||
|
||
# wait for controller to be loaded before we call the CM services | ||
found = {cname: False for cname in cnames} # Define 'found' as a dictionary | ||
start = time.time() | ||
# namespace is either "/" (empty) or "/ns" if set | ||
if namespace: | ||
namespace_api = namespace | ||
if not namespace_api.startswith("/"): | ||
namespace_api = "/" + namespace_api | ||
if namespace.endswith("/"): | ||
namespace_api = namespace_api[:-1] | ||
else: | ||
namespace_api = "/" | ||
|
||
while time.time() - start < 10.0 and not all(found.values()): | ||
node_names_namespaces = node.get_node_names_and_namespaces() | ||
for cname in cnames: | ||
if any(name == cname and ns == namespace_api for name, ns in node_names_namespaces): | ||
found[cname] = True | ||
time.sleep(0.1) | ||
assert all( | ||
found.values() | ||
), f"Controller node(s) not found: {', '.join(['ns: ' + namespace_api + ', ctrl:' + cname for cname, is_found in found.items() if not is_found])}, but seeing {node.get_node_names_and_namespaces()}" | ||
|
||
found = {cname: False for cname in cnames} # Define 'found' as a dictionary | ||
start = time.time() | ||
# namespace is either "/" (empty) or "/ns" if set | ||
if not namespace: | ||
cm = "controller_manager" | ||
else: | ||
if namespace.endswith("/"): | ||
cm = namespace + "controller_manager" | ||
else: | ||
cm = namespace + "/controller_manager" | ||
while time.time() - start < 10.0 and not all(found.values()): | ||
controllers = list_controllers(node, cm, 5.0).controller | ||
assert controllers, "No controllers found!" | ||
for c in controllers: | ||
for cname in cnames: | ||
if c.name == cname and c.state == state: | ||
found[cname] = True | ||
break | ||
time.sleep(0.1) | ||
|
||
assert all( | ||
found.values() | ||
), f"Controller(s) not found or not {state}: {', '.join([cname for cname, is_found in found.items() if not is_found])}" | ||
|
||
|
||
def check_if_js_published(topic, joint_names): | ||
""" | ||
Check if a JointState message is published on a given topic with the expected joint names. | ||
|
||
Args: | ||
topic (str): The name of the topic to check. | ||
joint_names (list of str): The expected joint names in the JointState message. | ||
|
||
Raises: | ||
AssertionError: If the topic is not found, the number of joints in the message is incorrect, | ||
or the joint names do not match the expected names. | ||
""" | ||
wait_for_topics = WaitForTopics([(topic, JointState)], timeout=20.0) | ||
assert wait_for_topics.wait(), f"Topic '{topic}' not found!" | ||
msgs = wait_for_topics.received_messages(topic) | ||
msg = msgs[0] | ||
assert len(msg.name) == len(joint_names), "Wrong number of joints in message" | ||
# use a set to compare the joint names, as the order might be different | ||
assert set(msg.name) == set(joint_names), "Wrong joint names" | ||
wait_for_topics.shutdown() |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't that the functionality that was causing trouble for us in the spawner when waiting for the controller manager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, I can't remember. I added this some time ago in the demo tests, and it seemed to be useful back then ;) maybe the list_controllers service is now more robust and we could skip this node check in advance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Atleast in the ros2_control_demos, we haven't seen flaky tests due to this right?. We can remove it, if this is not interesting for us to have consistency or we can get back to it when we see some flakiness. Both are fine for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no it didn't make any problems recently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll approve it as I don't have a strong opinion on this