@Test public void shouldUpdateResultOfStageWhenJobCompletes_irrespectiveOfOtherThreadsPrimingStageCache() throws Exception { Pipeline assigned = preCondition.createPipelineWithFirstStageAssigned(); Stage stage = assigned.findStage(preCondition.devStage); StageSummaryModel model = stageService.findStageSummaryByIdentifier( stage.getIdentifier(), new Username(new CaseInsensitiveString("foo")), new HttpLocalizedOperationResult()); JobIdentifier identifier = stage.getFirstJob().getIdentifier(); scheduleService.updateJobStatus(identifier, JobState.Building); scheduleService.jobCompleting(identifier, JobResult.Passed, "uuid"); Stage stageLoadedByOtherFlows = stageDao.stageById(stage.getId()); // priming the cache scheduleService.updateJobStatus(stage.getFirstJob().getIdentifier(), JobState.Completed); StageSummaryModel reloadedModel = stageService.findStageSummaryByIdentifier( stage.getIdentifier(), new Username(new CaseInsensitiveString("foo")), new HttpLocalizedOperationResult()); Stage reloadedStage = reloadedModel.getStage(); assertThat(reloadedStage.getFirstJob().getState(), is(JobState.Completed)); assertThat( reloadedStage.getCompletedByTransitionId(), is(reloadedStage.getFirstJob().getTransitions().byState(JobState.Completed).getId())); assertThat(reloadedStage.getResult(), is(StageResult.Passed)); assertThat(reloadedStage.getState(), is(StageState.Passed)); }
@Test public void shouldUpdateResultOfStageWhenJobCompletesOnTransactionCommitOnly() throws Exception { StageService stageService = mock(StageService.class); StageDao stageDao = mock(StageDao.class); SchedulingPerformanceLogger schedulingPerformanceLogger = mock(SchedulingPerformanceLogger.class); ScheduleService service = new ScheduleService( goConfigService, pipelineService, stageService, schedulingChecker, pipelineScheduledTopic, pipelineDao, stageDao, stageOrderService, securityService, pipelineScheduleQueue, jobInstanceService, jobInstanceDao, agentAssignment, environmentConfigService, pipelineLockService, serverHealthService, transactionTemplate, agentService, synchronizationManager, null, null, null, null, schedulingPerformanceLogger); Pipeline assigned = preCondition.createPipelineWithFirstStageAssigned(); Stage stage = assigned.findStage(preCondition.devStage); when(stageService.stageById(stage.getId())).thenThrow(new RuntimeException("find fails")); try { service.updateJobStatus(stage.getFirstJob().getIdentifier(), JobState.Completed); fail("should have failed because stage lookup bombed"); } catch (Exception e) { // ignore } verify(stageDao, never()) .clearCachedAllStages( stage.getIdentifier().getPipelineName(), stage.getIdentifier().getPipelineCounter(), stage.getName()); }
@Test public void shouldUpdateResultOfStageWhenJobCompletes() throws Exception { // ScheduleService service = new ScheduleService(goConfigService, pipelineService, // stageService, currentActivityService, schedulingChecker, pipelineScheduledTopic, pipelineDao, // stageDao, stageOrderService, securityService, pipelineScheduleQueue, // jobInstanceService, jobInstanceDao, agentAssignment, environmentConfigService, // pipelineLockService, serverHealthService, transactionTemplate, agentService); Pipeline assigned = preCondition.createPipelineWithFirstStageAssigned(); Stage stage = assigned.findStage(preCondition.devStage); StageSummaryModel model = stageService.findStageSummaryByIdentifier( stage.getIdentifier(), new Username(new CaseInsensitiveString("foo")), new HttpLocalizedOperationResult()); assertThat(model.getStage().getFirstJob().getState(), is(JobState.Assigned)); scheduleService.updateJobStatus(stage.getFirstJob().getIdentifier(), JobState.Building); StageSummaryModel reloadedModel = stageService.findStageSummaryByIdentifier( stage.getIdentifier(), new Username(new CaseInsensitiveString("foo")), new HttpLocalizedOperationResult()); assertThat(reloadedModel.getStage().getFirstJob().getState(), is(JobState.Building)); }
private Pipeline manualSchedule(String pipelineName) throws Exception, StageAlreadyBuildingException { scheduleHelper.manuallySchedulePipelineWithRealMaterials( pipelineName, new Username(new CaseInsensitiveString("some user name"))); scheduleService.autoSchedulePipelinesFromRequestBuffer(); return pipelineService.mostRecentFullPipelineByName(pipelineName); }
private Pipeline tryToScheduleAPipeline() { BuildCause buildCause = BuildCause.createWithModifications(modifyOneFile(preCondition.pipelineConfig()), ""); dbHelper.saveMaterials(buildCause.getMaterialRevisions()); pipelineScheduleQueue.schedule(preCondition.pipelineName, buildCause); scheduleService.autoSchedulePipelinesFromRequestBuffer(); return pipelineService.mostRecentFullPipelineByName(preCondition.pipelineName); }
@Test public void shouldConsumeAllBuildCausesInServerHealth() throws Exception { pipelineScheduleQueue.schedule( "mingle", BuildCause.createManualForced(modifyOneFile(mingleConfig), Username.ANONYMOUS)); pipelineScheduleQueue.schedule( "evolve", BuildCause.createManualForced(modifyOneFile(evolveConfig), Username.ANONYMOUS)); scheduleService.autoSchedulePipelinesFromRequestBuffer(); assertThat(pipelineScheduleQueue.toBeScheduled().size(), is(0)); }
private Pipeline manualSchedule(String pipelineName) { final HashMap<String, String> revisions = new HashMap<String, String>(); final HashMap<String, String> environmentVariables = new HashMap<String, String>(); buildCauseProducer.manualProduceBuildCauseAndSave( pipelineName, new Username(new CaseInsensitiveString("some user name")), new ScheduleOptions(revisions, environmentVariables, new HashMap<String, String>()), new ServerHealthStateOperationResult()); scheduleService.autoSchedulePipelinesFromRequestBuffer(); return pipelineService.mostRecentFullPipelineByName(pipelineName); }
private void sendReminder(DateTime date) { mockStatic(DateTime.class); when(DateTime.now()).thenReturn(date); Member member = EntityFactory.createMember(); member.setId(randomLong); when(mockMemberService.findActiveMembers()).thenReturn(Lists.newArrayList(member)); when(mockAbsenceDayService.findByMemberAndMonth(anyLong(), anyInt())) .thenReturn(Lists.newArrayListWithExpectedSize(0)); scheduleService.sendReminderAfternoon(); }
@Test public void shouldScheduleJobForAllAgentsWhenToBeRunOnAllAgents() throws Exception { configHelper.addAgent("localhost", "uuid1"); configHelper.addAgent("localhost", "uuid2"); configHelper.addAgent("localhost", "uuid3"); configHelper.addAgentToEnvironment("dev", "uuid1"); configHelper.setRunOnAllAgents( CaseInsensitiveString.str(evolveConfig.name()), STAGE_NAME, "unit", true); Material stubMaterial = new TestingMaterial(); evolveConfig.setMaterialConfigs(new MaterialConfigs(stubMaterial.config())); MaterialRevisions revisions = new MaterialRevisions(); revisions.addRevision( stubMaterial, ((TestingMaterial) stubMaterial) .modificationsSince(null, null, subprocessExecutionContext)); BuildCause buildCause = BuildCause.createWithModifications(revisions, ""); dbHelper.saveMaterials(buildCause.getMaterialRevisions()); Pipeline pipeline = instanceFactory.createPipelineInstance( evolveConfig, buildCause, new DefaultSchedulingContext( DEFAULT_APPROVED_BY, environmentConfigService.agentsForPipeline(evolveConfig.name())), md5, new TimeProvider()); pipelineService.save(pipeline); Stage instance = scheduleService.scheduleStage( pipeline, STAGE_NAME, "anyone", new ScheduleService.NewStageInstanceCreator(goConfigService), new ScheduleService.ExceptioningErrorHandler()); JobInstances scheduledJobs = instance.getJobInstances(); assertThat( scheduledJobs.toArray(), hasItemInArray( hasProperty( "name", is(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("unit", 1))))); assertThat(scheduledJobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid2")))); assertThat( scheduledJobs.toArray(), hasItemInArray( hasProperty( "name", is(RunOnAllAgents.CounterBasedJobNameGenerator.appendMarker("unit", 2))))); assertThat(scheduledJobs.toArray(), hasItemInArray(hasProperty("agentUuid", is("uuid3")))); assertThat(scheduledJobs.size(), is(2)); }
@Test public void shouldNotThrowErrorWhenMaterialsChange() throws Exception { configHelper.addPipeline("cruise", "dev", repository); goConfigService.forceNotifyListeners(); scheduleHelper.autoSchedulePipelinesWithRealMaterials("mingle", "evolve", "cruise"); configHelper.replaceMaterialForPipeline( "cruise", svnMaterialConfig("http://new-material", null)); goConfigService.forceNotifyListeners(); try { scheduleService.autoSchedulePipelinesFromRequestBuffer(); } catch (Exception e) { fail("#2520 - should not cause an error if materials have changed"); } }
@Test public void shouldRemoveBuildCauseIfAnyExceptionIsThrown() throws Exception { configHelper.addPipeline("cruise", "dev", repository); goConfigService.forceNotifyListeners(); goConfigService .getCurrentConfig() .pipelineConfigByName(new CaseInsensitiveString("cruise")) .get(0) .jobConfigByConfigName(new CaseInsensitiveString("unit")) .setRunOnAllAgents(true); scheduleHelper.autoSchedulePipelinesWithRealMaterials("cruise"); goConfigService.forceNotifyListeners(); scheduleService.autoSchedulePipelinesFromRequestBuffer(); assertThat(pipelineScheduleQueue.toBeScheduled().size(), is(0)); }
@Test public void shouldRemoveBuildCauseIfPipelineNotExist() throws Exception { configHelper.addPipeline("cruise", "dev", repository); goConfigService.forceNotifyListeners(); scheduleHelper.autoSchedulePipelinesWithRealMaterials("mingle", "evolve", "cruise"); Assertions.assertWillHappen( 2, PipelineScheduleQueueMatcher.numberOfScheduledPipelinesIsAtLeast(pipelineScheduleQueue), Timeout.FIVE_SECONDS); int originalSize = pipelineScheduleQueue.toBeScheduled().size(); assertThat(originalSize, greaterThan(1)); configHelper.initializeConfigFile(); goConfigService.forceNotifyListeners(); scheduleService.autoSchedulePipelinesFromRequestBuffer(); assertThat(pipelineScheduleQueue.toBeScheduled().size(), is(0)); }
@Test public void shouldLockPipelineWhenSchedulingStage() throws Exception { scheduleAndCompleteInitialPipelines(); Pipeline pipeline = pipelineDao.mostRecentPipeline("mingle"); configHelper.lockPipeline("mingle"); assertThat(pipelineLockService.isLocked("mingle"), is(false)); scheduleService.scheduleStage( pipeline, STAGE_NAME, "anonymous", new ScheduleService.NewStageInstanceCreator(goConfigService), new ScheduleService.ExceptioningErrorHandler()); assertThat(pipelineLockService.isLocked("mingle"), is(true)); }
@Test public void shouldUpdateServerHealthWhenSchedulePipelineFails() throws Exception { pipelineScheduleQueue.schedule( "blahPipeline", saveMaterials( modifySomeFiles( goConfigService.pipelineConfigNamed(new CaseInsensitiveString("blahPipeline"))))); scheduleService.autoSchedulePipelinesFromRequestBuffer(); List<ServerHealthState> stateList = serverHealthService.filterByScope(HealthStateScope.forStage("blahPipeline", "blahStage")); assertThat(stateList.size(), is(1)); assertThat( stateList.get(0).getMessage(), is("Failed to trigger stage [blahStage] pipeline [blahPipeline]")); assertThat( stateList.get(0).getDescription(), is("Could not find matching agents to run job [job2] of stage [blahStage].")); }
@Test public void shouldReScheduleToCorrectAgent() throws Exception { JobConfig plan = evolveConfig .findBy(new CaseInsensitiveString(STAGE_NAME)) .jobConfigByInstanceName("unit", true); plan.addResource("some-resource"); scheduleHelper.schedule(evolveConfig, modifySomeFiles(evolveConfig), DEFAULT_APPROVED_BY); buildAssignmentService.onTimer(); AgentConfig agentConfig = AgentMother.localAgent(); agentConfig.addResource(new Resource("some-resource")); Work work = buildAssignmentService.assignWorkToAgent(agent(agentConfig)); assertThat(work, is(not((Work) BuildAssignmentService.NO_WORK))); Pipeline pipeline = pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(evolveConfig.name())); JobInstance job = pipeline.findStage(STAGE_NAME).findJob("unit"); JobInstance runningJob = jobInstanceDao.buildByIdWithTransitions(job.getId()); scheduleService.rescheduleJob(runningJob); pipeline = pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(evolveConfig.name())); JobInstance rescheduledJob = pipeline.findStage(STAGE_NAME).findJob("unit"); assertThat(rescheduledJob.getId(), not(runningJob.getId())); buildAssignmentService.onTimer(); Work noResourcesWork = buildAssignmentService.assignWorkToAgent( agent(AgentMother.localAgentWithResources("WITHOUT_RESOURCES"))); assertThat(noResourcesWork, is((Work) BuildAssignmentService.NO_WORK)); buildAssignmentService.onTimer(); Work correctAgentWork = buildAssignmentService.assignWorkToAgent(agent(agentConfig)); assertThat(correctAgentWork, is(not((Work) BuildAssignmentService.NO_WORK))); }
@Test public void shouldPassEnvironmentLevelEnvironmentVariablesToJobsForNewlyScheduledStage() throws Exception { scheduleAndCompleteInitialPipelines(); Pipeline pipeline = pipelineDao.mostRecentPipeline("go"); Stage stage = scheduleService.scheduleStage( pipeline, "ft", "anonymous", new ScheduleService.NewStageInstanceCreator(goConfigService), new ScheduleService.ExceptioningErrorHandler()); EnvironmentVariablesConfig jobVariables = stage.getJobInstances().first().getPlan().getVariables(); assertThat( jobVariables.size(), is(3)); // pipeline, stage, job, env is applied while creating work assertThat( jobVariables, hasItem(new EnvironmentVariableConfig("PIPELINE_LVL", "pipeline value"))); assertThat(jobVariables, hasItem(new EnvironmentVariableConfig("STAGE_LVL", "stage value"))); assertThat(jobVariables, hasItem(new EnvironmentVariableConfig("JOB_LVL", "job value"))); }
@Test public void shouldUpdateServerHealthWhenScheduleStageFails() throws Exception { try { scheduleService.scheduleStage( manualSchedule("blahPipeline"), "blahStage", "blahUser", new ScheduleService.NewStageInstanceCreator(goConfigService), new ScheduleService.ExceptioningErrorHandler()); fail("should throw CannotScheduleException"); } catch (CannotScheduleException e) { } List<ServerHealthState> stateList = serverHealthService.filterByScope(HealthStateScope.forStage("blahPipeline", "blahStage")); assertThat(stateList.size(), is(1)); assertThat( stateList.get(0).getMessage(), is("Failed to trigger stage [blahStage] pipeline [blahPipeline]")); assertThat( stateList.get(0).getDescription(), is("Could not find matching agents to run job [job2] of stage [blahStage].")); }
@Test public void generateWorkList() throws Exception { DateTime dateTimeMonth = DateTime.now().withDayOfMonth(1).withTimeAtStartOfDay(); Member member = EntityFactory.createMember(); member.setId(randomLong); List<AbsenceDay> absenceDayList = EntityFactory.createList(EntityFactory::createAbsenceDay); List<Task> taskList = EntityFactory.createList(EntityFactory::createTask); when(mockMemberService.findActiveMembers()).thenReturn(Collections.singletonList(member)); when(mockAbsenceDayService.findByMemberAndMonth(randomLong, dateTimeMonth.getMonthOfYear())) .thenReturn(absenceDayList); when(mockTaskService.findTasksForMember(member)).thenReturn(taskList); scheduleService.generateWorkList(); verify(mockMemberService).findActiveMembers(); verify(mockAbsenceDayService) .findByMemberAndMonth(eq(randomLong), eq(dateTimeMonth.getMonthOfYear())); verify(mockWorkService) .findOrCreateWorks(eq(member), eq(dateTimeMonth), eq(taskList), eq(absenceDayList)); }
private void autoSchedulePipelines(String... pipelineNames) throws Exception { scheduleHelper.autoSchedulePipelinesWithRealMaterials(pipelineNames); scheduleService.autoSchedulePipelinesFromRequestBuffer(); }