@Test public void shouldCommandStringBeAssembledCorrectlyForSingleUser() throws Exception { payload = mockPayload(1); mockExecutionCommand(payload.size()); String payloadJson = om.writeValueAsString(payload); Map<String, String> commandParams = new HashMap<>(); commandParams.put(UserHookParams.PAYLOAD.param(), payloadJson); commandParams.put(UserHookParams.SCRIPT.param(), "/hookfolder/hook.name"); commandParams.put(UserHookParams.CMD_TIME_FRAME.param(), "1000"); commandParams.put(UserHookParams.CMD_INPUT_FILE.param(), "/test/user_data.csv"); commandParams.put(UserHookParams.CLUSTER_SECURITY_TYPE.param(), SecurityType.KERBEROS.name()); commandParams.put(UserHookParams.CMD_HDFS_USER.param(), "test-hdfs-user"); EasyMock.expect(executionCommand.getCommandParams()).andReturn(commandParams); EasyMock.expect(objectMapperMock.readValue(payloadJson, Map.class)).andReturn(payload); commandCapture = EasyMock.newCapture(); EasyMock.expect(shellCommandUtilityWrapper.runCommand(EasyMock.capture(commandCapture))).andReturn(new ShellCommandUtil.Result(0, null, null)).times(payload.size()); customScriptServerAction.setExecutionCommand(executionCommand); EasyMock.expect(collectionPersisterServiceFactoryMock.createCsvFilePersisterService(EasyMock.anyString())).andReturn(collectionPersisterService); EasyMock.expect(collectionPersisterService.persistMap(EasyMock.<Map<String, List<String>>>anyObject())).andReturn(Boolean.TRUE); replayAll(); CommandReport commandReport = customScriptServerAction.execute(requestSharedDataContext); String[] commandArray = commandCapture.getValue(); Assert.assertNotNull("The command to be executed must not be null!", commandArray); Assert.assertEquals("The command argument array length is not as expected!", 6, commandArray.length); Assert.assertEquals("The command script is not as expected", "/hookfolder/hook.name", commandArray[0]); }
@Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } @Inject  PostUserCreationHookServerAction(); }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } @Inject  PostUserCreationHookServerAction(); @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext); }
PostUserCreationHookServerAction extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { LOGGER.debug("Executing custom script server action; Context: {}", requestSharedDataContext); ShellCommandUtil.Result result = null; CommandReport cmdReport = null; try { Map<String, String> commandParams = getCommandParameters(); validateCommandParams(commandParams); CollectionPersisterService<String, List<String>> csvPersisterService = collectionPersisterServiceFactory.createCsvFilePersisterService(commandParams.get(UserHookParams.CMD_INPUT_FILE.param())); csvPersisterService.persistMap(getPayload(commandParams)); String[] cmd = assembleCommand(commandParams); result = shellCommandUtilityWrapper.runCommand(cmd); logCommandResult(Arrays.asList(cmd).toString(), result); cmdReport = createCommandReport(result.getExitCode(), result.isSuccessful() ? HostRoleStatus.COMPLETED : HostRoleStatus.FAILED, "{}", result.getStdout(), result.getStderr()); LOGGER.debug("Command report: {}", cmdReport); } catch (InterruptedException e) { LOGGER.error("The server action thread has been interrupted", e); throw e; } catch (Exception e) { LOGGER.error("Server action is about to quit due to an exception.", e); throw new AmbariException("Server action execution failed to complete!", e); } return cmdReport; } @Inject  PostUserCreationHookServerAction(); @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }