@After
 public void teardown() throws Exception {
   TestRepo.internalTearDown();
   dbHelper.onTearDown();
   FileUtil.deleteFolder(goConfigService.artifactsDir());
   FileUtil.deleteFolder(workingFolder);
   TestRepo.internalTearDown();
   pipelineScheduleQueue.clear();
 }
 @After
 public void teardown() throws Exception {
   dbHelper.onTearDown();
   FileUtil.deleteFolder(goConfigService.artifactsDir());
   pipelineScheduleQueue.clear();
   agentAssignment.clear();
   CONFIG_HELPER.onTearDown();
 }
Пример #3
0
 public String arguments() {
   StringBuffer buffer = new StringBuffer();
   if (buildFile != null) {
     buffer.append("-f ").append('\"').append(FileUtil.normalizePath(buildFile)).append('\"');
   }
   if (target != null) {
     buffer.append(" ").append(target);
   }
   return buffer.toString();
 }
 @After
 public void teardown() throws Exception {
   goCache.clear();
   agentService.clearAll();
   fixture.onTearDown();
   dbHelper.onTearDown();
   configHelper.onTearDown();
   FileUtil.deleteFolder(goConfigService.artifactsDir());
   agentAssignment.clear();
   agentRemoteHandler.connectedAgents().clear();
 }
Пример #5
0
 @Test
 public void shouldGetAgentLauncherJarInputStream() throws IOException {
   try {
     writeToFile(SystemEnvironment.AGENT_LAUNCHER_JAR_PATH);
     assertThat(
         FileUtil.readToEnd(agentService.agentLauncherJarInputStream()),
         is(SystemEnvironment.AGENT_LAUNCHER_JAR_PATH));
   } finally {
     new File(SystemEnvironment.AGENT_LAUNCHER_JAR_PATH).delete();
   }
 }
Пример #6
0
 @After
 public void teardown() throws Exception {
   if (pipelineWithTwoStages != null) {
     pipelineWithTwoStages.onTearDown();
   }
   dbHelper.onTearDown();
   pipelineScheduleQueue.clear();
   testRepo.tearDown();
   FileUtil.deleteFolder(new File("pipelines"));
   configHelper.onTearDown();
 }
Пример #7
0
 private void generateHTMLResponse(ServletResponse response) {
   String path = "backup_in_progress.html";
   InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(path);
   response.setContentType("text/html");
   try {
     String content = FileUtil.readToEnd(resourceAsStream);
     content = replaceStringLiterals(content);
     response.getWriter().print(content);
     resourceAsStream.close();
   } catch (IOException e) {
     LOGGER.error(String.format("General IOException: %s", e.getMessage()));
   }
 }
 private void validateNotOutsideSandbox() {
   String dest = this.getFolder();
   if (dest == null) {
     return;
   }
   if (!(FileUtil.isFolderInsideSandbox(dest))) {
     addError(
         FOLDER,
         String.format(
             "Dest folder '%s' is not valid. It must be a sub-directory of the working folder.",
             dest));
   }
 }
Пример #9
0
  @Test
  public void shouldNotThrowNPEIfTheWorkingDirectoryIsEmpty() throws Exception {
    SvnMaterial svnMaterial1 = repo.createMaterial("multiple-materials/trunk/part1", "part1");
    SvnMaterial svnMaterial2 = repo.createMaterial("multiple-materials/trunk/part2", "part2");
    Materials materials = new Materials(svnMaterial1, svnMaterial2);

    Revision revision =
        latestRevision(svnMaterial1, pipelineDir, new TestSubprocessExecutionContext());
    updateMaterials(materials, revision);

    FileUtil.deleteFolder(pipelineDir);

    updateMaterials(materials, revision);
  }
 @Before
 public void setup() throws Exception {
   configHelper = new GoConfigFileHelper();
   dbHelper.onSetUp();
   configHelper.usingCruiseConfigDao(goConfigDao).initializeConfigFile();
   configHelper.onSetUp();
   goConfigService.forceNotifyListeners();
   File dumpDir = tempFolder.newFolder("perf-pipelineapi-test");
   FileUtil.deleteDirectoryNoisily(dumpDir);
   dumpDir.mkdirs();
   result = new HttpLocalizedOperationResult();
   user = new Username(new CaseInsensitiveString("admin"));
   consoleAppenderForPerformanceTest = "ConsoleAppenderForPerformanceTest";
   rollingFileAppenderForPerformanceTest = "RollingFileAppenderForPerformanceTest";
 }
