Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviceManager refactor2: Split deviceManger into agentManagementService and testDeviceManager #332

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.microsoft.hydralab.agent.runner.smart.SmartTestUtil;
import com.microsoft.hydralab.agent.service.AgentWebSocketClientService;
import com.microsoft.hydralab.agent.socket.AgentWebSocketClient;
import com.microsoft.hydralab.common.file.StorageServiceClientProxy;
import com.microsoft.hydralab.common.management.AgentManagementService;
import com.microsoft.hydralab.common.management.AgentType;
import com.microsoft.hydralab.common.management.AppiumServerManager;
import com.microsoft.hydralab.common.management.device.TestDeviceManager;
Expand All @@ -19,7 +21,6 @@
import com.microsoft.hydralab.common.monitor.MetricPushGateway;
import com.microsoft.hydralab.common.util.ADBOperateUtil;
import com.microsoft.hydralab.common.util.Const;
import com.microsoft.hydralab.common.file.StorageServiceClientProxy;
import io.micrometer.core.instrument.MeterRegistry;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.PushGateway;
Expand Down Expand Up @@ -86,6 +87,44 @@ private File getScreenshotDir() {
return dir;
}

@Bean
public AgentManagementService agentManagementService(StorageServiceClientProxy storageServiceClientProxy,
DeviceStatusListenerManager deviceStatusListenerManager) {
AgentManagementService agentManagementService = new AgentManagementService();
File testBaseDir = new File(appOptions.getTestCaseResultLocation());
if (!testBaseDir.exists()) {
if (!testBaseDir.mkdirs()) {
throw new RuntimeException("agentManager dir.mkdirs() failed: " + testBaseDir);
}
}
agentManagementService.setTestBaseDir(testBaseDir);
File preAppDir = new File(appOptions.getPreAppStorageLocation());
if (!preAppDir.exists()) {
if (!preAppDir.mkdirs()) {
throw new RuntimeException("agentManager dir.mkdirs() failed: " + preAppDir);
}
}
agentManagementService.setPreAppDir(preAppDir);
agentManagementService.setPreInstallFailurePolicy(
shutdownIfFail ? Const.PreInstallFailurePolicy.SHUTDOWN : Const.PreInstallFailurePolicy.IGNORE);
agentManagementService.setDeviceStatusListenerManager(deviceStatusListenerManager);
agentManagementService.setTestBaseDirUrlMapping(AppOptions.TEST_CASE_RESULT_STORAGE_MAPPING_REL_PATH);
File deviceLogBaseDir = new File(appOptions.getDeviceLogStorageLocation());
if (!deviceLogBaseDir.exists()) {
if (!deviceLogBaseDir.mkdirs()) {
throw new RuntimeException("agentManager dir.mkdirs() failed: " + deviceLogBaseDir);
}
}
agentManagementService.setDeviceLogBaseDir(deviceLogBaseDir);
agentManagementService.setStorageServiceClientProxy(storageServiceClientProxy);

agentManagementService.setScreenshotDir(getScreenshotDir());
agentManagementService.setDeviceFolderUrlPrefix(AppOptions.DEVICE_STORAGE_MAPPING_REL_PATH);
agentManagementService.setDeviceStoragePath(appOptions.getDeviceStorageLocation());

return agentManagementService;
}

