@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));
  }
Example #2
0
 public void cancelStage(Stage stage) {
   for (JobInstance job : stage.getJobInstances()) {
     job.cancel();
     jobInstanceDao.updateStateAndResult(job);
   }
   stage.calculateResult();
   updateResultInTransaction(stage, StageResult.Cancelled);
 }
 private MaterialRevision materialRevisionForDownstream(
     DependencyMaterial material, Stage upstreamStage) {
   StageIdentifier identifier = upstreamStage.getIdentifier();
   String rev = identifier.getStageLocator();
   String pipelineLabel = identifier.getPipelineLabel();
   return new MaterialRevision(
       material, new Modification(new Date(), rev, pipelineLabel, upstreamStage.getPipelineId()));
 }
Example #4
0
 public Stage saveBuildingStage(String pipelineName, String stageName) throws SQLException {
   Pipeline pipeline = saveTestPipeline(pipelineName, stageName);
   Stage stage = saveBuildingStage(pipeline.getStages().byName(stageName));
   for (JobInstance job : stage.getJobInstances()) {
     job.setIdentifier(new JobIdentifier(pipeline, stage, job));
   }
   return stage;
 }
Example #5
0
 public void buildingBuildInstance(Stage stage) {
   if (!stage.getJobInstances().isEmpty()) {
     JobInstance jobInstance = stage.getJobInstances().get(0);
     jobInstance.setAgentUuid(AGENT_UUID);
     jobInstance.changeState(JobState.Building);
     jobInstanceDao.updateAssignedInfo(jobInstance);
   }
 }
 private void assertPipelineScheduled(PipelineConfig config) {
   Stage evolveStage =
       stageDao.mostRecentWithBuilds(
           CaseInsensitiveString.str(config.name()),
           config.findBy(new CaseInsensitiveString("dev")));
   assertThat(evolveStage.getName(), is("dev"));
   assertThat(evolveStage.getJobInstances().size(), is(1));
   assertThat(evolveStage.getJobInstances().first().getState(), is(JobState.Scheduled));
 }
Example #7
0
 public void failStage(Stage stage, Date completionDate) {
   for (JobInstance job : stage.getJobInstances()) {
     job.completing(Failed, completionDate);
     job.completed(completionDate);
     jobInstanceDao.updateStateAndResult(job);
   }
   stage.calculateResult();
   updateResultInTransaction(stage, StageResult.Failed);
 }
Example #8
0
 public void onefailAndOnePassedBuildInstances(Stage instance) {
   final JobInstance first = instance.getJobInstances().get(0);
   final JobInstance second = instance.getJobInstances().get(1);
   first.completing(Failed);
   second.completing(Failed);
   first.completed(new Date());
   second.completed(new Date());
   jobInstanceDao.updateStateAndResult(first);
   jobInstanceDao.updateStateAndResult(second);
 }
Example #9
0
 public void buildInstanceWithDiscontinuedState(Stage instance) {
   final JobInstance first = instance.getJobInstances().get(0);
   final JobInstance second = instance.getJobInstances().get(1);
   first.completing(JobResult.Passed);
   second.changeState(JobState.Discontinued);
   second.setResult(JobResult.Passed);
   first.completed(new Date());
   jobInstanceDao.updateStateAndResult(first);
   jobInstanceDao.updateStateAndResult(second);
   updateResultInTransaction(instance, StageResult.Passed);
 }
  private void assertPipelinesScheduled() {
    Pipeline minglePipeline =
        pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(mingleConfig.name()));
    Stage mingleStage = minglePipeline.getFirstStage();
    assertThat(mingleStage.getName(), is(STAGE_NAME));
    assertThat(mingleStage.getJobInstances().size(), is(2));
    JobInstance mingleJob = mingleStage.getJobInstances().first();
    assertThat(mingleJob.getState(), is(JobState.Scheduled));

    assertPipelineScheduled(evolveConfig);
    assertPipelineScheduled(goConfig);
  }
 private Pipeline passFirstStage(PipelineConfig pipelineConfig) {
   Stage completedMingleStage =
       stageDao.mostRecentWithBuilds(
           CaseInsensitiveString.str(pipelineConfig.name()),
           pipelineConfig.findBy(new CaseInsensitiveString("dev")));
   dbHelper.passStage(completedMingleStage);
   dbHelper.passStage(completedMingleStage);
   assertThat(completedMingleStage.getJobInstances().first().getState(), is(JobState.Completed));
   Pipeline pipeline =
       pipelineDao.mostRecentPipeline(CaseInsensitiveString.str(pipelineConfig.name()));
   return dbHelper.passPipeline(pipeline);
 }
  @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 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());
  }
