1# Copyright 2024 The gRPC Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14""" 15Houses py_grpc_logging_threshold_test. 16""" 17 18_COPIED_MAIN_SUFFIX = ".logging_threshold.main" 19 20def py_grpc_logging_threshold_test( 21 name, 22 srcs, 23 main = None, 24 deps = None, 25 data = None, 26 **kwargs): 27 """Runs a Python unit test and checks amount of logging against a threshold. 28 29 Args: 30 name: The name of the test. 31 srcs: The source files. 32 main: The main file of the test. 33 deps: The dependencies of the test. 34 data: The data dependencies of the test. 35 **kwargs: Any other test arguments. 36 """ 37 if main == None: 38 if len(srcs) != 1: 39 fail("When main is not provided, srcs must be of size 1.") 40 main = srcs[0] 41 deps = [] if deps == None else deps 42 data = [] if data == None else data 43 44 lib_name = name + ".logging_threshold.lib" 45 native.py_library( 46 name = lib_name, 47 srcs = srcs, 48 ) 49 augmented_deps = deps + [ 50 ":{}".format(lib_name), 51 ] 52 53 # The main file needs to be in the same package as the test file. 54 copied_main_name = name + _COPIED_MAIN_SUFFIX 55 copied_main_filename = copied_main_name + ".py" 56 native.genrule( 57 name = copied_main_name, 58 srcs = ["//bazel:_logging_threshold_test_main.py"], 59 outs = [copied_main_filename], 60 cmd = "cp $< $@", 61 ) 62 63 native.py_test( 64 name = name + ".logging_threshold", 65 args = ["$(location //bazel:_single_module_tester)", name], 66 data = data + ["//bazel:_single_module_tester"], 67 deps = augmented_deps, 68 srcs = [copied_main_filename], 69 main = copied_main_filename, 70 python_version = "PY3", 71 flaky = False, 72 **kwargs 73 ) 74