From 3196c136d03e61743583fdc466b8fb2641cacf25 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sat, 9 Dec 2023 21:17:42 -0700 Subject: [PATCH] Fix `AxisTest#submitInvalidAxisValue` for PCT / plugin BOM https://github.com/jenkinsci/bom/issues/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. --- src/test/java/hudson/matrix/AxisTest.java | 30 +++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/test/java/hudson/matrix/AxisTest.java b/src/test/java/hudson/matrix/AxisTest.java index 39340c00..a138f33e 100644 --- a/src/test/java/hudson/matrix/AxisTest.java +++ b/src/test/java/hudson/matrix/AxisTest.java @@ -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; @@ -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 { @@ -85,17 +87,30 @@ public void submitInvalidAxisName() throws Exception { assertFailedWith(expectedMsg, withName("a=b", "Label expression")); } + private void setName(HtmlForm form, String value) { + List 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)); } @@ -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)); } }