Пример #11
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldBeAbleToRunCommandsFromRelativeDirectories() throws IOException {
    File shellScript = new File(tempFolder, "hello-world.sh");
    FileUtil.writeContentToFile("echo ${PWD}", shellScript);
    assertThat(shellScript.setExecutable(true), is(true));

    CommandLine line = CommandLine.createCommandLine("../hello-world.sh").withWorkingDir(subFolder);

    InMemoryStreamConsumer out = new InMemoryStreamConsumer();
    line.execute(out, new EnvironmentVariableContext(), null).waitForExit();

    assertThat(out.getAllOutput().trim(), endsWith("subFolder"));
  }
Пример #12
0
  @Test
  public void shouldRefreshWorkingDirectoryIfUsernameInUrlChanges() throws Exception {
    HgMaterial material = new HgMaterial("http://*****:*****@localhost:9999", null);
    final List<Modification> modifications =
        material.latestModification(workingFolder, new TestSubprocessExecutionContext());
    final File unversionedFile = new File(workingFolder, "unversioned.txt");
    FileUtil.writeContentToFile("something", unversionedFile);
    assertTrue(unversionedFile.exists());

    material = new HgMaterial("http://*****:*****@localhost:9999", null);
    material.modificationsSince(
        workingFolder,
        new StringRevision(modifications.get(0).getRevision()),
        new TestSubprocessExecutionContext());
    assertFalse(unversionedFile.exists());
  }
 public void validateNotSubdirectoryOf(String otherSCMMaterialFolder) {
   String myDirPath = this.getFolder();
   if (myDirPath == null || otherSCMMaterialFolder == null) {
     return;
   }
   File myDir = new File(myDirPath);
   File otherDir = new File(otherSCMMaterialFolder);
   try {
     if (FileUtil.isSubdirectoryOf(myDir, otherDir)) {
       addError(
           FOLDER,
           "Invalid Destination Directory. Every material needs a different destination directory and the directories should not be nested.");
     }
   } catch (IOException e) {
     throw bomb("Dest folder specification is not valid. " + e.getMessage());
   }
 }
Пример #14
0
  public Properties generate(File[] allTestFiles, String uploadDestPath) {
    FileOutputStream transformedHtml = null;
    File mergedResults =
        new File(folderToUpload.getAbsolutePath() + FileUtil.fileseparator() + TEST_RESULTS_FILE);
    File mergedResource = null;
    FileInputStream mergedFileStream = null;
    try {
      mergedResource = mergeAllTestResultToSingleFile(allTestFiles);
      transformedHtml = new FileOutputStream(mergedResults);

      try {
        mergedFileStream = new FileInputStream(mergedResource);
        InputStream xslt = getClass().getResourceAsStream("unittests.xsl");
        Source xmlSource = new StreamSource(mergedFileStream);
        Source xsltSource = new StreamSource(xslt);
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);
        StreamResult result = new StreamResult(transformedHtml);
        trans.transform(xmlSource, result);
      } catch (Exception e) {
        publisher.reportErrorMessage(
            "Unable to publish test properties. Error was " + e.getMessage(), e);
      } finally {
        IOUtils.closeQuietly(mergedFileStream);
      }

      extractProperties(mergedResults);

      publisher.upload(mergedResults, uploadDestPath);

      return null;
    } catch (Exception e) {
      publisher.reportErrorMessage(
          "Unable to publish test properties. Error was " + e.getMessage(), e);
    } finally {
      IOUtils.closeQuietly(mergedFileStream);
      IOUtils.closeQuietly(transformedHtml);
      if (mergedResource != null) {
        mergedResource.delete();
      }
    }
    return new Properties();
  }
