diff --git a/agent/src/main/java/com/microsoft/hydralab/agent/command/DeviceScriptCommandLoader.java b/agent/src/main/java/com/microsoft/hydralab/agent/command/DeviceScriptCommandLoader.java index 7a6d99260..3efc5c1d0 100644 --- a/agent/src/main/java/com/microsoft/hydralab/agent/command/DeviceScriptCommandLoader.java +++ b/agent/src/main/java/com/microsoft/hydralab/agent/command/DeviceScriptCommandLoader.java @@ -5,6 +5,7 @@ import com.microsoft.hydralab.common.entity.common.DeviceAction; import com.microsoft.hydralab.common.entity.common.TestTask; +import com.microsoft.hydralab.common.util.Const; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -19,6 +20,7 @@ */ @Service public class DeviceScriptCommandLoader { + // preset commands in agent application.yml @Resource(name = "DeviceCommandProperty") private List commands; @@ -55,7 +57,7 @@ private List command2Action(DeviceScriptCommand deviceCommand) { String[] commandLines = deviceCommand.getInline().split("\n"); for (String commandLine : commandLines) { if (!StringUtils.isEmpty(commandLine)) { - actionList.add(converter.getAction(commandLine)); + actionList.add(converter.getAction(commandLine, deviceCommand.getDevice())); } } return actionList; @@ -65,21 +67,29 @@ private enum ActionConverter { //generate action by command type ADBShell() { @Override - public DeviceAction getAction(String commandline) { - DeviceAction deviceAction = new DeviceAction("Android", "execCommandOnDevice"); + public DeviceAction getAction(String commandline, String deviceType) { + String type = deviceType; + if (StringUtils.isEmpty(type)) { + type = Const.OperatedDevice.ANDROID; + } + DeviceAction deviceAction = new DeviceAction(type, "execCommandOnDevice"); deviceAction.getArgs().add(commandline); return deviceAction; } }, AgentShell() { @Override - public DeviceAction getAction(String commandline) { - DeviceAction deviceAction = new DeviceAction("Windows", "execCommandOnAgent"); + public DeviceAction getAction(String commandline, String deviceType) { + String type = deviceType; + if (StringUtils.isEmpty(type)) { + type = Const.OperatedDevice.ANDROID; + } + DeviceAction deviceAction = new DeviceAction(type, "execCommandOnAgent"); deviceAction.getArgs().add(commandline); return deviceAction; } }; - public abstract DeviceAction getAction(String commandline); + public abstract DeviceAction getAction(String commandline, String deviceType); } } diff --git a/agent/src/main/java/com/microsoft/hydralab/agent/runner/ActionExecutor.java b/agent/src/main/java/com/microsoft/hydralab/agent/runner/ActionExecutor.java index 3a684e3bf..67156ea17 100644 --- a/agent/src/main/java/com/microsoft/hydralab/agent/runner/ActionExecutor.java +++ b/agent/src/main/java/com/microsoft/hydralab/agent/runner/ActionExecutor.java @@ -8,6 +8,7 @@ import com.microsoft.hydralab.common.entity.common.DeviceInfo; import com.microsoft.hydralab.common.entity.common.TestRunDevice; import com.microsoft.hydralab.common.management.device.DeviceDriver; +import com.microsoft.hydralab.common.util.Const; import com.microsoft.hydralab.common.util.HydraLabRuntimeException; import com.microsoft.hydralab.common.util.ThreadUtils; import org.apache.commons.lang3.StringUtils; @@ -24,6 +25,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * @author zhoule @@ -35,7 +37,7 @@ public class ActionExecutor { /** * the implementation of supported actions should not be overload */ - private Set actionTypes = + private final Set actionTypes = Set.of("setProperty", "setDefaultLauncher", "backToHome", "changeGlobalSetting", "changeSystemSetting", "execCommandOnDevice", "execCommandOnAgent", "pushFileToDevice", "pullFileFromDevice"); @@ -43,12 +45,17 @@ public class ActionExecutor { public List doActions(@NotNull DeviceDriver deviceDriverManager, @NotNull TestRunDevice testRunDevice, @NotNull Logger logger, - @NotNull Map> actions, @NotNull String when) { + @NotNull Map> actions, @NotNull String when, boolean runOnAgentOnly) { List exceptions = new ArrayList<>(); //filter todoActions - List todoActions = actions.getOrDefault(when, new ArrayList<>()).stream() - .filter(deviceAction -> actionTypes.contains(deviceAction.getMethod())) - .collect(Collectors.toList()); + Stream legalActionStream = actions.getOrDefault(when, new ArrayList<>()).stream() + .filter(deviceAction -> actionTypes.contains(deviceAction.getMethod()) && StringUtils.isNotEmpty(deviceAction.getDeviceType())); + if (runOnAgentOnly) { + legalActionStream = legalActionStream.filter(deviceAction -> Const.OperatedDevice.AGENT.equalsIgnoreCase(deviceAction.getDeviceType())); + } else { + legalActionStream = legalActionStream.filter(deviceAction -> testRunDevice.getDeviceInfo().getType().equalsIgnoreCase(deviceAction.getDeviceType())); + } + List todoActions = legalActionStream.collect(Collectors.toList()); logger.info("Start to execute actions! Current timing is {}, action size is {}", when, todoActions.size()); for (DeviceAction deviceAction : todoActions) { @@ -64,7 +71,7 @@ public List doActions(@NotNull DeviceDriver deviceDriverManager, return exceptions; } - public void doAction(@NotNull DeviceDriver deviceDriverManager, + private void doAction(@NotNull DeviceDriver deviceDriverManager, @NotNull TestRunDevice testRunDevice, @NotNull Logger logger, @NotNull DeviceAction deviceAction) @@ -72,9 +79,6 @@ public void doAction(@NotNull DeviceDriver deviceDriverManager, if (!actionTypes.contains(deviceAction.getMethod())) { return; } - if (!StringUtils.isEmpty(deviceAction.getDeviceType()) && !testRunDevice.getDeviceInfo().getType().equalsIgnoreCase(deviceAction.getDeviceType())){ - return; - } DeviceInfo deviceInfo = testRunDevice.getDeviceInfo(); logger.info("Start to analysis action type! Current action is {}", deviceAction.getMethod()); Method method = Arrays.stream(deviceDriverManager.getClass().getMethods()) @@ -122,5 +126,4 @@ private Object[] convertArgs(@NotNull DeviceInfo deviceInfo, @NotNull Logger log } return methodArgs; } - } diff --git a/agent/src/main/java/com/microsoft/hydralab/agent/runner/TestRunDeviceOrchestrator.java b/agent/src/main/java/com/microsoft/hydralab/agent/runner/TestRunDeviceOrchestrator.java index 8a0a2459f..3edfd5694 100644 --- a/agent/src/main/java/com/microsoft/hydralab/agent/runner/TestRunDeviceOrchestrator.java +++ b/agent/src/main/java/com/microsoft/hydralab/agent/runner/TestRunDeviceOrchestrator.java @@ -336,14 +336,15 @@ public Logger getDeviceLogger(TestRunDevice testRunDevice) { } public List doActions(TestRunDevice testRunDevice, Logger logger, Map> deviceActions, String when) { + List exceptions = actionExecutor.doActions(deviceDriverManager, testRunDevice, logger, deviceActions, when, true); + if (testRunDevice instanceof TestRunDeviceCombo) { - List exceptions = new ArrayList<>(); - for (TestRunDevice testRunDevice1 : ((TestRunDeviceCombo) testRunDevice).getDevices()) { - exceptions.addAll(actionExecutor.doActions(deviceDriverManager, testRunDevice1, logger, deviceActions, when)); + for (TestRunDevice subTestRunDevice : ((TestRunDeviceCombo) testRunDevice).getDevices()) { + exceptions.addAll(actionExecutor.doActions(deviceDriverManager, subTestRunDevice, logger, deviceActions, when, false)); } - return exceptions; } else { - return actionExecutor.doActions(deviceDriverManager, testRunDevice, logger, deviceActions, when); + exceptions.addAll(actionExecutor.doActions(deviceDriverManager, testRunDevice, logger, deviceActions, when, false)); } + return exceptions; } } \ No newline at end of file diff --git a/agent/src/test/java/com/microsoft/hydralab/agent/runner/ActionExecutorTest.java b/agent/src/test/java/com/microsoft/hydralab/agent/runner/ActionExecutorTest.java index 5fa73d388..5bf7ee36b 100644 --- a/agent/src/test/java/com/microsoft/hydralab/agent/runner/ActionExecutorTest.java +++ b/agent/src/test/java/com/microsoft/hydralab/agent/runner/ActionExecutorTest.java @@ -37,19 +37,23 @@ void createAndExecuteActions() throws InvocationTargetException, IllegalAccessEx DeviceAction action1 = JSONObject.parseObject(actionJson.toJSONString(), DeviceAction.class); List args1 = List.of("paramA", "paramB"); action1.setArgs(args1); - actionExecutor.doAction(deviceDriver, new TestRunDevice(deviceInfo, deviceInfo.getType()), baseLogger, action1); - verify(deviceDriver).setProperty(deviceInfo, args1.get(0), args1.get(1), baseLogger); + List actions1 = new ArrayList<>(); + actions1.add(action1); + List exceptions1 = actionExecutor.doActions(deviceDriver, new TestRunDevice(deviceInfo, deviceInfo.getType()), baseLogger, + Map.of(DeviceAction.When.SET_UP, actions1), DeviceAction.When.SET_UP, true); + Assertions.assertEquals(0, exceptions1.size(), () -> exceptions1.get(0).getMessage()); + verify(deviceDriver, times(0)).setProperty(deviceInfo, args1.get(0), args1.get(1), baseLogger); DeviceAction action2 = new DeviceAction("Android", "changeGlobalSetting"); List args2 = List.of("paramC", "paramD"); action2.setArgs(args2); - List actions = new ArrayList<>(); - actions.add(action1); - actions.add(action2); - List exceptions = actionExecutor.doActions(deviceDriver, new TestRunDevice(deviceInfo, deviceInfo.getType()), baseLogger, - Map.of(DeviceAction.When.SET_UP, actions), DeviceAction.When.SET_UP); - Assertions.assertEquals(0, exceptions.size(), () -> exceptions.get(0).getMessage()); - verify(deviceDriver, times(2)).setProperty(deviceInfo, args1.get(0), args1.get(1), baseLogger); + List actions2 = new ArrayList<>(); + actions2.add(action1); + actions2.add(action2); + List exceptions2 = actionExecutor.doActions(deviceDriver, new TestRunDevice(deviceInfo, deviceInfo.getType()), baseLogger, + Map.of(DeviceAction.When.SET_UP, actions2), DeviceAction.When.SET_UP, false); + Assertions.assertEquals(0, exceptions2.size(), () -> exceptions2.get(0).getMessage()); + verify(deviceDriver, times(1)).setProperty(deviceInfo, args1.get(0), args1.get(1), baseLogger); verify(deviceDriver, times(1)).changeGlobalSetting(deviceInfo, args2.get(0), args2.get(1), baseLogger); } diff --git a/common/src/main/java/com/microsoft/hydralab/common/util/Const.java b/common/src/main/java/com/microsoft/hydralab/common/util/Const.java index 87bb6468a..84c1fbb83 100644 --- a/common/src/main/java/com/microsoft/hydralab/common/util/Const.java +++ b/common/src/main/java/com/microsoft/hydralab/common/util/Const.java @@ -187,4 +187,10 @@ interface TestDeviceTag { String TERTIARY_PHONE = "TERTIARY_PHONE"; String PRIMARY_PC = "PRIMARY_PC"; } + + interface OperatedDevice { + String ANY = "Any"; + String AGENT = "Agent"; + String ANDROID = "Android"; + } }