• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2023 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Launch a pw_target_runner client that sends a test request."""
16
17import argparse
18import subprocess
19import sys
20
21SERVER_RUNNER_CMD = 'bazelisk run @pigweed//targets/rp2040/py:unit_test_server'
22
23try:
24    from rp2040_utils import unit_test_server
25except ImportError:
26    # Load from this directory if rp2040_utils is not available.
27    import unit_test_server  # type: ignore
28
29# If the script is being run through Bazel, our client is provided at a well
30# known location in its runfiles.
31try:
32    from python.runfiles import runfiles  # type: ignore
33
34    r = runfiles.Create()
35    _TARGET_CLIENT_COMMAND = r.Rlocation(
36        'pigweed/pw_target_runner/go/cmd/client_/client'
37    )
38except ImportError:
39    _TARGET_CLIENT_COMMAND = 'pw_target_runner_client'
40
41
42def parse_args():
43    """Parses command-line arguments."""
44
45    parser = argparse.ArgumentParser(description=__doc__)
46    parser.add_argument('binary', help='The target test binary to run')
47    parser.add_argument(
48        '--server-port',
49        type=int,
50        default=unit_test_server.DEFAULT_PORT,
51        help='Port the test server is located on',
52    )
53
54    return parser.parse_args()
55
56
57def launch_client(binary: str, server_port: int) -> int:
58    """Sends a test request to the specified server port."""
59    cmd = (
60        _TARGET_CLIENT_COMMAND,
61        '-binary',
62        binary,
63        '-port',
64        str(server_port),
65        '-server_suggestion',
66        SERVER_RUNNER_CMD,
67    )
68    return subprocess.call(cmd)
69
70
71def main() -> int:
72    """Launch a test by sending a request to a pw_target_runner_server."""
73    args = parse_args()
74    return launch_client(args.binary, args.server_port)
75
76
77if __name__ == '__main__':
78    sys.exit(main())
79