public DefaultSchedulingContext(String approvedBy) { this(approvedBy, new Agents()); }
public SchedulingContext overrideEnvironmentVariables(EnvironmentVariablesConfig environmentVariablesConfig) { DefaultSchedulingContext context = new DefaultSchedulingContext(approvedBy, new Agents(agents), profiles); context.variables = variables.overrideWith(environmentVariablesConfig); context.rerun = rerun; return context; }
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; }
@Before public void before() { resourceConfigs = new HashSet<>(); resourceConfigs.add(new ResourceConfig("one")); resourceConfigs.add(new ResourceConfig("two")); agents = new Agents(); }
@Test public void shouldOnlyFindAgentsThatMatchResourcesSpecified() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); Agents matchingAgents = new Agents(linux, windows); DefaultSchedulingContext context = new DefaultSchedulingContext("approved", matchingAgents); assertThat(context.findAgentsMatching(resources("linux")), is(new Agents(linux))); }
@Test public void shouldFindNoAgentsIfNoneMatch() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); Agents matchingAgents = new Agents(linux, windows); DefaultSchedulingContext context = new DefaultSchedulingContext("approved", matchingAgents); assertThat(context.findAgentsMatching(resources("macosx")), is(new Agents())); }
@Test public void shouldNotMatchDeniedAgents() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); windows.disable(); Agents matchingAgents = new Agents(linux, windows); DefaultSchedulingContext context = new DefaultSchedulingContext("approved", matchingAgents); assertThat(context.findAgentsMatching(resources()), is(new Agents(linux))); }
@Test public void shouldClearAgentAssignment_ForSingleInstanceJobType() { Date old = new DateTime().minusDays(2).toDate(); JobInstance rails = jobInstance(old, "rails", 7, 10); JobInstance java = jobInstance(old, "java", 12, 22); Stage stage = stage(9, rails, java); Stage newStage = instanceFactory.createStageForRerunOfJobs(stage, a("rails"), new DefaultSchedulingContext("loser", new Agents()), StageConfigMother.custom("dev", "rails", "java"), new TimeProvider(), "md5"); assertThat(newStage.getJobInstances().getByName("rails").getAgentUuid(), is(nullValue())); assertThat(newStage.getJobInstances().getByName("java").getAgentUuid(), is(not(nullValue()))); }
@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 shouldAddAgentIntoMemoryAfterAgentIsManuallyAddedInConfigFile() throws Exception { AgentInstances agentInstances = new AgentInstances(mock(AgentStatusChangeListener.class)); AgentConfig agentConfig = new AgentConfig("uuid20", "CCeDev01", "10.18.5.20"); agentInstances.sync(new Agents(agentConfig)); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid20").agentConfig(), is(agentConfig)); }
@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 shouldFindAllAgentsIfNoResourcesAreSpecified() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); Agents matchingAgents = new Agents(linux, windows); DefaultSchedulingContext context = new DefaultSchedulingContext("approved", matchingAgents); assertThat(context.findAgentsMatching(new ResourceConfigs()), is(matchingAgents)); }
@Test public void shouldNotRerun_WhenJobConfigIsChangedToRunMultipleInstance_ForSingleJobInstance() { Date old = new DateTime().minusDays(2).toDate(); StageConfig stageConfig = StageConfigMother.custom("dev", "rails", "java"); JobConfig railsConfig = stageConfig.getJobs().getJob(new CaseInsensitiveString("rails")); DefaultSchedulingContext schedulingContext = new DefaultSchedulingContext("loser", new Agents()); JobInstances jobs = instanceFactory.createJobInstance(new CaseInsensitiveString("dev"), railsConfig, schedulingContext, new TimeProvider(), null); Stage stage = createStageInstance(old, jobs); Stage newStage = null; railsConfig.setRunInstanceCount(10); CannotRerunJobException exception = null; try { newStage = instanceFactory.createStageForRerunOfJobs(stage, a("rails"), schedulingContext, stageConfig, new TimeProvider(), "md5"); fail("should not schedule since job config changed to run multiple instance"); } catch (CannotRerunJobException e) { exception = e; } assertThat(exception.getJobName(), is("rails")); assertThat(exception.getInformation(), is("Run configuration for job has been changed to 'run multiple instance'.")); assertThat(newStage, is(nullValue())); }
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 shouldRemoveAgentWhenAgentIsRemovedFromConfigFile() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, idle, building); Agents oneAgentIsRemoved = new Agents(new AgentConfig("uuid2", "CCeDev01", "10.18.5.1")); agentInstances.sync(oneAgentIsRemoved); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid2"), is(idle)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid1"), is(new NullAgentInstance("uuid1"))); }
@Test public void shouldNotRemovePendingAgentDuringSync() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building()); agentInstances.add(pending); Agents agents = new Agents(); agentInstances.sync(agents); assertThat(agentInstances.size(), is(1)); assertThat(agentInstances.findAgentAndRefreshStatus("uuid4").getStatus(), is(AgentStatus.Pending)); }
@Test public void shouldSyncAgent() throws Exception { AgentInstances agentInstances = new AgentInstances(systemEnvironment, agentStatusChangeListener, AgentInstanceMother.building(), idle); AgentConfig agentConfig = new AgentConfig("uuid2", "CCeDev01", "10.18.5.1"); agentConfig.setDisabled(true); Agents oneAgentIsRemoved = new Agents(agentConfig); agentInstances.sync(oneAgentIsRemoved); assertThat(agentInstances.findAgentAndRefreshStatus("uuid2").getStatus(), is(AgentStatus.Disabled)); }