Skip to content

Commit

Permalink
Fix AxisTest#submitInvalidAxisValue for PCT / plugin BOM (#184)
Browse files Browse the repository at this point in the history
jenkinsci/bom#615 describes the problem in
more detail.

When the git plugin is loaded along with the matrix project plugin,
there are multiple input fields on the job configuration page with the
name `_.name`.  The previous code waited until there was at least one
input field with the name `_.name`, but instead it needs to wait for
the arrival of an additional input field with the name `_.name`.

Adds a new `setName` method that sets the value of the `_.name` field
that was created by this plugin.  Previously, it would set the first
`_.name` field.  Setting the first `_.name` field works when running
the matrix plugin tests without the git plugin loaded, but fails when
the git plugin is loaded.

The combination of waiting for the correct `_.name` field to appear and
setting the value on the correct `_.name` field allows oone of the four
tests to pass when the git plugin is loaded.  The remaining tests need
more investigation to pass when the git plugin is loaded.

Also removes a nearly silent skip of the tests when the input does not
appear within the timeout.

Also increases the time between retries so that my fast computer needs
only two or three retries.  I confirmed on a much slower computer
(Intel Core i5-2410M CPU @ 2.30GHz) that even on that slow computer,
it found the necessary field in 8 tries or less.  The upper bound of 18
tries should be more than enough for all the test environments.

The retry period should not be increased beyond the current ~300ms
because one of the tests is using a form that does not include `_.name`.
That test falls through to the end of the retry period.
  • Loading branch information
MarkEWaite authored Dec 19, 2023
1 parent 7ebe657 commit 0389fc6
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/test/java/hudson/matrix/AxisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import hudson.model.JDK;
import hudson.util.VersionNumber;
import java.util.List;
import jenkins.model.Jenkins;

import org.hamcrest.collection.IsEmptyCollection;
Expand All @@ -41,6 +42,7 @@
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlInput;
import org.htmlunit.html.HtmlPage;

public class AxisTest {
Expand Down Expand Up @@ -85,17 +87,30 @@ public void submitInvalidAxisName() throws Exception {
assertFailedWith(expectedMsg, withName("a=b", "Label expression"));
}

private void setName(HtmlForm form, String value) {
List<HtmlInput> inputs = form.getInputsByName("_.name");
int fieldCount = 0;
for (HtmlInput input : inputs) {
// Set the value on the `_.name` field from the "Add Axis" button
if (input.toString().contains("hudson.matrix.")) {
input.setValue(value);
fieldCount++;
}
}
assertThat(fieldCount, equalTo(1));
}

@Test
public void submitInvalidAxisValue() throws Exception {
wc.getOptions().setThrowExceptionOnFailingStatusCode(false);

HtmlForm form = addAxis("User-defined Axis");
form.getInputByName("_.name").setValue("a_name");
setName(form, "a_name");
form.getInputByName("_.valueString").setValue("a,b");
assertFailedWith("Matrix axis value 'a,b' is invalid: ‘,’ is an unsafe character", j.submit(form));

form = addAxis("Label expression");
form.getInputByName("_.name").setValue("a_name");
setName(form, "a_name");
form.getElementsByAttribute("textarea", "name", "values").get(0).setTextContent("a,b");
assertFailedWith("Matrix axis value 'a,b' is invalid: ‘,’ is an unsafe character", j.submit(form));
}
Expand Down Expand Up @@ -156,13 +171,14 @@ private HtmlForm addAxis(String axis) throws Exception {

private void waitForInput(HtmlForm form) throws InterruptedException {
int numberInputs = form.getInputsByName("_.name").size();
int tries = 30;
while (tries > 0 && numberInputs == 0) {
int initialInputs = numberInputs;
int tries = 18; // 18 * 17 == 306
while (tries > 0 && numberInputs == initialInputs) {
tries--;
Thread.sleep(10);
Thread.sleep(17);
numberInputs = form.getInputsByName("_.name").size();
}

assumeTrue("Input should have appeared (TODO sometimes does not)", numberInputs != 0);
// One test seems OK with not finding '_.name' field on the page
// assertThat("Additional '_.name' field not found on page", numberInputs, greaterThan(initialInputs));
}
}

0 comments on commit 0389fc6

Please sign in to comment.