Skip to content

Commit

Permalink
Reduce the number of restarts needed to install plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Oct 27, 2016
1 parent d339a18 commit 01991ea
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions src/main/java/org/jenkinsci/test/acceptance/junit/WithPlugins.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Logger;
Expand Down Expand Up @@ -61,14 +65,14 @@
@RuleAnnotation(value=WithPlugins.RuleImpl.class, priority=WithPlugins.PRIORITY)
public @interface WithPlugins {

public static final int PRIORITY = 10;
int PRIORITY = 10;

/**
* See {@link PluginManager.PluginSpec} for the syntax.
*/
String[] value();

public class RuleImpl implements TestRule {
class RuleImpl implements TestRule {

private static final Logger LOGGER = Logger.getLogger(WithPlugins.class.getName());

Expand All @@ -88,15 +92,18 @@ public Statement apply(final Statement base, final Description d) {
@Override
public void evaluate() throws Throwable {
jenkins = injector.getInstance(Jenkins.class);
Set<String> plugins = new TreeSet<>();
boolean restartRequired = installPlugins(d.getAnnotation(WithPlugins.class), plugins);
restartRequired |= installPlugins(d.getTestClass().getAnnotation(WithPlugins.class), plugins);
List<String> plugins = combinePlugins(
d.getAnnotation(WithPlugins.class),
d.getTestClass().getAnnotation(WithPlugins.class)
);
boolean restartRequired = installPlugins(plugins);
LOGGER.info("for " + d + " asked to install " + plugins + "; restartRequired? " + restartRequired);
if (restartRequired) {
assumeTrue("This test requires a restartable Jenkins", jenkins.canRestart());
jenkins.restart();
}
for (String name : plugins) {
name = name.replaceAll("@.*", "");
Plugin installedPlugin = jenkins.getPlugin(name);
VersionNumber installedVersion = installedPlugin.getVersion();
String version = installedVersion.toString();
Expand All @@ -105,40 +112,46 @@ public void evaluate() throws Throwable {
base.evaluate();
}

private List<String> combinePlugins(WithPlugins... wp) {
ArrayList<String> plugins = new ArrayList<>();
for (WithPlugins withPlugins : wp) {
if (withPlugins != null) {
Collections.addAll(plugins, withPlugins.value());
}
}

private boolean installPlugins(WithPlugins wp, Set<String> plugins) {
if (wp == null) return false;
return plugins;
}

private boolean installPlugins(List<String> install) {
PluginManager pm = jenkins.getPluginManager();
boolean restartRequired = false;
for (String c: wp.value()) {
PluginSpec candidate = new PluginSpec(c);
String name = candidate.getName();
plugins.add(name);

switch (pm.installationStatus(c)) {
case NOT_INSTALLED:
restartRequired |= doInstall(pm, c);
break;
case OUTDATED:
if (neverReplaceExistingPlugins) {
throw new AssumptionViolatedException(String.format(
"Test requires %s plugin", c));
} else {
restartRequired |= doInstall(pm, c);
}
break;
default:
// OK

for (Iterator<String> iterator = install.iterator(); iterator.hasNext(); ) {
String spec = iterator.next();
switch (pm.installationStatus(spec)) {
case NOT_INSTALLED:
LOGGER.info(spec + " is up to date");
break;
case UP_TO_DATE:
iterator.remove(); // Already installed
break;
case OUTDATED:
if (neverReplaceExistingPlugins) {
throw new AssumptionViolatedException(String.format(
"Test requires %s plugin", spec
));
}
break;
default:
assert false;
}
}
return restartRequired;
}

@SuppressWarnings("deprecation")
private boolean doInstall(PluginManager pm, String c) {
LOGGER.info("Installing plugins for test: " + install);
String[] installList = install.toArray(new String[install.size()]);
try {
return pm.installPlugins(c);
//noinspection deprecation
return pm.installPlugins(installList);
} catch (UnableToResolveDependencies ex) {
throw new AssumptionViolatedException("Unable to install required plugins", ex);
}
Expand Down

0 comments on commit 01991ea

Please sign in to comment.