1*c2e18aaaSAndroid Build Coastguard Worker# Atest Bazel Mode 2*c2e18aaaSAndroid Build Coastguard WorkerAtest is a command line tool that allows users to run Android tests locally 3*c2e18aaaSAndroid Build Coastguard Workerwithout requiring knowledge of Trade Federation test harness command line 4*c2e18aaaSAndroid Build Coastguard Workeroptions. It wraps the logic and calls Trade Federation under the hood. This is 5*c2e18aaaSAndroid Build Coastguard Workerwhat we call Atest Standard Mode in this document. 6*c2e18aaaSAndroid Build Coastguard Worker 7*c2e18aaaSAndroid Build Coastguard WorkerAtest Bazel Mode creates a synthetic Bazel workspace and executes tests using 8*c2e18aaaSAndroid Build Coastguard WorkerBazel instead of calling Trade Federation directly. This mode opens up Bazel 9*c2e18aaaSAndroid Build Coastguard Workerfeatures such as parallelized execution, caching, and remote execution. 10*c2e18aaaSAndroid Build Coastguard WorkerCurrently it is able to run all host unit tests only. Capability to run tests 11*c2e18aaaSAndroid Build Coastguard Workerthat requires a device is still work in progress. 12*c2e18aaaSAndroid Build Coastguard Worker 13*c2e18aaaSAndroid Build Coastguard Worker##### Table of Contents 14*c2e18aaaSAndroid Build Coastguard Worker1. [Basic Usage](#basic-usage) 15*c2e18aaaSAndroid Build Coastguard Worker2. [Advanced Usage](#advanced-usage) 16*c2e18aaaSAndroid Build Coastguard Worker3. [How It Works](#how-it-works) 17*c2e18aaaSAndroid Build Coastguard Worker4. [Difference from Atest Standard Mode](#difference-from-atest-standard-mode) 18*c2e18aaaSAndroid Build Coastguard Worker5. [Frequently Asked Questions](#faq) 19*c2e18aaaSAndroid Build Coastguard Worker 20*c2e18aaaSAndroid Build Coastguard Worker## <a name="basic-usage">Basic Usage</a> 21*c2e18aaaSAndroid Build Coastguard Worker 22*c2e18aaaSAndroid Build Coastguard WorkerAtest Bazel Mode commands take the following form: 23*c2e18aaaSAndroid Build Coastguard Worker 24*c2e18aaaSAndroid Build Coastguard Worker>```$ atest --bazel-mode --host HelloWorldHostTest``` 25*c2e18aaaSAndroid Build Coastguard Worker<p>Note: "--host" is needed to run the test completely on the host without a device. 26*c2e18aaaSAndroid Build Coastguard Worker 27*c2e18aaaSAndroid Build Coastguard WorkerTo run multiple tests, separate test references with spaces. For example: 28*c2e18aaaSAndroid Build Coastguard Worker 29*c2e18aaaSAndroid Build Coastguard Worker>```$ atest --bazel-mode --host HelloWorldHostTest fastdeploy_test aapt2_tests``` 30*c2e18aaaSAndroid Build Coastguard Worker 31*c2e18aaaSAndroid Build Coastguard WorkerTo run all host unit tests from the current directory: 32*c2e18aaaSAndroid Build Coastguard Worker 33*c2e18aaaSAndroid Build Coastguard Worker>```$ atest --bazel-mode --host --host-unit-test-only``` 34*c2e18aaaSAndroid Build Coastguard Worker 35*c2e18aaaSAndroid Build Coastguard Worker## <a name="advanced-usage">Advanced Usage</a> 36*c2e18aaaSAndroid Build Coastguard Worker 37*c2e18aaaSAndroid Build Coastguard WorkerUse `--bazel-arg` to forward arguments to Bazel. For example, the following 38*c2e18aaaSAndroid Build Coastguard Workercommand increases the test timeout: 39*c2e18aaaSAndroid Build Coastguard Worker 40*c2e18aaaSAndroid Build Coastguard Worker>```$ atest --bazel-mode --host CtsNNAPITestCases --bazel-arg=--test_timeout=600``` 41*c2e18aaaSAndroid Build Coastguard Worker 42*c2e18aaaSAndroid Build Coastguard Worker## <a name="how-it-works">How It Works</a> 43*c2e18aaaSAndroid Build Coastguard WorkerBazel needs a Bazel workspace to execute tests. 44*c2e18aaaSAndroid Build Coastguard WorkerIn Atest Bazel Mode, we construct a synthetic workspace using module-info.json. 45*c2e18aaaSAndroid Build Coastguard WorkerThe workspace contains required directory structure, symlinks and Bazel BUILD 46*c2e18aaaSAndroid Build Coastguard Workerfiles to correctly invoke ```bazel test``` command. The Bazel BUILD files are 47*c2e18aaaSAndroid Build Coastguard Workerwritten with customized Bazel rules. An example Build file is as follows: 48*c2e18aaaSAndroid Build Coastguard Worker 49*c2e18aaaSAndroid Build Coastguard Worker``` 50*c2e18aaaSAndroid Build Coastguard Workerpackage(default_visibility = ["//visibility:public"]) 51*c2e18aaaSAndroid Build Coastguard Worker 52*c2e18aaaSAndroid Build Coastguard Workerload("//bazel/rules:soong_prebuilt.bzl", "soong_prebuilt") 53*c2e18aaaSAndroid Build Coastguard Workerload("//bazel/rules:tradefed_test.bzl", "tradefed_deviceless_test") 54*c2e18aaaSAndroid Build Coastguard Worker 55*c2e18aaaSAndroid Build Coastguard Workertradefed_deviceless_test( 56*c2e18aaaSAndroid Build Coastguard Worker name = "HelloWorldHostTest_host", 57*c2e18aaaSAndroid Build Coastguard Worker test = "//platform_testing/tests/example/jarhosttest:HelloWorldHostTest", 58*c2e18aaaSAndroid Build Coastguard Worker) 59*c2e18aaaSAndroid Build Coastguard Worker 60*c2e18aaaSAndroid Build Coastguard Workersoong_prebuilt( 61*c2e18aaaSAndroid Build Coastguard Worker name = "HelloWorldHostTest", 62*c2e18aaaSAndroid Build Coastguard Worker module_name = "HelloWorldHostTest", 63*c2e18aaaSAndroid Build Coastguard Worker files = select({ 64*c2e18aaaSAndroid Build Coastguard Worker "//bazel/rules:host": glob(["HelloWorldHostTest/host/**/*"]), 65*c2e18aaaSAndroid Build Coastguard Worker }), 66*c2e18aaaSAndroid Build Coastguard Worker) 67*c2e18aaaSAndroid Build Coastguard Worker``` 68*c2e18aaaSAndroid Build Coastguard Worker 69*c2e18aaaSAndroid Build Coastguard WorkerAtest bazel Mode will create the Bazel workspace on first run, or upon detecting 70*c2e18aaaSAndroid Build Coastguard Workera change to module-info.json. 71*c2e18aaaSAndroid Build Coastguard Worker 72*c2e18aaaSAndroid Build Coastguard WorkerIt will then use Bazel query to find out dependencies for the build step. 73*c2e18aaaSAndroid Build Coastguard Worker 74*c2e18aaaSAndroid Build Coastguard WorkerIn the build step, it will use Soong to build those dependencies returned by 75*c2e18aaaSAndroid Build Coastguard WorkerBazel query. 76*c2e18aaaSAndroid Build Coastguard Worker 77*c2e18aaaSAndroid Build Coastguard WorkerAt last, ```bazel test``` command is executed for the test targets. 78*c2e18aaaSAndroid Build Coastguard Worker 79*c2e18aaaSAndroid Build Coastguard Worker## <a name="difference-from-atest-standard-mode">Difference from Atest Standard Mode</a> 80*c2e18aaaSAndroid Build Coastguard Worker 81*c2e18aaaSAndroid Build Coastguard WorkerHere is a list of major differences from the Atest Standard Mode: 82*c2e18aaaSAndroid Build Coastguard Worker* In Atest Standard Mode, user can view detailed test case result in the 83*c2e18aaaSAndroid Build Coastguard Workerterminal, while in Bazel Mode only test target result is showing. For test case 84*c2e18aaaSAndroid Build Coastguard Workerdetail, user would need to look at test logs. The reason Bazel Mode only shows 85*c2e18aaaSAndroid Build Coastguard Workerthe summary result is that atest invokes Bazel command with default parameters. 86*c2e18aaaSAndroid Build Coastguard WorkerBazel command option "--test_output" is defaulted to be "summary". User has the 87*c2e18aaaSAndroid Build Coastguard Workeroption to view "all" output when we later implement command option passing from 88*c2e18aaaSAndroid Build Coastguard WorkerAtest to Bazel. 89*c2e18aaaSAndroid Build Coastguard WorkerMore details about Bazel [--test_output flag](https://docs.bazel.build/versions/main/command-line-reference.html#flag--test_output) 90*c2e18aaaSAndroid Build Coastguard Worker* In Atest Standard Mode, user can identify tests by module name, class name, 91*c2e18aaaSAndroid Build Coastguard Workerfile path or package name, while in Bazel Mode, we only support module name 92*c2e18aaaSAndroid Build Coastguard Workercurrently. Supporting flexible test finder is work in progress. 93*c2e18aaaSAndroid Build Coastguard Worker* In Atest Standard Mode, test logs are saved under ```/tmp/atest_result```, while in 94*c2e18aaaSAndroid Build Coastguard WorkerBazel Mode, test logs are saved under ```$ANDROID_BUILD_TOP/out/atest_bazel_workspace/bazel-testlogs``` 95*c2e18aaaSAndroid Build Coastguard Worker 96*c2e18aaaSAndroid Build Coastguard Worker 97*c2e18aaaSAndroid Build Coastguard Worker## <a name="faq">Frequently Asked Questions</a> 98*c2e18aaaSAndroid Build Coastguard Worker 99*c2e18aaaSAndroid Build Coastguard Worker### 1. Why my test failed with "error: Read-only file system" in the test log? 100*c2e18aaaSAndroid Build Coastguard Worker 101*c2e18aaaSAndroid Build Coastguard WorkerBazel execution is done within a sandbox. The purpose is to create a hermetic 102*c2e18aaaSAndroid Build Coastguard Workerenvironment for the test. This could sometimes cause issues if the test writer 103*c2e18aaaSAndroid Build Coastguard Workeris not careful when reading and writting test data. 104*c2e18aaaSAndroid Build Coastguard Worker 105*c2e18aaaSAndroid Build Coastguard WorkerFor reading, there is not much restriction as the new Bazel sandbox design 106*c2e18aaaSAndroid Build Coastguard Workerallows all read access to "/" since it mounted "/" as readable in the sandbox. 107*c2e18aaaSAndroid Build Coastguard Worker 108*c2e18aaaSAndroid Build Coastguard WorkerFor writting, Bazel only allows write access to the target's private execroot 109*c2e18aaaSAndroid Build Coastguard Workerdirectory and a private $TMPDIR. 110*c2e18aaaSAndroid Build Coastguard Worker 111*c2e18aaaSAndroid Build Coastguard WorkerMore details about [Bazel sandbox.](https://bazel.build/designs/2016/06/02/sandboxing.html) 112*c2e18aaaSAndroid Build Coastguard Worker 113*c2e18aaaSAndroid Build Coastguard Worker 114*c2e18aaaSAndroid Build Coastguard Worker### 2. Why I got "Too many levels of symbolic links" while reading files in Bazel Mode? 115*c2e18aaaSAndroid Build Coastguard Worker 116*c2e18aaaSAndroid Build Coastguard WorkerSome tests try to read the test data using relative path. This some times does 117*c2e18aaaSAndroid Build Coastguard Workernot work in Bazel Mode. 118*c2e18aaaSAndroid Build Coastguard Worker 119*c2e18aaaSAndroid Build Coastguard WorkerIn Bazel Mode, Bazel creates symbolic links for all the test artifacts in the 120*c2e18aaaSAndroid Build Coastguard WorkerBazel private execution root directory in the sandbox. The symbolic links are 121*c2e18aaaSAndroid Build Coastguard Workereventially resolved to the physical file in Android source tree. 122*c2e18aaaSAndroid Build Coastguard WorkerReading the symlink as file without following symlinks may fail with the above 123*c2e18aaaSAndroid Build Coastguard Workererror message. 124*c2e18aaaSAndroid Build Coastguard Worker 125*c2e18aaaSAndroid Build Coastguard WorkerOne example is C++ android::base::ReadFileToString function. The solution is to 126*c2e18aaaSAndroid Build Coastguard Workerenable following symbolic link when calling the function. 127*c2e18aaaSAndroid Build Coastguard WorkerMore details can be find [here.](https://cs.android.com/android/platform/superproject/+/master:external/googletest/googletest/include/gtest/gtest.h;drc=master;l=2353) 128