private FreeStyleProject createJob(Builder builder) throws IOException {
   FreeStyleProject p = createFreeStyleProject();
   p.setAssignedLabel(
       null); // let it roam free, or else it ties itself to the master since we have no slaves
   p.getBuildersList().add(builder);
   return p;
 }
  /**
   * Returns the future object for a newly created project.
   *
   * @param blockingJobName the name for the project
   * @param shell the shell command task to add
   * @param label the label to bind to master or slave
   * @return the future object for a newly created project
   * @throws IOException
   */
  private Future<FreeStyleBuild> createBlockingProject(
      String blockingJobName, Shell shell, Label label) throws IOException {
    FreeStyleProject blockingProject = this.createFreeStyleProject(blockingJobName);
    blockingProject.setAssignedLabel(label);

    blockingProject.getBuildersList().add(shell);
    Future<FreeStyleBuild> future = blockingProject.scheduleBuild2(0);

    while (!blockingProject.isBuilding()) {
      // wait until job is started
    }

    return future;
  }
Esempio n. 3
0
  @Test
  public void nonSensitiveParameters() throws Exception {
    FreeStyleProject project = j.createFreeStyleProject();
    ParametersDefinitionProperty pdb =
        new ParametersDefinitionProperty(
            new StringParameterDefinition("string", "defaultValue", "string description"));
    project.addProperty(pdb);

    CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
    project.getBuildersList().add(builder);

    FreeStyleBuild build = project.scheduleBuild2(0).get();
    Set<String> sensitiveVars = build.getSensitiveBuildVariables();

    assertNotNull(sensitiveVars);
    assertFalse(sensitiveVars.contains("string"));
  }
