• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 android.virt.test;
18 
19 import static org.hamcrest.CoreMatchers.is;
20 import static org.junit.Assert.fail;
21 import static org.junit.Assume.assumeThat;
22 
23 import com.android.tradefed.device.DeviceNotAvailableException;
24 import com.android.tradefed.device.ITestDevice;
25 import com.android.tradefed.log.LogUtil.CLog;
26 import com.android.tradefed.util.CommandResult;
27 import com.android.tradefed.util.CommandStatus;
28 
29 import java.util.Arrays;
30 
31 import javax.annotation.Nonnull;
32 
33 /** A helper class to provide easy way to run commands on a test device. */
34 public class CommandRunner {
35 
36     /** Default timeout. 30 sec because Microdroid is extremely slow on GCE-on-CF. */
37     private static final long DEFAULT_TIMEOUT = 30000;
38 
39     private ITestDevice mDevice;
40 
CommandRunner(@onnull ITestDevice device)41     public CommandRunner(@Nonnull ITestDevice device) {
42         mDevice = device;
43     }
44 
getDevice()45     public ITestDevice getDevice() {
46         return mDevice;
47     }
48 
run(String... cmd)49     public String run(String... cmd) throws DeviceNotAvailableException {
50         CommandResult result = runForResult(cmd);
51         if (result.getStatus() != CommandStatus.SUCCESS) {
52             fail(join(cmd) + " has failed: " + result);
53         }
54         return result.getStdout().trim();
55     }
56 
tryRun(String... cmd)57     public String tryRun(String... cmd) throws DeviceNotAvailableException {
58         CommandResult result = runForResult(cmd);
59         if (result.getStatus() == CommandStatus.SUCCESS) {
60             return result.getStdout().trim();
61         } else {
62             CLog.d(join(cmd) + " has failed (but ok): " + result);
63             return null;
64         }
65     }
66 
runWithTimeout(long timeoutMillis, String... cmd)67     public String runWithTimeout(long timeoutMillis, String... cmd)
68             throws DeviceNotAvailableException {
69         CommandResult result =
70                 mDevice.executeShellV2Command(
71                         join(cmd), timeoutMillis, java.util.concurrent.TimeUnit.MILLISECONDS);
72         if (result.getStatus() != CommandStatus.SUCCESS) {
73             fail(join(cmd) + " has failed: " + result);
74         }
75         return result.getStdout().trim();
76     }
77 
runForResultWithTimeout(long timeoutMillis, String... cmd)78     public CommandResult runForResultWithTimeout(long timeoutMillis, String... cmd)
79             throws DeviceNotAvailableException {
80         return mDevice.executeShellV2Command(
81                 join(cmd), timeoutMillis, java.util.concurrent.TimeUnit.MILLISECONDS);
82     }
83 
runForResult(String... cmd)84     public CommandResult runForResult(String... cmd) throws DeviceNotAvailableException {
85         return mDevice.executeShellV2Command(join(cmd));
86     }
87 
assumeSuccess(String... cmd)88     public void assumeSuccess(String... cmd) throws DeviceNotAvailableException {
89         assumeThat(runForResult(cmd).getStatus(), is(CommandStatus.SUCCESS));
90     }
91 
join(String... strs)92     private static String join(String... strs) {
93         return String.join(" ", Arrays.asList(strs));
94     }
95 }
96