xunit: support versions 3+

A new major version (3.0.0) of XUnit plugin has been released in January
2021:

  https://github.com/jenkinsci/xunit-plugin/releases/

I didn't have an issue when I upgraded the plugin from version 2.4.0 to
3.0.0. But I started to have this error at the end of a build and I
guess it appeared only after having modified the configuration of the
Jenkins jobs with JJB:

  ERROR: Build step failed with exception
  java.lang.IllegalArgumentException: The tools section is required.
	at org.jenkinsci.plugins.xunit.XUnitProcessor.<init>(XUnitProcessor.java:139)
	at org.jenkinsci.plugins.xunit.XUnitPublisher.perform(XUnitPublisher.java:204)
	at jenkins.tasks.SimpleBuildStep.perform(SimpleBuildStep.java:123)
	at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:80)
	at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:803)
	at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:752)
	at hudson.model.Build$BuildExecution.post2(Build.java:177)
	at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:697)
	at hudson.model.Run.execute(Run.java:1932)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:429)
  Build step 'Publish xUnit test result report' marked build as failure

When looking at the configuration of the job from the Web UI, the XUnit
plugin was enabled but left "unconfigured": no file to look at.

Apparently, JJB is sending a XML config that is not understood by XUnit
plugin anymore. I guess I didn't have any issue before updating the
Jenkins jobs with JJB because when upgrading XUnit plugin from 2.4.0 to
3.0.0, Jenkins migrated the config to the new format.

From what I see when analysing the generated XML from JJB and the one
from Jenkins after having configured a job manually, a sub-element has
been renamed, from 'types' to 'tools'. I suspect this is caused by this
commit:

  https://github.com/jenkinsci/xunit-plugin/commit/aa47ac57d

(XUnitBuilder class had this line: @XStreamAlias("types"))

So here, I simply picked 'tools' or 'types' for the sub-element and
depending on the plugin's version in both the builder and the publisher
parts. I'm only using the publisher but I guess the problem is the same
with the builder.

Tests have been adapted to verify both versions <3.0.0 and >=3.0.0.

Change-Id: Ie78539d0ae25ddc139ad9982cdffd2e683dd40b2
Signed-off-by: Matthieu Baerts <matthieu.baerts@tessares.net>
This commit is contained in:
Matthieu Baerts 2021-03-17 12:50:08 +01:00
parent 4393f8ba1a
commit 1d6e580ff3
No known key found for this signature in database
GPG Key ID: F6B7824F4269A073
10 changed files with 56 additions and 2 deletions

View File

@ -4694,6 +4694,9 @@ def xunit(registry, xml_parent, data):
:language: yaml
"""
info = registry.get_plugin_info("xunit")
plugin_version = pkg_resources.parse_version(info.get("version", str(sys.maxsize)))
logger = logging.getLogger(__name__)
xunit = XML.SubElement(xml_parent, "org.jenkinsci.plugins.xunit.XUnitBuilder")
xunit.set("plugin", "xunit")
@ -4733,7 +4736,13 @@ def xunit(registry, xml_parent, data):
supported_types.append(configured_type)
# Generate XML for each of the supported framework types
xmltypes = XML.SubElement(xunit, "types")
# Note: versions 3+ are now using the 'tools' sub-element instead of 'types'
if plugin_version < pkg_resources.parse_version("3.0.0"):
types_name = "types"
else:
types_name = "tools"
xmltypes = XML.SubElement(xunit, types_name)
mappings = [
("pattern", "pattern", ""),
("requireupdate", "failIfNotNew", True),

View File

@ -1832,6 +1832,9 @@ def xunit(registry, xml_parent, data):
:language: yaml
"""
info = registry.get_plugin_info("xunit")
plugin_version = pkg_resources.parse_version(info.get("version", str(sys.maxsize)))
logger = logging.getLogger(__name__)
xunit = XML.SubElement(xml_parent, "xunit")
xunit.set("plugin", "xunit")
@ -1871,7 +1874,13 @@ def xunit(registry, xml_parent, data):
supported_types.append(configured_type)
# Generate XML for each of the supported framework types
xmltypes = XML.SubElement(xunit, "types")
# Note: versions 3+ are now using the 'tools' sub-element instead of 'types'
if plugin_version < pkg_resources.parse_version("3.0.0"):
types_name = "types"
else:
types_name = "tools"
xmltypes = XML.SubElement(xunit, types_name)
for supported_type in supported_types:
framework_name = next(iter(supported_type.keys()))
xmlframework = XML.SubElement(xmltypes, types_to_plugin_types[framework_name])

View File

@ -0,0 +1 @@
../../publishers/fixtures/xunit001.plugins_info.yaml

View File

@ -0,0 +1 @@
../../publishers/fixtures/xunit001.plugins_info.yaml

View File

@ -0,0 +1,3 @@
- longName: 'xUnit'
shortName: 'xunit'
version: "2.4.0"

View File

@ -0,0 +1 @@
xunit001.plugins_info.yaml

View File

@ -0,0 +1 @@
xunit001.plugins_info.yaml

View File

@ -0,0 +1,3 @@
- longName: 'xUnit'
shortName: 'xunit'
version: "3.0.0"

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<publishers>
<xunit plugin="xunit">
<tools>
<PHPUnitJunitHudsonTestType>
<pattern>junit.log</pattern>
<failIfNotNew>true</failIfNotNew>
<deleteOutputFiles>true</deleteOutputFiles>
<skipNoTestFiles>false</skipNoTestFiles>
<stopProcessingIfError>true</stopProcessingIfError>
</PHPUnitJunitHudsonTestType>
</tools>
<thresholds/>
<thresholdMode>1</thresholdMode>
<extraConfiguration>
<testTimeMargin>3000</testTimeMargin>
</extraConfiguration>
</xunit>
</publishers>
</project>

View File

@ -0,0 +1,5 @@
publishers:
- xunit:
types:
- phpunit:
pattern: "junit.log"