Esempio n. 4
0
  @Test
  public void choiceWithLTGT() throws Exception {
    FreeStyleProject project = j.createFreeStyleProject();
    ParametersDefinitionProperty pdp =
        new ParametersDefinitionProperty(
            new ChoiceParameterDefinition("choice", "Choice 1\nChoice <2>", "choice description"));
    project.addProperty(pdp);
    CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
    project.getBuildersList().add(builder);

    WebClient wc = j.createWebClient();
    wc.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec");
    HtmlForm form = page.getFormByName("parameters");

    HtmlElement element =
        (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']");
    assertNotNull(element);
    assertEquals(
        "choice description",
        ((HtmlElement)
                element
                    .getNextSibling()
                    .getNextSibling()
                    .selectSingleNode("td[@class='setting-description']"))
            .getTextContent());
    assertEquals(
        "choice",
        ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
    HtmlOption opt =
        (HtmlOption) element.selectSingleNode("td/div/select/option[@value='Choice <2>']");
    assertNotNull(opt);
    assertEquals("Choice <2>", opt.asText());
    opt.setSelected(true);

    j.submit(form);
    Queue.Item q = j.jenkins.getQueue().getItem(project);
    if (q != null) q.getFuture().get();
    else Thread.sleep(1000);

    assertNotNull(builder.getEnvVars());
    assertEquals("Choice <2>", builder.getEnvVars().get("CHOICE"));
  }
  public void testAnt() throws Exception {
    Ant.AntInstallation ant = configureDefaultAnt();
    String antPath = ant.getHome();
    Jenkins.getInstance()
        .getDescriptorByType(Ant.DescriptorImpl.class)
        .setInstallations(new AntInstallation("ant", "THIS IS WRONG"));

    project.setScm(new SingleFileSCM("build.xml", "<project name='foo'/>"));
    project.getBuildersList().add(new Ant("-version", "ant", null, null, null));
    configureDumpEnvBuilder();

    Build build = project.scheduleBuild2(0).get();
    assertBuildStatus(Result.FAILURE, build);

    ToolLocationNodeProperty property =
        new ToolLocationNodeProperty(
            new ToolLocationNodeProperty.ToolLocation(
                jenkins.getDescriptorByType(AntInstallation.DescriptorImpl.class), "ant", antPath));
    slave.getNodeProperties().add(property);

    build = project.scheduleBuild2(0).get();
    System.out.println(build.getLog());
    assertBuildStatus(Result.SUCCESS, build);
  }
  public void testMaven() throws Exception {
    MavenInstallation maven = configureDefaultMaven();
    String mavenPath = maven.getHome();
    Jenkins.getInstance()
        .getDescriptorByType(Maven.DescriptorImpl.class)
        .setInstallations(new MavenInstallation("maven", "THIS IS WRONG", NO_PROPERTIES));

    project.getBuildersList().add(new Maven("--version", "maven"));
    configureDumpEnvBuilder();

    Build build = project.scheduleBuild2(0).get();
    assertBuildStatus(Result.FAILURE, build);

    ToolLocationNodeProperty property =
        new ToolLocationNodeProperty(
            new ToolLocationNodeProperty.ToolLocation(
                jenkins.getDescriptorByType(MavenInstallation.DescriptorImpl.class),
                "maven",
                mavenPath));
    slave.getNodeProperties().add(property);

    build = project.scheduleBuild2(0).get();
    assertBuildStatus(Result.SUCCESS, build);
  }
  public void testMultipleExecutors() throws Exception {

    // Job1 runs for 1 second, no dependencies
    FreeStyleProject theJob1 = createFreeStyleProject("MultipleExecutor_Job1");
    theJob1.getBuildersList().add(new Shell("sleep 1; exit 0"));
    assertTrue(theJob1.getBuilds().isEmpty());

    // Job2 returns immediatly but can't run while Job1 is running.
    FreeStyleProject theJob2 = createFreeStyleProject("MultipleExecutor_Job2");
    {
      BuildBlockerProperty theProperty = new BuildBlockerProperty();
      theProperty.setBlockingJobs("MultipleExecutor_Job1");
      theJob2.addProperty(theProperty);
    }
    assertTrue(theJob1.getBuilds().isEmpty());

    // allow executing two simultanious jobs
    int theOldNumExecutors = Hudson.getInstance().getNumExecutors();
    Hudson.getInstance().setNumExecutors(2);

    Future<FreeStyleBuild> theFuture1 = theJob1.scheduleBuild2(0);
    Future<FreeStyleBuild> theFuture2 = theJob2.scheduleBuild2(0);
    while (!theFuture1.isDone() || !theFuture2.isDone()) {
      // let the jobs process
    }

    // check if job2 was not started before job1 was finished
    Run theRun1 = theJob1.getLastBuild();
    Run theRun2 = theJob2.getLastBuild();
    assertTrue(theRun1.getTimeInMillis() + theRun1.getDuration() <= theRun2.getTimeInMillis());

    // restore changed settings
    Hudson.getInstance().setNumExecutors(theOldNumExecutors);
    theJob2.delete();
    theJob1.delete();
  }
Esempio n. 8
0
  public void testFoldableCauseAction() throws Exception {
    final OneShotEvent buildStarted = new OneShotEvent();
    final OneShotEvent buildShouldComplete = new OneShotEvent();

    hudson.quietPeriod = 0;
    FreeStyleProject project = createFreeStyleProject();
    // Make build sleep a while so it blocks new builds
    project
        .getBuildersList()
        .add(
            new TestBuilder() {
              public boolean perform(
                  AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
                  throws InterruptedException, IOException {
                buildStarted.signal();
                buildShouldComplete.block();
                return true;
              }
            });

    // Start one build to block others
    assertTrue(project.scheduleBuild(new UserCause()));
    buildStarted.block(); // wait for the build to really start

    // Schedule a new build, and trigger it many ways while it sits in queue
    Future<FreeStyleBuild> fb = project.scheduleBuild2(0, new UserCause());
    assertNotNull(fb);
    assertFalse(project.scheduleBuild(new SCMTriggerCause()));
    assertFalse(project.scheduleBuild(new UserCause()));
    assertFalse(project.scheduleBuild(new TimerTriggerCause()));
    assertFalse(project.scheduleBuild(new RemoteCause("1.2.3.4", "test")));
    assertFalse(project.scheduleBuild(new RemoteCause("4.3.2.1", "test")));
    assertFalse(project.scheduleBuild(new SCMTriggerCause()));
    assertFalse(project.scheduleBuild(new RemoteCause("1.2.3.4", "test")));
    assertFalse(project.scheduleBuild(new RemoteCause("1.2.3.4", "foo")));
    assertFalse(project.scheduleBuild(new SCMTriggerCause()));
    assertFalse(project.scheduleBuild(new TimerTriggerCause()));

    // Wait for 2nd build to finish
    buildShouldComplete.signal();
    FreeStyleBuild build = fb.get();

    // Make sure proper folding happened.
    CauseAction ca = build.getAction(CauseAction.class);
    assertNotNull(ca);
    StringBuilder causes = new StringBuilder();
    for (Cause c : ca.getCauses()) causes.append(c.getShortDescription() + "\n");
    assertEquals(
        "Build causes should have all items, even duplicates",
        "Started by user anonymous\nStarted by an SCM change\n"
            + "Started by user anonymous\nStarted by timer\n"
            + "Started by remote host 1.2.3.4 with note: test\n"
            + "Started by remote host 4.3.2.1 with note: test\n"
            + "Started by an SCM change\n"
            + "Started by remote host 1.2.3.4 with note: test\n"
            + "Started by remote host 1.2.3.4 with note: foo\n"
            + "Started by an SCM change\nStarted by timer\n",
        causes.toString());

    // View for build should group duplicates
    WebClient wc = new WebClient();
    String buildPage = wc.getPage(build, "").asText().replace('\n', ' ');
    assertTrue(
        "Build page should combine duplicates and show counts: " + buildPage,
        buildPage.contains(
            "Started by user anonymous (2 times) "
                + "Started by an SCM change (3 times) "
                + "Started by timer (2 times) "
                + "Started by remote host 1.2.3.4 with note: test (2 times) "
                + "Started by remote host 4.3.2.1 with note: test "
                + "Started by remote host 1.2.3.4 with note: foo"));
  }
 private void configureDumpEnvBuilder() throws IOException {
   if (Functions.isWindows()) project.getBuildersList().add(new BatchFile("set"));
   else project.getBuildersList().add(new Shell("export"));
 }
Esempio n. 10
0
  @Test
  public void parameterTypes() throws Exception {
    FreeStyleProject otherProject = j.createFreeStyleProject();
    otherProject.scheduleBuild2(0).get();

    FreeStyleProject project = j.createFreeStyleProject();
    ParametersDefinitionProperty pdp =
        new ParametersDefinitionProperty(
            new StringParameterDefinition("string", "defaultValue", "string description"),
            new BooleanParameterDefinition("boolean", true, "boolean description"),
            new ChoiceParameterDefinition("choice", "Choice 1\nChoice 2", "choice description"),
            new RunParameterDefinition("run", otherProject.getName(), "run description", null));
    project.addProperty(pdp);
    CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
    project.getBuildersList().add(builder);

    WebClient wc = j.createWebClient();
    wc.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec");

    HtmlForm form = page.getFormByName("parameters");

    HtmlElement element = (HtmlElement) form.selectSingleNode("//tr[td/div/input/@value='string']");
    assertNotNull(element);
    assertEquals(
        "string description",
        ((HtmlElement)
                element
                    .getNextSibling()
                    .getNextSibling()
                    .selectSingleNode("td[@class='setting-description']"))
            .getTextContent());

    HtmlTextInput stringParameterInput =
        (HtmlTextInput) element.selectSingleNode(".//input[@name='value']");
    assertEquals("defaultValue", stringParameterInput.getAttribute("value"));
    assertEquals(
        "string",
        ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());
    stringParameterInput.setAttribute("value", "newValue");

    element = (HtmlElement) form.selectSingleNode("//tr[td/div/input/@value='boolean']");
    assertNotNull(element);
    assertEquals(
        "boolean description",
        ((HtmlElement)
                element
                    .getNextSibling()
                    .getNextSibling()
                    .selectSingleNode("td[@class='setting-description']"))
            .getTextContent());
    Object o = element.selectSingleNode(".//input[@name='value']");
    System.out.println(o);
    HtmlCheckBoxInput booleanParameterInput = (HtmlCheckBoxInput) o;
    assertEquals(true, booleanParameterInput.isChecked());
    assertEquals(
        "boolean",
        ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());

    element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']");
    assertNotNull(element);
    assertEquals(
        "choice description",
        ((HtmlElement)
                element
                    .getNextSibling()
                    .getNextSibling()
                    .selectSingleNode("td[@class='setting-description']"))
            .getTextContent());
    assertEquals(
        "choice",
        ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());

    element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='run']");
    assertNotNull(element);
    assertEquals(
        "run description",
        ((HtmlElement)
                element
                    .getNextSibling()
                    .getNextSibling()
                    .selectSingleNode("td[@class='setting-description']"))
            .getTextContent());
    assertEquals(
        "run",
        ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent());

    j.submit(form);
    Queue.Item q = j.jenkins.getQueue().getItem(project);
    if (q != null) q.getFuture().get();
    else Thread.sleep(1000);

    assertEquals("newValue", builder.getEnvVars().get("STRING"));
    assertEquals("true", builder.getEnvVars().get("BOOLEAN"));
    assertEquals("Choice 1", builder.getEnvVars().get("CHOICE"));
    assertEquals(
        j.jenkins.getRootUrl() + otherProject.getLastBuild().getUrl(),
        builder.getEnvVars().get("RUN"));
  }