Skip to content

Commit

Permalink
Added unit test case
Browse files Browse the repository at this point in the history
  • Loading branch information
hrishin committed Jun 5, 2018
1 parent c63fe3e commit 8adc666
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 34 deletions.
28 changes: 17 additions & 11 deletions src/main/java/hudson/plugins/buildblocker/BuildBlockerProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public BuildBlockerProperty(boolean useBuildBlocker, String blockLevel, String s
this.blockingJobs = blockingJobs;
}

public Object readResolve() {
if(blockLevel == null) {
blockLevel = BlockLevel.GLOBAL;
}

if(scanQueueFor == null) {
scanQueueFor = QueueScanScope.DISABLED;
}

return this;
}

@Override
public String toString() {
return "BuildBlockerProperty{" +
Expand All @@ -95,25 +107,19 @@ public String toString() {
'}';
}

public static BuildBlockerProperty getDefaultProperties() {
public static BuildBlockerProperty getDefaultProperties() throws Descriptor.FormException {
DescriptorImpl descriptor = Jenkins.getInstance().getDescriptorByType(DescriptorImpl.class);
try {
return (BuildBlockerProperty) descriptor.newInstance(null, null);
} catch (Descriptor.FormException e) {
LOG.log(Level.SEVERE, "failed to retrieve build blocking properties", e);
}

return null;
}
return (BuildBlockerProperty) descriptor.newInstance(null, null);
}

/**
* Descriptor
*/
@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {
private boolean useBuildBlocker;
private BlockLevel blockLevel;
private QueueScanScope scanQueueFor;
private BlockLevel blockLevel = BlockLevel.GLOBAL;
private QueueScanScope scanQueueFor = QueueScanScope.DISABLED;
private String blockingJobs;

public DescriptorImpl() {
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/hudson/plugins/buildblocker/PipelineJobListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hudson.plugins.buildblocker;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.listeners.ItemListener;
Expand All @@ -21,20 +22,27 @@ public class PipelineJobListener extends ItemListener {
@Override
public void onCreated(Item item) {
super.onCreated(item);

if(isNotAJob(item)) {
LOG.log(Level.INFO, "Skipping to add build blocking property, as received item is not a job");
return;
}

Job job = (Job)item;
if(job.getProperty(BuildBlockerProperty.class) == null) {
BuildBlockerProperty buildBlockerProperty = BuildBlockerProperty.getDefaultProperties();
if(buildBlockerProperty != null) {
try {
BuildBlockerProperty buildBlockerProperty = BuildBlockerProperty.getDefaultProperties();
LOG.log(Level.INFO, "Adding blocking properties "+buildBlockerProperty+" to job "+ job.getName());
try {
job.addProperty(buildBlockerProperty);
} catch (IOException e) {
LOG.log(Level.SEVERE, "Exception occurred while adding blocking properties "+buildBlockerProperty+" to job "+ job);
}
} else {
LOG.log(Level.INFO, "Not adding blocking properties "+buildBlockerProperty+" to job "+ job.getName());
job.addProperty(buildBlockerProperty);
} catch (IOException e) {
LOG.log(Level.SEVERE, "Exception occurred while adding blocking properties to job "+ job);
} catch (Descriptor.FormException e) {
LOG.log(Level.SEVERE, "Exception occurred while adding blocking properties to job "+ job);
}
}
}

private boolean isNotAJob(Item item) {
return !(item instanceof Job);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@

package hudson.plugins.buildblocker;

import hudson.model.Action;
import hudson.model.Computer;
import hudson.model.Executor;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.Label;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.*;
import hudson.model.labels.LabelAtom;
import hudson.model.queue.CauseOfBlockage;
import hudson.slaves.DumbSlave;
Expand Down Expand Up @@ -97,7 +89,7 @@ public void testCanRun() throws Exception {
.setBlockOnGlobalLevel()
.createBuildBlockerProperty();

project.addProperty(property);
updateJobProperty(project, property);

causeOfBlockage = dispatcher.canRun(item);
assertNotNull(causeOfBlockage);
Expand All @@ -116,7 +108,7 @@ public void testMultipleExecutors() throws Exception {
theJob1.getBuildersList().add(new Shell("sleep 1; exit 0"));
assertTrue(theJob1.getBuilds().isEmpty());

// Job2 returns immediatly but can't run while Job1 is running.
// Job2 returns immediately but can't run while Job1 is running.
FreeStyleProject theJob2 = createFreeStyleProject("MultipleExecutor_Job2");
{
BuildBlockerProperty theProperty = new BuildBlockerPropertyBuilder()
Expand All @@ -125,7 +117,7 @@ public void testMultipleExecutors() throws Exception {
.setBlockOnGlobalLevel()
.setScanBuildableQueueItemStates()
.createBuildBlockerProperty();
theJob2.addProperty(theProperty);
updateJobProperty(theJob2, theProperty);
}
assertTrue(theJob1.getBuilds().isEmpty());

Expand All @@ -150,6 +142,15 @@ public void testMultipleExecutors() throws Exception {
theJob1.delete();
}

private void updateJobProperty(FreeStyleProject job, BuildBlockerProperty property) {
try {
job.removeProperty(BuildBlockerProperty.class);
job.addProperty(property);
} catch (IOException e) {

}
}

public void testSelfExcludingJobs() throws Exception {

BuildBlockerProperty theProperty = new BuildBlockerPropertyBuilder()
Expand All @@ -160,11 +161,11 @@ public void testSelfExcludingJobs() throws Exception {
.createBuildBlockerProperty();

FreeStyleProject theJob1 = createFreeStyleProject("SelfExcluding_Job1");
theJob1.addProperty(theProperty);
updateJobProperty(theJob1,theProperty);
assertTrue(theJob1.getBuilds().isEmpty());

FreeStyleProject theJob2 = createFreeStyleProject("SelfExcluding_Job2");
theJob2.addProperty(theProperty);
updateJobProperty(theJob2,theProperty);
assertTrue(theJob2.getBuilds().isEmpty());

// allow executing two simultanious jobs
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/hudson/plugins/buildblocker/JenkinsRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hudson.plugins.buildblocker;

import hudson.model.Hudson;
import org.apache.commons.io.FileUtils;
import org.junit.internal.AssumptionViolatedException;
import org.jvnet.hudson.test.JenkinsRecipe;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;

/**
* Jenkins rule to inject the plugin config file
*/
public class JenkinsRule extends org.jvnet.hudson.test.JenkinsRule {

@Override
protected Hudson newHudson() throws Exception {
ServletContext webServer = createWebServer();
File home = homeLoader.allocate();
setBlockPluginConfigFile(home);
for (JenkinsRecipe.Runner r : recipes)
r.decorateHome(this,home);
try {
return new Hudson(home, webServer, getPluginManager());
} catch (InterruptedException x) {
throw new AssumptionViolatedException("Jenkins startup interrupted", x);
}
}

private void setBlockPluginConfigFile(File home) throws IOException {
File sourceFile = new File(getClass().getResource("/hudson.plugins.buildblocker.BuildBlockerProperty.xml").getFile());
FileUtils.copyFileToDirectory(sourceFile, home);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hudson.plugins.buildblocker;

import hudson.model.FreeStyleProject;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class PipelineJobListenerTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Test
public void should_add_blocking_properties_from_config_file() throws IOException {
FreeStyleProject job1 = jenkins.createFreeStyleProject("testJob");

BuildBlockerProperty blockingJobProperty = (BuildBlockerProperty) job1.getProperty(BuildBlockerProperty.class);


assertNotNull(blockingJobProperty);
assertEquals(blockingJobProperty.getBlockingJobs(), ".*");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson.plugins.buildblocker.BuildBlockerProperty_-DescriptorImpl plugin="[email protected]">
<useBuildBlocker>true</useBuildBlocker>
<blockLevel>GLOBAL</blockLevel>
<scanQueueFor>DISABLED</scanQueueFor>
<blockingJobs>.*</blockingJobs>
</hudson.plugins.buildblocker.BuildBlockerProperty_-DescriptorImpl>

0 comments on commit 8adc666

Please sign in to comment.