From 58221e02d1ea44c0b3523bea9336753b0ab26fd0 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 21 Jun 2024 06:19:12 -0700 Subject: [PATCH] [JENKINS-73282] Run PCT with a Jetty 12 EE 8 test harness when core is Jetty 12 EE 8 (#675) --- .../jenkins/tools/test/hook/Jetty12Hook.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/main/java/org/jenkins/tools/test/hook/Jetty12Hook.java diff --git a/src/main/java/org/jenkins/tools/test/hook/Jetty12Hook.java b/src/main/java/org/jenkins/tools/test/hook/Jetty12Hook.java new file mode 100644 index 00000000..ca08f858 --- /dev/null +++ b/src/main/java/org/jenkins/tools/test/hook/Jetty12Hook.java @@ -0,0 +1,66 @@ +package org.jenkins.tools.test.hook; + +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.util.VersionNumber; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.util.jar.JarFile; +import java.util.jar.JarInputStream; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; +import org.jenkins.tools.test.model.hook.BeforeExecutionContext; +import org.jenkins.tools.test.model.hook.PluginCompatTesterHookBeforeExecution; +import org.kohsuke.MetaInfServices; + +/** + * Ensure that if the core is on Jetty 12, that the test harness is on Jetty 12 as well. + */ +@MetaInfServices(PluginCompatTesterHookBeforeExecution.class) +public class Jetty12Hook extends PropertyVersionHook { + + @Override + public String getProperty() { + return "jenkins-test-harness.version"; + } + + @Override + public String getMinimumVersion() { + return "2230.v4fa_477b_634f4"; + } + + @Override + public boolean check(@NonNull BeforeExecutionContext context) { + VersionNumber winstoneVersion = getWinstoneVersion(context.getConfig().getWar()); + if (winstoneVersion.getDigitAt(0) < 7) { + return false; + } + return super.check(context); + } + + private VersionNumber getWinstoneVersion(File war) { + try (JarFile jarFile = new JarFile(war)) { + ZipEntry zipEntry = jarFile.getEntry("executable/winstone.jar"); + if (zipEntry == null) { + throw new IllegalArgumentException("Failed to find winstone.jar in " + war); + } + try (InputStream is = jarFile.getInputStream(zipEntry); + BufferedInputStream bis = new BufferedInputStream(is); + JarInputStream jis = new JarInputStream(bis)) { + Manifest manifest = jis.getManifest(); + if (manifest == null) { + throw new IllegalArgumentException("Failed to read manifest in " + war); + } + String version = manifest.getMainAttributes().getValue("Implementation-Version"); + if (version == null) { + throw new IllegalArgumentException("Failed to read Winstone version from manifest in " + war); + } + return new VersionNumber(version); + } + } catch (IOException e) { + throw new UncheckedIOException("Failed to read Winstone version in " + war, e); + } + } +}