-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a803361
commit c358e15
Showing
6 changed files
with
146 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/test/java/org/jenkinsci/infra/tools/HyperLocalPluginManagerInit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.jenkinsci.infra.tools; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.jvnet.hudson.reactor.Reactor; | ||
import org.jvnet.hudson.reactor.ReactorException; | ||
import org.jvnet.hudson.reactor.Task; | ||
import org.jvnet.hudson.reactor.TaskBuilder; | ||
|
||
import hudson.MockJenkins; | ||
import hudson.init.InitMilestone; | ||
import hudson.init.InitStrategy; | ||
import hudson.security.ACL; | ||
import hudson.security.ACLContext; | ||
import jenkins.InitReactorRunner; | ||
import jenkins.model.Jenkins; | ||
|
||
/** | ||
* Process and find all the Pipeline steps definied in Jenkins plugins. | ||
*/ | ||
public class HyperLocalPluginManagerInit { | ||
private static final Logger LOG = Logger.getLogger(HyperLocalPluginManagerInit.class.getName()); | ||
public InitMilestone lastMilestone; | ||
public HyperLocalPluginManager pluginManager; | ||
|
||
public HyperLocalPluginManager initializeHyperLocalPluginManager(String pluginDir) { | ||
try { | ||
pluginManager = new HyperLocalPluginManager(pluginDir, false); | ||
|
||
// Set up mocks | ||
Jenkins.JenkinsHolder mockJenkinsHolder = mock(Jenkins.JenkinsHolder.class); | ||
MockJenkins mJ = new MockJenkins(); | ||
Jenkins mockJenkins = mJ.getMockJenkins(pluginManager); | ||
when(mockJenkinsHolder.getInstance()).thenReturn(mockJenkins); | ||
|
||
java.lang.reflect.Field jenkinsHolderField = Jenkins.class.getDeclaredField("HOLDER"); | ||
jenkinsHolderField.setAccessible(true); | ||
jenkinsHolderField.set(null, mockJenkinsHolder); | ||
|
||
InitStrategy initStrategy = new InitStrategy(); | ||
executeReactor(initStrategy, pluginManager.diagramPlugins(initStrategy)); | ||
|
||
} catch (Exception ex) { | ||
LOG.log(Level.SEVERE, "Plugin Manager failed to initialize", ex); | ||
} | ||
return pluginManager; | ||
} | ||
|
||
/** | ||
* Executes a reactor. | ||
* | ||
* @param is | ||
* If non-null, this can be consulted for ignoring some tasks. Only | ||
* used during the initialization of Jenkins. | ||
*/ | ||
private void executeReactor(final InitStrategy is, TaskBuilder... builders) | ||
throws IOException, InterruptedException, ReactorException { | ||
Reactor reactor = new Reactor(builders) { | ||
/** | ||
* Sets the thread name to the task for better diagnostics. | ||
*/ | ||
@Override | ||
protected void runTask(Task task) throws Exception { | ||
if (is != null && is.skipInitTask(task)) | ||
return; | ||
|
||
String taskName = task.getDisplayName(); | ||
|
||
Thread t = Thread.currentThread(); | ||
String name = t.getName(); | ||
|
||
try (ACLContext context = ACL.as2(ACL.SYSTEM2)) { // full access in the initialization thread | ||
if (taskName != null) { | ||
t.setName(taskName); | ||
} | ||
super.runTask(task); | ||
} finally { | ||
t.setName(name); | ||
} | ||
} | ||
}; | ||
|
||
new InitReactorRunner() { | ||
@Override | ||
protected void onInitMilestoneAttained(InitMilestone milestone) { | ||
lastMilestone = milestone; | ||
} | ||
}.run(reactor); | ||
} | ||
} |
73 changes: 45 additions & 28 deletions
73
src/test/java/org/jenkinsci/infra/tools/HyperLocalPluginManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,68 @@ | ||
package org.jenkinsci.infra.tools; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
|
||
import org.jenkinsci.pipeline_steps_doc_generator.PipelineStepExtractor; | ||
import org.jenkinsci.pipeline_steps_doc_generator.QuasiDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import hudson.PluginWrapper; | ||
|
||
public class HyperLocalPluginManagerTest { | ||
private final static String pluginDir = HyperLocalPluginManagerTest.class.getResource("/git-plugin").getPath(); | ||
private static PipelineStepExtractor pse = new PipelineStepExtractor(); | ||
private static Map<String, Map<String, List<QuasiDescriptor>>> steps; | ||
private static final Logger LOG = Logger.getLogger(PipelineStepExtractor.class.getName()); | ||
private static int count = 0; | ||
private static HyperLocalPluginManagerInit starter = new HyperLocalPluginManagerInit(); | ||
private static HyperLocalPluginManager pluginManager; | ||
private static List<StepDescriptor> steps; | ||
|
||
@BeforeAll | ||
public static void init() { | ||
steps = pse.findSteps(pluginDir); | ||
for (String plugin : steps.keySet()) { | ||
LOG.info("processing " + plugin); | ||
List<QuasiDescriptor> byPlugin = steps.get(plugin).get("Steps"); | ||
// PluginWrapper thePlugin = pse.pluginManager.getPlugin(plugin); | ||
// String displayName = thePlugin == null ? "Jenkins Core" : | ||
// thePlugin.getDisplayName(); | ||
// LOG.info(displayName); | ||
// for (QuasiDescriptor qd : byPlugin) { | ||
// LOG.info(qd.getSymbol()); | ||
// count++; | ||
// } | ||
count += byPlugin.size(); | ||
pluginManager = starter.initializeHyperLocalPluginManager(pluginDir); | ||
steps = pluginManager.getPluginStrategy().findComponents(StepDescriptor.class); | ||
} | ||
|
||
@Test | ||
public void TotalStepsShouldBeFortySeven() { | ||
assertEquals(47, steps.size()); | ||
} | ||
|
||
@Test | ||
public void isGitStepPresent() { | ||
boolean containsGit = false; | ||
for (StepDescriptor step : steps) { | ||
if (step.getFunctionName() == "git") { | ||
containsGit = true; | ||
} | ||
} | ||
assertTrue(containsGit); | ||
} | ||
|
||
@Test | ||
public void isUnarchiveStepAdvanced() { | ||
for (StepDescriptor step : steps) { | ||
if (step.getFunctionName() == "unarchive") { | ||
assertTrue(step.isAdvanced()); | ||
return; | ||
} | ||
} | ||
LOG.info("Total " + count + " steps found in " + steps.size() + " plugins"); | ||
fail("unarchive step not found"); | ||
} | ||
|
||
@Test | ||
public void areTotalStepsFortySeven() { | ||
assertEquals(47, count); | ||
public void checkoutShouldBelongToWorkflowSCMStep() { | ||
for (StepDescriptor step : steps) { | ||
if (step.getFunctionName() == "checkout") { | ||
String pluginName = pluginManager.getPluginNameForDescriptor(step); | ||
assertEquals("workflow-scm-step", pluginName); | ||
return; | ||
} | ||
} | ||
fail("checkout step not found"); | ||
} | ||
|
||
@Test | ||
public void isLastMilestoneCompletedInitialization() { | ||
assertEquals("Completed initialization", pse.lastMilestone.toString()); | ||
public void LastMilestoneShouldBeCompletedInitialization() { | ||
assertEquals("Completed initialization", starter.lastMilestone.toString()); | ||
} | ||
} |
Oops, something went wrong.