Пример #15
0
 public File findArtifact(LocatableEntity locatableEntity, String path)
     throws IllegalArtifactLocationException {
   try {
     File root = chooseExistingRoot(locatableEntity);
     if (root == null) {
       root = preferredRoot(locatableEntity);
     }
     File file = new File(root, path);
     if (!FileUtil.isSubdirectoryOf(root, file)) {
       throw new IllegalArtifactLocationException(
           "Artifact path ["
               + path
               + "] is illegal."
               + " Path must be inside the artifact directory.");
     }
     return file;
   } catch (IOException e) {
     throw new IllegalArtifactLocationException(
         "Artifact path [" + path + "] is illegal." + e.getMessage(), e);
   }
 }
Пример #16
0
  @Test
  @RunIf(
      value = EnhancedOSChecker.class,
      arguments = {DO_NOT_RUN_ON, OSChecker.WINDOWS})
  public void shouldNotLogPasswordsOnExceptionThrown() throws IOException {
    File dir = FileUtil.createTempFolder();
    File file = new File(dir, "test.sh");
    FileOutputStream out = new FileOutputStream(file);
    out.write("echo $1 && exit 10".getBytes());
    out.close();

    CommandLine line =
        CommandLine.createCommandLine("/bin/sh")
            .withArg(file.getAbsolutePath())
            .withArg(new PasswordArgument("secret"));
    try {
      line.runOrBomb(null);
    } catch (CommandLineException e) {
      assertThat(e.getMessage(), not(containsString("secret")));
    }
  }
  private void setupPipelines(Integer numberOfPipelinesToBeCreated) throws Exception {
    String groupName = "jumbo";
    String configFile = "<FULL PATH TO YOUR CONFIG FILE>";
    String xml = FileUtil.readContentFromFile(new File(configFile));
    xml = goConfigMigration.upgradeIfNecessary(xml);
    goConfigService
        .fileSaver(false)
        .saveConfig(xml, goConfigService.getConfigForEditing().getMd5());
    LOGGER.info(
        String.format(
            "Total number of pipelines in this config: %s",
            goConfigService.getConfigForEditing().allPipelines().size()));
    if (goConfigService.getConfigForEditing().hasPipelineGroup(groupName)) {
      ((BasicPipelineConfigs) goConfigService.getConfigForEditing().findGroup(groupName)).clear();
    }
    final CruiseConfig configForEditing = goConfigService.getConfigForEditing();
    for (int i = 0; i < numberOfPipelinesToBeCreated; i++) {
      JobConfig jobConfig = new JobConfig(new CaseInsensitiveString("job"));
      StageConfig stageConfig =
          new StageConfig(new CaseInsensitiveString("stage"), new JobConfigs(jobConfig));
      PipelineConfig pipelineConfig =
          new PipelineConfig(
              new CaseInsensitiveString("pipeline" + i),
              new MaterialConfigs(new GitMaterialConfig("FOO")),
              stageConfig);
      configForEditing.addPipeline(groupName, pipelineConfig);
    }

    goConfigService.updateConfig(
        new UpdateConfigCommand() {
          @Override
          public CruiseConfig update(CruiseConfig cruiseConfig) throws Exception {
            return configForEditing;
          }
        });
  }
Пример #18
0
 @After
 public void cleanupRepo() {
   repo.tearDown();
   FileUtil.deleteFolder(pipelineDir);
 }
Пример #19
0
 public File getSource(File rootPath) {
   return new File(FileUtil.applyBaseDirIfRelativeAndNormalize(rootPath, new File(getSrc())));
 }
Пример #20
0
 @After
 public void tearDown() {
   FileUtil.deleteFolder(rootPath);
 }
  @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."));
  }
Пример #22
0
 @After
 public void tearDown() throws Exception {
   for (File folder : toDelete) {
     FileUtil.deleteFolder(folder);
   }
 }
Пример #23
0
  private File createScript(String name, String content) throws IOException {
    File shellScript = new File(subFolder, name);

    FileUtil.writeContentToFile(content, shellScript);
    return shellScript;
  }