Skip to content

Commit

Permalink
Merge pull request #12 from dawidmalina/multi
Browse files Browse the repository at this point in the history
JENKINS-36697 Must show "Run Only on Parent" for
  • Loading branch information
dawidmalina committed Jul 30, 2016
2 parents 3dae55b + 799792f commit a872dd6
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 8 deletions.
17 changes: 15 additions & 2 deletions pom.xml
Expand Up @@ -6,7 +6,7 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<!-- Which version of Jenkins is this plugin built against? -->
<version>1.565</version>
<version>1.642.4</version>
</parent>

<groupId>com.lookout.jenkins</groupId>
Expand Down Expand Up @@ -79,7 +79,20 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.4</version>
<version>1.6</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.mjdetullio.jenkins.plugins</groupId>
<artifactId>multi-branch-project-plugin</artifactId>
<version>0.5.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ivy</artifactId>
<version>1.26</version>
<optional>true</optional>
</dependency>
<!-- Test Dependencies -->
<dependency>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/lookout/jenkins/EnvironmentScript.java
Expand Up @@ -35,6 +35,8 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import com.github.mjdetullio.jenkins.plugins.multibranch.MatrixMultiBranchProject;

import com.lookout.jenkins.commands.Commands;
import com.lookout.jenkins.commands.PowerShell;
import com.lookout.jenkins.commands.Shebangs;
Expand Down Expand Up @@ -228,7 +230,7 @@ public String[] buildCommandLine(FilePath scriptFile) {
}

/**
* Create an aggregator that will calculate the environment once iff onlyRunOnParent is true.
* Create an aggregator that will calculate the environment once if onlyRunOnParent is true.
*
* The aggregator we return is called on the parent job for matrix jobs. In it we generate the environment once and
* persist it in an Action (of type {@link PersistedEnvironment}) if the job has onlyRunOnParent enabled. The
Expand Down Expand Up @@ -287,7 +289,11 @@ public boolean isApplicable(AbstractProject<?, ?> project) {
}

public boolean isMatrix(StaplerRequest request) {
return (request.findAncestorObject(AbstractProject.class) instanceof MatrixProject);
if (request.findAncestorObject(MatrixMultiBranchProject.class) != null ||
request.findAncestorObject(MatrixProject.class) != null) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
}
}
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<j:choose>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/index.jelly
@@ -1,3 +1,4 @@
<?jelly escape-by-default='true'?>
<!--
This view is used to render the installed plugins page.
-->
Expand Down
@@ -0,0 +1,105 @@
package com.lookout.jenkins;

import static org.junit.Assert.*;

import java.io.File;
import java.nio.charset.Charset;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.google.common.io.Files;

import hudson.FilePath;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
import hudson.matrix.DefaultMatrixExecutionStrategyImpl;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;

public class EnvironmentScriptMultiMatrixTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

class MultiMatrixTestJob {
public MatrixProject project;
public CountBuilder countBuilder;
public MatrixBuild build;
public TaskListener listener;

public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
listener = new StreamTaskListener(System.err, Charset.defaultCharset());
project = jenkins.createMatrixProject();

// This forces it to run the builds sequentially, to prevent any
// race conditions when concurrently updating the 'counter' file.
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));

project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
project.getBuildWrappersList().add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
countBuilder = new CountBuilder();
project.getBuildersList().add(countBuilder);
build = jenkins.buildAndAssertSuccess(project);
jenkins.waitUntilNoActivity();
}
}

final static String SCRIPT_COUNTER =
"file='%s/counter'\n"
+ "if [ -f $file ]; then\n"
+ " let i=$(cat $file)+1\n"
+ "else\n"
+ " i=1\n"
+ "fi\n"
+ "echo 1 >was_run\n"
+ "echo $i >$file\n"
+ "echo seen=yes";

// Generate a random directory that we pass to the shell script.
File tempDir = Files.createTempDir();
String script = String.format(SCRIPT_COUNTER, tempDir.getPath());
String scriptType = "unixScript";
boolean hideGeneratedValue = Boolean.TRUE;

@Test
public void testWithParentOnly() throws Exception {
MultiMatrixTestJob job = new MultiMatrixTestJob(script, true);
buildAndAssert(job);

// We ensure that this was only run once (on the parent)
assertEquals("1", new FilePath(tempDir).child("counter").readToString().trim());

// Then make sure that it was in fact in the parent's WS that we ran.
assertTrue(job.build.getWorkspace().child("was_run").exists());
for (MatrixRun run : job.build.getRuns())
assertFalse(run.getWorkspace().child("was_run").exists());
}

@Test
public void testWithEachChild() throws Exception {
MultiMatrixTestJob job = new MultiMatrixTestJob(script, false);

// We ensure that this was only run twice - once for each axis combination - but not on the parent.
assertEquals("2", new FilePath(tempDir).child("counter").readToString().trim());

// Then make sure that it was in fact in the combination jobs' workspace.
assertFalse(job.build.getWorkspace().child("was_run").exists());
for (MatrixRun run : job.build.getRuns())
assertTrue(run.getWorkspace().child("was_run").exists());
}

private void buildAndAssert(MultiMatrixTestJob job) throws Exception {
assertEquals(Result.SUCCESS, job.build.getResult());

// Make sure that the environment variables set in the script are properly propagated.
assertEquals("yes", job.build.getEnvironment(job.listener).get("seen"));
// Make sure that the builder was executed twice, once for each axis value.
assertEquals(2, job.countBuilder.getCount());
}
}
4 changes: 0 additions & 4 deletions src/test/java/com/lookout/jenkins/EnvironmentScriptTest.java
Expand Up @@ -3,8 +3,6 @@
import static org.junit.Assert.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import hudson.EnvVars;
Expand All @@ -19,8 +17,6 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SingleFileSCM;

import com.kenai.jffi.Array;

public class EnvironmentScriptTest {

@Rule
Expand Down

0 comments on commit a872dd6

Please sign in to comment.