@Bean
public AgentWebSocketClient agentWebSocketClient(AgentWebSocketClientService agentWebSocketClientService)
throws Exception {
Expand All @@ -98,8 +137,9 @@ public AgentWebSocketClient agentWebSocketClient(AgentWebSocketClientService age
}

@Bean
public TestDeviceManager initDeviceManager(StorageServiceClientProxy storageServiceClientProxy, ADBOperateUtil adbOperateUtil, AppiumServerManager appiumServerManager,
DeviceStatusListenerManager deviceStatusListenerManager) {
public TestDeviceManager initDeviceManager(AgentManagementService agentManagementService,
ADBOperateUtil adbOperateUtil,
AppiumServerManager appiumServerManager) {
AgentType agentType = AgentType.formAgentType(agentTypeValue);
TestDeviceManager testDeviceManager = agentType.getManager();
if (testDeviceManager instanceof AndroidTestDeviceManager) {
Expand All @@ -109,43 +149,14 @@ public TestDeviceManager initDeviceManager(StorageServiceClientProxy storageServ
logger.info("Setting the adb server hostname to {}", adbServerHost);
adbOperateUtil.setAdbServerHost(adbServerHost);
}
File testBaseDir = new File(appOptions.getTestCaseResultLocation());
if (!testBaseDir.exists()) {
if (!testBaseDir.mkdirs()) {
throw new RuntimeException("adbDeviceManager dir.mkdirs() failed: " + testBaseDir);
}
}
testDeviceManager.setTestBaseDir(testBaseDir);
File preAppDir = new File(appOptions.getPreAppStorageLocation());
if (!preAppDir.exists()) {
if (!preAppDir.mkdirs()) {
throw new RuntimeException("adbDeviceManager dir.mkdirs() failed: " + preAppDir);
}
}
testDeviceManager.setPreAppDir(preAppDir);
testDeviceManager.setPreInstallFailurePolicy(
shutdownIfFail ? Const.PreInstallFailurePolicy.SHUTDOWN : Const.PreInstallFailurePolicy.IGNORE);
testDeviceManager.setDeviceStatusListenerManager(deviceStatusListenerManager);
testDeviceManager.setTestBaseDirUrlMapping(AppOptions.TEST_CASE_RESULT_STORAGE_MAPPING_REL_PATH);
File deviceLogBaseDir = new File(appOptions.getDeviceLogStorageLocation());
if (!deviceLogBaseDir.exists()) {
if (!deviceLogBaseDir.mkdirs()) {
throw new RuntimeException("adbDeviceManager dir.mkdirs() failed: " + deviceLogBaseDir);
}
}
testDeviceManager.setDeviceLogBaseDir(deviceLogBaseDir);
testDeviceManager.setStorageServiceClientProxy(storageServiceClientProxy);

testDeviceManager.setScreenshotDir(getScreenshotDir());
testDeviceManager.setDeviceFolderUrlPrefix(AppOptions.DEVICE_STORAGE_MAPPING_REL_PATH);
testDeviceManager.setDeviceStoragePath(appOptions.getDeviceStorageLocation());

if (StringUtils.isNotBlank(appiumServerHost)) {
logger.info("Setting the appium server hostname to {}", appiumServerHost);
appiumServerManager.setAppiumServerHost(appiumServerHost);
}
appiumServerManager.setWorkspacePath(appOptions.getLocation());

testDeviceManager.setAgentManagementService(agentManagementService);
testDeviceManager.setAppiumServerManager(appiumServerManager);
return testDeviceManager;
}
Expand Down Expand Up @@ -177,13 +188,14 @@ public SmartTestUtil smartTestUtil() {
}

@Bean
public DeviceStabilityMonitor deviceStabilityMonitor(TestDeviceManager testDeviceManager, MeterRegistry meterRegistry) {
public DeviceStabilityMonitor deviceStabilityMonitor(AgentManagementService agentManagementService,
MeterRegistry meterRegistry) {
DeviceStabilityMonitor deviceStabilityMonitor = new DeviceStabilityMonitor();

deviceStabilityMonitor.setDeviceStateChangeThreshold(deviceStateChangeThreshold);
deviceStabilityMonitor.setDeviceStateChangeWindowTime(deviceStateChangeWindowTime);
deviceStabilityMonitor.setDeviceStateChangeRecoveryTime(deviceStateChangeRecoveryTime);
deviceStabilityMonitor.setTestDeviceManager(testDeviceManager);
deviceStabilityMonitor.setAgentManagementService(agentManagementService);
deviceStabilityMonitor.setMeterRegistry(meterRegistry);

return deviceStabilityMonitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.microsoft.hydralab.agent.runner.t2c.T2CRunner;
import com.microsoft.hydralab.agent.service.TestTaskEngineService;
import com.microsoft.hydralab.common.entity.common.TestTask;
import com.microsoft.hydralab.common.management.device.TestDeviceManager;
import com.microsoft.hydralab.common.management.AgentManagementService;
import com.microsoft.hydralab.common.util.ADBOperateUtil;
import com.microsoft.hydralab.performance.PerformanceTestManagementService;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -49,54 +49,62 @@ public PerformanceTestManagementService performanceTestManagementService() {
}

@Bean
public EspressoRunner espressoRunner(TestDeviceManager testDeviceManager, TestTaskEngineService testTaskEngineService,
public EspressoRunner espressoRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService,
ADBOperateUtil adbOperateUtil) {
return new EspressoRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService,
return new EspressoRunner(agentManagementService, testTaskEngineService, performanceTestManagementService,
adbOperateUtil);
}

@Bean
public AdbMonkeyRunner adbMonkeyRunner(TestDeviceManager testDeviceManager, TestTaskEngineService testTaskEngineService,
public AdbMonkeyRunner adbMonkeyRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService,
ADBOperateUtil adbOperateUtil) {
return new AdbMonkeyRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService,
return new AdbMonkeyRunner(agentManagementService, testTaskEngineService, performanceTestManagementService,
adbOperateUtil);
}

@Bean
public AppiumMonkeyRunner appiumMonkeyRunner(TestDeviceManager testDeviceManager,
public AppiumMonkeyRunner appiumMonkeyRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService) {
return new AppiumMonkeyRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService);
return new AppiumMonkeyRunner(agentManagementService, testTaskEngineService,
performanceTestManagementService);
}

@Bean
public AppiumRunner appiumRunner(TestDeviceManager testDeviceManager, TestTaskEngineService testTaskEngineService,
public AppiumRunner appiumRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService) {
return new AppiumRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService);
return new AppiumRunner(agentManagementService, testTaskEngineService, performanceTestManagementService);
}

@Bean
public AppiumCrossRunner appiumCrossRunner(TestDeviceManager testDeviceManager,
public AppiumCrossRunner appiumCrossRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService) {
return new AppiumCrossRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService,
return new AppiumCrossRunner(agentManagementService, testTaskEngineService,
performanceTestManagementService,
agentName);
}

@Bean
public SmartRunner smartRunner(TestDeviceManager testDeviceManager, TestTaskEngineService testTaskEngineService,
public SmartRunner smartRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService,
SmartTestUtil smartTestUtil) {
return new SmartRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService,
return new SmartRunner(agentManagementService, testTaskEngineService, performanceTestManagementService,
smartTestUtil);
}

@Bean
public T2CRunner t2cRunner(TestDeviceManager testDeviceManager, TestTaskEngineService testTaskEngineService,
public T2CRunner t2cRunner(AgentManagementService agentManagementService,
TestTaskEngineService testTaskEngineService,
PerformanceTestManagementService performanceTestManagementService) {
return new T2CRunner(testDeviceManager, testTaskEngineService, performanceTestManagementService, agentName);
return new T2CRunner(agentManagementService, testTaskEngineService, performanceTestManagementService,
agentName);
}

@ConfigurationProperties(prefix = "app.device-script.commands")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

import com.microsoft.hydralab.common.entity.agent.DeviceTaskControl;
import com.microsoft.hydralab.common.entity.common.DeviceInfo;
import com.microsoft.hydralab.common.management.device.TestDeviceManager;
import com.microsoft.hydralab.common.util.ThreadPoolUtil;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -22,8 +20,6 @@
public class DeviceTaskControlExecutor {
@SuppressWarnings("constantname")
static final Logger log = LoggerFactory.getLogger(DeviceTaskControlExecutor.class);
@Resource
TestDeviceManager testDeviceManager;

@Nullable
public DeviceTaskControl runForAllDeviceAsync(Collection<DeviceInfo> allDevices, DeviceTask task,
Expand Down Expand Up @@ -72,7 +68,7 @@ public DeviceTaskControl runForAllDeviceAsync(Collection<DeviceInfo> allDevices,
}
Logger logger = null;
if (logging) {
logger = testDeviceManager.getDeviceLogger(device);
logger = device.getTestDeviceManager().getDeviceLogger(device);
}
final Logger fLogger = logger;
devices.add(device);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.microsoft.hydralab.common.entity.common.DeviceInfo;
import com.microsoft.hydralab.common.entity.common.TestRun;
import com.microsoft.hydralab.common.entity.common.TestTask;
import com.microsoft.hydralab.common.management.AgentManagementService;
import com.microsoft.hydralab.common.management.device.TestDeviceManager;
import com.microsoft.hydralab.common.management.device.impl.IOSTestDeviceManager;
import com.microsoft.hydralab.common.util.DateUtil;
Expand All @@ -29,16 +30,17 @@
import java.util.concurrent.TimeoutException;

public abstract class TestRunner {
protected final Logger log = LoggerFactory.getLogger(TestDeviceManager.class);
protected final TestDeviceManager testDeviceManager;
protected final Logger log = LoggerFactory.getLogger(TestRunner.class);
protected final AgentManagementService agentManagementService;
protected final TestTaskRunCallback testTaskRunCallback;
protected final PerformanceTestManagementService performanceTestManagementService;
protected final XmlBuilder xmlBuilder = new XmlBuilder();
protected final ActionExecutor actionExecutor = new ActionExecutor();
protected TestDeviceManager testDeviceManager;

public TestRunner(TestDeviceManager testDeviceManager, TestTaskRunCallback testTaskRunCallback,
public TestRunner(AgentManagementService agentManagementService, TestTaskRunCallback testTaskRunCallback,
PerformanceTestManagementService performanceTestManagementService) {
this.testDeviceManager = testDeviceManager;
this.agentManagementService = agentManagementService;
this.testTaskRunCallback = testTaskRunCallback;
this.performanceTestManagementService = performanceTestManagementService;
}
Expand Down Expand Up @@ -177,7 +179,7 @@ protected void tearDown(DeviceInfo deviceInfo, TestTask testTask, TestRun testRu
if (testRun.getTotalCount() > 0) {
try {
String absoluteReportPath = xmlBuilder.buildTestResultXml(testTask, testRun);
testRun.setTestXmlReportPath(testDeviceManager.getTestBaseRelPathInUrl(new File(absoluteReportPath)));
testRun.setTestXmlReportPath(agentManagementService.getTestBaseRelPathInUrl(new File(absoluteReportPath)));
} catch (Exception e) {
testRun.getLogger().error("Error in buildTestResultXml", e);
}
Expand Down Expand Up @@ -241,8 +243,7 @@ private Logger createLoggerForTestRun(TestRun testRun, String loggerNamePrefix,
Logger reportLogger =
LogUtils.getLoggerWithRollingFileAppender(loggerName, instrumentLogFile.getAbsolutePath(),
"%d %p -- %m%n");
// TODO the getTestBaseRelPathInUrl method shouldn't be in deviceManager, testBaseDir should be managed by agent
testRun.setInstrumentReportPath(testDeviceManager.getTestBaseRelPathInUrl(instrumentLogFile));
testRun.setInstrumentReportPath(agentManagementService.getTestBaseRelPathInUrl(instrumentLogFile));

return reportLogger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import com.microsoft.hydralab.common.entity.common.DeviceInfo;
import com.microsoft.hydralab.common.entity.common.TestRun;
import com.microsoft.hydralab.common.entity.common.TestTask;
import com.microsoft.hydralab.common.management.device.TestDeviceManager;
import com.microsoft.hydralab.common.management.AgentManagementService;
import com.microsoft.hydralab.performance.PerformanceTestManagementService;
import org.slf4j.Logger;

public class AppiumCrossRunner extends AppiumRunner {
String agentName;

public AppiumCrossRunner(TestDeviceManager testDeviceManager, TestTaskRunCallback testTaskRunCallback,
public AppiumCrossRunner(AgentManagementService agentManagementService, TestTaskRunCallback testTaskRunCallback,
PerformanceTestManagementService performanceTestManagementService, String agentName) {
super(testDeviceManager, testTaskRunCallback, performanceTestManagementService);
super(agentManagementService, testTaskRunCallback, performanceTestManagementService);
this.agentName = agentName;
}

Expand Down
Loading