Skip to content

Commit

Permalink
Merge pull request #292 from jglick/RealJenkinsRule-no-GroovyInitScript
Browse files Browse the repository at this point in the history
Using a plugin to implement RealJenkinsRuleInit rather than a Groovy init script
  • Loading branch information
jglick authored Mar 29, 2021
2 parents ba84362 + 51b0350 commit aea1e97
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 14 deletions.
63 changes: 63 additions & 0 deletions RealJenkinsRuleInit/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.17</version>
<relativePath />
</parent>
<groupId>io.jenkins.plugins</groupId>
<artifactId>RealJenkinsRuleInit</artifactId>
<version>0-SNAPSHOT</version>
<packaging>hpi</packaging>
<properties>
<jenkins.version>2.204</jenkins.version>
<java.level>8</java.level>
<maven-hpi-plugin.disabledTestInjection>true</maven-hpi-plugin.disabledTestInjection>
<spotbugs.skip>true</spotbugs.skip>
</properties>
<name>RealJenkinsRule initialization wrapper</name>
<build>
<plugins>
<plugin> <!-- https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-installed</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
<outputDirectory>${project.basedir}/../src/main/resources/org/jvnet/hudson/test</outputDirectory>
<destFileName>RealJenkinsRuleInit.jpi</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@
* THE SOFTWARE.
*/

import jenkins.model.Jenkins
URL[] urls = [new URL(System.getProperty('RealJenkinsRule.location'))]
new URLClassLoader(urls, ClassLoader.systemClassLoader.parent).loadClass('org.jvnet.hudson.test.RealJenkinsRule$Init2').getMethod('run', Object).invoke(null, Jenkins.instance)
package org.jvnet.hudson.test;

import hudson.Plugin;
import java.net.URL;
import java.net.URLClassLoader;
import jenkins.model.Jenkins;

public class RealJenkinsRuleInit extends Plugin {

@SuppressWarnings("deprecation") // @Initializer just gets run too late, even with before = InitMilestone.PLUGINS_PREPARED
public RealJenkinsRuleInit() {}

@Override
public void start() throws Exception {
new URLClassLoader(new URL[] {new URL(System.getProperty("RealJenkinsRule.location"))}, ClassLoader.getSystemClassLoader().getParent()).
loadClass("org.jvnet.hudson.test.RealJenkinsRule$Init2").
getMethod("run", Object.class).
invoke(null, Jenkins.get());
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-war</artifactId>
<version>2.138.4</version>
<version>2.204</version>
<type>executable-war</type>
<exclusions>
<exclusion>
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/jvnet/hudson/test/RealJenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
package org.jvnet.hudson.test;

import hudson.ExtensionList;
import hudson.model.DownloadService;
import hudson.model.UnprotectedRootAction;
import hudson.model.UpdateSite;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.csrf.CrumbExclusion;
Expand Down Expand Up @@ -193,12 +191,10 @@ public RealJenkinsRule withTimeout(int timeout) {
System.out.println("=== Starting " + description);
try {
home = tmp.allocate();
File initGroovyD = new File(home, "init.groovy.d");
initGroovyD.mkdir();
FileUtils.copyURLToFile(RealJenkinsRule.class.getResource("RealJenkinsRuleInit.groovy"), new File(initGroovyD, "RealJenkinsRuleInit.groovy"));
port = new Random().nextInt(16384) + 49152; // https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic,_private_or_ephemeral_ports
File plugins = new File(home, "plugins");
plugins.mkdir();
FileUtils.copyURLToFile(RealJenkinsRule.class.getResource("RealJenkinsRuleInit.jpi"), new File(plugins, "RealJenkinsRuleInit.jpi"));
// Adapted from UnitTestSupportingPluginManager & JenkinsRule.recipeLoadCurrentPlugin:
Set<String> snapshotPlugins = new TreeSet<>();
Enumeration<URL> indexJellies = RealJenkinsRule.class.getClassLoader().getResources("index.jelly");
Expand Down Expand Up @@ -471,7 +467,8 @@ private Init2() {}
public static final class Endpoint implements UnprotectedRootAction {
@SuppressWarnings("deprecation")
public static void register() throws Exception {
Jenkins.get().getActions().add(new Endpoint());
Jenkins j = Jenkins.get();
j.getActions().add(new Endpoint());
CrumbExclusion.all().add(new CrumbExclusion() {
@Override public boolean process(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request.getPathInfo().startsWith("/RealJenkinsRule/")) {
Expand All @@ -481,9 +478,7 @@ public static void register() throws Exception {
return false;
}
});
Jenkins.get().setNoUsageStatistics(true);
DownloadService.neverUpdate = true;
UpdateSite.neverUpdate = true;
JenkinsRule._configureUpdateCenter(j);
System.err.println("RealJenkinsRule ready");
Timer.get().scheduleAtFixedRate(JenkinsRule::dumpThreads, 2, 2, TimeUnit.MINUTES);
}
Expand Down Expand Up @@ -532,7 +527,7 @@ public static final class CustomJenkinsRule extends JenkinsRule implements AutoC
public CustomJenkinsRule() throws Exception {
this.jenkins = Jenkins.get();
localPort = Integer.getInteger("RealJenkinsRule.port");
// Stuff picked out of before(), configureUpdateCenter():
jenkins.setNoUsageStatistics(true); // cannot use JenkinsRule._configureJenkinsForTest earlier because it tries to save config before loaded
JenkinsLocationConfiguration.get().setUrl(getURL().toString());
testDescription = Description.createSuiteDescription(System.getProperty("RealJenkinsRule.description"));
env = new TestEnvironment(this.testDescription);
Expand Down
Binary file not shown.

0 comments on commit aea1e97

Please sign in to comment.