Esempio n. 1
0
def test_generate_policy_services():
    with tempfile.TemporaryDirectory() as tmpdir:
        # Create a test-specific context so that generate_policy can still init
        context = rclpy.Context()
        rclpy.init(context=context)
        node = rclpy.create_node('test_generate_policy_services_node',
                                 context=context)

        try:
            # Create a server and client
            node.create_client(Trigger, 'test_generate_policy_services_client')
            node.create_service(Trigger,
                                'test_generate_policy_services_server',
                                lambda request, response: response)

            # Generate the policy for the running node
            assert cli.main(argv=[
                'security', 'generate_policy',
                os.path.join(tmpdir, 'test-policy.xml')
            ]) == 0
        finally:
            node.destroy_node()
            rclpy.shutdown(context=context)

        # Load the policy and pull out allowed replies and requests
        policy = load_policy(os.path.join(tmpdir, 'test-policy.xml'))
        profile = policy.find(
            path=
            'profiles/profile[@ns="/"][@node="test_generate_policy_services_node"]'
        )
        assert profile is not None
        service_reply_allowed = profile.find(path='services[@reply="ALLOW"]')
        assert service_reply_allowed is not None
        service_request_allowed = profile.find(
            path='services[@request="ALLOW"]')
        assert service_request_allowed is not None

        # Verify that the allowed replies include service_server and not service_client
        services = service_reply_allowed.findall('service')
        assert len([
            s for s in services
            if s.text == 'test_generate_policy_services_server'
        ]) == 1
        assert len([
            s for s in services
            if s.text == 'test_generate_policy_services_client'
        ]) == 0

        # Verify that the allowed requests include service_client and not service_server
        services = service_request_allowed.findall('service')
        assert len([
            s for s in services
            if s.text == 'test_generate_policy_services_client'
        ]) == 1
        assert len([
            s for s in services
            if s.text == 'test_generate_policy_services_server'
        ]) == 0
Esempio n. 2
0
def test_generate_policy_topics():
    with tempfile.TemporaryDirectory() as tmpdir:
        # Create a test-specific context so that generate_policy can still init
        context = rclpy.Context()
        rclpy.init(context=context)
        node = rclpy.create_node('test_generate_policy_topics_node',
                                 context=context)

        try:
            # Create a publisher and subscription
            node.create_publisher(Strings, 'test_generate_policy_topics_pub',
                                  1)
            node.create_subscription(Strings,
                                     'test_generate_policy_topics_sub',
                                     lambda msg: None, 1)

            # Generate the policy for the running node
            assert cli.main(argv=[
                'security', 'generate_policy',
                os.path.join(tmpdir, 'test-policy.xml')
            ]) == 0
        finally:
            node.destroy_node()
            rclpy.shutdown(context=context)

        # Load the policy and pull out the allowed publications and subscriptions
        policy = load_policy(os.path.join(tmpdir, 'test-policy.xml'))
        profile = policy.find(
            path=
            'profiles/profile[@ns="/"][@node="test_generate_policy_topics_node"]'
        )
        assert profile is not None
        topics_publish_allowed = profile.find(path='topics[@publish="ALLOW"]')
        assert topics_publish_allowed is not None
        topics_subscribe_allowed = profile.find(
            path='topics[@subscribe="ALLOW"]')
        assert topics_subscribe_allowed is not None

        # Verify that the allowed publications include topic_pub and not topic_sub
        topics = topics_publish_allowed.findall('topic')
        assert len([
            t for t in topics if t.text == 'test_generate_policy_topics_pub'
        ]) == 1
        assert len([
            t for t in topics if t.text == 'test_generate_policy_topics_sub'
        ]) == 0

        # Verify that the allowed subscriptions include topic_sub and not topic_pub
        topics = topics_subscribe_allowed.findall('topic')
        assert len([
            t for t in topics if t.text == 'test_generate_policy_topics_sub'
        ]) == 1
        assert len([
            t for t in topics if t.text == 'test_generate_policy_topics_pub'
        ]) == 0
Esempio n. 3
0
    def setUpClass(cls):
        cls.asd = []
        cls.rcl_ctx = rclpy.Context()
        rclpy.init(context=cls.rcl_ctx)
        cls.rcl_executor = rclpy.executors.SingleThreadedExecutor(
            context=cls.rcl_ctx)

        cls.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(cls.loop)
        cls.client = TestClient(app)
        cls.client.__enter__()

        cls.node = rclpy.node.Node("test_node", context=cls.rcl_ctx)