Example #14
0
  private Pipeline scheduleJobInstancesAndSavePipeline(Pipeline pipeline) {
    assertNotInserted(pipeline.getId());
    for (Stage stage : pipeline.getStages()) {
      for (JobInstance jobInstance : stage.getJobInstances()) {
        jobInstance.schedule();
      }
    }
    this.savePipelineWithStagesAndMaterials(pipeline);

    long pipelineId = pipeline.getId();
    assertIsInserted(pipelineId);
    return pipeline;
  }
 @Test
 public void shouldEncodeStageLocator() throws Exception {
   Stage stage1 =
       new Stage(
           "stage-c%d",
           new JobInstances(), GoConstants.DEFAULT_APPROVED_BY, "manual", new TimeProvider());
   stage1.setIdentifier(new StageIdentifier("pipeline-a%b", 1, "label-1", "stage-c%d", "1"));
   StageJsonPresentationModel presenter =
       new StageJsonPresentationModel(pipeline, stage1, null, new Agents());
   Map json = presenter.toJson();
   assertThat(
       JsonUtils.from(json).getString("stageLocator"), is("pipeline-a%25b/1/stage-c%25d/1"));
 }
Example #16
0
 @Deprecated // Only actually passes the first stage. Use newPipelineWithAllStagesPassed instead
 public Pipeline passPipeline(Pipeline pipeline) {
   for (Stage stage : pipeline.getStages()) {
     passStage(stage);
   }
   Stages loadedStages = new Stages();
   for (Stage stage : pipeline.getStages()) {
     loadedStages.add(stageDao.stageById(stage.getId()));
   }
   Pipeline loadedPipeline = this.pipelineDao.loadPipeline(pipeline.getId());
   loadedPipeline.setStages(loadedStages);
   return loadedPipeline;
 }
 @Before
 public void setUp() {
   MaterialRevisions materialRevisions = multipleModifications();
   stage =
       StageMother.withOneScheduledBuild("stage", "job-that-will-fail", "job-that-will-pass", 1);
   modifications = BuildCause.createWithModifications(materialRevisions, "");
   pipeline = new Pipeline("pipeline", PipelineLabel.COUNT_TEMPLATE, modifications, stage);
   stage.setIdentifier(new StageIdentifier(pipeline, stage));
   for (JobInstance job : stage.getJobInstances()) {
     job.setIdentifier(new JobIdentifier(pipeline, stage, job));
   }
   pipeline.setId(PIPELINE_ID);
   pipeline.updateCounter(9);
 }
  @Test
  public void shouldReturnLastSuccesfulLabel() throws Exception {
    StageIdentifier successfulStage =
        new StageIdentifier(pipeline.getName(), 1, "LABEL:1", stage.getName(), "1");
    StageJsonPresentationModel presenter =
        new StageJsonPresentationModel(pipeline, stage, successfulStage, new Agents());
    Map json = presenter.toJson();

    new JsonTester(json)
        .shouldContain(
            "{ 'last_successful_label' : 'LABEL:1', 'last_successful_stage_locator' : '"
                + String.format("%s/%s/%s/%s", pipeline.getName(), "1", stage.getName(), "1")
                + "' }");
  }
  @Test
  public void shouldCancelBuildBelongingToNonExistentPipelineWhenCreatingWork() throws Exception {
    fixture.createPipelineWithFirstStageScheduled();
    Pipeline pipeline = pipelineDao.mostRecentPipeline(fixture.pipelineName);

    ScheduledPipelineLoader scheduledPipelineLoader = mock(ScheduledPipelineLoader.class);
    when(scheduledPipelineLoader.pipelineWithPasswordAwareBuildCauseByBuildId(
            pipeline.getFirstStage().getJobInstances().first().getId()))
        .thenThrow(new PipelineNotFoundException("thrown by mockPipelineService"));

    GoConfigService mockGoConfigService = mock(GoConfigService.class);
    CruiseConfig config = configHelper.currentConfig();
    configHelper.removePipeline(fixture.pipelineName, config);
    when(mockGoConfigService.getCurrentConfig()).thenReturn(config);

    buildAssignmentService =
        new BuildAssignmentService(
            mockGoConfigService,
            jobInstanceService,
            scheduleService,
            agentService,
            environmentConfigService,
            timeProvider,
            transactionTemplate,
            scheduledPipelineLoader,
            pipelineService,
            builderFactory,
            agentRemoteHandler);
    buildAssignmentService.onTimer();

    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("some-other-resource"));

    try {
      buildAssignmentService.assignWorkToAgent(agent(agentConfig));
      fail("should have thrown PipelineNotFoundException");
    } catch (PipelineNotFoundException e) {
      // ok
    }

    pipeline = pipelineDao.mostRecentPipeline(fixture.pipelineName);

    JobInstance job = pipeline.getFirstStage().getJobInstances().first();
    assertThat(job.getState(), is(JobState.Completed));
    assertThat(job.getResult(), is(JobResult.Cancelled));
    Stage stage = stageDao.findStageWithIdentifier(job.getIdentifier().getStageIdentifier());
    assertThat(stage.getState(), is(StageState.Cancelled));
    assertThat(stage.getResult(), is(StageResult.Cancelled));
  }
 private long rerunJob(
     String jobName,
     PipelineConfig pipelineConfig,
     Pipeline previousSuccessfulBuildWithOlderPackageConfig) {
   Stage stage =
       instanceFactory.createStageForRerunOfJobs(
           previousSuccessfulBuildWithOlderPackageConfig.getFirstStage(),
           asList(jobName),
           new DefaultSchedulingContext(),
           pipelineConfig.getFirstStageConfig(),
           new TimeProvider(),
           configHelper.getGoConfigDao().md5OfConfigFile());
   stage = stageService.save(previousSuccessfulBuildWithOlderPackageConfig, stage);
   return stage.getFirstJob().getId();
 }
  @Test
  public void shouldSaveBuildStateCorrectly() throws Exception {
    PipelineConfig cruisePlan = configHelper.addPipeline("cruise", "dev", repository);
    goConfigService.forceNotifyListeners();

    autoSchedulePipelines("mingle", "evolve", "cruise");

    Stage cruise =
        stageDao.mostRecentWithBuilds(
            CaseInsensitiveString.str(cruisePlan.name()),
            cruisePlan.findBy(new CaseInsensitiveString("dev")));
    System.out.println("cruise = " + cruise);
    JobInstance instance = cruise.getJobInstances().first();
    System.out.println("instance = " + instance);
    assertThat(instance.getState(), is(JobState.Scheduled));
  }
  @Test
  public void shouldGetAPresenterWithLabelAndRelevantBuildPlans() throws Exception {
    DurationBeans durations =
        new DurationBeans(
            new DurationBean(stage.getJobInstances().getByName("job-that-will-fail").getId(), 12L));

    StageJsonPresentationModel presenter =
        new StageJsonPresentationModel(
            pipeline, stage, null, new Agents(), durations, new TrackingTool());
    Map json = presenter.toJson();

    new JsonTester(json)
        .shouldContain(
            "{ 'stageName' : 'stage',"
                + "  'builds' : ["
                + "    { 'name' : 'job-that-will-fail', 'last_build_duration' : '12' },"
                + "    { 'name' : 'job-that-will-pass' }, "
                + "    { 'name' : 'scheduledBuild' }"
                + "  ],"
                + " 'current_label' : '"
                + pipeline.getLabel()
                + "',"
                + " 'id' : '1' "
                + "}");
    assertFalse(
        "JSON shouldn't contain last_successful_label",
        json.toString().contains("last_successful_label"));
  }
