Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
olivershen-wow committed Dec 29, 2023
1 parent b47a2ca commit 24793f4
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
4 changes: 3 additions & 1 deletion agent/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
storage
log
log
!/src/main/resources/test-services.apk
!/src/main/resources/orchestrator.apk
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.microsoft.hydralab.agent.runner.t2c.T2CRunner;
import com.microsoft.hydralab.agent.runner.xctest.XCTestRunner;
import com.microsoft.hydralab.agent.service.TestTaskEngineService;
import com.microsoft.hydralab.agent.util.FileLoadUtil;
import com.microsoft.hydralab.common.entity.agent.LLMProperties;
import com.microsoft.hydralab.common.entity.common.TestTask;
import com.microsoft.hydralab.common.management.AgentManagementService;
Expand Down Expand Up @@ -61,9 +62,9 @@ public EspressoRunner espressoRunner(AgentManagementService agentManagementServi
TestTaskEngineService testTaskEngineService,
TestRunDeviceOrchestrator testRunDeviceOrchestrator,
PerformanceTestManagementService performanceTestManagementService,
ADBOperateUtil adbOperateUtil) {
ADBOperateUtil adbOperateUtil, FileLoadUtil fileLoadUtil) {
return new EspressoRunner(agentManagementService, testTaskEngineService, testRunDeviceOrchestrator, performanceTestManagementService,
adbOperateUtil);
adbOperateUtil, fileLoadUtil);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,20 @@
import com.microsoft.hydralab.common.util.ThreadUtils;
import com.microsoft.hydralab.performance.InspectionStrategy;
import com.microsoft.hydralab.performance.PerformanceTestManagementService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.microsoft.hydralab.common.util.AgentConstant.ESPRESSO_TEST_ORCHESTRATOR_APK;
import static com.microsoft.hydralab.common.util.AgentConstant.ESPRESSO_TEST_SERVICES_APK;

