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