2020-03-27 03:26:10 +03:00
|
|
|
|
# Copyright (c) 2020 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
|
|
|
|
|
|
|
|
|
|
def _expand_args(ctx, args):
|
2019-04-12 14:10:16 +03:00
|
|
|
|
return " ".join([ctx.expand_location(a, ctx.attr.data).replace("'", "'\\''") for a in args])
|
2019-04-11 18:31:54 +03:00
|
|
|
|
|
|
|
|
|
def _client_server_test_impl(ctx):
|
2019-04-12 14:10:16 +03:00
|
|
|
|
# Construct wrapper to execute the runner, which in turn
|
|
|
|
|
# will start the client and server.
|
|
|
|
|
wrapper = ctx.actions.declare_file(ctx.label.name + "_wrapper.sh")
|
|
|
|
|
ctx.actions.write(
|
|
|
|
|
output = wrapper,
|
|
|
|
|
content = """#!/usr/bin/env bash
|
2019-07-03 10:59:29 +03:00
|
|
|
|
set -eou pipefail
|
2020-04-28 17:06:36 +03:00
|
|
|
|
canonicalize_rlocation() {{
|
|
|
|
|
# Note (MK): This is a fun one: Let’s say $TEST_WORKSPACE is "compatibility"
|
|
|
|
|
# and the argument points to a target from an external workspace, e.g.,
|
|
|
|
|
# @daml-sdk-0.0.0//:daml. Then the short path will point to
|
|
|
|
|
# ../daml-sdk-0.0.0/daml. Putting things together we end up with
|
|
|
|
|
# compatibility/../daml-sdk-0.0.0/daml. On Linux and MacOS this works
|
|
|
|
|
# just fine. However, on windows we need to normalize the path
|
|
|
|
|
# or rlocation will fail to find the path in the manifest file.
|
|
|
|
|
rlocation $(realpath -L -s -m --relative-to=$PWD $TEST_WORKSPACE/$1)
|
|
|
|
|
}}
|
2019-07-03 10:59:29 +03:00
|
|
|
|
|
2020-04-28 17:06:36 +03:00
|
|
|
|
runner=$(canonicalize_rlocation "{runner}")
|
2020-02-26 17:45:35 +03:00
|
|
|
|
runner_args="{runner_args}"
|
2020-04-28 17:06:36 +03:00
|
|
|
|
client=$(canonicalize_rlocation "{client}")
|
|
|
|
|
server=$(canonicalize_rlocation "{server}")
|
2019-08-22 14:32:41 +03:00
|
|
|
|
server_args="{server_args}"
|
|
|
|
|
for file in {server_files}; do
|
2020-04-28 17:06:36 +03:00
|
|
|
|
server_args+=" $(canonicalize_rlocation $file)"
|
2019-08-22 14:32:41 +03:00
|
|
|
|
done
|
2019-07-03 10:59:29 +03:00
|
|
|
|
|
2019-08-22 14:32:41 +03:00
|
|
|
|
client_args="$@"
|
|
|
|
|
if [ -z "$client_args" ]; then
|
|
|
|
|
client_args="{client_args}"
|
|
|
|
|
for file in {client_files}; do
|
2020-04-28 17:06:36 +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-04-28 17:06:36 +03:00
|
|
|
|
$runner $client "$client_args" $server "$server_args" "$runner_args"
|
2019-04-11 18:31:54 +03:00
|
|
|
|
""".format(
|
2020-02-26 17:45:35 +03:00
|
|
|
|
runner = ctx.executable.runner.short_path,
|
|
|
|
|
runner_args = _expand_args(ctx, ctx.attr.runner_args),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
client = ctx.executable.client.short_path,
|
|
|
|
|
client_args = _expand_args(ctx, ctx.attr.client_args),
|
2019-08-22 14:32:41 +03:00
|
|
|
|
client_files = _expand_args(ctx, ctx.attr.client_files),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
server = ctx.executable.server.short_path,
|
|
|
|
|
server_args = _expand_args(ctx, ctx.attr.server_args),
|
2019-08-22 14:32:41 +03:00
|
|
|
|
server_files = _expand_args(ctx, ctx.attr.server_files),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
),
|
|
|
|
|
is_executable = True,
|
|
|
|
|
)
|
2019-04-11 18:31:54 +03:00
|
|
|
|
|
2019-04-12 14:10:16 +03:00
|
|
|
|
runfiles = ctx.runfiles(files = [wrapper], collect_data = True)
|
2020-02-26 17:45:35 +03:00
|
|
|
|
runfiles = runfiles.merge(ctx.attr.runner[DefaultInfo].default_runfiles)
|
2019-04-12 14:10:16 +03:00
|
|
|
|
runfiles = runfiles.merge(ctx.attr.client[DefaultInfo].default_runfiles)
|
|
|
|
|
runfiles = runfiles.merge(ctx.attr.server[DefaultInfo].default_runfiles)
|
2019-04-11 18:31:54 +03:00
|
|
|
|
|
2019-04-12 14:10:16 +03:00
|
|
|
|
return DefaultInfo(
|
|
|
|
|
executable = wrapper,
|
|
|
|
|
files = depset([wrapper]),
|
|
|
|
|
runfiles = runfiles,
|
|
|
|
|
)
|
2019-04-11 18:31:54 +03:00
|
|
|
|
|
|
|
|
|
client_server_test = rule(
|
2019-04-12 14:10:16 +03:00
|
|
|
|
implementation = _client_server_test_impl,
|
|
|
|
|
test = True,
|
|
|
|
|
executable = True,
|
|
|
|
|
attrs = {
|
2020-02-26 17:45:35 +03:00
|
|
|
|
"runner": attr.label(
|
2019-04-12 14:10:16 +03:00
|
|
|
|
cfg = "host",
|
|
|
|
|
allow_single_file = True,
|
|
|
|
|
executable = True,
|
2019-11-07 12:41:54 +03:00
|
|
|
|
default = Label("@//bazel_tools/client_server/runner:runner"),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
),
|
2020-02-26 17:45:35 +03:00
|
|
|
|
"runner_args": attr.string_list(),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
"client": attr.label(
|
|
|
|
|
cfg = "target",
|
|
|
|
|
executable = True,
|
|
|
|
|
),
|
|
|
|
|
"client_args": attr.string_list(),
|
2019-08-22 14:32:41 +03:00
|
|
|
|
"client_files": attr.string_list(),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
"server": attr.label(
|
|
|
|
|
cfg = "target",
|
|
|
|
|
executable = True,
|
|
|
|
|
),
|
|
|
|
|
"server_args": attr.string_list(),
|
2019-08-22 14:32:41 +03:00
|
|
|
|
"server_files": attr.string_list(),
|
2019-04-12 14:10:16 +03:00
|
|
|
|
"data": attr.label_list(allow_files = True),
|
|
|
|
|
},
|
2019-04-11 18:31:54 +03:00
|
|
|
|
)
|
|
|
|
|
"""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.
|
|
|
|
|
|
2019-08-22 14:32:41 +03:00
|
|
|
|
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.)
|
|
|
|
|
Once expended using rlocation, those are simply appended to client
|
|
|
|
|
and server arguments, respectively.
|
|
|
|
|
|
2019-04-11 18:31:54 +03:00
|
|
|
|
Example:
|
|
|
|
|
```bzl
|
|
|
|
|
client_server_test(
|
|
|
|
|
name = "my_test",
|
2020-02-26 17:45:35 +03:00
|
|
|
|
runner_args = [],
|
2019-04-11 18:31:54 +03:00
|
|
|
|
client = ":my_client",
|
|
|
|
|
client_args = ["--extra-argument"],
|
2019-08-22 14:32:41 +03:00
|
|
|
|
client_files = ["$(rootpath :target-for-client)"]
|
2019-04-11 18:31:54 +03:00
|
|
|
|
server = ":my_server",
|
|
|
|
|
server_args = ["--fast"],
|
2019-08-22 14:32:41 +03:00
|
|
|
|
server_files = ["$(rootpath :target-for-client)"]
|
2019-04-11 18:31:54 +03:00
|
|
|
|
)
|
|
|
|
|
```
|
|
|
|
|
"""
|