1# Autotest Documentation For Enterprise 2To provide all the information needed about the current state of Enterprise 3autotest automation. Current coverage, location of tests, how to execute 4the tests, what machine to run the tests on, test breakdown, etc. 5 6[TOC] 7 8## Current coverage 9 10Calculating coverage could be tricky as there are many different ways 11it could be done. We were using two ways to do it: 12 13* By policy: 14 * Look at this recently updated [spreadsheet](http://go/ent-pol-auto): 15 There are 265 policies available for ChromeOS via C/D Panel. We have 16 96 policies automated, 75 of those are in C/D Panel. So that’s 17 75/264 = %28 coverage + 21 more tests covering various other policies. 18* By section: 19 * Refer to this recently updated [spreadsheet](http://go/ent-sec-auto) 20 in which we list out current coverage. 21 22## Test Location 23 24* Tests that automate user policies are located [here](http://go/usr-pol-loc). 25* Tests that automate device policies are located [here](http://go/dev-pol-loc). 26* Most of Enterprise tests start with *policy_* but there are some 27 that begin with *enterprise_*. 28 29## Test Results 30 31* The best way to view test results is by using stainless: 32* Go to https://stainless.corp.google.com/ 33* Click on Test History Matrix 34* In the Test dropdown, select “policy_*” 35* Hit Search and you should see the results like so: 36![Results](https://screenshot.googleplex.com/UxMiYrVMDdF.png) 37 38## Running a test 39 40A test can be executed using this command from chroot: 41```sh 42test_that --board=$BOARD_NAME $IP_ADDRESS FULL_TEST_NAME* 43``` 44Example: 45```sh 46/trunk/src/scripts $ test_that --board=hana 100.107.106.138 47policy_DeviceServer.AllowBluetooth_true 48``` 49 50**--board** - should be the board that you have setup locally. You only need to 51setup the board ones and you shouldn’t have to touch it again for a long time. 52The board that you setup on your workstation doesn’t have to match the 53DUT(device under test) board that you’re executing the test on. To set up the 54board please follow instructions [here](http://go/run-autotest). You will also 55need to run the build_packages command. 56 57**IP_ADDRESS** - IP of the DUT. If you have a device locally, it needs to be 58plugged into the test network and not corp network. You can also use a device 59in the lab. To reserve a device from the lab please follow these steps: 60 61* Setup skylab using go/skylab-tools-guide (Advanced users: Manual 62 installation) 63* "Lease" a dut go/skylab-dut-locking 64* Grab the host name, for example: chromeos15-row3-rack13-host2. Do not 65 include the prefix (e.g. "crossk") 66* Use this as the IP: chromeos15-row3-rack13-host2**.cros**. 67 68Full test name - test name can be grabbed from the control file. 69[Example](http://go/control-file-name). 70 71You can check other options for test_that by running: *test_that --help*. 72 73## Setting up a local DUT 74 75To run a test on a local DUT you need to make sure the DUT has been 76properly setup with a test build. You can use this helpful 77[tool](http://go/crosdl-usage). Execute from [here](https://cs.corp.google.com/chromeos_public/src/platform/crostestutils/provingground/crosdl.py) 78Run this command to put the build on a USB stick: 79```sh 80*./crosdl.py -c dev -t -b 12503.0.0 -p sarien --to_stick /dev/sda* 81``` 82Or this command to update the DUT directly(flaky): 83```sh 84*./crosdl.py -c dev -t -b 12105.54.0 -p sarien --to_ip $IP_ADDRESS* 85``` 86Note: The DUT must be reachable via SSH for this to work. 87 88 89To find out the right build number, please use [goldeneye](http://go/goldeneye) 90and search for the right build for your board. 91 92## Test Breakdown 93 94See the [Autotest Best Practices](https://chromium.googlesource.com/chromiumos/third_party/autotest/+/refs/heads/main/docs/best-practices.md#control-files) for general autotest information. 95This section will provide details on how Enterprise autotests are written. 96Each test will require the following: 97* A control file 98* Control files for each test configuration 99* A .py defining the test, which inherits test.test 100 101### Control files 102 103[Control files](https://chromium.googlesource.com/chromiumos/third_party/autotest/+/refs/heads/main/docs/best-practices.md#control-files) are used as the entry point to a test. 104 105A typical dir for a user policy (client) test will consist of control file(s) 106and, along with .py test file(s). A control file will contain basic description of the 107test as well as options such as these: 108``` python 109 AUTHOR = 'name' 110 NAME = 'full_test_name' 111 ATTRIBUTES = 'suite:ent-nightly, suite:policy' 112 TIME = 'SHORT' 113 TEST_CATEGORY = 'General' 114 TEST_CLASS = 'enterprise' 115 TEST_TYPE = 'client' 116``` 117 118On a user policy (client) test, there will be a base control file, plus an 119additional file for each test configuration. [Example](https://cs.corp.google.com/aosp-android10/external/autotest/client/site_tests/policy_AllowDinosaurEasterEgg/) 120In this example there is the "base" control file, with no args specified, which 121is simply named "control". Additionally there is a control file for each 122configuration of the test (.allow, .disallow, .not_set). The args to be 123passed to the test (.py) are specified in the final line of each of those 124control files. Example: 125``` python 126job.run_test('policy_AllowDinosaurEasterEgg', 127 case=True) 128```` 129 130### Test file 131 132Example of a basic [test](http://go/basic-ent-test). 133The class name of the test, ```policy_ShowHomeButton``` has to match the name 134of the .py file, and should ideally match the directory name as well. 135 136**run_once** - The function that gets called first. Parameters from the 137 control passed into this function. 138 139**setup_case** - sets up DMS, logs in, verifies policies values and various 140other login arguments. Defined: [enterprise_policy_base](http://go/ent-pol-base). Explained in detail below. 141 142**start_ui_root** - needed if you’re planning on interacting with UI objects 143during your test. Defined:[ui_utils](http://go/ent-ui-utils). 144This [CL](http://crrev.com/c/1531141) describes what ui_utils is based off 145and the usefulness of it. 146 147**check_home_button** - Function that verifies the presence of the Home button 148in this test. Depending on the policy setting, the test is using 149*ui.item_present* to verify the status of the Home button. 150 151Every enterprise test will require a run_once function and will most likely 152require setup_case. You will need to pass in a dictionary with the policy 153name and value into setup_case. 154 155### Useful utility 156 157This [utils.py](http://go/ent_util) file which contains many useful functions 158that you’ll come across in tests. 159 160**Some examples:** 161 162* **poll_for_condition** - keeps checking for condition to be true until a 163 timeout is reached at which point an error is raised. 164* **run** - runs a shell command on the DUT. 165 166### Difference between device policy test and user policy test 167 168To run test device policies the DUT will need to be fully enrolled, starting 169with a cleared TPM (thus a reboot). Client tests do not support rebooting the 170device before/during/after a test. 171 172In order to support clearing the TPM & rebooting, all device policies must be 173written as a ["server"](https://chromium.googlesource.com/chromiumos/third_party/autotest/+/refs/heads/main/docs/best-practices.md#when_why-to-write-a-server_side-test) test. 174Server tests (for Enterprise) will need a "server" control & test, in addition 175to having a client control file and a .py test file. The server test will do 176any server operations (reboot, servo control, wifi cell control, etc) 177 178Below is an example of testing a device 179[Example](http://go/ent-cont-example) of the server control file. This will 180run the server test [policy_DeviceServer](http://go/ent-test-example) and pass the parameters specified. 181The server test will clear the tpm, create an autotest client of the DUT, then 182run the autotest specified in the control file policy_DeviceAllowBluetooth. 183 184**Note** The parameterization control files are all of the server control 185files. The Client side [control file](http://go/ent-device-client-example) is only a 186pass through for the parameters from the control file, and does not set any new 187behavior. 188 189### Debugging an autotest 190 191Unfortunately there's no good debugging tool in autotest and you can't use pdb 192so you're left with using time.sleep and logging. With time.sleep you can pause 193the test and see what's going on in the actual device. When using logging you 194can run 'logging.info("what you want to log")' and then when the test is done 195running you can check the results here: 196/tmp/test_that_latest/results-1-TESTNAME/TESTNAME/debug/TESTNAME.INFO 197 198If a test is failing remotely, on stainless, you can view the logs there by 199clicking on the Logs link. You can also see the screenshot of the screen 200when a test errored/failed. 201 202### Using Servo board with Autotests 203 204Some tests require the use of the [Servo Board](http://go/servo-ent). 205If you want to get ahold of a servo board you need to reach out to crosdistros@ 206and request one. You can either get a Servo type A or Servo type C, in case 207your test involves controlling the power to the DUT. 208 209Setting up the servo, hopefully you'll find this 210[screenshot](https://screenshot.googleplex.com/PcZGhW5eqk3) useful. You can see 211that two cables on the left go to the DUT and the cable on the right goes into 212the host machine. If you're going to be feeding the power to the DUT you will 213also need to connect a Type-C charger to the Servo by plugging it into the 214slot marked "Dut Power". Note: if you grabbed the micro usb -> USB A cables 215in the tech stop make sure that the light on the switch glows orange and not 216green. If it's green the tests will not work. 217 218Starting the servo, from chroot run: "sudo servo_updater" make sure everything 219is up to date. Then run "sudo servod -b BOARD_NAME" BOARD_NAME being the board 220you have built on your server. While this is running, in another terminal tab 221you can now execute dut-control commands such as 222"dut-control servo_v4_role:scr". 223 224With the servod running you can now execute local tests using the servo board. 225[Example test using servo](http://go/servo-ent-example-test). 226 227## Enterprise Autotest Infra 228 229This section will focus on a basic explination of the [Enterprise base class](http://go/ent-pol-base) 230used for autotest, along with commonly used calls, APIs, etc. 231 232### Base class overview: 233 234The enterprise base class currently supports the following: 235* Enrolling with a fake account & DMS through the full OOBE flow. Commonly 236 used for device policy testing) 237* Kiosk enrollment with fake account 238* Enrolling for user policies (not requiring OOBE flow). 239* Enterprise ARC tests 240* Logging in with a real account/DMS 241* Enrolling with a real account- currently broken see http://crbug.com/1019320 242* Configuring User/Device/Extension policies with a fake DMS 243* Obtaining policies through an API 244* Verifying policies 245* UI interaction 246 247In addition to the features above, the base class will setup chrome for 248testing. This includes passing in username/password, browser flags, ARC 249settings, etc. 250 251 252### Policy Management 253 254Policy Managing with a fake DMS is mostly handled via the [policy_manager](http://go/ent-pol-manager). 255 256The Enterprise base class uses the policy manager to configure policies, 257set the policies with the fake DMS server, obtain policies from a DUT, and 258verify they are properly set (ie match the configured). In addition the policy 259manager handles features such as adding/updating/removing policies once after 260the initial setup, and make complex testing, such as extension of obfuscated 261policies easier to test. 262 263If a test is to fail with "Policy <POLICY_NAME> value was not set correctly.", 264the verification within the policy_manager is failing. This means the policy 265that was configured via the policy_manager does not match the value obtained 266from the DUT. 267 268When using the fake DMS (see [enterprise_fake_dmserver](http://go/fake-ent-dms)and [policy_testserver](http://go/fake-policy-server), 269policies are provided to the fDMS via a json blob which is created by the 270policy_manager. 271 272Policies from the DUT are obtained via an autotestprivate API, called via 273the [enterprise_policy_utils](http://go/ent-pol-utils) ```get_all_policies``` 274and policies are refreshed (ie force a refetch from the DMS) via 275```refresh_policies```. 276 277### Enrollment and Kiosk Mode 278 279Enterprise autotest uses the autotest [enrollment](http://go/ent-at-enrollment) to support 280device enrollment. 281 282This class has the ability to enroll both real and fake accounts, including 283walking through the enrollment OOBE flow. The actual interaction with the 284UI/APIs for login is acomplished by calling telemetry. 285 286Additionally Kiosk mode is also supported. 287 288 289### Chrome 290 291Tests interact with chrome (ie launch, define plugins, ARC settings, etc) via 292[chrome.py](http://go/autotest-chrome). chrome.py is built upon telemetry 293for browser interactions. The base class will handle chrome 294interaction for you, however there are specific examples such as the 295enrollment retainment test, that will interact with chrome.py directly. 296 297 298### Common Issues and possible solutions 299 300* Historically there have been issues with DUT enrollment via APIs. As of 301 R80-x, this should be resolved. Typically enrollment issues have an error 302 message along the lines of: 303 ```test did not pass (reason: Unhandled TimeoutException: Timed out while waiting 60s for _EnterpriseWebviewVisible.).``` 304 If this error is seen, it is typically related to something during the OOBE 305 flow, when waiting for the enterprise enrollment screen. 306* Some of the Enterprise Autotests use UI interaction/reading for the tests. 307 These UI elements change somewhat often, and will occasionally cause these 308 tests to break. UI based errors usually have a traceback leading to 309 ui.utils, and can often be fixed by simply update the UI element the test 310 is looking for. 311* Errors from chrome.py can also lead to Enterprise tests failing. This 312 package is not directly owned by Enterprise, or anyone other group, but 313 is a shared resource. If a test fails due to this package, it is likely 314 up to the test owner to fix, but they should be cognisant of other teams 315 using the package. 316* inspector_backend timeouts occasionally occur (<0.5% of all tests.) 317 The problem is traces backto a inspector backend crash/disconnect between 318 telemetry and the DUT.This error is well outside the scope of Enterprise 319 autotest. Rerunning the test is likely the easiest solution 320 321 322