Skip to content

Commit

Permalink
[FIXED JENKINS-14438] Merge branch 'ISSUE-14438' of github.com:cjo990…
Browse files Browse the repository at this point in the history
…0/run-condition-plugin
  • Loading branch information
imod committed Oct 5, 2013
2 parents 1125b04 + 1918a2b commit a669d82
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 6 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Expand Up @@ -29,8 +29,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<!-- tried 1.409 to get to latest before LTS, but has mvn 3 dependency :-( -->
<version>1.420</version>
<version>1.424</version>
</parent>

<artifactId>run-condition</artifactId>
Expand Down Expand Up @@ -71,6 +70,12 @@
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>conditional-buildstep</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
Expand Up @@ -81,7 +81,7 @@ public boolean isCausedBy(String className) {

/**
* Constructor to build causes the cause class is NOT available at build time.
*
*
* @param causeClassName
* @param displayName
*/
Expand All @@ -92,7 +92,7 @@ private BuildCause(String causeClassName, String displayName) {

/**
* allow enum definition to overwrite
*
*
* @param className
* @return true if this cause is meant by the given className
*/
Expand All @@ -102,7 +102,7 @@ boolean isCausedBy(String className) {

/**
* Constructor to build causes the cause class is available at build time.
*
*
* @param clazz
* cause class
* @param displayName
Expand Down Expand Up @@ -135,7 +135,8 @@ public boolean isExclusiveCause() {
public boolean runPerform(final AbstractBuild<?, ?> build, final BuildListener listener) {
final String name = buildCause == null ? "N/A" : buildCause.displayName;
listener.getLogger().println(Messages.causeCondition_check(name));
final List<Cause> causes = build.getCauses();
// final List<Cause> causes = build.getCauses();
final List<Cause> causes = build.getRootBuild().getCauses();
if (buildCause != null) {
if (isExclusiveCause()) {
return causes.size() == 1 && buildCause.isCausedBy(causes.get(0).getClass().getName());
Expand Down
@@ -0,0 +1,251 @@
/*
* The MIT License
*
* Copyright (C) 2012 by Chris Johnson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkins_ci.plugins.run_condition.core;

import hudson.model.Cause.LegacyCodeCause;
import hudson.model.Cause.RemoteCause;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Cause.UserCause;
import hudson.model.*;
import hudson.matrix.TextAxis;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixRun;
import hudson.matrix.AxisList;
import hudson.tasks.Builder;
import org.jvnet.hudson.test.MockBuilder;
import org.jvnet.hudson.test.Bug;

import hudson.triggers.TimerTrigger.TimerTriggerCause;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.jenkins_ci.plugins.run_condition.RunCondition;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner;
import org.jenkins_ci.plugins.run_condition.BuildStepRunner.Run;

import org.jenkinsci.plugins.conditionalbuildstep.ConditionalBuilder;
//import org.jenkins_ci.plugins.run_condition.core.CauseCondition;

import org.junit.Test;
import org.jvnet.hudson.test.HudsonTestCase;
import java.util.Collections;


public class CauseConditionTest extends HudsonTestCase {
//-------------------------------------------------------
//UserCause deprecated after Jenkins 1.427
@Test
public void testUCAnyUser() throws Exception {

List<Cause> BuildCauses = new ArrayList<Cause>();

// tests with no users defined

RunCondition condition = new CauseCondition("USER_CAUSE", false);

// started by SYSTEM user
BuildCauses.add(new UserCause());
runtest(BuildCauses, condition, true);

// started by user
BuildCauses.clear();
BuildCauses.add(createUserCause("fred"));
runtest(BuildCauses, condition, true);

// started by different causes
BuildCauses.clear();
BuildCauses.add(new LegacyCodeCause());
runtest(BuildCauses, condition, false);


// started by mltiple users including requested
BuildCauses.clear();
BuildCauses.add(createUserCause("tom"));
BuildCauses.add(createUserCause("fred"));
BuildCauses.add(createUserCause("harry"));

runtest(BuildCauses, condition, true);


// started by different causes
BuildCauses.clear();
BuildCauses.add(new LegacyCodeCause());
runtest(BuildCauses, condition, false);

// multiple different causes
// add a second cause
BuildCauses.add(new RemoteCause("dummy_host", "dummynote") );
runtest(BuildCauses, condition, false);


// test with Exclusive set
condition = new CauseCondition("USER_CAUSE", true);

// started by correct user
BuildCauses.clear();
BuildCauses.add(createUserCause("fred"));
runtest(BuildCauses, condition, true);

// started by several users
BuildCauses.clear();
BuildCauses.add(createUserCause("eviloverlord"));
BuildCauses.add(createUserCause("fred"));
runtest(BuildCauses, condition, false);

// started by several causes
BuildCauses.clear();
BuildCauses.add(createUserCause("eviloverlord"));
BuildCauses.add(createUserCause("fred"));
BuildCauses.add(new LegacyCodeCause());
BuildCauses.add(new RemoteCause("dummy_host", "dummynote") );
BuildCauses.add(new TimerTriggerCause());
runtest(BuildCauses, condition, false);
}


@Bug(14438)
@Test
public void testMatrixUpstreamCause() throws Exception {

// setup some Causes
FreeStyleProject upProject = createFreeStyleProject("firstProject");
FreeStyleBuild upBuild = upProject.scheduleBuild2(0).get();

Cause upstreamCause = new UpstreamCause(upBuild);
Cause userCause = createUserCause("testUser");

// test upstream condition
RunCondition condition = new CauseCondition("UPSTREAM_CAUSE", false);
runMatrixTest(upstreamCause, condition, true);
runMatrixTest(userCause, condition, false);

//test User condition
condition = new CauseCondition("USER_CAUSE", false);
runMatrixTest(upstreamCause, condition, false);
runMatrixTest(userCause, condition, true);


}
@Bug(14438)
@Test
public void testMatrixUserCause() throws Exception {

// setup some Causes
FreeStyleProject upProject = createFreeStyleProject("secondProject");
FreeStyleBuild upBuild = upProject.scheduleBuild2(0).get();

Cause upstreamCause = new UpstreamCause(upBuild);
Cause userCause = createUserCause("testUser");

//test User condition
RunCondition condition = new CauseCondition("USER_CAUSE", false);
runMatrixTest(upstreamCause, condition, false);
runMatrixTest(userCause, condition, true);
}


private void runMatrixTest(Cause buildTrigger, RunCondition condition, Boolean builderRuns) throws Exception {
MatrixProject matrixProject = createMatrixProject();

// if the builder should run the result for each subbuild should be unstable
// if the builder is not to run the result for each subbuild should be success.
Result testResult = builderRuns ? Result.UNSTABLE:Result.SUCCESS;

// create conditional build step requirements
List<Builder> builders = Collections.singletonList((Builder)new MockBuilder(Result.UNSTABLE));

BuildStepRunner runner = new Run();

// add conditional build step
matrixProject.getBuildersList().add(new ConditionalBuilder(condition, runner, builders));

MatrixBuild matrixBuild = matrixProject.scheduleBuild2(0, buildTrigger).get();

List<MatrixRun> runs = matrixBuild.getRuns();
assertEquals(4,runs.size());
for (MatrixRun run : runs) {
assertBuildStatus(testResult, run);
}

}

@Override
protected MatrixProject createMatrixProject(String name) throws IOException {
MatrixProject p = super.createMatrixProject(name);

// set up 2x2 matrix
AxisList axes = new AxisList();
axes.add(new TextAxis("db","mysql","oracle"));
axes.add(new TextAxis("direction","north","south"));
p.setAxes(axes);

return p;
}
private void runtest(List<Cause> causes, RunCondition condition, boolean expected) throws Exception {

FreeStyleProject project = createFreeStyleProject();
FreeStyleBuild build;

if (causes.size() > 0) {
build = project.scheduleBuild2(5, causes.remove(0)).get();
} else {
build = project.scheduleBuild2(5).get();
}
if (causes.size() > 0) {
// add other causes
build.getAction(CauseAction.class).getCauses().addAll(causes);
}

System.out.println(build.getDisplayName()+" completed");
List<Cause> buildCauses = build.getCauses();
for (Cause cause2 : buildCauses) {
System.out.println("DESC:" + cause2.getShortDescription());
if (Cause.UserCause.class.isInstance(cause2)) {
UserCause userCause = (UserCause)cause2;
System.out.println("UN:" + userCause.getUserName());
}
}
StreamBuildListener listener = new StreamBuildListener(System.out,Charset.defaultCharset());

boolean testresult = condition.runPerform(build, listener);
assertEquals(expected, testresult);
}

private UserCause createUserCause(String userid) {
Authentication a = new UsernamePasswordAuthenticationToken(userid, userid);

a = hudson.getSecurityRealm().getSecurityComponents().manager.authenticate(a);
SecurityContextHolder.getContext().setAuthentication(a);
UserCause cause = new UserCause();

return cause;
}
}

0 comments on commit a669d82

Please sign in to comment.