public boolean validateTree(ValidationContext validationContext) { validate(validationContext); return errors().isEmpty(); }
public ConfigErrors errors() { ConfigErrors configErrors = new ConfigErrors(); configErrors.addAll(agentInstance.agentConfig().errors()); for (ResourceConfig resourceConfig : agentInstance.getResourceConfigs()) { configErrors.addAll(resourceConfig.errors()); } return configErrors; } }
public static AgentInstance createFromConfig(AgentConfig agentInConfig, SystemEnvironment systemEnvironment, AgentStatusChangeListener agentStatusChangeListener) { AgentType type = agentInConfig.isFromLocalHost() ? AgentType.LOCAL : AgentType.REMOTE; AgentInstance result = new AgentInstance(agentInConfig, type, systemEnvironment, agentStatusChangeListener); result.agentConfigStatus = agentInConfig.isDisabled() ? AgentConfigStatus.Disabled : AgentConfigStatus.Enabled; result.errors.addAll(agentInConfig.errors()); for (ResourceConfig resourceConfig : agentInConfig.getResourceConfigs()) { result.errors.addAll(resourceConfig.errors()); } return result; }
@Test public void shouldValidateTree(){ ResourceConfig resourceConfig1 = new ResourceConfig("a#"); ResourceConfig resourceConfig2 = new ResourceConfig("b"); ResourceConfigs resourceConfigs = new ResourceConfigs(resourceConfig1, resourceConfig2); resourceConfigs.validateTree(PipelineConfigSaveValidationContext.forChain(true, "group", new PipelineConfig())); assertThat(resourceConfig1.errors().size(), is(1)); assertThat(resourceConfig1.errors().firstError(), is(String.format("Resource name 'a#' is not valid. Valid names much match '%s'", ResourceConfig.VALID_REGEX))); assertThat(resourceConfig2.errors().isEmpty(), is(true)); } }
@Test public void shouldNotAllowParamsInsideResourceNameWhenOutsideTemplates() throws Exception { ResourceConfig resourceConfig = resource("#{PARAMS}"); ValidationContext context = ConfigSaveValidationContext.forChain(new BasicCruiseConfig(), new PipelineConfig()); resourceConfig.validate(context); assertThat(resourceConfig.errors().isEmpty(), is(false)); assertThat(resourceConfig.errors().on(JobConfig.RESOURCES), is(String.format("Resource name '#{PARAMS}' is not valid. Valid names much match '%s'", ResourceConfig.VALID_REGEX))); }
@Test public void shouldNotAllowInvalidResourceNamesWhenInsideTemplates() throws Exception { ResourceConfig resourceConfig = resource("#?{45}"); ValidationContext context = ConfigSaveValidationContext.forChain(new BasicCruiseConfig(), new TemplatesConfig()); resourceConfig.validate(context); assertThat(resourceConfig.errors().isEmpty(), is(false)); assertThat(resourceConfig.errors().on(JobConfig.RESOURCES), is("Resource name '#?{45}' is not valid. Valid names can contain valid parameter syntax or valid alphanumeric with hyphens,dots or pipes")); }
@Test public void shouldNotAllowInvalidResourceNameForAgentResources() throws Exception { ResourceConfig resourceConfig = resource("foo$bar"); resourceConfig.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); ConfigErrors configErrors = resourceConfig.errors(); assertThat(configErrors.isEmpty(), is(false)); assertThat(configErrors.on(JobConfig.RESOURCES), is(String.format("Resource name 'foo$bar' is not valid. Valid names much match '%s'", ResourceConfig.VALID_REGEX))); }
@Test public void shouldAllowValidResourceNameForAgentResources() throws Exception { ResourceConfig resourceConfig = resource("- foo|bar baz.quux"); resourceConfig.validate(ConfigSaveValidationContext.forChain(new BasicCruiseConfig())); assertThat(resourceConfig.errors().isEmpty(), is(true)); }
@Test public void shouldAllowParamsInsideResourceNameWhenInsideTemplates() throws Exception { ResourceConfig resourceConfig = resource("#{PARAMS}"); ValidationContext context = ConfigSaveValidationContext.forChain(new BasicCruiseConfig(), new TemplatesConfig()); resourceConfig.validate(context); assertThat(resourceConfig.errors().isEmpty(), is(true)); }
@Test // Note : At the Resource class level there is no way of accurately validating Parameters. This will only be invalidated when template gets used. public void validate_shouldAllowAnyCombinationOfHashesAndCurlyBraces() throws Exception { ResourceConfig resourceConfig = resource("}#PARAMS{"); ValidationContext context = ConfigSaveValidationContext.forChain(new BasicCruiseConfig(), new TemplatesConfig()); resourceConfig.validate(context); assertThat(resourceConfig.errors().isEmpty(), is(true)); }
@Test public void shouldValidateTree() { ResourceConfig resourceConfig = new ResourceConfig("junk%"); AgentConfig agentConfig = new AgentConfig("uuid", "junk", "junk", new ResourceConfigs(resourceConfig)); boolean isValid = agentConfig.validateTree(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is("'junk' is an invalid IP address.")); assertThat(resourceConfig.errors().on(JobConfig.RESOURCES), contains("Resource name 'junk%' is not valid.")); assertThat(isValid, is(false)); }
@Test public void shouldAddErrorTheMessageOnTheRightFieldOfTheRightElement() throws NoSuchFieldException { ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.setName("#{not-found}"); PipelineConfig pipelineConfig = PipelineConfigMother.createPipelineConfig("cruise", "dev", "ant"); pipelineConfig.setLabelTemplate("#a"); pipelineConfig.get(0).getJobs().addJobWithoutValidityAssertion(new JobConfig(new CaseInsensitiveString("another"), new ResourceConfigs(resourceConfig), new ArtifactConfigs())); new ParamResolver(new ParamSubstitutionHandlerFactory(params(param("foo", "pavan"), param("bar", "jj"))), fieldCache).resolve(pipelineConfig); assertThat(pipelineConfig.errors().on("labelTemplate"), is("Error when processing params for '#a' used in field 'labelTemplate', # must be followed by a parameter pattern or escaped by another #")); assertThat(resourceConfig.errors().on(JobConfig.RESOURCES), is("Parameter 'not-found' is not defined. All pipelines using this parameter directly or via a template must define it.")); }