Skip to content

Commit

Permalink
Merge pull request #2 from ikedam/feature/JENKINS-27424_TestsForCurre…
Browse files Browse the repository at this point in the history
…ntJob

[JENKINS-27424] Added tests for currentJob
  • Loading branch information
ikedam committed Mar 14, 2015
2 parents 9b780e3 + ef071a8 commit 3abb680
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
8 changes: 0 additions & 8 deletions pom.xml
Expand Up @@ -36,14 +36,6 @@
</license>
</licenses>

<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>[2.3,]</version>
</dependency>
</dependencies>

<!-- get every artifact through repo.jenkins-ci.org, which proxies all the artifacts that we need -->
<repositories>
<repository>
Expand Down
Expand Up @@ -6,18 +6,27 @@
<li>When exception occurred or Label Expression is not parsed correctly, builds are canceled.</li>
</ul>
<br>
<p>Following variables are binded, but there may be cases that some type of parameters are not set, or set to wrong value.
<p>Following variables are bound:</p>
<dl>
<dt>currentJob</dt>
<dd>
The job to be built.
For multi-configuration projects, an instance of MatrixProject is passed for a parent build
and an instance of MatrixConfiguration is passed for a child build.
</dd>
</dl>
<p>Additionally, following variables are bound, but there may be cases that some type of parameters are not set, or set to wrong value.
This is for a build is not started yet when this Groovy script is evaluated.</p>
<ul>
<li>Parameters specified in "This build is parameterized"</li>
<li>Axes defined in a matrix project.</li>
<li>Axes defined in a multi-configuration project.</li>
<li>Other variables that plugins create.</li>
</ul>
<br>
<p>Here is an example:</p>
<code style="white-space: pre-wrap;">
Example1-----------------------------------------------------------------------------
// parameters defined in "This build is parameterized" are binded,
// parameters defined in "This build is parameterized" are bound,
// and you can use them just as variables.
if(is_test == "true")
{
Expand All @@ -29,8 +38,8 @@
return null;

Example2-----------------------------------------------------------------------------
// Values defined with axe with a matrix project are also binded.
// But that value is not defined in a root matrix build,
// Values defined with axes with a multi-configuration project are also bound.
// But that value is not defined in a parent build,
// so access with binding.getVariables().get(VARNAME) .
// (accessing VARNAME directly results exceptions)
switch(binding.getVariables().get("target"))
Expand All @@ -44,5 +53,9 @@
// run on Linux.
return "Linux";
}

Example3-----------------------------------------------------------------------------
// Decides the node to run on for its job name.
["win", "linux"].find(currentJob.name.contains(it))
</code>
</div>
Expand Up @@ -6,7 +6,12 @@
<li>例外が発生したり、文字列がラベル式として評価できない場合、ビルドをキャンセルします。</li>
</ul>
<br>
<p>以下の変数をバインドし、Groovyスクリプトからアクセスできるようにします。ただし、特定の変数が設定されなかったり、おかしな値が設定される場合があるかもしれません。
<p>以下の変数が定義されます。</p>
<dl>
<dt>currentJob</dt>
<dd>ビルド対象のジョブ。マトリクス構成プロジェクトの場合、親ビルドには MatrixProject、 子ビルドには MatrixConfiguration のインスタンスが渡されるので注意してください。</dd>
</dl>
<p>また、以下の変数をバインドし、Groovyスクリプトからアクセスできるようにします。ただし、特定の変数が設定されなかったり、おかしな値が設定される場合があるかもしれません。
これはGroovyスクリプトの実行時点で、まだビルドが実際には始まっていないため各種の値の取得処理に制限があるためです。</p>
<ul>
<li>「ビルドのパラメータ化」で指定したパラメータ</li>
Expand Down Expand Up @@ -43,5 +48,9 @@
// run on Linux.
return "Linux";
}

Example3-----------------------------------------------------------------------------
// ジョブの名称から実行するノードを決定します。
["win", "linux"].find(currentJob.name.contains(it))
</code>
</div>
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.ExecutionException;

import jenkins.model.Jenkins;

import hudson.Functions;
import hudson.Util;
import hudson.PluginWrapper;
Expand Down Expand Up @@ -147,6 +146,13 @@ public FreeStyleProject createFreeStyleProject() throws IOException
return super.createFreeStyleProject();
}

@Override
public FreeStyleProject createFreeStyleProject(String name) throws IOException
{
// createFreeStyleProject is protected with Jenkins < 1.479
return super.createFreeStyleProject(name);
}

@Override
public MatrixProject createMatrixProject() throws IOException
{
Expand Down
Expand Up @@ -33,7 +33,7 @@
import java.util.concurrent.TimeoutException;

import jenkins.model.Jenkins;

import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixRun;
import hudson.matrix.LabelAxis;
Expand Down Expand Up @@ -545,4 +545,61 @@ public void testConfiguration() throws Exception
assertNotNull(prop);
assertEquals(groovyScript, prop.getGroovyScript());
}

@Test
public void testCurrentJobWithFreeStyleProject() throws Exception {
// This allows a build decides which slave to run on with its project name.
// not applicable for slave3.
String script = "['test1', 'test2'].find { currentJob.name.contains(it) }";

// project names and expected nodes map
Map<String, Node> projectAndNodeMap = new HashMap<String, Node>();
projectAndNodeMap.put("project_test1", slave1);
projectAndNodeMap.put("project_test2", slave2);
projectAndNodeMap.put("project_test3", j.jenkins);


for(Map.Entry<String, Node> entry: projectAndNodeMap.entrySet())
{
FreeStyleProject projectToTest = j.createFreeStyleProject(entry.getKey());
projectToTest.addProperty(new GroovyLabelAssignmentProperty(script));
projectToTest.setAssignedLabel(LabelExpression.parseExpression("master")); // overridden with GroovyLabelAssignmentProperty

for(int i = 0; i < BUILD_REPEAT; ++i)
{
FreeStyleBuild build = projectToTest.scheduleBuild2(0).get();
assertBuiltOn(entry.getValue(), build);
}
}
}

@Test
public void testCurrentJobWithMatrixProject() throws Exception {
// As MatrixRun is passed as currentJob for matrix childs and the name of MatrixRun contains the axis value,
// this allows child builds decide which slave to run on with threir axis value.
// not applicable for slave3.
String script = "['test1', 'test2'].find { currentJob.name.contains(it) }";

MatrixProject p = j.createMatrixProject();
p.setAxes(new AxisList(new Axis("axisParam", "run-on-test1", "run-on-test2", "run-on-test3")));
p.addProperty(new GroovyLabelAssignmentProperty(script));
p.setAssignedLabel(LabelExpression.parseExpression("master")); // overridden with GroovyLabelAssignmentProperty

// axis names and expected nodes map
Map<String, Node> axisValueAndNodeMap = new HashMap<String, Node>();
axisValueAndNodeMap.put("run-on-test1", slave1);
axisValueAndNodeMap.put("run-on-test2", slave2);
axisValueAndNodeMap.put("run-on-test3", j.jenkins);

for(int i = 0; i < BUILD_REPEAT; ++i)
{
MatrixBuild build = p.scheduleBuild2(0).get();

for(MatrixRun child: build.getRuns())
{
String axisValue = child.getProject().getCombination().get("axisParam");
assertBuiltOn(axisValueAndNodeMap.get(axisValue), child);
}
}
}
}

0 comments on commit 3abb680

Please sign in to comment.