2022-01-03 19:36:51 +03:00
|
|
|
# Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
2019-04-11 18:31:54 +03:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-07-23 10:46:04 +03:00
|
|
|
load("//bazel_tools/sh:sh.bzl", "sh_inline_test")
|
2019-04-11 18:31:54 +03:00
|
|
|
|
2020-07-23 10:46:04 +03:00
|
|
|
def _escape_args(args):
|
|
|
|
return " ".join([
|
|
|
|
a.replace("'", "'\\''")
|
|
|
|
for a in args
|
|
|
|
])
|
2019-07-03 10:59:29 +03:00
|
|
|
|
2020-07-23 10:46:04 +03:00
|
|
|
def client_server_test(
|
|
|
|
name,
|
2021-08-18 18:23:45 +03:00
|
|
|
runner = "//bazel_tools/client_server/runner_with_port_file",
|
2020-07-23 10:46:04 +03:00
|
|
|
runner_args = [],
|
2022-01-26 20:43:34 +03:00
|
|
|
runner_files = [],
|
|
|
|
runner_files_prefix = "",
|
2020-07-23 10:46:04 +03:00
|
|
|
client = None,
|
|
|
|
client_args = [],
|
|
|
|
client_files = [],
|
|
|
|
server = None,
|
|
|
|
server_args = [],
|
|
|
|
server_files = [],
|
2022-01-20 20:10:11 +03:00
|
|
|
server_files_prefix = "",
|
2020-07-23 10:46:04 +03:00
|
|
|
data = [],
|
|
|
|
**kwargs):
|
|
|
|
"""Create a client-server test.
|
|
|
|
|
|
|
|
The rule takes a client and server executables and their
|
|
|
|
arguments as parameters. The server port is passed via a
|
|
|
|
temporary file, which is passed to the server executable via the
|
|
|
|
"--port-file" parameter. This file is parsed and the port number
|
|
|
|
is passed to the client application via the "--target-port" argument.
|
|
|
|
|
|
|
|
The server process is killed after the client process exits.
|
|
|
|
|
|
|
|
The client and server executables can be any Bazel target that
|
|
|
|
is executable, e.g. scala_binary, sh_binary, etc.
|
|
|
|
|
|
|
|
The client and server files must be valid arguments to rlocation, as
|
|
|
|
can be obtained using $(rootpath ...) or $(rootpaths ...). (See
|
|
|
|
https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.)
|
2021-08-30 10:24:52 +03:00
|
|
|
Once expanded using rlocation, those are simply appended to client
|
2020-07-23 10:46:04 +03:00
|
|
|
and server arguments, respectively.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
```bzl
|
|
|
|
client_server_test(
|
|
|
|
name = "my_test",
|
|
|
|
runner_args = [],
|
|
|
|
client = ":my_client",
|
|
|
|
client_args = ["--extra-argument"],
|
|
|
|
client_files = ["$(rootpath :target-for-client)"]
|
|
|
|
server = ":my_server",
|
|
|
|
server_args = ["--fast"],
|
|
|
|
server_files = ["$(rootpath :target-for-client)"]
|
|
|
|
)
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
sh_inline_test(
|
|
|
|
name = name,
|
|
|
|
# Deduplicate in case any of runner, client, server are identical.
|
|
|
|
data = depset([runner, client, server]).to_list() + data,
|
|
|
|
cmd = """\
|
|
|
|
runner=$$(canonicalize_rlocation $$(get_exe $(rootpaths {runner})))
|
2020-02-26 17:45:35 +03:00
|
|
|
runner_args="{runner_args}"
|
2022-01-26 20:43:34 +03:00
|
|
|
for file in {runner_files}; do
|
|
|
|
runner_args+=" {runner_files_prefix}$$(canonicalize_rlocation $$file)"
|
|
|
|
done
|
2020-07-23 10:46:04 +03:00
|
|
|
client=$$(canonicalize_rlocation $$(get_exe $(rootpaths {client})))
|
|
|
|
server=$$(canonicalize_rlocation $$(get_exe $(rootpaths {server})))
|
2019-08-22 14:32:41 +03:00
|
|
|
server_args="{server_args}"
|
|
|
|
for file in {server_files}; do
|
2022-01-20 20:10:11 +03:00
|
|
|
server_args+=" {server_files_prefix}$$(canonicalize_rlocation $$file)"
|
2019-08-22 14:32:41 +03:00
|
|
|
done
|
2019-07-03 10:59:29 +03:00
|
|
|
|
2020-07-23 10:46:04 +03:00
|
|
|
client_args="$$@"
|
|
|
|
if [ -z "$$client_args" ]; then
|
2019-08-22 14:32:41 +03:00
|
|
|
client_args="{client_args}"
|
|
|
|
for file in {client_files}; do
|
2020-07-23 10:46:04 +03:00
|
|
|
client_args+=" $$(canonicalize_rlocation $$file)"
|
2019-08-22 14:32:41 +03:00
|
|
|
done
|
2019-06-07 13:04:31 +03:00
|
|
|
fi
|
2019-07-03 10:59:29 +03:00
|
|
|
|
2020-07-23 10:46:04 +03:00
|
|
|
$$runner $$client "$$client_args" $$server "$$server_args" "$$runner_args"
|
2019-04-11 18:31:54 +03:00
|
|
|
""".format(
|
2020-07-23 10:46:04 +03:00
|
|
|
runner = runner,
|
|
|
|
runner_args = _escape_args(runner_args),
|
2022-01-26 20:43:34 +03:00
|
|
|
runner_files = _escape_args(runner_files),
|
|
|
|
runner_files_prefix = runner_files_prefix,
|
2020-07-23 10:46:04 +03:00
|
|
|
client = client,
|
|
|
|
client_args = _escape_args(client_args),
|
|
|
|
client_files = _escape_args(client_files),
|
|
|
|
server = server,
|
|
|
|
server_args = _escape_args(server_args),
|
|
|
|
server_files = _escape_args(server_files),
|
2022-01-20 20:10:11 +03:00
|
|
|
server_files_prefix = server_files_prefix,
|
2019-04-12 14:10:16 +03:00
|
|
|
),
|
2020-07-23 10:46:04 +03:00
|
|
|
**kwargs
|
2019-04-12 14:10:16 +03:00
|
|
|
)
|