public boolean validateTree(ValidationContext validationContext) { validate(validationContext); boolean isValid = errors().isEmpty(); isValid = resourceConfigs.validateTree(validationContext) && isValid; return isValid; } public void validate(ValidationContext validationContext) {
public void validate(ValidationContext validationContext) { boolean validity = validateDuplicateElasticAgentIds(); for (AgentConfig agentConfig : this) { agentConfig.validate(validationContext); validity = agentConfig.errors().isEmpty() && validity; } }
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 shouldPassValidationWhenUUidIsAvailable() { AgentConfig agentConfig = new AgentConfig("uuid"); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.UUID), is(nullValue())); }
private void shouldBeValid(String ipAddress) throws Exception { AgentConfig agentConfig = new AgentConfig(); agentConfig.setIpAddress(ipAddress); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is(nullValue())); } }
@Test public void shouldFailValidationIfIPAddressIsAString() throws Exception { AgentConfig agentConfig = new AgentConfig("uuid", "host", "blahinvalid"); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is("'blahinvalid' is an invalid IP address.")); agentConfig = new AgentConfig("uuid", "host", "blah.invalid"); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is("'blah.invalid' is an invalid IP address.")); }
@Test public void shouldFailValidationWhenUUidIsBlank() { AgentConfig agentConfig = new AgentConfig(""); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.UUID), is("UUID cannot be empty")); agentConfig = new AgentConfig(null); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.UUID), is("UUID cannot be empty")); }
@Test public void shouldValidateDuplicateElasticAgentId() throws Exception { Agents agents = new Agents(); AgentConfig elasticAgent1 = new AgentConfig("1", "localhost", "1"); elasticAgent1.setElasticAgentId("elastic-agent-id"); elasticAgent1.setElasticPluginId("awesome-elastic-agent"); AgentConfig elasticAgent2 = new AgentConfig("2", "localhost", "2"); elasticAgent2.setElasticAgentId("elastic-agent-id"); elasticAgent2.setElasticPluginId("awesome-elastic-agent"); agents.add(elasticAgent1); agents.add(elasticAgent2); agents.validate(new ConfigSaveValidationContext(agents)); assertThat(elasticAgent1.errors().size(), is(1)); assertThat(elasticAgent1.errors().getAllOn("elasticAgentId").get(0), is("Duplicate ElasticAgentId found for agents [1, 2]")); assertThat(elasticAgent2.errors().size(), is(1)); assertThat(elasticAgent2.errors().getAllOn("elasticAgentId").get(0), is("Duplicate ElasticAgentId found for agents [1, 2]")); } }
@Test public void shouldInvalidateEmptyAddress() { AgentConfig agentConfig = new AgentConfig("uuid", "host", ""); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is("IpAddress cannot be empty if it is present.")); }
@Test public void shouldFailValidationIfIPAddressHasAnIncorrectValue() throws Exception { AgentConfig agentConfig = new AgentConfig("uuid", "host", "399.0.0.1"); agentConfig.validate(ConfigSaveValidationContext.forChain(agentConfig)); assertThat(agentConfig.errors().on(AgentConfig.IP_ADDRESS), is("'399.0.0.1' is an invalid IP address.")); }
@Test public void shouldNotAllowResourcesElasticAgents() throws Exception { BasicCruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("dev", "qa"); AgentConfig agentConfig = new AgentConfig("uuid", "hostname", "10.10.10.10"); cruiseConfig.agents().add(agentConfig); agentConfig.setElasticPluginId("com.example.foo"); agentConfig.setElasticAgentId("foobar"); agentConfig.addResourceConfig(new ResourceConfig("foo")); assertThat(cruiseConfig.validateAfterPreprocess().isEmpty(), is(false)); assertEquals(1, agentConfig.errors().size()); assertThat(agentConfig.errors().on("elasticAgentId"), is("Elastic agents cannot have resources.")); }
@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)); }