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 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/master/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/master/docs/best-practices.md#control-files) are used as the entry point to a test. 104A typical dir for a user policy (client) test will consist of control file(s) 105and, along with .py test file(s). A control file will contain basic description of the 106test as well as options such as these: 107``` python 108 AUTHOR = 'name' 109 NAME = 'full_test_name' 110 ATTRIBUTES = 'suite:ent-nightly, suite:policy' 111 TIME = 'SHORT' 112 TEST_CATEGORY = 'General' 113 TEST_CLASS = 'enterprise' 114 TEST_TYPE = 'client' 115``` 116 117On a user policy (client) test, there will be a base control file, plus an 118additional file for each test configuration. [Example](https://cs.corp.google.com/aosp-android10/external/autotest/client/site_tests/policy_AllowDinosaurEasterEgg/) 119In this example there is the "base" control file, with no args specified, which 120is simply named "control". Additionally there is a control file for each 121configuration of the test (.allow, .disallow, .not_set). The args to be 122passed to the test (.py) are specified in the final line of each of those 123control files. Example: 124``` python 125job.run_test('policy_AllowDinosaurEasterEgg', 126 case=True) 127```` 128 129### Test file 130 131Example of a basic [test](http://go/basic-ent-test). 132The class name of the test, ```policy_ShowHomeButton``` has to match the name 133of the .py file, and should ideally match the directory name as well. 134 135**run_once** - The function that gets called first. Parameters from the 136 control passed into this function. 137 138**setup_case** - sets up DMS, logs in, verifies policies values and various 139other login arguments. Defined: [enterprise_policy_base](http://go/ent-pol-base). Explained in detail below. 140 141**start_ui_root** - needed if you’re planning on interacting with UI objects 142during your test. Defined:[ui_utils](http://go/ent-ui-utils). 143This [CL](http://crrev.com/c/1531141) describes what ui_utils is based off 144and the usefulness of it. 145 146**check_home_button** - Function that verifies the presence of the Home button 147in this test. Depending on the policy setting, the test is using 148*ui.item_present* to verify the status of the Home button. 149 150Every enterprise test will require a run_once function and will most likely 151require setup_case. You will need to pass in a dictionary with the policy 152name and value into setup_case. 153 154### Useful utility 155 156This [utils.py](http://go/ent_util) file which contains many useful functions 157that you’ll come across in tests. 158 159**Some examples:** 160 161* **poll_for_condition** - keeps checking for condition to be true until a 162 timeout is reached at which point an error is raised. 163* **run** - runs a shell command on the DUT. 164 165### Difference between device policy test and user policy test 166 167To run test device policies the DUT will need to be fully enrolled, starting 168with a cleared TPM (thus a reboot). Client tests do not support rebooting the 169device before/during/after a test. 170 171In order to support clearing the TPM & rebooting, all device policies must be 172written as a ["server"](https://chromium.googlesource.com/chromiumos/third_party/autotest/+/refs/heads/master/docs/best-practices.md#when_why-to-write-a-server_side-test) test. 173Server tests (for Enterprise) will need a "server" control & test, in addition 174to having a client control file and a .py test file. The server test will do 175any server operations (reboot, servo control, wifi cell control, etc) 176 177Below is an example of testing a device 178[Example](http://go/ent-cont-example) of the server control file. This will 179run the server test [policy_DeviceServer](http://go/ent-test-example) and pass the parameters specified. 180The server test will clear the tpm, create an autotest client of the DUT, then 181run the autotest specified in the control file policy_DeviceAllowBluetooth. 182 183**Note** The parameterization control files are all of the server control 184files. The Client side [control file](http://go/ent-device-client-example) is only a 185pass through for the parameters from the control file, and does not set any new 186behavior. 187 188### Debugging an autotest 189 190Unfortunately there's no good debugging tool in autotest and you can't use pdb 191so you're left with using time.sleep and logging. With time.sleep you can pause 192the test and see what's going on in the actual device. When using logging you 193can run 'logging.info("what you want to log")' and then when the test is done 194running you can check the results here: 195/tmp/test_that_latest/results-1-TESTNAME/TESTNAME/debug/TESTNAME.INFO 196 197If a test is failing remotely, on stainless, you can view the logs there by 198clicking on the Logs link. You can also see the screenshot of the screen 199when a test errored/failed. 200 201### Using Servo board with Autotests 202 203Some tests require the use of the [Servo Board](http://go/servo-ent). 204If you want to get ahold of a servo board you need to reach out to crosdistros@ 205and request one. You can either get a Servo type A or Servo type C, in case 206your test involves controlling the power to the DUT. 207 208Setting up the servo, hopefully you'll find this 209[screenshot](https://screenshot.googleplex.com/PcZGhW5eqk3) useful. You can see 210that two cables on the left go to the DUT and the cable on the right goes into 211the host machine. If you're going to be feeding the power to the DUT you will 212also need to connect a Type-C charger to the Servo by plugging it into the 213slot marked "Dut Power". Note: if you grabbed the micro usb -> USB A cables 214in the tech stop make sure that the light on the switch glows orange and not 215green. If it's green the tests will not work. 216 217Starting the servo, from chroot run: "sudo servo_updater" make sure everything 218is up to date. Then run "sudo servod -b BOARD_NAME" BOARD_NAME being the board 219you have built on your server. While this is running, in another terminal tab 220you can now execute dut-control commands such as 221"dut-control servo_v4_role:scr". 222 223With the servod running you can now execute local tests using the servo board. 224[Example test using servo](http://go/servo-ent-example-test). 225 226## Enterprise Autotest Infra 227 228This section will focus on a basic explination of the [Enterprise base class](http://go/ent-pol-base) 229used for autotest, along with commonly used calls, APIs, etc. 230 231### Base class overview: 232 233The enterprise base class currently supports the following: 234* Enrolling with a fake account & DMS through the full OOBE flow. Commonly 235 used for device policy testing) 236* Kiosk enrollment with fake account 237* Enrolling for user policies (not requiring OOBE flow). 238* Enterprise ARC tests 239* Logging in with a real account/DMS 240* Enrolling with a real account- currently broken see http://crbug.com/1019320 241* Configuring User/Device/Extension policies with a fake DMS 242* Obtaining policies through an API 243* Verifying policies 244* UI interaction 245 246In addition to the features above, the base class will setup chrome for 247testing. This includes passing in username/password, browser flags, ARC 248settings, etc. 249 250 251### Policy Management 252 253Policy Managing with a fake DMS is mostly handled via the [policy_manager](http://go/ent-pol-manager). 254 255The Enterprise base class uses the policy manager to configure policies, 256set the policies with the fake DMS server, obtain policies from a DUT, and 257verify they are properly set (ie match the configured). In addition the policy 258manager handles features such as adding/updating/removing policies once after 259the initial setup, and make complex testing, such as extension of obfuscated 260policies easier to test. 261 262If a test is to fail with "Policy <POLICY_NAME> value was not set correctly.", 263the verification within the policy_manager is failing. This means the policy 264that was configured via the policy_manager does not match the value obtained 265from the DUT. 266 267When using the fake DMS (see [enterprise_fake_dmserver](http://go/fake-ent-dms)and [policy_testserver](http://go/fake-policy-server), 268policies are provided to the fDMS via a json blob which is created by the 269policy_manager. 270 271Policies from the DUT are obtained via an autotestprivate API, called via 272the [enterprise_policy_utils](http://go/ent-pol-utils) ```get_all_policies``` 273and policies are refreshed (ie force a refetch from the DMS) via 274```refresh_policies```. 275 276### Enrollment and Kiosk Mode 277 278Enterprise autotest uses the autotest [enrollment](http://go/ent-at-enrollment) to support 279device enrollment. 280 281This class has the ability to enroll both real and fake accounts, including 282walking through the enrollment OOBE flow. The actual interaction with the 283UI/APIs for login is acomplished by calling telemetry. 284 285Additionally Kiosk mode is also supported. 286 287 288### Chrome 289 290Tests interact with chrome (ie launch, define plugins, ARC settings, etc) via 291[chrome.py](http://go/autotest-chrome). chrome.py is built upon telemetry 292for browser interactions. The base class will handle chrome 293interaction for you, however there are specific examples such as the 294enrollment retainment test, that will interact with chrome.py directly. 295 296 297### Common Issues and possible solutions 298 299* Historically there have been issues with DUT enrollment via APIs. As of 300 R80-x, this should be resolved. Typically enrollment issues have an error 301 message along the lines of: 302 ```test did not pass (reason: Unhandled TimeoutException: Timed out while waiting 60s for _EnterpriseWebviewVisible.).``` 303 If this error is seen, it is typically related to something during the OOBE 304 flow, when waiting for the enterprise enrollment screen. 305* Some of the Enterprise Autotests use UI interaction/reading for the tests. 306 These UI elements change somewhat often, and will occasionally cause these 307 tests to break. UI based errors usually have a traceback leading to 308 ui.utils, and can often be fixed by simply update the UI element the test 309 is looking for. 310* Errors from chrome.py can also lead to Enterprise tests failing. This 311 package is not directly owned by Enterprise, or anyone other group, but 312 is a shared resource. If a test fails due to this package, it is likely 313 up to the test owner to fix, but they should be cognisant of other teams 314 using the package. 315* inspector_backend timeouts occasionally occur (<0.5% of all tests.) 316 The problem is traces backto a inspector backend crash/disconnect between 317 telemetry and the DUT.This error is well outside the scope of Enterprise 318 autotest. Rerunning the test is likely the easiest solution 319 320 321