def test_mavlink_router(valid_output_endpoints: Set[Endpoint],
                        valid_master_endpoints: Set[Endpoint]) -> None:
    if not MAVLinkRouter.is_ok():
        warnings.warn("Failed to test MAVLinkRouter service", UserWarning)
        return

    assert AbstractRouter.get_interface(
        "MAVLinkRouter"), "Failed to find interface MAVLinkRouter"

    mavlink_router = MAVLinkRouter()
    assert mavlink_router.name() == "MAVLinkRouter", "Name does not match."
    assert re.search(r"\d+", str(mavlink_router.version())
                     ) is not None, "Version does not follow pattern."

    allowed_output_types = [
        EndpointType.UDPServer,
        EndpointType.UDPClient,
        EndpointType.TCPServer,
        EndpointType.TCPClient,
    ]
    allowed_master_types = [
        EndpointType.UDPServer, EndpointType.Serial, EndpointType.TCPServer
    ]
    run_common_routing_tests(mavlink_router, allowed_output_types,
                             allowed_master_types, valid_output_endpoints,
                             valid_master_endpoints)
def run_common_routing_tests(
    router: AbstractRouter,
    allowed_output_types: List[EndpointType],
    allowed_master_types: List[EndpointType],
    output_endpoints: Set[Endpoint],
    master_endpoints: Set[Endpoint],
) -> None:
    assert router.logdir().exists(
    ), "Default router log directory does not exist."
    router.set_logdir(pathlib.Path("."))

    allowed_output_endpoints = set(
        filter(
            lambda endpoint: endpoint.connection_type in allowed_output_types,
            output_endpoints))

    allowed_master_endpoints = set(
        filter(
            lambda endpoint: endpoint.connection_type in allowed_master_types,
            master_endpoints))

    unallowed_output_endpoints = output_endpoints.difference(
        allowed_output_endpoints)
    unallowed_master_endpoints = master_endpoints.difference(
        allowed_master_endpoints)

    for endpoint in unallowed_output_endpoints:
        with pytest.raises(ValueError):
            router.add_endpoint(endpoint)

    for endpoint in unallowed_master_endpoints:
        with pytest.raises(ValueError):
            router.set_master_endpoint(endpoint)
            router.start()

    def test_endpoint_combinations(master_endpoints: Set[Endpoint],
                                   output_endpoints: List[Endpoint]) -> None:
        for master_endpoint in master_endpoints:
            router.clear_endpoints()

            for output_endpoint in output_endpoints:
                router.add_endpoint(output_endpoint)
            assert set(router.endpoints()) == set(
                output_endpoints), "Endpoint list does not match."

            router.set_master_endpoint(master_endpoint)
            router.start()
            assert router.is_running(
            ), f"{router.name()} is not running after start."
            router.exit()
            while router.is_running():
                pass
            assert not router.is_running(
            ), f"{router.name()} is not stopping after exit."

    types_order = {
        EndpointType.UDPServer: 0,
        EndpointType.UDPClient: 1,
        EndpointType.TCPServer: 2,
        EndpointType.TCPClient: 3,
        EndpointType.Serial: 4,
    }
    sorted_endpoints = sorted(
        allowed_output_endpoints,
        key=lambda e: types_order[e.connection_type])  # type: ignore

    # Test endpoint combinationsin two orders: regular and reversed
    test_endpoint_combinations(allowed_master_endpoints, sorted_endpoints)
    test_endpoint_combinations(allowed_master_endpoints,
                               sorted_endpoints[::-1])
Exemple #3
0
 def available_interfaces() -> List[Type[AbstractRouter]]:
     return AbstractRouter.available_interfaces()
Exemple #4
0
        required=True,
        metavar="endpoint",
        help=
        "List of endpoints that will be used to connect or allow connection.",
    )

    parser.add_argument(
        "--tool",
        dest="tool",
        type=str,
        default=interfaces[0].name(),
        choices=[interface.name() for interface in interfaces],
        help=
        "Selected the desired tool to use, the default will be the first one available.",
    )

    args = parser.parse_args()

    tool = AbstractRouter.get_interface(args.tool)()
    print(f"Starting {tool.name()} version {tool.version()}.")

    manager.use(tool)
    manager.add_endpoints(args.output)
    manager.set_master_endpoint(args.master)

    print(f"Command: {manager.command_line()}")
    manager.start()
    while manager.is_running():
        time.sleep(1)
    print("Done.")
Exemple #5
0
 def possible_interfaces() -> List[str]:
     return AbstractRouter.possible_interfaces()