Skip to content

Commit

Permalink
Refine test class
Browse files Browse the repository at this point in the history
  • Loading branch information
vihaanthora committed Jul 13, 2022
1 parent a803361 commit c358e15
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 258 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>mockito-core</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public String getPluginNameForDescriptor(Descriptor<?> d) {
try {
uberPlusClassLoader.findClass(className);
} catch (ClassNotFoundException e) {
LOG.log(Level.WARNING, "Class not found for " + d.getId());
LOG.log(Level.FINER, "Class not found for " + d.getId());
}
if (pluginName == null) {
LOG.info("No plugin found, assuming core: " + className);
Expand Down
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);
}
}
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());
}
}

0 comments on commit c358e15

Please sign in to comment.