1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compatibility.targetprep;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.invoker.TestInformation;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.error.DeviceErrorIdentifier;
24 import com.android.tradefed.targetprep.ITargetPreparer;
25 import com.android.tradefed.targetprep.TargetSetupError;
26 import com.android.tradefed.util.CommandResult;
27 
28 import com.google.common.annotations.VisibleForTesting;
29 
30 /**
31  * Checks and recover GMS on a device.
32  *
33  * <p>This preparer checks whether the GMS process is running during setUp and tearDown. If GMS is
34  * not running before the test, a reboot will be attempted to recover.
35  */
36 public final class CheckGmsPreparer implements ITargetPreparer {
37 
38     private static final long WAIT_FOR_BOOT_COMPLETE_TIMEOUT_MILLIS = 1000 * 60;
39     @VisibleForTesting static final String CHECK_GMS_COMMAND = "pidof com.google.android.gms";
40     @VisibleForTesting static final String OPTION_ENABLE = "enable";
41 
42     @Option(name = OPTION_ENABLE, description = "Enable GMS checks.")
43     protected boolean mEnable = false;
44 
45     /** {@inheritDoc} */
46     @Override
setUp(TestInformation testInfo)47     public void setUp(TestInformation testInfo)
48             throws TargetSetupError, DeviceNotAvailableException {
49         if (!mEnable || isGmsRunning(testInfo)) {
50             return;
51         }
52 
53         CLog.e("Did not detect a running GMS process, rebooting device to recover");
54         testInfo.getDevice().reboot();
55         testInfo.getDevice().waitForBootComplete(WAIT_FOR_BOOT_COMPLETE_TIMEOUT_MILLIS);
56 
57         if (!isGmsRunning(testInfo)) {
58             CLog.e("GMS process still not running, throwing error");
59             mEnable = false;
60             throw new TargetSetupError(
61                     "GMS required but did not detect a running GMS process after device reboot",
62                     testInfo.getDevice().getDeviceDescriptor(),
63                     DeviceErrorIdentifier.DEVICE_UNEXPECTED_RESPONSE);
64         }
65     }
66 
isGmsRunning(TestInformation testInfo)67     private static boolean isGmsRunning(TestInformation testInfo)
68             throws DeviceNotAvailableException {
69         CommandResult res = testInfo.getDevice().executeShellV2Command(CHECK_GMS_COMMAND);
70         if (res.getExitCode() == 0) {
71             CLog.d("Detected a running GMS process with PID: %s", res.getStdout());
72             return true;
73         }
74 
75         CLog.e(
76                 "Check GMS command returned non zero exit code. Command: %s, Result: %s",
77                 CHECK_GMS_COMMAND, res.toString());
78         return false;
79     }
80 
81     /** {@inheritDoc} */
82     @Override
tearDown(TestInformation testInfo, Throwable e)83     public void tearDown(TestInformation testInfo, Throwable e) throws DeviceNotAvailableException {
84         if (!mEnable || isGmsRunning(testInfo)) {
85             return;
86         }
87 
88         CLog.e("Did not detect a running GMS process on tearDown");
89     }
90 }
91