public boolean add(StageConfig stageConfig) { ensureNoTemplateDefined(stageConfig.name()); verifyUniqueName(stageConfig); return addStageWithoutValidityAssertion(stageConfig); }
private static void setStages(JsonReader jsonReader, PipelineConfig pipelineConfig) { pipelineConfig.getStages().clear(); jsonReader.readArrayIfPresent("stages", stages -> { stages.forEach(stage -> { pipelineConfig.addStageWithoutValidityAssertion(StageRepresenter.fromJSON(new JsonReader(stage.getAsJsonObject()))); }); }); } }
@Test public void shouldNotEncryptSecurePropertiesInStagesIfPipelineHasATemplate() { PipelineConfig pipelineConfig = new PipelineConfig(); pipelineConfig.setTemplateName(new CaseInsensitiveString("some-template")); StageConfig mockStageConfig = mock(StageConfig.class); pipelineConfig.addStageWithoutValidityAssertion(mockStageConfig); pipelineConfig.encryptSecureProperties(new BasicCruiseConfig(), pipelineConfig); verify(mockStageConfig, never()).encryptSecureProperties(eq(new BasicCruiseConfig()), eq(pipelineConfig), ArgumentMatchers.any(StageConfig.class)); }
@Test public void shouldEnsureStageNameUniqueness() { CruiseConfig cruiseConfig = new BasicCruiseConfig(); PipelineConfig pipelineConfig = goConfigMother.addPipeline(cruiseConfig, "pipeline1", "stage", "build"); JobConfig jobConfig = new JobConfig("my-build"); jobConfig.addTask(new ExecTask("ls", "-la", "tmp")); StageConfig stageConfig = new StageConfig(new CaseInsensitiveString("stage"), new JobConfigs(jobConfig)); pipelineConfig.addStageWithoutValidityAssertion(stageConfig); pipelineConfig.validate(null); assertThat(stageConfig.errors().getAllOn("name"), is(singletonList("You have defined multiple stages called 'stage'. Stage names are case-insensitive and must be unique."))); assertThat(pipelineConfig.get(0).errors().getAllOn("name"), is(singletonList("You have defined multiple stages called 'stage'. Stage names are case-insensitive and must be unique."))); assertThat(cruiseConfig.validateAfterPreprocess().get(0).getAllOn("name"), is(singletonList("You have defined multiple stages called 'stage'. Stage names are case-insensitive and must be unique."))); }
@Test public void shouldPopulateErrorMessagesWhenHasJobNamesRepeated() { CruiseConfig config = new BasicCruiseConfig(); PipelineConfig pipelineConfig = PipelineConfigMother.createPipelineConfig("pipeline", "stage-1", "con-job"); config.addPipeline("group-foo", pipelineConfig); StageConfig stageConfig = pipelineConfig.get(0); JobConfig newJob = new JobConfig("foo!"); StageConfig newlyAddedStage = new StageConfig(new CaseInsensitiveString("."), new JobConfigs(newJob)); pipelineConfig.addStageWithoutValidityAssertion(newlyAddedStage); stageConfig.getJobs().addJobWithoutValidityAssertion(new JobConfig(new CaseInsensitiveString("con-job"), new ResourceConfigs(), new ArtifactConfigs(), new Tasks(new ExecTask("ls", "-la", "foo")))); List<ConfigErrors> allErrors = config.validateAfterPreprocess(); assertThat(allErrors.size(), is(4)); assertThat(allErrors.get(0).on(JobConfig.NAME), is("You have defined multiple jobs called 'con-job'. Job names are case-insensitive and must be unique.")); assertThat(allErrors.get(1).on(JobConfig.NAME), is("You have defined multiple jobs called 'con-job'. Job names are case-insensitive and must be unique.")); assertThat(allErrors.get(2).on(StageConfig.NAME), is("Invalid stage name '.'. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); assertThat(allErrors.get(3).on(JobConfig.NAME), is("Invalid job name 'foo!'. This must be alphanumeric and may contain underscores and periods. The maximum allowed length is 255 characters.")); assertThat(stageConfig.getJobs().get(0).errors().on(JobConfig.NAME), is("You have defined multiple jobs called 'con-job'. Job names are case-insensitive and must be unique.")); assertThat(stageConfig.getJobs().get(1).errors().on(JobConfig.NAME), is("You have defined multiple jobs called 'con-job'. Job names are case-insensitive and must be unique.")); assertThat(newlyAddedStage.errors().on(StageConfig.NAME), is("Invalid stage name '.'. This must be alphanumeric and can contain underscores and periods (however, it cannot start with a period). The maximum allowed length is 255 characters.")); assertThat(newJob.errors().on(JobConfig.NAME), is("Invalid job name 'foo!'. This must be alphanumeric and may contain underscores and periods. The maximum allowed length is 255 characters.")); }
@Test public void shouldValidateThatPipelineAssociatedToATemplateDoesNotHaveStagesDefinedLocally() { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("wunderbar"), new MaterialConfigs()); config.addPipeline("g", pipelineConfig); pipelineConfig.setTemplateName(new CaseInsensitiveString("template-name")); pipelineConfig.addStageWithoutValidityAssertion(new StageConfig(new CaseInsensitiveString("stage"), new JobConfigs())); pipelineConfig.validateTemplate(null); assertThat(pipelineConfig.errors().on("stages"), is("Cannot add stages to pipeline 'wunderbar' which already references template 'template-name'")); assertThat(pipelineConfig.errors().on("template"), is("Cannot set template 'template-name' on pipeline 'wunderbar' because it already has stages defined")); }
@Test public void shouldValidatePipelineToCheckItDoesNotAllowBothTemplateAndStages() throws Exception { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("p"), new MaterialConfigs()); pipelineConfig.templatize(new CaseInsensitiveString("template")); pipelineConfig.addStageWithoutValidityAssertion(new StageConfig(new CaseInsensitiveString("stage"), new JobConfigs())); preprocessor.process(new BasicCruiseConfig(new BasicPipelineConfigs(pipelineConfig))); assertThat(pipelineConfig.hasTemplateApplied(), is(false)); assertThat(pipelineConfig.errors().on("stages"), is("Cannot add stages to pipeline 'p' which already references template 'template'")); assertThat(pipelineConfig.errors().on("template"), is("Cannot set template 'template' on pipeline 'p' because it already has stages defined")); }