Example #23
0
 public Stage saveBuildingStage(Stage stage) {
   for (JobInstance jobInstance : stage.getJobInstances()) {
     JobInstanceMother.setBuildingState(jobInstance);
     jobInstance.setAgentUuid(AGENT_UUID);
     jobInstanceDao.updateAssignedInfo(jobInstance);
   }
   return stage;
 }
  @Test
  public void shouldNotScheduleBuildIfNoModification() throws Exception {
    autoSchedulePipelines("mingle", "evolve");
    // Get the scheduled evolve stage and complete it.
    Stage evolveInstance =
        stageDao.mostRecentWithBuilds(
            CaseInsensitiveString.str(evolveConfig.name()),
            evolveConfig.findBy(new CaseInsensitiveString("dev")));
    dbHelper.passStage(evolveInstance);
    stageDao.stageStatusChanged(evolveInstance);

    autoSchedulePipelines();
    Stage mostRecent =
        stageDao.mostRecentWithBuilds(
            CaseInsensitiveString.str(evolveConfig.name()),
            evolveConfig.findBy(new CaseInsensitiveString("dev")));

    assertThat(mostRecent.getId(), is(evolveInstance.getId()));
    assertThat(mostRecent.getJobInstances().first().getState(), is(JobState.Completed));
  }
  @Test
  public void shouldCancelBuildBelongingToNonExistentPipeline() throws Exception {
    fixture.createPipelineWithFirstStageScheduled();
    buildAssignmentService.onTimer();

    configHelper.removePipeline(fixture.pipelineName);

    AgentConfig agentConfig = AgentMother.localAgent();
    agentConfig.addResource(new Resource("some-other-resource"));

    assertThat(
        (NoWork) buildAssignmentService.assignWorkToAgent(agent(agentConfig)),
        Matchers.is(BuildAssignmentService.NO_WORK));
    Pipeline pipeline = pipelineDao.mostRecentPipeline(fixture.pipelineName);
    JobInstance job = pipeline.getFirstStage().getJobInstances().first();
    assertThat(job.getState(), is(JobState.Completed));
    assertThat(job.getResult(), is(JobResult.Cancelled));
    Stage stage = stageDao.findStageWithIdentifier(job.getIdentifier().getStageIdentifier());
    assertThat(stage.getState(), is(StageState.Cancelled));
    assertThat(stage.getResult(), is(StageResult.Cancelled));
  }
Example #26
0
 public StageResult completeAllJobs(Stage stage, JobResult jobResult) {
   for (JobInstance job : stage.getJobInstances()) {
     JobInstanceMother.setBuildingState(job);
     job.setAgentUuid(AGENT_UUID);
     job.completing(jobResult);
     job.completed(new DateTime().plusMinutes(5).toDate());
     jobInstanceDao.updateAssignedInfo(job);
   }
   StageResult stageResult;
   switch (jobResult) {
     case Failed:
       stageResult = StageResult.Failed;
       break;
     case Cancelled:
       stageResult = StageResult.Cancelled;
       break;
     default:
       stageResult = StageResult.Passed;
   }
   stage.calculateResult();
   return stageResult;
 }
  @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
      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));
  }
Example #29
0
 public Stage saveStage(Pipeline pipeline, Stage instance, int order) {
   instance.setOrderId(order);
   return stageDao.saveWithJobs(pipeline, instance);
 }