public void testSelfExcludingJobs() throws Exception {

    BuildBlockerProperty theProperty = new BuildBlockerProperty();
    theProperty.setBlockingJobs("SelfExcluding_.*");

    FreeStyleProject theJob1 = createFreeStyleProject("SelfExcluding_Job1");
    theJob1.addProperty(theProperty);
    assertTrue(theJob1.getBuilds().isEmpty());

    FreeStyleProject theJob2 = createFreeStyleProject("SelfExcluding_Job2");
    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);

    long theStartTime = System.currentTimeMillis();
    long theEndTime = theStartTime;
    while ((!theFuture1.isDone() || !theFuture2.isDone()) && theEndTime < theStartTime + 5000) {
      theEndTime = System.currentTimeMillis();
    }

    // if more then five seconds have passed, we assume its a deadlock.
    assertTrue(theEndTime < theStartTime + 5000);

    // restore changed settings
    Hudson.getInstance().setNumExecutors(theOldNumExecutors);
    theJob2.delete();
    theJob1.delete();
  }
  @Test
  public void testRebuild() throws Exception {
    // job with promotion process
    FreeStyleProject p1 = j.createFreeStyleProject("promojob");

    // setup promotion process
    JobPropertyImpl promotion = new JobPropertyImpl(p1);
    p1.addProperty(promotion);
    PromotionProcess proc = promotion.addProcess("promo");
    proc.conditions.add(new SelfPromotionCondition(false));

    // build it
    FreeStyleBuild b1 = j.assertBuildStatusSuccess(p1.scheduleBuild2(0));
    j.waitUntilNoActivity();

    // verify that promotion happened
    Assert.assertSame(proc.getBuilds().getLastBuild().getTarget(), b1);

    // job with parameter
    FreeStyleProject p2 = j.createFreeStyleProject("paramjob");

    // add promoted build param
    p2.addProperty(
        new ParametersDefinitionProperty(
            new PromotedBuildParameterDefinition(
                "var", "promojob", "promo", "promoted build param to test rebuild")));

    // build with parameter
    FreeStyleBuild b2 = j.assertBuildStatusSuccess(p2.scheduleBuild2(0));

    // validate presence of parameter
    ParametersAction a1 = b2.getAction(ParametersAction.class);
    Assert.assertNotNull(a1);
    Assert.assertFalse(a1.getParameters().isEmpty());
    ParameterValue v1 = a1.getParameter("var");
    Assert.assertTrue(v1 instanceof PromotedBuildParameterValue);
    PromotedBuildParameterValue pbpv1 = (PromotedBuildParameterValue) v1;
    Assert.assertEquals(b1.getNumber(), pbpv1.getRun().getNumber());

    // rebuild it
    JenkinsRule.WebClient wc = j.createWebClient();
    HtmlPage page = wc.getPage(b2, "rebuild");
    HtmlForm form = page.getFormByName("config");
    j.submit(form);
    j.waitUntilNoActivity();

    // validate presence of parameter
    FreeStyleBuild rebuild = p2.getLastBuild();
    j.assertBuildStatusSuccess(rebuild);
    Assert.assertNotEquals(b2.getNumber(), rebuild.getNumber());
    ParametersAction a2 = rebuild.getAction(ParametersAction.class);
    Assert.assertNotNull(a2);
    Assert.assertFalse(a2.getParameters().isEmpty());
    ParameterValue v2 = a2.getParameter("var");
    Assert.assertTrue(v2 instanceof PromotedBuildParameterValue);
    PromotedBuildParameterValue pbpv2 = (PromotedBuildParameterValue) v2;
    Assert.assertEquals(b1.getNumber(), pbpv2.getRun().getNumber());
  }
  /**
   * One test for all for faster execution.
   *
   * @throws Exception
   */
  public void testCanRun() throws Exception {
    // init slave
    LabelAtom slaveLabel = new LabelAtom("slave");
    LabelAtom masterLabel = new LabelAtom("master");

    DumbSlave slave = this.createSlave(slaveLabel);
    SlaveComputer c = slave.getComputer();
    c.connect(false).get(); // wait until it's connected
    if (c.isOffline()) {
      fail("Slave failed to go online: " + c.getLog());
    }

    BuildBlockerQueueTaskDispatcher dispatcher = new BuildBlockerQueueTaskDispatcher();

    String blockingJobName = "blockingJob";

    Shell shell = new Shell("sleep 1");

    Future<FreeStyleBuild> future1 = createBlockingProject("xxx", shell, masterLabel);
    Future<FreeStyleBuild> future2 = createBlockingProject(blockingJobName, shell, masterLabel);
    Future<FreeStyleBuild> future3 = createBlockingProject("yyy", shell, slaveLabel);
    // add project to slave
    FreeStyleProject project = this.createFreeStyleProject();
    project.setAssignedLabel(slaveLabel);

    Queue.BuildableItem item =
        new Queue.BuildableItem(
            new Queue.WaitingItem(Calendar.getInstance(), project, new ArrayList<Action>()));

    CauseOfBlockage causeOfBlockage = dispatcher.canRun(item);

    assertNull(causeOfBlockage);

    BuildBlockerProperty property = new BuildBlockerProperty();

    property.setBlockingJobs(".*ocki.*");

    project.addProperty(property);

    causeOfBlockage = dispatcher.canRun(item);
    assertNotNull(causeOfBlockage);

    assertEquals(
        "Blocking job " + blockingJobName + " is running.", causeOfBlockage.getShortDescription());

    while (!(future1.isDone() && future2.isDone() && future3.isDone())) {
      // wait until jobs are done.
    }
  }
コード例 #4
0
ファイル: ParametersTest.java プロジェクト: ZouYu99/hudson
  @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"));
  }
コード例 #5
0
ファイル: ParametersTest.java プロジェクト: ZouYu99/hudson
  @Test
  @Issue("JENKINS-11543")
  public void unicodeParametersArePresetCorrectly() throws Exception {
    final FreeStyleProject p = j.createFreeStyleProject();
    ParametersDefinitionProperty pdb =
        new ParametersDefinitionProperty(
            new StringParameterDefinition("sname:a¶‱ﻷ", "svalue:a¶‱ﻷ", "sdesc:a¶‱ﻷ"),
            new FileParameterDefinition("fname:a¶‱ﻷ", "fdesc:a¶‱ﻷ"));
    p.addProperty(pdb);

    WebClient wc = j.createWebClient();
    wc.setThrowExceptionOnFailingStatusCode(false); // Ignore 405
    HtmlPage page = wc.getPage(p, "build");

    // java.lang.IllegalArgumentException: No such parameter definition: <gibberish>.
    wc.setThrowExceptionOnFailingStatusCode(true);
    final HtmlForm form = page.getFormByName("parameters");
    form.submit(form.getButtonByCaption("Build"));
  }
コード例 #6
0
ファイル: ParametersTest.java プロジェクト: ZouYu99/hudson
  @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"));
  }
コード例 #7
0
ファイル: ParametersTest.java プロジェクト: ZouYu99/hudson
  @Test
  @Issue("JENKINS-3539")
  public void fileParameterNotSet() throws Exception {
    FreeStyleProject project = j.createFreeStyleProject();
    ParametersDefinitionProperty pdp =
        new ParametersDefinitionProperty(new FileParameterDefinition("filename", "description"));
    project.addProperty(pdp);

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

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

    assertFalse("file must not exist", project.getSomeWorkspace().child("filename").exists());
  }
  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();
  }
コード例 #9
0
ファイル: ParametersTest.java プロジェクト: ZouYu99/hudson
  @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"));
  }