public Task execTask() { if (isExecTask()) { return task; } return new ExecTask(); }
@Override public Builder createBuilder(BuilderFactory builderFactory, ExecTask task, Pipeline pipeline, UpstreamPipelineResolver pipelineResolver) { String workingDir = task.workingDirectory(); String command = task.command(); File newWorkingDir = workingDir == null ? pipeline.defaultWorkingFolder() : new File( applyBaseDirIfRelativeAndNormalize(pipeline.defaultWorkingFolder(), new File(workingDir))); Builder builder = builderFactory.builderFor(task.cancelTask(), pipeline, pipelineResolver); String description = task.describe(); if (!task.getArgList().isEmpty()) { return new CommandBuilderWithArgList(command, task.getArgList().toStringArray(), newWorkingDir, task.getConditions(), builder, description); } else { return new CommandBuilder(command, task.getArgs(), newWorkingDir, task.getConditions(), builder, description); } } }
public static void toJSON(OutputWriter jsonWriter, ExecTask execTask) { BaseTaskRepresenter.toJSON(jsonWriter, execTask); jsonWriter.add("command", execTask.command()); if (execTask.getArgList().isEmpty()) { jsonWriter.addIfNotNull("args", execTask.getArgs()); } else { List<String> arguments = execTask.getArgList().stream().map(Argument::getValue).collect(Collectors.toList()); jsonWriter.addChildList("arguments", arguments); } jsonWriter.addIfNotNull("working_directory", execTask.workingDirectory()); }
private CRExecTask execTasktoCRExecTask(ExecTask task) { CRExecTask crExecTask = new CRExecTask(task.getCommand()); crExecTask.setTimeout(task.getTimeout()); crExecTask.setWorkingDirectory(task.workingDirectory()); crExecTask.setArgs(task.getArgList().stream() .map(Argument::getValue) .collect(Collectors.toList()) ); commonCRTaskMembers(crExecTask, task); return crExecTask; }
@Test public void shouldAllowSettingOfConfigAttributes() throws Exception { ExecTask exec = new ExecTask(); exec.setConfigAttributes(m(ExecTask.COMMAND, "ls", ExecTask.ARGS, "-la", ExecTask.WORKING_DIR, "my_dir")); assertThat(exec.command(), is("ls")); assertThat(exec.getArgs(), is("-la")); assertThat(exec.getArgListString(), is("")); assertThat(exec.workingDirectory(), is("my_dir")); exec.setConfigAttributes(m(ExecTask.COMMAND, null, ExecTask.ARGS, null, ExecTask.WORKING_DIR, null)); assertThat(exec.command(), is(nullValue())); assertThat(exec.getArgs(), is("")); assertThat(exec.workingDirectory(), is(nullValue())); exec.setConfigAttributes(m(ExecTask.COMMAND, null, ExecTask.ARG_LIST_STRING, "-l\n-a\npavan's\\n working dir?", ExecTask.WORKING_DIR, null)); assertThat(exec.command(), is(nullValue())); assertThat(exec.getArgListString(), is("-l\n-a\npavan's\\n working dir?")); assertThat(exec.getArgList().size(), is(3)); assertThat(exec.getArgList().get(0), is(new Argument("-l"))); assertThat(exec.getArgList().get(1), is(new Argument("-a"))); assertThat(exec.getArgList().get(2), is(new Argument("pavan's\\n working dir?"))); assertThat(exec.workingDirectory(), is(nullValue())); }
@Test public void shouldNotSetAttributesWhenKeysNotPresentInAttributeMap() throws Exception { ExecTask exec = new ExecTask(); exec.setConfigAttributes(m(ExecTask.COMMAND, "ls", ExecTask.ARGS, "-la", ExecTask.WORKING_DIR, "my_dir")); exec.setConfigAttributes(m());//Key is not present assertThat(exec.command(), is("ls")); assertThat(exec.getArgs(), is("-la")); assertThat(exec.workingDirectory(), is("my_dir")); }
@Test public void shouldNotSetArgsIfTheValueIsBlank() throws Exception { ExecTask exec = new ExecTask(); exec.setConfigAttributes(m(ExecTask.COMMAND, "ls", ExecTask.ARGS, "", ExecTask.WORKING_DIR, "my_dir")); exec.setConfigAttributes(m()); assertThat(exec.command(), is("ls")); assertThat(exec.getArgList().size(), is(0)); assertThat(exec.workingDirectory(), is("my_dir")); }
@Test public void shouldReturnCommandTaskAttributes(){ ExecTask task = new ExecTask("ls", "-laht", "src/build"); assertThat(task.command(),is("ls")); assertThat(task.arguments(), is("-laht")); assertThat(task.workingDirectory(), is("src/build")); }
@Test public void shouldBeValid() throws Exception { ExecTask execTask = new ExecTask("", new Arguments(new Argument("arg1"), new Argument("arg2"))); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().isEmpty(), is(true)); execTask = new ExecTask("command", "", "blah"); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().isEmpty(), is(true)); }
@Test public void describeTest() { ExecTask task = new ExecTask("ant", "-f build.xml run", "subfolder"); task.setTimeout(600); assertThat(task.describe(), is("ant -f build.xml run")); }
@Test public void validateTask_shouldValidateThatCommandIsRequired() { ExecTask execTask = new ExecTask(); execTask.validateTask(null); assertThat(execTask.errors().isEmpty(), is(false)); assertThat(execTask.errors().on(ExecTask.COMMAND), is("Command cannot be empty")); }
@Test public void shouldUseConfiguredWorkingDirectory() throws Exception { File absoluteFile = new File("test").getAbsoluteFile(); ExecTask task = new ExecTask("command", "arguments", absoluteFile.getAbsolutePath()); assertThat(task.workingDirectory(), Matchers.is(absoluteFile.getPath())); } }
@Test public void shouldSetPrimitiveAttributesForExecTask() { Map hashMap = new HashMap(); hashMap.put(OnCancelConfig.ON_CANCEL_OPTIONS, "exec"); Map valueMap = new HashMap(); valueMap.put(ExecTask.COMMAND, "ls"); valueMap.put(ExecTask.ARGS, "blah"); valueMap.put(ExecTask.WORKING_DIR, "pwd"); hashMap.put(OnCancelConfig.EXEC_ON_CANCEL, valueMap); hashMap.put(OnCancelConfig.ANT_ON_CANCEL, new HashMap()); ExecTask execTask = new ExecTask(); when(taskFactory.taskInstanceFor(execTask.getTaskType())).thenReturn(execTask); OnCancelConfig cancelConfig = OnCancelConfig.create(hashMap, taskFactory); assertThat(cancelConfig.getTask(), is(new ExecTask("ls", "blah", "pwd"))); }
@Test public void shouldValidateWorkingDirectory() { ExecTask task = new ExecTask("ls", "-l", "../../../assertTaskInvalid"); CruiseConfig config = GoConfigMother.configWithPipelines("pipeline"); PipelineConfig pipeline = config.pipelineConfigByName(new CaseInsensitiveString("pipeline")); StageConfig stage = pipeline.get(0); JobConfig job = stage.getJobs().get(0); job.addTask(task); List<ConfigErrors> errors = config.validateAfterPreprocess(); assertThat(errors.size(), is(1)); String message = "The path of the working directory for the custom command in job 'job' in stage 'stage' of pipeline 'pipeline' is outside the agent sandbox."; assertThat(errors.get(0).firstError(), is(message)); assertThat(task.errors().on(ExecTask.WORKING_DIR), is(message)); }
public static ExecTask fromJSON(JsonReader jsonReader) { ExecTask execTask = new ExecTask(); if (jsonReader == null) { return execTask; } BaseTaskRepresenter.fromJSON(jsonReader, execTask); jsonReader.readStringIfPresent("command", execTask::setCommand); jsonReader.readArrayIfPresent("arguments", arguments -> { ArrayList<String> argList = new ArrayList<>(); arguments.forEach(argument -> { argList.add(argument.getAsString()); }); execTask.setArgsList(argList.toArray(new String[arguments.size()])); }); jsonReader.readStringIfPresent("args", execTask::setArgs); jsonReader.readStringIfPresent("working_directory", execTask::setWorkingDirectory); return execTask; } }
@Test public void shouldSetOnCancelExecTask() { AbstractTask task = new ExecTask(); Map onCancelMapAttrib = new HashMap(); onCancelMapAttrib.put(ExecTask.COMMAND, "sudo"); onCancelMapAttrib.put(ExecTask.ARGS, "ls -la"); onCancelMapAttrib.put(ExecTask.WORKING_DIR, "working_dir"); onCancelMapAttrib.put(AbstractTask.RUN_IF_CONFIGS_ANY, "1"); onCancelMapAttrib.put(AbstractTask.RUN_IF_CONFIGS_FAILED, "1"); onCancelMapAttrib.put(AbstractTask.RUN_IF_CONFIGS_PASSED, "1"); Map onCancelConfigAttributes = new HashMap(); onCancelConfigAttributes.put(OnCancelConfig.EXEC_ON_CANCEL, onCancelMapAttrib); onCancelConfigAttributes.put(OnCancelConfig.ON_CANCEL_OPTIONS, "exec"); Map actualTaskAttributes = new HashMap(); actualTaskAttributes.put(AbstractTask.ON_CANCEL_CONFIG, onCancelConfigAttributes); actualTaskAttributes.put(AbstractTask.HAS_CANCEL_TASK, "1"); ExecTask execTask = new ExecTask(); when(taskFactory.taskInstanceFor(execTask.getTaskType())).thenReturn(execTask); task.setConfigAttributes(actualTaskAttributes, taskFactory); assertThat(task.hasCancelTask(), is(true)); ExecTask expected = new ExecTask("sudo", "ls -la", "working_dir"); expected.getConditions().add(RunIfConfig.ANY); expected.getConditions().add(RunIfConfig.FAILED); expected.getConditions().add(RunIfConfig.PASSED); assertThat(task.cancelTask(), is(expected)); }
@Test public void shouldPopulateAllAttributesOnPropertiesForDisplay() { ExecTask execTask = new ExecTask("ls", "-la", "holy/dir"); execTask.setTimeout(10l); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"), new TaskProperty("Arguments", "-la", "arguments"), new TaskProperty("Working Directory", "holy/dir", "working_directory"), new TaskProperty("Timeout", "10", "timeout"))); assertThat(execTask.getPropertiesForDisplay().size(), is(4)); execTask = new ExecTask("ls", new Arguments(new Argument("-la"), new Argument("/proc")), "holy/dir"); execTask.setTimeout(10l); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"), new TaskProperty("Arguments", "-la /proc", "arguments"), new TaskProperty("Working Directory", "holy/dir", "working_directory"), new TaskProperty("Timeout", "10", "timeout"))); assertThat(execTask.getPropertiesForDisplay().size(), is(4)); execTask = new ExecTask("ls", new Arguments(new Argument()), null); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"))); assertThat(execTask.getPropertiesForDisplay().size(), is(1)); execTask = new ExecTask("ls", "", (String) null); assertThat(execTask.getPropertiesForDisplay(), hasItems(new TaskProperty("Command", "ls", "command"))); assertThat(execTask.getPropertiesForDisplay().size(), is(1)); }
public ExecTask toExecTask(CRExecTask crTask) { ExecTask execTask = new ExecTask(crTask.getCommand(), toArgList(crTask.getArgs()), crTask.getWorkingDirectory()); if (crTask.getTimeout() != null) execTask.setTimeout(crTask.getTimeout()); // else default global-wide time setCommonTaskMembers(execTask, crTask); return execTask; }