public CruiseConfig validateCruiseConfig(CruiseConfig config) throws Exception { LOGGER.debug("[Config Save] In validateCruiseConfig: Starting."); List<ConfigErrors> allErrors = validate(config); if (!allErrors.isEmpty()) { if (config.isLocal()) throw new GoConfigInvalidException(config, allErrors); else throw new GoConfigInvalidMergeException(config, config.getMergedPartials(), allErrors); } LOGGER.debug("[Config Save] In validateCruiseConfig: Running validate."); for (GoConfigValidator validator : VALIDATORS) { validator.validate(config); } LOGGER.debug("[Config Save] In validateCruiseConfig: Done."); return config; }
@Test public void shouldWalkCruiseConfigObjectsParallelly() { CruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("bad pipeline name"); CruiseConfig rawCruiseConfig = new Cloner().deepClone(cruiseConfig); MagicalGoConfigXmlLoader.validate(cruiseConfig); cruiseConfig.copyErrorsTo(rawCruiseConfig); assertThat(rawCruiseConfig.pipelineConfigByName(new CaseInsensitiveString("bad pipeline name")).errors(), is(cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("bad pipeline name")).errors())); }
@Test public void shouldAllowResourcesWithParamsForJobs() throws Exception { CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.addTemplate(new PipelineTemplateConfig(new CaseInsensitiveString("template"), stageWithJobResource("#{PLATFORM}"))); PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("pipeline"), new MaterialConfigs()); pipelineConfig.setTemplateName(new CaseInsensitiveString("template")); pipelineConfig.addParam(new ParamConfig("PLATFORM", "windows")); cruiseConfig.addPipeline("group", pipelineConfig); List<ConfigErrors> errorses = MagicalGoConfigXmlLoader.validate(cruiseConfig); assertThat(errorses.isEmpty(), is(true)); }
@Test public void shouldAllowRoleWithParamsForStageInTemplate() throws Exception { CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.server().security().addRole(new RoleConfig(new CaseInsensitiveString("role"))); cruiseConfig.addTemplate(new PipelineTemplateConfig(new CaseInsensitiveString("template"), stageWithAuth("#{ROLE}"))); PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("pipeline"), new MaterialConfigs()); pipelineConfig.setTemplateName(new CaseInsensitiveString("template")); pipelineConfig.addParam(new ParamConfig("ROLE", "role")); cruiseConfig.addPipeline("group", pipelineConfig); List<ConfigErrors> errorses = MagicalGoConfigXmlLoader.validate(cruiseConfig); assertThat(errorses.isEmpty(), is(true)); }
@Test public void shouldAddErrorsToRawCruiseConfigWhenTemplateHasErrors() { CruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("pipeline-1"); cruiseConfig.getTemplates().add( new PipelineTemplateConfig(new CaseInsensitiveString("invalid template name"), new StageConfig(new CaseInsensitiveString("stage-1"), new JobConfigs(new JobConfig("job-1")) ))); PipelineConfig pipelineWithTemplate = new PipelineConfig(new CaseInsensitiveString("pipeline-with-template"), MaterialConfigsMother.defaultMaterialConfigs()); pipelineWithTemplate.setTemplateName(new CaseInsensitiveString("invalid template name")); cruiseConfig.getGroups().get(0).add(pipelineWithTemplate); CruiseConfig rawCruiseConfig = new Cloner().deepClone(cruiseConfig); MagicalGoConfigXmlLoader.validate(cruiseConfig); cruiseConfig.copyErrorsTo(rawCruiseConfig); ConfigErrors templateErrors = rawCruiseConfig.getTemplateByName(new CaseInsensitiveString("invalid template name")).errors(); assertThat(templateErrors.getAll().size(), is(1)); assertThat(templateErrors.getAll().get(0), is("Invalid template name 'invalid template 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.")); }
@Test public void shouldHandlePipelinesWithTemplates() { CruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("pipeline-1"); cruiseConfig.getTemplates().add( new PipelineTemplateConfig(new CaseInsensitiveString("template-1"), new StageConfig(new CaseInsensitiveString("invalid stage name"), new JobConfigs(new JobConfig("job-1")) ))); PipelineConfig pipelineWithTemplate = new PipelineConfig(new CaseInsensitiveString("pipeline-with-template"), MaterialConfigsMother.defaultMaterialConfigs()); pipelineWithTemplate.setTemplateName(new CaseInsensitiveString("template-1")); cruiseConfig.getGroups().get(0).add(pipelineWithTemplate); CruiseConfig rawCruiseConfig = new Cloner().deepClone(cruiseConfig); MagicalGoConfigXmlLoader.validate(cruiseConfig); cruiseConfig.copyErrorsTo(rawCruiseConfig); assertThat(rawCruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline-with-template")).errors().isEmpty(), is(true)); assertThat(cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline-with-template")).getStage(new CaseInsensitiveString("invalid stage name")).errors().isEmpty(), is(false)); assertThat(rawCruiseConfig.getTemplateByName(new CaseInsensitiveString("template-1")).errors().isEmpty(), is(true)); }