diff --git a/examples/dynamodb/BUILD.bazel b/examples/dynamodb/BUILD.bazel index 001b01f..e1ac86f 100644 --- a/examples/dynamodb/BUILD.bazel +++ b/examples/dynamodb/BUILD.bazel @@ -48,6 +48,7 @@ itest_service( autoassign_port = True, exe = ":bin", health_check = ":health_check", + visibility = ["//visibility:public"], ) go_library( diff --git a/examples/redis/BUILD.bazel b/examples/redis/BUILD.bazel index 6b605d0..b63b555 100644 --- a/examples/redis/BUILD.bazel +++ b/examples/redis/BUILD.bazel @@ -20,3 +20,27 @@ itest_service( "PING", ], ) + +itest_service( + name = "redis_network", + args = [ + "--port", + "$${PORT}", + "--dir", + "$${TMPDIR}", + ], + autoassign_port = True, + exe = "@com_github_redis_redis//:redis", + health_check = "@com_github_redis_redis//:redis_cli", + health_check_args = [ + "-h", + "localhost", + "-p", + "$${PORT}", + "PING", + ], + tags = [ + "requires-network", + ], + visibility = ["//visibility:public"], +) diff --git a/examples/service_group/BUILD.bazel b/examples/service_group/BUILD.bazel new file mode 100644 index 0000000..744371b --- /dev/null +++ b/examples/service_group/BUILD.bazel @@ -0,0 +1,15 @@ +load("@rules_itest//:itest.bzl", "itest_service_group", "port_alias") + +itest_service_group( + name = "service_group", + port_aliases = { + "dynamodb": port_alias("//dynamodb"), + "mysql": port_alias("//mysql:mysql_listening_on_port"), + "redis": port_alias("//redis:redis_network"), + }, + services = [ + "//dynamodb:dynamodb", + "//mysql:mysql_listening_on_port", + "//redis:redis_network", + ], +) diff --git a/itest.bzl b/itest.bzl index 21c3cb2..b24ab3d 100644 --- a/itest.bzl +++ b/itest.bzl @@ -9,11 +9,39 @@ load( _service_test = "service_test", ) +def _to_relative_port(label): + return str(native.package_relative_label(label)) + +def _to_relative_named_port(label, name): + return _to_relative_port(label + "." + name) + def port(label): - return "$${%s}" % (str(native.package_relative_label(label))) + """This function is used to reference the auto assigned port of a service in the `args` or `env` attributes of an `itest_service` or `itest_task`. + + To reference the auto assigned port in the `itest_service_group` use `port_alias` instead + """ + return "$${%s}" % _to_relative_port(label) def named_port(label, name): - return port(label + "." + name) + """This function is used to reference a named port of a service in the `args` or `env` attributes of an `itest_service` or `itest_task`. + + To reference a named port in the `itest_service_group` use `named_port_alias` instead + """ + return port(_to_relative_named_port(label, name)) + +def port_alias(label): + """This function is used to reference the auto assigned port of a service in the `aliases` attribute of an `itest_service_group`. + + To reference the auto assigned port in `itest_service` or `itest_task` use `port` instead + """ + return _to_relative_port(label) + +def named_port_alias(label, name): + """This function is used to reference a named port of a service in the `aliases` attribute of an `itest_service_group`. + + To reference a named port in `itest_service` or `itest_task` use `named_port` instead + """ + return _to_relative_named_port(label, name) def itest_service(name, tags = [], hygienic = True, named_ports = [], **kwargs): if "port" in kwargs: diff --git a/private/itest.bzl b/private/itest.bzl index 02a3498..32712b0 100644 --- a/private/itest.bzl +++ b/private/itest.bzl @@ -253,7 +253,7 @@ _itest_service_attrs = _itest_binary_attrs | { For example, the following Bash command: `PORT=$($GET_ASSIGNED_PORT_BIN @@//label/for:service)`""", ), - "port": attr.label(doc="Internal"), + "port": attr.label(doc = "Internal"), "named_ports": attr.label_keyed_string_dict( doc = """For each element of the list, the service manager will pick a free port and assign it to the service. The port's fully-qualified name is the service's fully-qualified label and the port name, separated by a colon. @@ -375,7 +375,9 @@ _itest_service_group_attrs = _svcinit_attrs | { "port_aliases": attr.string_dict( doc = """Port aliases allow you to 're-export' another service's port as belonging to this service group. This can be used to create abstractions (such as an itest_service combined with an itest_task) but not leak -their implementation through how client code accesses port names.""", +their implementation naming details through how client code accesses port names. + +Use the functions `port_alias` and `named_port_alias` to reference ports from the services to alias ports for""", ), "services": attr.label_list( providers = [_ServiceGroupInfo],