1 /*
2  * Copyright (C) 2022 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.tradefed.device;
18 
19 import com.android.tradefed.config.GlobalConfiguration;
20 import com.android.tradefed.log.LogUtil.CLog;
21 import com.android.tradefed.result.error.DeviceErrorIdentifier;
22 import com.android.tradefed.util.CommandResult;
23 import com.android.tradefed.util.CommandStatus;
24 import com.android.tradefed.util.IRunUtil;
25 import com.android.tradefed.util.RunUtil;
26 
27 import java.util.Arrays;
28 
29 import javax.annotation.Nonnull;
30 
31 /** A helper class for running commands on Microdroid {@link TestDevice}. */
32 class MicrodroidHelper {
33 
34     // Run a shell command on Microdroid
runOnMicrodroid(@onnull final String microdroidSerial, String... cmd)35     public String runOnMicrodroid(@Nonnull final String microdroidSerial, String... cmd) {
36         final long timeout = 30000; // 30 sec. Microdroid is extremely slow on GCE-on-CF.
37         IDeviceManager deviceManager = GlobalConfiguration.getDeviceManagerInstance();
38         CommandResult result =
39                 getRunUtil()
40                         .runTimedCmd(
41                                 timeout,
42                                 deviceManager.getAdbPath(),
43                                 "-s",
44                                 microdroidSerial,
45                                 "shell",
46                                 join(cmd));
47         if (result.getStatus() != CommandStatus.SUCCESS) {
48             throw new DeviceRuntimeException(
49                     join(cmd) + " has failed: " + result,
50                     DeviceErrorIdentifier.SHELL_COMMAND_ERROR);
51         }
52         return result.getStdout().trim();
53     }
54 
55     // Same as runOnMicrodroid, but returns null on error.
tryRunOnMicrodroid(@onnull final String microdroidSerial, String... cmd)56     public String tryRunOnMicrodroid(@Nonnull final String microdroidSerial, String... cmd) {
57         final long timeout = 30000; // 30 sec. Microdroid is extremely slow on GCE-on-CF.
58         IDeviceManager deviceManager = GlobalConfiguration.getDeviceManagerInstance();
59         CommandResult result =
60                 getRunUtil()
61                         .runTimedCmd(
62                                 timeout,
63                                 deviceManager.getAdbPath(),
64                                 "-s",
65                                 microdroidSerial,
66                                 "shell",
67                                 join(cmd));
68         if (result.getStatus() == CommandStatus.SUCCESS) {
69             return result.getStdout().trim();
70         } else {
71             CLog.d(join(cmd) + " has failed (but ok): " + result);
72             return null;
73         }
74     }
75 
getRunUtil()76     private IRunUtil getRunUtil() {
77         return RunUtil.getDefault();
78     }
79 
join(String... strs)80     private static String join(String... strs) {
81         return String.join(" ", Arrays.asList(strs));
82     }
83 }
84