@Test
  public void shouldStopAutoSchedulingIfDiskSpaceIsLessThanMinimum() throws Exception {
    diskSpaceSimulator.simulateDiskFull();

    scheduleHelper.autoSchedulePipelinesWithRealMaterials();

    assertThat(
        serverHealthService.getLogsAsText(),
        containsString(
            "Go Server has run out of artifacts disk space. Scheduling has been stopped"));
    assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), not(hasItem(MINGLE_PIPELINE_NAME)));
  }
  @Test
  public void shouldStopManualSchedulingIfDiskSpaceIsLessThanMinimum() throws Exception {
    diskSpaceSimulator.simulateDiskFull();

    final HashMap<String, String> revisions = new HashMap<String, String>();
    final HashMap<String, String> environmentVariables = new HashMap<String, String>();
    buildCauseProducer.manualProduceBuildCauseAndSave(
        MINGLE_PIPELINE_NAME,
        Username.ANONYMOUS,
        new ScheduleOptions(revisions, environmentVariables, new HashMap<String, String>()),
        new ServerHealthStateOperationResult());

    assertThat(
        serverHealthService.getLogsAsText(),
        containsString(
            "Go Server has run out of artifacts disk space. Scheduling has been stopped"));
    assertThat(pipelineScheduleQueue.toBeScheduled().keySet(), not(hasItem(MINGLE_PIPELINE_NAME)));
  }
 @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 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 shouldSetAServerHealthMessageWhenMaterialForPipelineWithBuildCauseIsNotFound()
      throws IllegalArtifactLocationException, IOException {
    PipelineConfig pipelineConfig =
        PipelineConfigMother.pipelineConfig(
            "last",
            new StageConfig(
                new CaseInsensitiveString("stage"), new JobConfigs(new JobConfig("job-one"))));
    pipelineConfig.materialConfigs().clear();
    SvnMaterialConfig onDirOne =
        MaterialConfigsMother.svnMaterialConfig(
            "google.com", "dirOne", "loser", "boozer", false, "**/*.html");
    final P4MaterialConfig onDirTwo =
        MaterialConfigsMother.p4MaterialConfig(
            "host:987654321", "zoozer", "secret", "through-the-window", true);
    onDirTwo.setConfigAttributes(Collections.singletonMap(ScmMaterialConfig.FOLDER, "dirTwo"));
    pipelineConfig.addMaterialConfig(onDirOne);
    pipelineConfig.addMaterialConfig(onDirTwo);

    configHelper.addPipeline(pipelineConfig);

    Pipeline building = PipelineMother.building(pipelineConfig);
    final Pipeline pipeline = dbHelper.savePipelineWithMaterials(building);

    CruiseConfig cruiseConfig = configHelper.currentConfig();
    PipelineConfig cfg = cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("last"));
    cfg.removeMaterialConfig(cfg.materialConfigs().get(1));
    configHelper.writeConfigFile(cruiseConfig);

    assertThat(
        serverHealthService.filterByScope(HealthStateScope.forPipeline("last")).size(), is(0));

    final long jobId = pipeline.getStages().get(0).getJobInstances().get(0).getId();

    Date currentTime = new Date(System.currentTimeMillis() - 1);
    Pipeline loadedPipeline =
        (Pipeline)
            transactionTemplate.execute(
                new TransactionCallback() {
                  public Object doInTransaction(TransactionStatus status) {
                    Pipeline loadedPipeline = null;
                    try {
                      loadedPipeline = loader.pipelineWithPasswordAwareBuildCauseByBuildId(jobId);
                      fail(
                          "should not have loaded pipeline with build-cause as one of the necessary materials was not found");
                    } catch (Exception e) {
                      assertThat(e, is(instanceOf(StaleMaterialsOnBuildCause.class)));
                      assertThat(
                          e.getMessage(),
                          is(
                              "Cannot load job 'last/"
                                  + pipeline.getCounter()
                                  + "/stage/1/job-one' because material "
                                  + onDirTwo
                                  + " was not found in config."));
                    }
                    return loadedPipeline;
                  }
                });

    assertThat(loadedPipeline, is(nullValue()));

    JobInstance reloadedJobInstance = jobInstanceService.buildById(jobId);
    assertThat(reloadedJobInstance.getState(), is(JobState.Completed));
    assertThat(reloadedJobInstance.getResult(), is(JobResult.Failed));

    assertThat(
        serverHealthService
            .filterByScope(HealthStateScope.forJob("last", "stage", "job-one"))
            .size(),
        is(1));
    ServerHealthState error =
        serverHealthService
            .filterByScope(HealthStateScope.forJob("last", "stage", "job-one"))
            .get(0);
    assertThat(
        error,
        is(
            ServerHealthState.error(
                "Cannot load job 'last/"
                    + pipeline.getCounter()
                    + "/stage/1/job-one' because material "
                    + onDirTwo
                    + " was not found in config.",
                "Job for pipeline 'last/"
                    + pipeline.getCounter()
                    + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed.",
                HealthStateType.general(HealthStateScope.forJob("last", "stage", "job-one")))));
    DateTime expiryTime = (DateTime) ReflectionUtil.getField(error, "expiryTime");
    assertThat(expiryTime.toDate().after(currentTime), is(true));
    assertThat(
        expiryTime.toDate().before(new Date(System.currentTimeMillis() + 5 * 60 * 1000 + 1)),
        is(true));

    String logText =
        FileUtil.readToEnd(consoleService.findConsoleArtifact(reloadedJobInstance.getIdentifier()));
    assertThat(
        logText,
        containsString(
            "Cannot load job 'last/"
                + pipeline.getCounter()
                + "/stage/1/job-one' because material "
                + onDirTwo
                + " was not found in config."));
    assertThat(
        logText,
        containsString(
            "Job for pipeline 'last/"
                + pipeline.getCounter()
                + "/stage/1/job-one' has been failed as one or more material configurations were either changed or removed."));
  }