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 com.android.microdroid.test.host; 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 = runForResultWithTimeout(timeoutMillis, cmd); 70 if (result.getStatus() != CommandStatus.SUCCESS) { 71 fail(join(cmd) + " has failed: " + result); 72 } 73 return result.getStdout().trim(); 74 } 75 runForResultWithTimeout(long timeoutMillis, String... cmd)76 public CommandResult runForResultWithTimeout(long timeoutMillis, String... cmd) 77 throws DeviceNotAvailableException { 78 return mDevice.executeShellV2Command( 79 join(cmd), timeoutMillis, java.util.concurrent.TimeUnit.MILLISECONDS); 80 } 81 runForResult(String... cmd)82 public CommandResult runForResult(String... cmd) throws DeviceNotAvailableException { 83 return mDevice.executeShellV2Command(join(cmd)); 84 } 85 assumeSuccess(String... cmd)86 public void assumeSuccess(String... cmd) throws DeviceNotAvailableException { 87 assumeThat(runForResult(cmd).getStatus(), is(CommandStatus.SUCCESS)); 88 } 89 join(String... strs)90 private static String join(String... strs) { 91 return String.join(" ", Arrays.asList(strs)); 92 } 93 } 94