public CruiseConfig update(CruiseConfig cruiseConfig) { cruiseConfig.agents().add(agentConfig); return cruiseConfig; }
public Agents filter(List<String> uuids) { Agents agents = new Agents(); for (String uuid : uuids) { agents.add(getAgentByUuid(uuid)); } return agents; }
public Agents findAgentsMatching(ResourceConfigs resourceConfigs) { Agents found = new Agents(); for (AgentConfig agent : agents) { if (agent.hasAllResources(resourceConfigs) && !agent.isDisabled()) { found.add(agent); } } return found; }
public SchedulingContext permittedAgent(String permittedAgentUuid) { Agents permitted = new Agents(); for (AgentConfig agent : agents) { if (agent.getUuid().equals(permittedAgentUuid)) { permitted.add(agent); } } DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, permitted, profiles); context.variables = variables.overrideWith(new EnvironmentVariablesConfig()); context.rerun = rerun; return context; }
public Agents agentsForPipeline(final CaseInsensitiveString pipelineName) { Agents configs = new Agents(); if (environments.isPipelineAssociatedWithAnyEnvironment(pipelineName)) { EnvironmentConfig forPipeline = environments.findEnvironmentForPipeline(pipelineName); for (EnvironmentAgentConfig environmentAgentConfig : forPipeline.getAgents()) { configs.add(goConfigService.agentByUuid(environmentAgentConfig.getUuid())); } } else { for (AgentConfig agentConfig : goConfigService.agents()) { if (!environments.isAgentUnderEnvironment(agentConfig.getUuid())) { configs.add(agentConfig); } } } return configs; }
@Test public void shouldGiveAListOfUuids() throws Exception { Agents agents = new Agents(); agents.add(new AgentConfig("1", "localhost", "2")); AgentConfig denied = new AgentConfig("2", "localhost", "2"); denied.setDisabled(true); agents.add(denied); Set<String> uuids = agents.acceptedUuids(); assertThat(uuids.size(), is(2)); assertThat(uuids, hasItem("1")); assertThat(uuids, hasItem("2")); }
@Test public void agentWithNoIpAddressShouldBeValid() throws Exception { CruiseConfig cruiseConfig = new BasicCruiseConfig(); AgentConfig agent = new AgentConfig("uuid", null, null); cruiseConfig.agents().add(agent); assertThat(cruiseConfig.validateAfterPreprocess().isEmpty(), is(true)); }
@Test public void shouldHaveActionAddIfAllAgentsHaveThatResource() { resourceConfigs.add(new ResourceConfig("all")); agents.add(new AgentConfig("uuid1", "host1", "127.0.0.1", new ResourceConfigs("all"))); agents.add(new AgentConfig("uuid2", "host2", "127.0.0.2", new ResourceConfigs("all"))); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections, hasItem(new TriStateSelection("all", TriStateSelection.Action.add))); }
@Test public void shouldBeNoChangeIfAllAgentsHaveThatResource() { resourceConfigs.add(new ResourceConfig("some")); agents.add(new AgentConfig("uuid1", "host1", "127.0.0.1", new ResourceConfigs("some"))); agents.add(new AgentConfig("uuid2", "host2", "127.0.0.2", new ResourceConfigs())); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections, hasItem(new TriStateSelection("some", TriStateSelection.Action.nochange))); }
@Test public void shouldHaveActionRemoveIfNoAgentsHaveResource() { resourceConfigs.add(new ResourceConfig("none")); agents.add(new AgentConfig("uuid1", "host1", "127.0.0.1", new ResourceConfigs("one"))); agents.add(new AgentConfig("uuid2", "host2", "127.0.0.2", new ResourceConfigs("two"))); List<TriStateSelection> selections = TriStateSelection.forAgentsResources(resourceConfigs, agents); assertThat(selections, hasItem(new TriStateSelection("none", TriStateSelection.Action.remove))); }
@Test public void shouldAllowResourcesOnNonElasticAgents() throws Exception { BasicCruiseConfig cruiseConfig = GoConfigMother.configWithPipelines("dev", "qa"); AgentConfig agentConfig = new AgentConfig("uuid", "hostname", "10.10.10.10"); cruiseConfig.agents().add(agentConfig); agentConfig.addResourceConfig(new ResourceConfig("foo")); assertThat(cruiseConfig.validateAfterPreprocess().isEmpty(), is(true)); }
@Test public void shouldFindAgentByUuid() { Agents agents = new Agents(); agents.add(new AgentConfig("1", "localhost", "2")); assertThat(agents.getAgentByUuid("1").getHostname(), is("localhost")); }
@Test public void shouldFindAllAgentResources() { cruiseConfig.agents().add(new AgentConfig("uuid", "host1", "127.0.0.1", new ResourceConfigs("from-agent"))); assertThat(cruiseConfig.getAllResources(), hasItem(new ResourceConfig("from-agent"))); }
@Test public void shouldReturnValidationErrorMessages(){ BasicCruiseConfig cruiseConfig = new BasicCruiseConfig(); BasicEnvironmentConfig environmentConfig = new BasicEnvironmentConfig(new CaseInsensitiveString("env")); environmentConfig.addAgent("does_not_exist_1"); environmentConfig.addAgent("does_not_exist_2"); environmentConfig.addAgent("exists_3"); cruiseConfig.addEnvironment(environmentConfig); cruiseConfig.agents().add(new AgentConfig("exists_3")); List<ConfigErrors> errors = new EnvironmentAgentValidator().validateConfig(cruiseConfig); assertThat(errors.size(), is(2)); assertThat(errors.get(0).on(EnvironmentAgentConfig.UUID), is("Environment 'env' has an invalid agent uuid 'does_not_exist_1'")); assertThat(errors.get(1).on(EnvironmentAgentConfig.UUID), is("Environment 'env' has an invalid agent uuid 'does_not_exist_2'")); } }
@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 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 shouldCreateAJobForEachAgentIfRunOnAllAgentsIsTrue() throws Exception { Agents agents = new Agents(); agents.add(new AgentConfig("uuid1")); agents.add(new AgentConfig("uuid2")); JobConfig jobConfig = new JobConfig("foo"); jobConfig.setRunOnAllAgents(true); SchedulingContext context = mock(SchedulingContext.class); when(context.getApprovedBy()).thenReturn("chris"); when(context.findAgentsMatching(new ResourceConfigs())).thenReturn(agents); when(context.getEnvironmentVariablesConfig()).thenReturn(new EnvironmentVariablesConfig()); when(context.overrideEnvironmentVariables(any(EnvironmentVariablesConfig.class))).thenReturn(context); RunOnAllAgents.CounterBasedJobNameGenerator jobNameGenerator = new RunOnAllAgents.CounterBasedJobNameGenerator(CaseInsensitiveString.str(jobConfig.name())); JobInstances jobs = instanceFactory.createJobInstance(new CaseInsensitiveString("stageName"), jobConfig, context, new TimeProvider(), jobNameGenerator); assertThat(jobs.toArray(), hasItemInArray(hasProperty("name", is("foo-runOnAll-1")))); assertThat(jobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid1")))); assertThat(jobs.toArray(), hasItemInArray(hasProperty("runOnAllAgents", is(true)))); assertThat(jobs.toArray(), hasItemInArray(hasProperty("name", is("foo-runOnAll-1")))); assertThat(jobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid2")))); assertThat(jobs.toArray(), hasItemInArray(hasProperty("runOnAllAgents", is(true)))); assertThat(jobs.size(), is(2)); }
@Test public void shouldNotThrowExceptionIfValidationPasses() throws Exception { BasicCruiseConfig cruiseConfig = new BasicCruiseConfig(); BasicEnvironmentConfig environmentConfig = new BasicEnvironmentConfig(new CaseInsensitiveString("env")); environmentConfig.addAgent("exists_1"); cruiseConfig.addEnvironment(environmentConfig); cruiseConfig.agents().add(new AgentConfig("exists_1")); new EnvironmentAgentValidator().validate(cruiseConfig); assertThat(environmentConfig.getAgents().first().errors().isEmpty(), is(true)); }