public Stage createStageInstance(PipelineConfig pipelineConfig, CaseInsensitiveString stageName, SchedulingContext context, String md5, Clock clock) { StageConfig stageConfig = pipelineConfig.findBy(stageName); if (stageConfig == null) { throw new StageNotFoundException(pipelineConfig.name(), stageName); } return createStageInstance(stageConfig, context, md5, clock); }
public Stage scheduleStage(String pipelineName, String stageName, SchedulingContext context) { PipelineConfig pipelineConfig = getCurrentConfig().pipelineConfigByName(new CaseInsensitiveString(pipelineName)); return instanceFactory.createStageInstance(pipelineConfig, new CaseInsensitiveString(stageName), context, getCurrentConfig().getMd5(), clock); }
private static Stage scheduleInstance(StageConfig stageConfig) { Stage stageInstance = new InstanceFactory().createStageInstance(stageConfig, new DefaultSchedulingContext(DEFAULT_APPROVED_BY), "md5-test", new TimeProvider()); stageInstance.building(); return stageInstance; }
@Test public void shouldSetTheConfigVersionOnSchedulingAStage() throws Exception { PipelineConfig pipelineConfig = PipelineConfigMother.createPipelineConfig("foo-pipeline", "foo-stage", "foo-job"); DefaultSchedulingContext schedulingContext = new DefaultSchedulingContext("loser"); String md5 = "foo-md5"; Stage actualStage = instanceFactory.createStageInstance(pipelineConfig, new CaseInsensitiveString("foo-stage"), schedulingContext, md5, clock); assertThat(actualStage.getConfigVersion(), is(md5)); }
@Test public void shouldSetManualApprovalOnStageInstance() { StageConfig stageConfig = StageConfigMother.custom("test", Approval.manualApproval()); Stage instance = instanceFactory.createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), "md5", new TimeProvider()); assertThat(instance.getApprovalType(), is(GoConstants.APPROVAL_MANUAL)); }
@Test public void shouldSetAutoApprovalOnStageInstance() { StageConfig stageConfig = StageConfigMother.custom("test", Approval.automaticApproval()); Stage instance = instanceFactory.createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), "md5", new TimeProvider()); assertThat(instance.getApprovalType(), is(GoConstants.APPROVAL_SUCCESS)); }
public Pipeline createPipelineInstance(PipelineConfig pipelineConfig, BuildCause buildCause, SchedulingContext context, String md5, Clock clock) { buildCause.assertMaterialsMatch(pipelineConfig.materialConfigs()); buildCause.assertPipelineConfigAndMaterialRevisionMatch(pipelineConfig); EnvironmentVariables variables = EnvironmentVariables.toEnvironmentVariables(pipelineConfig.getVariables()); variables.overrideWith(buildCause.getVariables()); return new Pipeline(CaseInsensitiveString.str(pipelineConfig.name()), pipelineConfig.getLabelTemplate(), buildCause, variables, createStageInstance(pipelineConfig.first(), context, md5, clock)); }
@Test public void shouldSetFetchMaterialsFlagOnStageInstance() throws Exception { StageConfig stageConfig = StageConfigMother.custom("test", Approval.automaticApproval()); stageConfig.setFetchMaterials(false); Stage instance = instanceFactory.createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), "md5", new TimeProvider()); assertThat(instance.shouldFetchMaterials(), is(false)); }
@Test public void shouldThrowStageNotFoundExceptionWhenStageDoesNotExist() { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("cruise"), new MaterialConfigs(), new StageConfig(new CaseInsensitiveString("first"), new JobConfigs())); try { instanceFactory.createStageInstance(pipelineConfig, new CaseInsensitiveString("doesNotExist"), new DefaultSchedulingContext(), "md5", clock); fail("Found the stage doesNotExist but, well, it doesn't"); } catch (StageNotFoundException expected) { assertThat(expected.getMessage(), is("Stage 'doesNotExist' not found in pipeline 'cruise'")); } }
public static StageInstanceModels stageHistory(PipelineConfig pipelineConfig, Date modificationDate) { StageInstanceModels history = new StageInstanceModels(); for (StageConfig stageConfig : pipelineConfig) { StageInstanceModel item = new StageInstanceModel(CaseInsensitiveString.str(stageConfig.name()), "1", buildHistory(stageConfig, modificationDate)); item.setCounter("1"); item.setApprovalType(new InstanceFactory().createStageInstance(stageConfig, new DefaultSchedulingContext("anyone"), md5, new TimeProvider()).getApprovalType()); if (stageConfig.requiresApproval()) { item.setApprovedBy(APPROVED_BY); } else { item.setApprovedBy(GoConstants.DEFAULT_APPROVED_BY); } history.add(item); } return history; }
@Test public void shouldCreateAStageInstanceThroughInstanceFactory() { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("cruise"), new MaterialConfigs(), new StageConfig(new CaseInsensitiveString("first"), new JobConfigs(new JobConfig("job1"), new JobConfig("job2")))); Stage actualStage = instanceFactory.createStageInstance(pipelineConfig, new CaseInsensitiveString("first"), new DefaultSchedulingContext(), "md5", clock); JobInstances jobInstances = new JobInstances(); jobInstances.add(new JobInstance("job1", clock)); jobInstances.add(new JobInstance("job2", clock)); Stage expectedStage = new Stage("first", jobInstances, "Unknown", null, Approval.SUCCESS, clock); assertThat(actualStage, is(expectedStage)); }
@Test public void shouldFailWhenNoAgentsmatchAJob() throws Exception { DefaultSchedulingContext context = new DefaultSchedulingContext("raghu/vinay", new Agents()); JobConfig fooJob = new JobConfig(new CaseInsensitiveString("foo"), new ResourceConfigs(), new ArtifactConfigs()); fooJob.setRunOnAllAgents(true); StageConfig stageConfig = new StageConfig( new CaseInsensitiveString("blah-stage"), new JobConfigs( fooJob, new JobConfig(new CaseInsensitiveString("bar"), new ResourceConfigs(), new ArtifactConfigs()) ) ); try { instanceFactory.createStageInstance(stageConfig, context, "md5", new TimeProvider()); fail("expected exception but not thrown"); } catch (Exception e) { assertThat(e.getMessage(), is("Could not find matching agents to run job [foo] of stage [blah-stage].")); } }
@Test public void shouldUseRightNameGenerator() { StageConfig stageConfig = StageConfigMother.custom("dev", "rails", "java", "html"); JobConfig railsConfig = stageConfig.getJobs().getJob(new CaseInsensitiveString("rails")); railsConfig.setRunOnAllAgents(true); railsConfig.addResourceConfig("foobar"); JobConfig javaConfig = stageConfig.getJobs().getJob(new CaseInsensitiveString("java")); javaConfig.setRunInstanceCount(2); AgentConfig agent1 = new AgentConfig("abcd1234", "host", "127.0.0.2", new ResourceConfigs(new ResourceConfig("foobar"))); AgentConfig agent2 = new AgentConfig("1234abcd", "ghost", "192.168.1.2", new ResourceConfigs(new ResourceConfig("baz"), new ResourceConfig("foobar"))); AgentConfig agent3 = new AgentConfig("7890abdc", "lost", "10.4.3.55", new ResourceConfigs(new ResourceConfig("crapyagent"))); DefaultSchedulingContext schedulingContext = new DefaultSchedulingContext("loser", new Agents(agent1, agent2, agent3)); Stage stageInstance = instanceFactory.createStageInstance(stageConfig, schedulingContext, "md5", clock); JobInstances jobInstances = stageInstance.getJobInstances(); assertThat(jobInstances.size(), is(5)); assertRunOnAllAgentsJobInstance(jobInstances.get(0), "rails-runOnAll-1"); assertRunOnAllAgentsJobInstance(jobInstances.get(1), "rails-runOnAll-2"); assertRunMultipleJobInstance(jobInstances.get(2), "java-runInstance-1"); assertRunMultipleJobInstance(jobInstances.get(3), "java-runInstance-2"); assertSimpleJobInstance(jobInstances.get(4), "html"); }