@Test public void shouldValidateConfig() throws Exception { ExecTask execTask = new ExecTask("arg1 arg2", new Arguments(new Argument("arg1"), new Argument("arg2"))); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().isEmpty(), is(false)); assertThat(execTask.errors().on(ExecTask.ARGS), is(ExecTask.EXEC_CONFIG_ERROR)); assertThat(execTask.errors().on(ExecTask.ARG_LIST_STRING), is(ExecTask.EXEC_CONFIG_ERROR)); }
@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)); }
@Test public void shouldValidateTreeForAllTasks() { AntTask antTask = antTask("build.xml", "compile", "/abc"); ExecTask execTask = new ExecTask("foo", new Arguments(new Argument("arg"))); Tasks tasks = new Tasks(antTask, execTask); String pipelineName = "p1"; PipelineConfig pipelineConfig = GoConfigMother.configWithPipelines(pipelineName).pipelineConfigByName(new CaseInsensitiveString(pipelineName)); StageConfig stageConfig = pipelineConfig.getStages().get(0); JobConfig jobConfig = stageConfig.getJobs().get(0); jobConfig.setTasks(tasks); PipelineConfigSaveValidationContext context = PipelineConfigSaveValidationContext.forChain(true, "group", pipelineConfig, stageConfig, jobConfig); assertFalse(tasks.validateTree(context)); assertThat(tasks.errors().isEmpty(), is(true)); assertThat(antTask.errors().isEmpty(), is(false)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).size(), is(1)); assertThat(antTask.errors().get(AntTask.WORKING_DIRECTORY).contains("Task of job 'job' in stage 'stage' of pipeline 'p1' has path '/abc' which is outside the working directory."), is(true)); assertThat(execTask.errors().get(ExecTask.ARG_LIST_STRING).size(), is(1)); assertThat(execTask.errors().get(ExecTask.ARG_LIST_STRING).contains("Can not use both 'args' attribute and 'arg' sub element in 'exec' element!"), is(true)); assertThat(execTask.errors().get(ExecTask.ARGS).size(), is(1)); assertThat(execTask.errors().get(ExecTask.ARGS).contains("Can not use both 'args' attribute and 'arg' sub element in 'exec' element!"), is(true)); }
@Test public void shouldErrorOutForTemplates_WhenItHasATaskWithInvalidWorkingDirectory() { CruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("some_pipeline"); StageConfig templateStage = StageConfigMother.stageWithTasks("templateStage"); ExecTask execTask = new ExecTask("ls", "-la", "/"); templateStage.getJobs().first().addTask(execTask); PipelineTemplateConfig template = new PipelineTemplateConfig(new CaseInsensitiveString("template_name"), templateStage); cruiseConfig.addTemplate(template); try { execTask.validateTask(ConfigSaveValidationContext.forChain(cruiseConfig, template, templateStage, templateStage.getJobs().first())); assertThat(execTask.errors().isEmpty(), is(false)); assertThat(execTask.errors().on(ExecTask.WORKING_DIR), is("The path of the working directory for the custom command in job 'job' in stage 'templateStage' of template 'template_name' is outside the agent sandbox.")); } catch (Exception e) { fail("should not have failed. Exception: " + e.getMessage()); } }
@Test public void shouldAddErrorsOfEachArgumentToTheParent() { Argument argument = new Argument("invalid-argument"); argument.addError(ExecTask.ARG_LIST_STRING, "Invalid argument"); ExecTask execTask = new ExecTask("echo", new Arguments(argument), null); execTask.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(execTask.errors().on(ExecTask.ARG_LIST_STRING), is("Invalid argument")); }
@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)); }