沙箱中的Bazel和py_test-定义输出的任何方式吗? [英] Bazel and py_test in sandbox - any way to define outputs?

查看:213
本文介绍了沙箱中的Bazel和py_test-定义输出的任何方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在多个项目上运行多个py_test()配置.既然有很多,默认的沙箱机制似乎很方便-测试不会互相干扰,并且可以免费并行运行.

I'm running multiple py_test() configurations on number of projects. Since there's a plenty of them, the default sandboxing mechanism seems convenient - tests don't intrude one another, and run in parallel for free.

这是有代价的,但是据我了解,沙箱会导致bazel在临时目录中运行测试.与未定义任何 outs 参数的py_test规则结合使用( https://docs.bazel.build/versions/master/be/python.html ),这很可能意味着测试后不会保留任何生成的文件.

This comes with a cost, though, as to my understanding sandboxing will cause bazel to run the tests in temporary directories. Combined with py_test rule not defining any outs parameter (https://docs.bazel.build/versions/master/be/python.html), this likely means no generated file will be preserved after the test.

我想要实现的是两件事:

What I want to achieve, are two things:

  1. 保留测试的输出,按测试用例划分(我想我可以使用capsys使其工作,并显式写入与测试名称相似的文件名).这里的问题是,该文件最终将位于沙盒目录中,即: /home/user/.cache/bazel/_bazel_user/12342134213421342134/sandbox/linux-sandbox/1/execroot/project_name/bazel-out/k8-fastbuild/bin/test_suite.runfiles/ 并随后将其删除.
  2. 我想获得XML格式的运行测试摘要. Bazel本身会生成JUnit格式的XML文件,这很好,但不幸的是它无法正常工作( https://docs.pytest.org/en/latest/usage.html#creating-junitxml-format-files ),该方法可以正常工作,但同样可以在临时的沙盒目录中生成文件.
  1. Preserve output of the test, split by test case (I think I can get it to work using capsys and explicitly writing to a file named similarly to the test name). The problem here is, the file will end up in a sandboxed directory, i.e.: /home/user/.cache/bazel/_bazel_user/12342134213421342134/sandbox/linux-sandbox/1/execroot/project_name/bazel-out/k8-fastbuild/bin/test_suite.runfiles/ and will be removed afterwards.
  2. I would like to get a run tests summary in an XML format. Bazel by itself generates an XML file in JUnit format, which would be fine, but unfortunately it does not work properly (https://github.com/bazelbuild/bazel/issues/3413). The easiest solution would be to provide a parameter --junitxml=path (https://docs.pytest.org/en/latest/usage.html#creating-junitxml-format-files) which works, but again - generates a file in the temporary, sandboxed directory.

bazel中的其他规则将 outs 定义为它们将生成的文件,即

Other rules in bazel define outs as the files that they will generate, i.e. https://docs.bazel.build/versions/master/be/make-variables.html#predefined_genrule_variables: genrule contains an outs parameter.

所以问题归结为:在bazel中是否有任何方法可以重用(或环绕)py_test规则并定义它将生成的某些输出文件?

So the question boils down to this: is there any way in bazel to reuse (or wrap around) py_test rule and define some output files it will generate?

推荐答案

在对Bazel进行了一些试验之后,我得出结论,没有简单的方法可以扩展py_test来向其中添加输出.从头开始创建自己的规则也很困难.

After a bit of experimenting with Bazel, I came to conclusion there's no easy to way to extend py_test to add outputs to it. It would also be rather difficult to create my own rule from scratch.

但是,事实证明,Bazel中的所有测试都定义了一些可以使用 的环境变量.实际上,有人问过另一个类似的问题,使用它们可以解决问题: bazel-测试运行时的可写归档路径

However, turns out all tests in Bazel define some environment variables that could be used. In fact, another, similar question was asked about this that solved the problem using them: bazel - writable archivable path for test runtime

在测试中,我从Python内部运行pytest,因此很容易以编程方式扩展启动参数:

In my tests I run pytest from within Python, so it is easy to programmatically extend startup arguments:

def _get_log_file_args():
    # Prepare the path to the log file, based on environmental
    # variables defined by Bazel.
    #
    # As per docs, tests should not rely on these variables
    # defined, so the framework will omit logging to file
    # if necessary variables are undefined.
    #   See: https://docs.bazel.build/versions/master/test-encyclopedia.html#initial-conditions
    LOG_DIR_ENV_VARIABLE = "TEST_UNDECLARED_OUTPUTS_DIR"

    log_dir = os.environ.get(LOG_DIR_ENV_VARIABLE)
    if log_dir:
        file_log_path = os.path.join(log_dir, "test_output.log")
        return [f"--log-file={file_log_path}"]

    logger.warning(f"Environment variable '{LOG_DIR_ENV_VARIABLE}' used as the logging directory is not set. "
                    "Logging to file will be disabled.")
    return []  # no file logging

这就是在./bazel-out/darwin-fastbuild/testlogs/<package-name>/<target-name>/test.outputs/outputs.zip处理最终的.zip存档的问题(根据链接的问题).

Then it's a matter of processing the final .zip archive at ./bazel-out/darwin-fastbuild/testlogs/<package-name>/<target-name>/test.outputs/outputs.zip (according to the linked question).

这篇关于沙箱中的Bazel和py_test-定义输出的任何方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