1*9c5db199SXin Li# Autotest for Chromium OS developers 2*9c5db199SXin Li 3*9c5db199SXin Li[TOC] 4*9c5db199SXin Li 5*9c5db199SXin Li## Useful documents 6*9c5db199SXin Li 7*9c5db199SXin Li[Autotest documentation](https://autotest.readthedocs.io/en/latest/index.html): 8*9c5db199SXin LiThis would be a good read if you want to familiarize yourself with the basic 9*9c5db199SXin LiAutotest concepts. 10*9c5db199SXin Li 11*9c5db199SXin Li[Gentoo Portage ebuild/eclass Information](https://devmanual.gentoo.org/): 12*9c5db199SXin LiGuides for getting to know the package build system we use. 13*9c5db199SXin Li 14*9c5db199SXin Li[ChromiumOS specific Portage FAQ](https://chromium.googlesource.com/chromiumos/docs/+/HEAD/portage/ebuild_faq.md): 15*9c5db199SXin LiLearn something about the way we use portage. 16*9c5db199SXin Li 17*9c5db199SXin Li## Autotest and ebuild workflow 18*9c5db199SXin Li 19*9c5db199SXin LiTo familiarize with autotest concepts, you should start with the upstream 20*9c5db199SXin LiAutotest documentation at: https://github.com/autotest/autotest/wiki/AutotestApi 21*9c5db199SXin Li 22*9c5db199SXin LiThe rest of this document is going to use some terms and only explain them 23*9c5db199SXin Livaguely. 24*9c5db199SXin Li 25*9c5db199SXin Li### Overview 26*9c5db199SXin Li 27*9c5db199SXin LiAt a high level, tests are organized in test cases, each test case being either 28*9c5db199SXin Liserver or client, with one main .py file named the same as the test case, and 29*9c5db199SXin Lione or more control files. In order to be able to perform all tasks on a given 30*9c5db199SXin Litest, autotest expects tests to be placed in a monolithic file structure 31*9c5db199SXin Liof: 32*9c5db199SXin Li 33*9c5db199SXin Li- `/client/tests/` 34*9c5db199SXin Li- `/client/site_tests/` 35*9c5db199SXin Li- `/server/tests/` 36*9c5db199SXin Li- `/server/site_tests/` 37*9c5db199SXin Li 38*9c5db199SXin LiEach test directory has to have at least a control file, but typically also has 39*9c5db199SXin Lia main job module (named the same as the test case). Furthermore, if it needs 40*9c5db199SXin Liany additional files checked in, they are typically placed in a `files/` 41*9c5db199SXin Lidirectory, and separate projects that can be built with a Makefile inside the 42*9c5db199SXin Li`src/` directory. 43*9c5db199SXin Li 44*9c5db199SXin LiDue to structural limitations in Chromium OS, it is not possible to store all 45*9c5db199SXin Litest cases in this structure in a single large source repository as upstream 46*9c5db199SXin Liautotest source would (placed at `third_party/autotest/files/` in Chromium OS). 47*9c5db199SXin LiIn particular, the following has been required in the past: 48*9c5db199SXin Li 49*9c5db199SXin Li- Having confidential (publicly inaccessible) tests or generally per-test ACLs 50*9c5db199SXin Li for sharing only with a particular partner only. 51*9c5db199SXin Li- Storing test cases along with the project they wrap around, because the test 52*9c5db199SXin Li requires binaries built as a by-product of the project’s own build system. 53*9c5db199SXin Li (e.g. chrome or tpm tests) 54*9c5db199SXin Li 55*9c5db199SXin LiFurthermore, it has been desired to generally build everything that is not 56*9c5db199SXin Listrongly ordered in parallel, significantly decreasing build times. That, 57*9c5db199SXin Lihowever, requires proper dependency tree declaration and being able to specify 58*9c5db199SXin Liwhich test cases require what dependencies, in addition to being able to 59*9c5db199SXin Liprocess different "independent" parts of a single source repository in 60*9c5db199SXin Liparallel. 61*9c5db199SXin Li 62*9c5db199SXin LiThis leads to the ebuild workflow, which generally allows compositing any 63*9c5db199SXin Linumber of sources in any format into a single monolithic tree, whose contents 64*9c5db199SXin Lidepend on build parameters. 65*9c5db199SXin Li 66*9c5db199SXin Li 67*9c5db199SXin Li 68*9c5db199SXin LiThis allows using standard autotest workflow without any change, however, 69*9c5db199SXin Liunlike what upstream does, the tests aren’t run directly from the source 70*9c5db199SXin Lirepository, rather from a staging read-only install location. This leads to 71*9c5db199SXin Licertain differences in workflow: 72*9c5db199SXin Li 73*9c5db199SXin Li- Source may live in an arbitrary location or can be generated on the fly. 74*9c5db199SXin Li Anything that can be created as an ebuild (shell script) can be a test source. 75*9c5db199SXin Li (cros-workon may be utilised, introducing a fairly standard Chromium OS 76*9c5db199SXin Li project workflow) 77*9c5db199SXin Li- The staging location (`/build/${board}/usr/local/autotest/`) may not be 78*9c5db199SXin Li modified; if one wants to modify it, they have to find the source to it 79*9c5db199SXin Li (using other tools, see FAQ). 80*9c5db199SXin Li- Propagating source changes requires an emerge step. 81*9c5db199SXin Li 82*9c5db199SXin Li### Ebuild setup, autotest eclass 83*9c5db199SXin Li 84*9c5db199SXin Li**NOTE**: This assumes some basic knowledge of how ebuilds in Chromium OS work. 85*9c5db199SXin LiFurther documentation is available at http://www.chromium.org/chromium-os/how-tos-and-troubleshooting/portage-build-faq 86*9c5db199SXin Li 87*9c5db199SXin LiAn **autotest ebuild** is an ebuild that produces test cases and installs them into 88*9c5db199SXin Lithe staging area. It has three general tasks: 89*9c5db199SXin Li 90*9c5db199SXin Li- Obtain the source - This is generally (but not necessarily) provided by 91*9c5db199SXin Li ‘cros-workon’ eclass. It could also work with the more standard tarball 92*9c5db199SXin Li SRC_URI pathway or generally any shell code executed in `src_unpack()`. 93*9c5db199SXin Li- Prepare test cases - This includes, but is not limited to preprocessing any 94*9c5db199SXin Li source, copying source files or intermediate binaries into the expected 95*9c5db199SXin Li locations, where they will be taken over by autotest code, specifically the 96*9c5db199SXin Li `setup()` function of the appropriate test. Typically, this is not needed. 97*9c5db199SXin Li- Call autotest to "build" all sources and subsequently install them - This 98*9c5db199SXin Li should be done exclusively by inheriting the **autotest eclass**, which 99*9c5db199SXin Li bundles up all the necessary code to install into the intermediate location. 100*9c5db199SXin Li 101*9c5db199SXin Li**Autotest eclass** is inherited by all autotest ebuilds, only requires a 102*9c5db199SXin Linumber of variables specified and works by itself otherwise. Most variables 103*9c5db199SXin Lidescribe the locations and listings of work that needs to be done: 104*9c5db199SXin Li 105*9c5db199SXin Li- Location variables define the paths to directories containing the test 106*9c5db199SXin Lifiles: 107*9c5db199SXin Li 108*9c5db199SXin Li - `AUTOTEST_{CLIENT,SERVER}_{TESTS,SITE_TESTS}` 109*9c5db199SXin Li - `AUTOTEST_{DEPS,PROFILERS,CONFIG}` 110*9c5db199SXin Li 111*9c5db199SXin Li These typically only need to be specified if they differ from the defaults 112*9c5db199SXin Li (which follow the upstream directory structure) 113*9c5db199SXin Li 114*9c5db199SXin Li- List variables (`AUTOTEST_*_LIST`) define the list of deps, profilers, 115*9c5db199SXin Li configs that should be handled by this ebuild. 116*9c5db199SXin Li- IUSE test list specification TESTS=, is a USE_EXPANDed specification of 117*9c5db199SXin Li tests managed by the given ebuild. By virtue of being an IUSE variable, all 118*9c5db199SXin Li of the options are visible as USE flag toggles while building the ebuild, 119*9c5db199SXin Li unlike with list variables which are a given and the ebuild has to be 120*9c5db199SXin Li modified for those to change. 121*9c5db199SXin Li 122*9c5db199SXin LiEach ebuild usually operates on a single source repository. That does not 123*9c5db199SXin Lialways have to hold true, however, and in case of autotest, many ebuilds check 124*9c5db199SXin Liout the sources of the same source repository (*autotest.git*). Invariably, this 125*9c5db199SXin Limeans that they have to be careful to not install the same files and split the 126*9c5db199SXin Lisources between themselves to avoid file install collisions. 127*9c5db199SXin LiIf more than one autotest ebuild operates on the same source repository, they 128*9c5db199SXin Li**have to** use the above variables to define mutually exclusive slices in order 129*9c5db199SXin Lito not collide during installation. Generally, if we have a source repository 130*9c5db199SXin Liwith client site_tests A and B, you can have either: 131*9c5db199SXin Li 132*9c5db199SXin Li- one ebuild with IUSE_TESTS="+tests_A +tests_B" 133*9c5db199SXin Li- two different ebuilds, one with IUSE_TESTS="+tests_A", the other with 134*9c5db199SXin Li IUSE_TESTS="+tests_B" 135*9c5db199SXin Li 136*9c5db199SXin LiAs soon as an overlap between ebuilds happens, either an outside mechanism has 137*9c5db199SXin Lito ensure the overlapping tests are never enabled at the same time, or file 138*9c5db199SXin Licollisions happen. 139*9c5db199SXin Li 140*9c5db199SXin Li 141*9c5db199SXin Li## Building tests 142*9c5db199SXin Li 143*9c5db199SXin LiFundamentally, a test has two main phases: 144*9c5db199SXin Li 145*9c5db199SXin Li- `run_*()` - This is is the main part that performs all testing and is 146*9c5db199SXin Li invoked by the control files, once or repeatedly. 147*9c5db199SXin Li- `setup()` - This function, present in the test case’s main .py file is 148*9c5db199SXin Li supposed to prepare the test for running. This includes building any 149*9c5db199SXin Li binaries, initializing data, etc. 150*9c5db199SXin Li 151*9c5db199SXin LiDuring building using emerge, autotest will call a `setup()` function of all 152*9c5db199SXin Litest cases/deps involved. This is supposed to prepare everything. Typically, 153*9c5db199SXin Lithis will invoke make on a Makefile present in the test’s src/ directory, but 154*9c5db199SXin Lican involve any other transformation of sources (also be empty if there’s 155*9c5db199SXin Linothing to build). 156*9c5db199SXin Li**Note**, however, that `setup()` is implicitly called many times as test 157*9c5db199SXin Liinitialization even during `run_*()` step, so it should be a noop on reentry 158*9c5db199SXin Lithat merely verifies everything is in order. 159*9c5db199SXin Li 160*9c5db199SXin LiUnlike `run_*()` functions, `setup()` gets called both during the prepare phase 161*9c5db199SXin Liwhich happens on the **host and target alike**. This creates a problem with 162*9c5db199SXin Licode that is being depended on or directly executed during `setup()`. Python 163*9c5db199SXin Limodules that are imported in any pathway leading to `setup()` are needed both 164*9c5db199SXin Liin the host chroot and on the target board to properly support the test. Any 165*9c5db199SXin Libinaries would need to be compiled using the host compiler and either ensured 166*9c5db199SXin Lithat they will be skipped on the target (incremental `setup()` runs) or 167*9c5db199SXin Licross-compiled again and dynamically chosen while running on target. 168*9c5db199SXin Li 169*9c5db199SXin Li**More importantly**, in Chromium OS scenario, doing any write operations 170*9c5db199SXin Liinside the `setup()` function will lead to **access denied failures**, because 171*9c5db199SXin Litests are being run from the intermediate read-only location. 172*9c5db199SXin Li 173*9c5db199SXin LiGiven the above, building is as easy as **emerge**-ing the autotest ebuild that 174*9c5db199SXin Licontains our test. 175*9c5db199SXin Li``` 176*9c5db199SXin Li$ emerge-${board} ${test_ebuild} 177*9c5db199SXin Li``` 178*9c5db199SXin Li 179*9c5db199SXin Li*Currently, tests are organized within these notable ebuilds*: (see 180*9c5db199SXin Li[FAQ](#Q1_What-autotest-ebuilds-are-out-there_) full list): 181*9c5db199SXin Li 182*9c5db199SXin Li- chromeos-base/autotest-tests - The main ebuild handling most of autotest.git 183*9c5db199SXin Li repository and its client and server tests. 184*9c5db199SXin Li- chromeos-base/autotest-tests-* - Various ebuilds that build other parts of 185*9c5db199SXin Li autotest.git 186*9c5db199SXin Li- chromeos-base/chromeos-chrome - chrome tests; the tests that are part of 187*9c5db199SXin Li chrome 188*9c5db199SXin Li 189*9c5db199SXin Li### Building tests selectively 190*9c5db199SXin Li 191*9c5db199SXin LiTest cases built by ebuilds generally come in large bundles. Sometimes, only a 192*9c5db199SXin Lisubset, or generally a different set of the tests provided by a given ebuild is 193*9c5db199SXin Lidesired. That is achieved using a 194*9c5db199SXin Li[USE_EXPANDed](http://devmanual.gentoo.org/general-concepts/use-flags/index.html) 195*9c5db199SXin Liflag called TESTS. 196*9c5db199SXin Li 197*9c5db199SXin LiAll USE flags (and therefore tests) have a default state, either enabled (+) or 198*9c5db199SXin Lidisabled (-), specified directly in the ebuild, that can be manually overridden 199*9c5db199SXin Lifrom the commandline. There are two ways to do that. 200*9c5db199SXin Li 201*9c5db199SXin Li- Non-Incremental - Simply override the default selection by an entirely new 202*9c5db199SXin Li selection, ignoring the defaults. This is useful if you develop a single 203*9c5db199SXin Li test and don’t want to waste time building the others. 204*9c5db199SXin Li 205*9c5db199SXin Li $ TESTS="test1 test2" emerge-${board} ${ebuild} 206*9c5db199SXin Li 207*9c5db199SXin Li- Incremental - All USE_EXPAND flags are also accessible as USE flags, with 208*9c5db199SXin Li the appropriate prefix, and can be used incrementally to selectively 209*9c5db199SXin Li enable/disable tests in addition to the defaults. This can be useful if you 210*9c5db199SXin Li aim to enable a test that is disabled by default and want to test locally. 211*9c5db199SXin Li 212*9c5db199SXin Li $ USE="test_to_be_enabled -test_to_be_disabled" emerge-${board} \ 213*9c5db199SXin Li ${ebuild} 214*9c5db199SXin Li 215*9c5db199SXin LiFor operations across all tests, following incremental USE wildcard is 216*9c5db199SXin Lisupported by portage: "tests_*" to select all tests at once (or - to 217*9c5db199SXin Lide-select). 218*9c5db199SXin Li 219*9c5db199SXin Li**NOTE**: Both Incremental and Non-Incremental methods can be set/overriden by 220*9c5db199SXin Li(in this order): the ebuild (default values), make.profile, make.conf, 221*9c5db199SXin Li/etc/portage, commandline (see above). That means that any settings provided on 222*9c5db199SXin Lithe emerge commandline override everything else. 223*9c5db199SXin Li 224*9c5db199SXin Li## Running tests 225*9c5db199SXin Li 226*9c5db199SXin Li**NOTE**: In order to run tests on your device, it needs to have a 227*9c5db199SXin Li[test-enabled image](#W4_Create-and-run-a-test-enabled-image-on-your-device). 228*9c5db199SXin Li 229*9c5db199SXin LiWhen running tests, fundamentally, you want to either: 230*9c5db199SXin Li 231*9c5db199SXin Li- Run sets of tests manually - Use case: Developing test cases 232*9c5db199SXin Li 233*9c5db199SXin Li Take your local test sources, modify them, and then attempt to run them on a 234*9c5db199SXin Li target machine using autotest. You are generally responsible for making sure 235*9c5db199SXin Li that the machine is imaged to a test image, and the image contains all the 236*9c5db199SXin Li dependencies needed to support your tests. 237*9c5db199SXin Li 238*9c5db199SXin Li- Verify a given image - Use case: Developing the projects subject to testing 239*9c5db199SXin Li 240*9c5db199SXin Li Take an image, re-image the target device and run a test suite on it. This 241*9c5db199SXin Li requires either use of build-time autotest artifacts or their reproduction 242*9c5db199SXin Li by not modifying or resyncing your sources after an image has been built. 243*9c5db199SXin Li 244*9c5db199SXin Li### Running tests on a machine 245*9c5db199SXin Li 246*9c5db199SXin LiAutotests are run with a tool called 247*9c5db199SXin Li[test_that](https://chromium.googlesource.com/chromiumos/third_party/autotest/+/refs/heads/main/docs/test-that.md). 248*9c5db199SXin Li 249*9c5db199SXin Li### Running tests in a VM - cros_run_test 250*9c5db199SXin Li 251*9c5db199SXin LiVM tests are conveniently wrapped into a script `cros_run_test` that sets up 252*9c5db199SXin Lithe VM using a given image and then calls `test_that`. This is run by builders 253*9c5db199SXin Lito test using the Smoke suite. 254*9c5db199SXin Li 255*9c5db199SXin LiIf you want to run your tests in a VM (see 256*9c5db199SXin Li[here](https://chromium.googlesource.com/chromiumos/docs/+/main/cros_vm.md#Run-an-autotest-in-the-VM) 257*9c5db199SXin Li 258*9c5db199SXin Li- `cros_run_test` starts up a VM and runs autotests using the port 259*9c5db199SXin Li- specified (defaults to 9222). As an example: 260*9c5db199SXin Li 261*9c5db199SXin Li $ cros_run_test --autotest=suite:smoke \ 262*9c5db199SXin Li --image-path=<my_image_to_start or don't set to use most recent build> \ 263*9c5db199SXin Li --board=amd64-generic 264*9c5db199SXin Li 265*9c5db199SXin Li- The emulator command line redirects localhost port 9222 to the emulated 266*9c5db199SXin Li machine's port 22 to allow you to ssh into the emulator. For Chromium OS to 267*9c5db199SXin Li actually listen on this port you must create & boot a test image. 268*9c5db199SXin Li- You can then run tests on the correct ssh port with something like 269*9c5db199SXin Li 270*9c5db199SXin Li $ test_that --board=x86-generic localhost:9222 'f:.*platform_BootPerf/control' 271*9c5db199SXin Li 272*9c5db199SXin Li- To set the sudo password run set_shared_user_password. Then within the 273*9c5db199SXin Li emulator you can press Ctrl-Alt-T to get a terminal, and sudo using this 274*9c5db199SXin Li password. This will also allow you to ssh into the unit with, e.g. 275*9c5db199SXin Li 276*9c5db199SXin Li $ ssh -p 9222 root@localhost 277*9c5db199SXin Li 278*9c5db199SXin Li- Warning: After 279*9c5db199SXin Li [crbug/710629](https://bugs.chromium.org/p/chromium/issues/detail?id=710629), 280*9c5db199SXin Li 'betty' is the only board regularly run through pre-CQ and CQ VMTest and so 281*9c5db199SXin Li is the most likely to work at ToT. 'betty' is based on 'amd64-generic', 282*9c5db199SXin Li so 'amd64-generic' is likely to also work for most (non-ARC) tests. 283*9c5db199SXin Li 284*9c5db199SXin Li 285*9c5db199SXin Li## Result log layout structure 286*9c5db199SXin Li 287*9c5db199SXin LiFor information regarding the layout structure please refer to the following: 288*9c5db199SXin Li[autotest-results-logs](https://www.chromium.org/chromium-os/testing/test-code-labs/autotest-client-tests/autotest-results-logs) 289*9c5db199SXin Li 290*9c5db199SXin Li### Interpreting test results 291*9c5db199SXin Li 292*9c5db199SXin LiRunning autotest will result in a lot of information going by which is probably 293*9c5db199SXin Linot too informative if you have not used autotest before. At the end of the 294*9c5db199SXin Li`test_that` run, you will see a summary of pass/failure status, along with 295*9c5db199SXin Liperformance results: 296*9c5db199SXin Li 297*9c5db199SXin Li``` 298*9c5db199SXin Li22:44:30 INFO | Using installation dir /home/autotest 299*9c5db199SXin Li22:44:30 ERROR| Could not install autotest from repos 300*9c5db199SXin Li22:44:32 INFO | Installation of autotest completed 301*9c5db199SXin Li22:44:32 INFO | GOOD ---- Autotest.install timestamp=1263509072 localtime=Jan 14 22:44:32 302*9c5db199SXin Li22:44:33 INFO | Executing /home/autotest/bin/autotest /home/autotest/control phase 0 303*9c5db199SXin Li22:44:36 INFO | START ---- ---- timestamp=1263509075 localtime=Jan 14 14:44:35 304*9c5db199SXin Li22:44:36 INFO | START sleeptest sleeptest timestamp=1263509076 localtime=Jan 14 14:44:36 305*9c5db199SXin Li22:44:36 INFO | Bundling /usr/local/autotest/client/tests/sleeptest into test-sleeptest.tar.bz2 306*9c5db199SXin Li22:44:40 INFO | GOOD sleeptest sleeptest timestamp=1263509079 localtime=Jan 14 14:44:39 completed successfully 307*9c5db199SXin Li22:44:40 INFO | END GOOD sleeptest sleeptest timestamp=1263509079 localtime=Jan 14 14:44:39 308*9c5db199SXin Li22:44:42 INFO | END GOOD ---- ---- timestamp=1263509082 localtime=Jan 14 14:44:42 309*9c5db199SXin Li22:44:44 INFO | Client complete 310*9c5db199SXin Li22:44:45 INFO | Finished processing control file 311*9c5db199SXin Li``` 312*9c5db199SXin Li 313*9c5db199SXin Li`test_that` will leave around a temp directory populated with diagnostic information: 314*9c5db199SXin Li 315*9c5db199SXin Li``` 316*9c5db199SXin LiFinished running tests. Results can be found in /tmp/test_that_results_j8GoWH or /tmp/test_that_latest 317*9c5db199SXin Li``` 318*9c5db199SXin Li 319*9c5db199SXin LiThis directory will contain a directory per test run. Each directory contains 320*9c5db199SXin Lithe logs pertaining to that test run. 321*9c5db199SXin Li 322*9c5db199SXin LiIn that directory some interesting files are: 323*9c5db199SXin Li 324*9c5db199SXin Li${TEST}/debug/client.DEBUG - the most detailed output from running the 325*9c5db199SXin Liclient-side test 326*9c5db199SXin Li 327*9c5db199SXin Li### Running tests automatically, Suites 328*9c5db199SXin Li 329*9c5db199SXin LiSuites provide a mechanism to group tests together in test groups. They also 330*9c5db199SXin Liserve as hooks for automated runs of tests verifying various builds. Most 331*9c5db199SXin Liimportantly, that is the BVT (board verification tests) and Smoke (a subset of 332*9c5db199SXin LiBVT that can run in a VM). 333*9c5db199SXin Li 334*9c5db199SXin LiPlease refer to the [suites documentation](https://www.chromium.org/chromium-os/testing/test-suites). 335*9c5db199SXin Li 336*9c5db199SXin Li## Writing and developing tests 337*9c5db199SXin Li 338*9c5db199SXin Li### Writing a test 339*9c5db199SXin Li 340*9c5db199SXin LiFor understanding and writing the actual python code for autotest, please refer 341*9c5db199SXin Lito the [Developer FAQ](http://www.chromium.org/chromium-os/testing/autotest-developer-faq#TOC-Writing-Autotests) 342*9c5db199SXin Li 343*9c5db199SXin LiCurrently, all code should be placed in a standard layout inside the 344*9c5db199SXin Liautotest.git repository, unless otherwise is necessary for technical reasons. 345*9c5db199SXin LiRegardless, the following text assumes that code is placed in generally any 346*9c5db199SXin Lirepository. 347*9c5db199SXin Li 348*9c5db199SXin LiFor a test to be fully functional in Chromium OS, it has to be associated with 349*9c5db199SXin Lian ebuild. It is generally possible to run tests without an ebuild using 350*9c5db199SXin Li`test_that` but discouraged, as the same will not function with other parts of 351*9c5db199SXin Lithe system. 352*9c5db199SXin Li 353*9c5db199SXin Li### Making a new test work with ebuilds 354*9c5db199SXin Li 355*9c5db199SXin LiThe choice of ebuild depends on the location of its sources. Structuring tests 356*9c5db199SXin Liinto more smaller ebuilds (as opposed to one ebuild per source repository) 357*9c5db199SXin Liserves two purposes: 358*9c5db199SXin Li 359*9c5db199SXin Li- Categorisation - Grouping similar tests together, possibly with deps they 360*9c5db199SXin Li use exclusively. 361*9c5db199SXin Li- Parallelisation - Multiple independent ebuilds can build entirely in 362*9c5db199SXin Li parallel. 363*9c5db199SXin Li- Dependency tracking - Larger bundles of tests depend on more system 364*9c5db199SXin Li packages without proper resolution which dependency belongs to which test. 365*9c5db199SXin Li This also increases paralellism. 366*9c5db199SXin Li 367*9c5db199SXin LiCurrent ebuild structure is largely a result of breaking off the biggest 368*9c5db199SXin Liblockers for parallelism, ie. tests depending on chrome or similar packages, 369*9c5db199SXin Liand as such, using any of the current ebuilds should be sufficient. (see FAQ 370*9c5db199SXin Lifor listing of ebuilds) 371*9c5db199SXin Li 372*9c5db199SXin LiAfter choosing the proper ebuild to add your test into, the test (in the form 373*9c5db199SXin Li“+tests_<testname>”) needs to be added to IUSE_TESTS list that all autotest 374*9c5db199SXin Liebuilds have. Failing to do so will simply make ebuilds ignore your tests 375*9c5db199SXin Lientirely. As with all USE flags, prepending it with + means the test will be 376*9c5db199SXin Lienabled by default, and should be the default, unless you want to keep the test 377*9c5db199SXin Liexperimental for your own use, or turn the USE flag on explicitly by other 378*9c5db199SXin Limeans, eg. in a config for a particular board only. 379*9c5db199SXin Li 380*9c5db199SXin LiShould a **new ebuild** be started, it should be added to 381*9c5db199SXin Li**chromeos-base/autotest-all** package, which is a meta-ebuild depending on all 382*9c5db199SXin Liautotest ebuild packages that can be built. autotest-all is used by the build 383*9c5db199SXin Lisystem to automatically build all tests that we have and therefore keep them 384*9c5db199SXin Lifrom randomly breaking. 385*9c5db199SXin Li 386*9c5db199SXin Li### Deps 387*9c5db199SXin Li 388*9c5db199SXin LiAutotest uses deps to provide a de-facto dependencies into the ecosystem. A dep 389*9c5db199SXin Liis a directory in ‘**client/deps**’ with a structure similar to a test case 390*9c5db199SXin Liwithout a control file. A test case that depends on a dep will invoke the dep’s 391*9c5db199SXin Li`setup()` function in its own `setup()` function and will be able to access the 392*9c5db199SXin Lifiles provided by the dep. Note that autotest deps have nothing to do with 393*9c5db199SXin Lisystem dependencies. 394*9c5db199SXin Li 395*9c5db199SXin LiAs the calls to a dep are internal autotest code, it is not possible to 396*9c5db199SXin Liautomatically detect these and make them an inter-package dependencies on the 397*9c5db199SXin Liebuild level. For that reason, deps should either be 398*9c5db199SXin Li[provided](#Ebuild-setup_autotest-eclass) by the same ebuild that builds test 399*9c5db199SXin Lithat consume them, or ebuild dependencies need to be declared manually between 400*9c5db199SXin Lithe dep ebuild and the test ebuild that uses it. An **autotest-deponly** 401*9c5db199SXin Lieclass exists to provide solution for ebuilds that build only deps and no 402*9c5db199SXin Litests. A number of deponly ebuilds already exist. 403*9c5db199SXin Li 404*9c5db199SXin LiCommon deps are: 405*9c5db199SXin Li 406*9c5db199SXin Li- chrome_test - Intending to use any of the test binaries produced by chrome. 407*9c5db199SXin Li- pyauto_dep - Using pyauto for your code. 408*9c5db199SXin Li 409*9c5db199SXin Li### Test naming conventions 410*9c5db199SXin Li 411*9c5db199SXin LiGenerally, the naming convention runs like this: 412*9c5db199SXin Li 413*9c5db199SXin Li\<component>\_\<TestName\> 414*9c5db199SXin Li 415*9c5db199SXin LiThat convention names the directory containing the test code. It also names 416*9c5db199SXin Lithe .py file containing the test code, and the class of the Autotest test. 417*9c5db199SXin Li 418*9c5db199SXin LiIf there's only one control file, it's named control. The test's NAME in the 419*9c5db199SXin Licontrol file is \<component\>_\<TestName\>, like the directory and .py 420*9c5db199SXin Lifile. 421*9c5db199SXin Li 422*9c5db199SXin LiIf there are multiple control files for a test, they are named 423*9c5db199SXin Licontrol.\<testcase\>. These tests' NAMEs are then 424*9c5db199SXin Li\<component\>_\<TestName\>.\<testcase\>. 425*9c5db199SXin Li 426*9c5db199SXin Li## Common workflows 427*9c5db199SXin Li 428*9c5db199SXin Li### W1. Develop and iterate on a test 429*9c5db199SXin Li 430*9c5db199SXin Li1. Set up the environment. 431*9c5db199SXin Li 432*9c5db199SXin Li $ cd ~/trunk/src/third_party/autotest/files/ 433*9c5db199SXin Li $ export TESTS=”<the test cases to iterate on>” 434*9c5db199SXin Li $ EBUILD=<the ebuild that contains TEST> 435*9c5db199SXin Li $ board=<the board on which to develop> 436*9c5db199SXin Li 437*9c5db199SXin Li2. Ensure cros_workon is started 438*9c5db199SXin Li 439*9c5db199SXin Li $ cros_workon --board=${board} start ${EBUILD} 440*9c5db199SXin Li $ repo sync # Necessary only if you use minilayout. 441*9c5db199SXin Li 442*9c5db199SXin Li3. Make modifications (on first run, you may want to just do 3,4 to verify 443*9c5db199SXin Li everything works before you touch it \& break it) 444*9c5db199SXin Li 445*9c5db199SXin Li $ ... 446*9c5db199SXin Li 447*9c5db199SXin Li4. Build test (TESTS= is not necessary if you exported it before) 448*9c5db199SXin Li 449*9c5db199SXin Li $ emerge-$board $EBUILD 450*9c5db199SXin Li 451*9c5db199SXin Li5. Run test to make sure it works before you touch it 452*9c5db199SXin Li 453*9c5db199SXin Li $ test_that <machine IP> ${TESTS} 454*9c5db199SXin Li 455*9c5db199SXin Li6. Go to 2) to iterate 456*9c5db199SXin Li7. Clean up environment 457*9c5db199SXin Li 458*9c5db199SXin Li $ cros_workon --board=${board} stop ${EBUILD} 459*9c5db199SXin Li $ unset TESTS 460*9c5db199SXin Li 461*9c5db199SXin Li### W2. Creating a test - steps and checklist 462*9c5db199SXin Li 463*9c5db199SXin LiWhen creating a test, the following steps should be done/verified. 464*9c5db199SXin Li 465*9c5db199SXin Li1. Create the actual test directory, main test files/sources, at least one 466*9c5db199SXin Li control file 467*9c5db199SXin Li2. Find the appropriate ebuild package and start working on it: 468*9c5db199SXin Li 469*9c5db199SXin Li $ cros_workon --board=${board} start <package> 470*9c5db199SXin Li 471*9c5db199SXin Li3. Add the new test into the IUSE_TESTS list of 9999 ebuild 472*9c5db199SXin Li4. Try building: (make sure it’s the 9999 version being built) 473*9c5db199SXin Li 474*9c5db199SXin Li $ TESTS=<test> emerge-$board <package> 475*9c5db199SXin Li 476*9c5db199SXin Li5. Try running: 477*9c5db199SXin Li 478*9c5db199SXin Li $ test_that <IP> <test> 479*9c5db199SXin Li 480*9c5db199SXin Li6. Iterate on 4,5 and modify source until happy with the initial version. 481*9c5db199SXin Li7. Commit test source first, when it is safely in, commit the 9999 ebuild 482*9c5db199SXin Li version change. 483*9c5db199SXin Li8. Cleanup 484*9c5db199SXin Li 485*9c5db199SXin Li $ cros_workon --board=${board} stop <package> 486*9c5db199SXin Li 487*9c5db199SXin Li### W3. Splitting autotest ebuild into two 488*9c5db199SXin Li 489*9c5db199SXin LiRemoving a test from one ebuild and adding to another in the same revision 490*9c5db199SXin Licauses portage file collisions unless counter-measures are taken. Generally, 491*9c5db199SXin Lisome things routinely go wrong in this process, so this checklist should serve 492*9c5db199SXin Lito help that. 493*9c5db199SXin Li 494*9c5db199SXin Li1. We have ebuild **foo-0.0.1-r100** with **test** and would like to split 495*9c5db199SXin Li that test off into ebuild **bar-0.0.1-r1**. 496*9c5db199SXin Li Assume that: 497*9c5db199SXin Li - both ebuilds are using cros-workon (because it’s likely the case). 498*9c5db199SXin Li - foo is used globally (eg. autotest-all depends on it), rather than just 499*9c5db199SXin Li some personal ebuild 500*9c5db199SXin Li2. Remove **test** from foo-{0.0.1-r100,9999}; uprev foo-0.0.1-r100 (to -r101) 501*9c5db199SXin Li3. Create bar-9999 (making a copy of foo and replacing IUSE_TESTS may be a good 502*9c5db199SXin Li start), with IUSE_TESTS containing just the entry for **test** 503*9c5db199SXin Li4. Verify package dependencies of test. Make bar-9999 only depend on what is 504*9c5db199SXin Li needed for test, remove the dependencies from foo-9999, unless they are 505*9c5db199SXin Li needed by tests that remained. 506*9c5db199SXin Li5. Add a blocker. Since bar installs files owned by foo-0.0.1-r100 and earlier, 507*9c5db199SXin Li the blocker’s format will be: 508*9c5db199SXin Li 509*9c5db199SXin Li RDEPEND="!<=foo-0.0.1-r100" 510*9c5db199SXin Li 511*9c5db199SXin Li6. Add a dependency to the new version of bar into 512*9c5db199SXin Li chromeos-base/autotest-all-0.0.1 513*9c5db199SXin Li 514*9c5db199SXin Li RDEPEND="bar" 515*9c5db199SXin Li 516*9c5db199SXin Li7. Change the dependency of foo in chromeos-base/autotest-all-0.0.1 to be 517*9c5db199SXin Li version locked to the new rev: 518*9c5db199SXin Li 519*9c5db199SXin Li RDEPEND=">foo-0.0.1-r100" 520*9c5db199SXin Li 521*9c5db199SXin Li8. Uprev (move) autotest-all-0.0.1-rX symlink by one. 522*9c5db199SXin Li9. Publish all as the same change list, have it reviewed, push. 523*9c5db199SXin Li 524*9c5db199SXin Li### W4. Create and run a test-enabled image on your device 525*9c5db199SXin Li 526*9c5db199SXin Li1. Choose which board you want to build for (we'll refer to this as ${BOARD}, 527*9c5db199SXin Li which is for example "x86-generic"). 528*9c5db199SXin Li2. Set up a proper portage build chroot setup. Go through the normal process 529*9c5db199SXin Li of setup_board if you haven't already. 530*9c5db199SXin Li 531*9c5db199SXin Li $ ./build_packages --board=${BOARD} 532*9c5db199SXin Li 533*9c5db199SXin Li3. Build test image. 534*9c5db199SXin Li 535*9c5db199SXin Li $ ./build_image --board=${BOARD} test 536*9c5db199SXin Li 537*9c5db199SXin Li4. Install the Chromium OS testing image to your target machine. This is 538*9c5db199SXin Li through the standard mechanisms: either USB, or by reimaging a device 539*9c5db199SXin Li currently running a previously built Chromium OS image modded for test, or 540*9c5db199SXin Li by entering the shell on the machine and forcing an auto update to your 541*9c5db199SXin Li machine when it's running a dev server. For clarity we'll walk through two 542*9c5db199SXin Li common ways below, but if you already know about this, just do what you 543*9c5db199SXin Li normally do. 544*9c5db199SXin Li 545*9c5db199SXin Li - If you choose to use a USB boot, you first put the image on USB and run 546*9c5db199SXin Li this from outside the chroot. 547*9c5db199SXin Li 548*9c5db199SXin Li $ ./image_to_usb.sh --to /dev/sdX --board=${BOARD} \ 549*9c5db199SXin Li --image_name=chromiumos_test_image.bin 550*9c5db199SXin Li 551*9c5db199SXin Li - Alternatively, if you happen to already have a machine running an image 552*9c5db199SXin Li modified for test and you know its IP address (${REMOTE_IP}), you can 553*9c5db199SXin Li avoid using a USB key and reimage it with a freshly built image by 554*9c5db199SXin Li running this from outside the chroot: 555*9c5db199SXin Li 556*9c5db199SXin Li $ ./image_to_live.sh --remote=${REMOTE_IP} \ 557*9c5db199SXin Li --image=`./get_latest_image.sh \ 558*9c5db199SXin Li --board=${BOARD}`/chromiumos_test_image.bin 559*9c5db199SXin Li 560*9c5db199SXin LiThis will automatically start dev server, ssh to your machine, cause it to 561*9c5db199SXin Liupdate to from that dev server using memento_updater, reboot, wait for reboot, 562*9c5db199SXin Liprint out the new version updated to, and shut down your dev server. 563*9c5db199SXin Li 564*9c5db199SXin Li## Troubleshooting/FAQ 565*9c5db199SXin Li 566*9c5db199SXin Li### Q1: What autotest ebuilds are out there? 567*9c5db199SXin Li 568*9c5db199SXin LiNote that the list of ebuilds may differ per board, as each board has 569*9c5db199SXin Lipotentially different list of overlays. To find all autotest ebuilds for board 570*9c5db199SXin Lifoo, you can run: 571*9c5db199SXin Li``` 572*9c5db199SXin Li$ board=foo 573*9c5db199SXin Li$ for dir in $(portageq-${board} envvar PORTDIR_OVERLAY); do 574*9c5db199SXin Li find . -name '*.ebuild' | xargs grep "inherit.*autotest" | grep "9999" | \ 575*9c5db199SXin Li cut -f1 -d: | \ 576*9c5db199SXin Li sed -e 's/.*\/\([^/]*\)\/\([^/]*\)\/.*\.ebuild/\1\/\2/' 577*9c5db199SXin Li done 578*9c5db199SXin Li``` 579*9c5db199SXin Li(Getting: "WARNING: 'portageq envvar PORTDIR_OVERLAY' is deprecated. Use 580*9c5db199SXin Li'portageq repositories_configuration' instead." Please fix documentation.) 581*9c5db199SXin Li 582*9c5db199SXin Li### Q2: I see a test of the name ‘greattests_TestsEverything’ in build output/logs/whatever! How do I find which ebuild builds it? 583*9c5db199SXin Li 584*9c5db199SXin LiAll ebuilds have lists of tests exported as **USE_EXPANDed** lists called 585*9c5db199SXin Li**TESTS**. An 586*9c5db199SXin Liexpanded use can be searched for in the same way as other use flags, but with 587*9c5db199SXin Lithe appropriate prefix, in this case, you would search for 588*9c5db199SXin Li**tests_greattests_TestsEverything**’: 589*9c5db199SXin Li``` 590*9c5db199SXin Li$ use_search=tests_greattests_TestsEverything 591*9c5db199SXin Li$ equery-$board hasuse $use_search 592*9c5db199SXin Li * Searching for USE flag tests_greattests_TestsEverything ... 593*9c5db199SXin Li * [I-O] [ ] some_ebuild_package_name:0 594*9c5db199SXin Li``` 595*9c5db199SXin Li 596*9c5db199SXin LiThis will however only work on ebuilds which are **already installed**, ie. 597*9c5db199SXin Litheir potentially outdated versions. 598*9c5db199SXin Li**Alternatively**, you can run a pretended emerge (emerge -p) of all autotest 599*9c5db199SXin Liebuilds and scan the output. 600*9c5db199SXin Li``` 601*9c5db199SXin Li$ emerge -p ${all_ebuilds_from_Q1} |grep -C 10 “${use_search}” 602*9c5db199SXin Li``` 603*9c5db199SXin Li 604*9c5db199SXin Li### Q3: I have an ebuild ‘foo’, where are its sources? 605*9c5db199SXin Li 606*9c5db199SXin LiGenerally speaking, one has to look at the ebuild source to figure that 607*9c5db199SXin Liquestion out (and it shouldn’t be hard). However, all present autotest ebuilds 608*9c5db199SXin Li(at the time of this writing) are also ‘cros-workon’, and for those, this 609*9c5db199SXin Lishould always work: 610*9c5db199SXin Li``` 611*9c5db199SXin Li$ ebuild_search=foo 612*9c5db199SXin Li$ ebuild $(equery-$board which $ebuild_search) info 613*9c5db199SXin LiCROS_WORKON_SRCDIR=”/home/you/trunk/src/third_party/foo” 614*9c5db199SXin LiCROS_WORKON_PROJECT=”chromiumos/third_party/foo” 615*9c5db199SXin Li``` 616*9c5db199SXin Li 617*9c5db199SXin Li### Q4: I have an ebuild, what tests does it build? 618*9c5db199SXin Li 619*9c5db199SXin LiYou can run a pretended emerge on the ebuild and observe the ‘TESTS=’ 620*9c5db199SXin Listatement: 621*9c5db199SXin Li``` 622*9c5db199SXin Li$ ebuild_name=foo 623*9c5db199SXin Li$ emerge-$board -pv ${ebuild_name} 624*9c5db199SXin LiThese are the packages that would be merged, in order: 625*9c5db199SXin Li 626*9c5db199SXin LiCalculating dependencies... done! 627*9c5db199SXin Li[ebuild R ] foo-foo_version to /build/$board/ USE="autox hardened tpmtools 628*9c5db199SXin Lixset -buildcheck -opengles" TESTS="enabled_test1 enabled_test2 ... enabled_testN 629*9c5db199SXin Li-disabled_test1 ...disabled_testN" 0 kB [1] 630*9c5db199SXin Li``` 631*9c5db199SXin Li 632*9c5db199SXin LiAlternately, you can use equery, which will list tests with the USE_EXPAND 633*9c5db199SXin Liprefix: 634*9c5db199SXin Li``` 635*9c5db199SXin Li$ equery-$board uses ${ebuild_name} 636*9c5db199SXin Li[ Legend : U - final flag setting for installation] 637*9c5db199SXin Li[ : I - package is installed with flag ] 638*9c5db199SXin Li[ Colors : set, unset ] 639*9c5db199SXin Li * Found these USE flags for chromeos-base/autotest-tests-9999: 640*9c5db199SXin Li U I 641*9c5db199SXin Li + + autotest : <unknown> 642*9c5db199SXin Li + + autotest : <unknown> 643*9c5db199SXin Li + + autox : <unknown> 644*9c5db199SXin Li + + buildcheck : <unknown> 645*9c5db199SXin Li + + hardened : activate default security enhancements for toolchain (gcc, glibc, binutils) 646*9c5db199SXin Li - - opengles : <unknown> 647*9c5db199SXin Li + + tests_enabled_test : <unknown> 648*9c5db199SXin Li - - tests_disabled_test : <unknown> 649*9c5db199SXin Li``` 650*9c5db199SXin Li 651*9c5db199SXin Li### Q5: I’m working on some test sources, how do I know which ebuilds to cros_workon start in order to properly propagate? 652*9c5db199SXin Li 653*9c5db199SXin LiYou should ‘workon’ and always cros_workon start all ebuilds that have files 654*9c5db199SXin Lithat you touched. If you’re interested in a particular file/directory, that 655*9c5db199SXin Liis installed in `/build/$board/usr/local/autotest/` and would like know which 656*9c5db199SXin Lipackage has provided that file, you can use equery: 657*9c5db199SXin Li 658*9c5db199SXin Li``` 659*9c5db199SXin Li$ equery-$board belongs /build/${board}/usr/local/autotest/client/site_tests/foo_bar/foo_bar.py 660*9c5db199SXin Li * Searching for <filename> ... 661*9c5db199SXin Lichromeos-base/autotest-tests-9999 (<filename>) 662*9c5db199SXin Li``` 663*9c5db199SXin Li 664*9c5db199SXin LiDON’T forget to do equery-$board. Just equery will also work, only never 665*9c5db199SXin Lireturn anything useful. 666*9c5db199SXin Li 667*9c5db199SXin LiAs a rule of thumb, if you work on anything from the core autotest framework or 668*9c5db199SXin Lishared libraries (anything besides 669*9c5db199SXin Li{server,client}/{test,site_tests,deps,profilers,config}), it belongs to 670*9c5db199SXin Lichromeos-base/autotest. Individual test case will each belong to a particular 671*9c5db199SXin Liebuild, see Q2. 672*9c5db199SXin Li 673*9c5db199SXin LiIt is important to cros_workon start every ebuild involved. 674*9c5db199SXin Li 675*9c5db199SXin Li### Q6: I created a test, added it into ebuild, emerged it, and I’m getting access denied failures. What did I do wrong? 676*9c5db199SXin Li 677*9c5db199SXin LiYour test’s `setup()` function (which runs on the host before being uploaded) is 678*9c5db199SXin Liprobably trying to write into the read-only intermediate location. See 679*9c5db199SXin Li[explanation](#Building-tests). 680*9c5db199SXin Li 681