public abstract class TestRunner implements TestRunEngine, TestRunLifecycle {
protected final Logger log = LoggerFactory.getLogger(TestRunner.class);
protected final AgentManagementService agentManagementService;
Expand All @@ -65,8 +55,6 @@ public TestRunner(AgentManagementService agentManagementService, TestTaskRunCall

void init() {
agentManagementService.registerFunctionAvailability(getClass().getName(), AgentFunctionAvailability.AgentFunctionType.TEST_RUNNER, true, getEnvCapabilityRequirements());
copyPreinstallAPK(agentManagementService.getPreAppDir(), ESPRESSO_TEST_ORCHESTRATOR_APK);
copyPreinstallAPK(agentManagementService.getPreAppDir(), ESPRESSO_TEST_SERVICES_APK);
}

protected abstract List<EnvCapabilityRequirement> getEnvCapabilityRequirements();
Expand Down Expand Up @@ -378,16 +366,4 @@ private Logger createLoggerForTestRun(TestRun testRun, String loggerNamePrefix,
public void stopTest(TestRunDevice testRunDevice) {
testRunDeviceOrchestrator.killAll(testRunDevice);
}

public void copyPreinstallAPK(File preAppDir, String fileName) {
File preinstallApk = new File(preAppDir, fileName);
if (preinstallApk.exists()) {
preinstallApk.delete();
}
try (InputStream resourceAsStream = FileUtils.class.getClassLoader().getResourceAsStream(fileName); OutputStream out = new FileOutputStream(preinstallApk)) {
IOUtils.copy(Objects.requireNonNull(resourceAsStream), out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.microsoft.hydralab.agent.runner.TestRunDeviceOrchestrator;
import com.microsoft.hydralab.agent.runner.TestRunner;
import com.microsoft.hydralab.agent.runner.TestTaskRunCallback;
import com.microsoft.hydralab.agent.util.FileLoadUtil;
import com.microsoft.hydralab.common.entity.agent.EnvCapability;
import com.microsoft.hydralab.common.entity.agent.EnvCapabilityRequirement;
import com.microsoft.hydralab.common.entity.common.DeviceInfo;
Expand Down Expand Up @@ -38,12 +39,14 @@ public class EspressoRunner extends TestRunner {
private static final int MAJOR_ADB_VERSION = 1;
private static final int MINOR_ADB_VERSION = -1;
final ADBOperateUtil adbOperateUtil;
final FileLoadUtil fileLoadUtil;

public EspressoRunner(AgentManagementService agentManagementService, TestTaskRunCallback testTaskRunCallback,
TestRunDeviceOrchestrator testRunDeviceOrchestrator, PerformanceTestManagementService performanceTestManagementService,
ADBOperateUtil adbOperateUtil) {
ADBOperateUtil adbOperateUtil, FileLoadUtil fileLoadUtil) {
super(agentManagementService, testTaskRunCallback, testRunDeviceOrchestrator, performanceTestManagementService);
this.adbOperateUtil = adbOperateUtil;
this.fileLoadUtil = fileLoadUtil;
}

@Override
Expand Down Expand Up @@ -188,16 +191,19 @@ private String buildCommand(String suiteName, String testPkgName,
protected void reinstallOrchestratorDependency(TestRunDevice testRunDevice, TestTask testTask, Logger reportLogger) throws Exception {
checkTestTaskCancel(testTask);

String pathToTestOrchestratorApk = this.fileLoadUtil.copyPreinstallAPK(ESPRESSO_TEST_ORCHESTRATOR_APK);
String pathToTestServicesApk = this.fileLoadUtil.copyPreinstallAPK(ESPRESSO_TEST_SERVICES_APK);

testRunDeviceOrchestrator.uninstallApp(testRunDevice, "androidx.test.services", reportLogger);
ThreadUtils.safeSleep(2000);
testRunDeviceOrchestrator.uninstallApp(testRunDevice, "androidx.test.orchestrator", reportLogger);
ThreadUtils.safeSleep(2000);

FlowUtil.retryAndSleepWhenFalse(3, 5,
() -> testRunDeviceOrchestrator.installApp(testRunDevice, new File(agentManagementService.getPreAppDir(), ESPRESSO_TEST_ORCHESTRATOR_APK).getAbsolutePath(),
() -> testRunDeviceOrchestrator.installApp(testRunDevice, new File(pathToTestOrchestratorApk).getAbsolutePath(),
reportLogger));
FlowUtil.retryAndSleepWhenFalse(3, 5,
() -> testRunDeviceOrchestrator.installApp(testRunDevice, new File(agentManagementService.getPreAppDir(), ESPRESSO_TEST_SERVICES_APK).getAbsolutePath(),
() -> testRunDeviceOrchestrator.installApp(testRunDevice, new File(pathToTestServicesApk).getAbsolutePath(),
reportLogger));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
import com.microsoft.hydralab.common.util.FileUtil;
import com.microsoft.hydralab.common.util.FlowUtil;
import com.microsoft.hydralab.common.util.HydraLabRuntimeException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
Expand Down Expand Up @@ -133,4 +140,25 @@ private File downloadFile(StorageFileInfo attachment) {
}
return file;
}

public String copyPreinstallAPK(String fileName) {
File dir = new File(appOptions.getTestPackageLocation(), "temp");
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new RuntimeException("create dir fail: " + dir.getAbsolutePath());
}
}

File preinstallApk = new File(dir.getAbsolutePath(), fileName);
if (preinstallApk.exists()) {
preinstallApk.delete();
}
try (InputStream resourceAsStream = FileUtils.class.getClassLoader().getResourceAsStream(fileName); OutputStream out = new FileOutputStream(preinstallApk)) {
IOUtils.copy(Objects.requireNonNull(resourceAsStream), out);
} catch (IOException e) {
e.printStackTrace();
}

return preinstallApk.getAbsolutePath();
}
}
Binary file added agent/src/main/resources/orchestrator.apk
Binary file not shown.
Binary file added agent/src/main/resources/test-services.apk
Binary file not shown.

0 comments on commit 24793f4

Please sign in to comment.