Esempio n. 4
0
    def __init__(self, topic_tuples, timeout=5.0):
        self.topic_tuples = topic_tuples
        self.timeout = timeout
        self.__ros_context = rclpy.Context()
        rclpy.init(context=self.__ros_context)
        self.__ros_executor = SingleThreadedExecutor(
            context=self.__ros_context)

        self._prepare_ros_node()

        # Start spinning
        self.__running = True
        self.__ros_spin_thread = Thread(target=self._spin_function)
        self.__ros_spin_thread.start()
Esempio n. 5
0
    def start(self):
        """Start ROS adapter."""
        if self.__is_running:
            raise RuntimeError(
                'Cannot start a ROS adapter that is already running')
        self.__ros_context = rclpy.Context()
        rclpy.init(args=self.__argv, context=self.__ros_context)
        self.__ros_node = rclpy.create_node('launch_ros_{}'.format(
            os.getpid()),
                                            context=self.__ros_context)
        self.__ros_executor = SingleThreadedExecutor(
            context=self.__ros_context)
        self.__is_running = True

        self.__ros_executor_thread = threading.Thread(target=self._run)
        self.__ros_executor_thread.start()
Esempio n. 6
0
    def setUp(self):
        self.context = rclpy.Context()
        rclpy.init(context=self.context)
        self.node = rclpy.create_node(
            'test_joy_twist_node',
            context=self.context,
            allow_undeclared_parameters=True,
            automatically_declare_parameters_from_overrides=True)
        self.message_pump = launch_testing_ros.MessagePump(
            self.node, context=self.context)
        self.pub = self.node.create_publisher(sensor_msgs.msg.Joy, 'joy', 1)
        self.sub = self.node.create_subscription(geometry_msgs.msg.Twist,
                                                 'cmd_vel', self.callback, 1)
        self.message_pump.start()

        self.expect_cmd_vel = {
            'angular': {
                'x':
                self.node.get_parameter('expect_cmd_vel.angular.x').value
                or 0.0,
                'y':
                self.node.get_parameter('expect_cmd_vel.angular.y').value
                or 0.0,
                'z':
                self.node.get_parameter('expect_cmd_vel.angular.z').value
                or 0.0,
            },
            'linear': {
                'x':
                self.node.get_parameter('expect_cmd_vel.linear.x').value
                or 0.0,
                'y':
                self.node.get_parameter('expect_cmd_vel.linear.y').value
                or 0.0,
                'z':
                self.node.get_parameter('expect_cmd_vel.linear.z').value
                or 0.0,
            },
        }
        self.joy_msg = {
            'axes': self.node.get_parameter('publish_joy.axes').value or [],
            'buttons': self.node.get_parameter('publish_joy.buttons').value
            or [],
        }
        self.received_cmd_vel = None
Esempio n. 7
0
    def setUpClass(cls):

        cls.context = rclpy.Context()
        rclpy.init(context=cls.context)

        cls.node = rclpy.create_node(
            'node', namespace='ns', context=cls.context)

        # Aim at emulating a 'wait_for_msg'
        cls._subscription = cls.node.create_subscription(
            Twist, 'cmd_vel_out', cls._cb, 1)
        cls._msg = None

        cls.executor = MultiThreadedExecutor(
            context=cls.context, num_threads=2)
        cls.executor.add_node(cls.node)

        cls._publishers = RatePublishers(cls.context)
        cls._vel1 = cls._publishers.add_topic('vel_1', Twist)
        cls._vel2 = cls._publishers.add_topic('vel_2', Twist)
        cls._vel3 = cls._publishers.add_topic('vel_3', Twist)

        cls._lock1 = cls._publishers.add_topic('lock_1', Bool)
        cls._lock2 = cls._publishers.add_topic('lock_2', Bool)

        cls.executor.add_node(cls._vel1)
        cls.executor.add_node(cls._vel2)
        cls.executor.add_node(cls._vel3)
        cls.executor.add_node(cls._lock1)
        cls.executor.add_node(cls._lock2)

        cls._timeout_manager = TimeoutManager()
        cls._timeout_manager.add(cls._publishers)
        cls._timeout_manager.spin_thread()

        cls.exec_thread = threading.Thread(target=cls.executor.spin)
        cls.exec_thread.start()
Esempio n. 8
0
 def setUpClass(cls):
     cls.rcl_ctx = rclpy.Context()
     rclpy.init(context=cls.rcl_ctx)
     cls.node = rclpy.node.Node("test_node", context=cls.rcl_ctx)