Skip to content

Commit

Permalink
[JENKINS-36623] Fix Tests for Jenkins 1.651.3 (#95)
Browse files Browse the repository at this point in the history
* [JENKINS-36623] Fix SelfPromotionInheritanceTest#testPromotionEnvironmentShouldIncludeTargetParameters to work after SECURITY-170

* [JENKINS-36623] Upgrade PromotionProcessTest to work against jenkins 1.575+

* [JENKINS-36623] Upgrade KeepBuildForeverActionTest work against jenkins 1.575+

* [JENKINS-36623] Upgrade SelfPromotionInheritanceTest to work against Jenkins 1.575+

* [JENKINS-36623] Code style conventions

* [JENKINS-36623] Move ItemListener invocation to an utility method

* [JENKINS-36623] Add TODO and mention to JENKINS-34831

* [JENKINS-36623] Code Style

* [JENKINS-36623] Move ItemListenerhelper to test folder

* [JENKINS-36623] Added TODO note
  • Loading branch information
Raúl Arabaolaza Barquin authored and oleg-nenashev committed Aug 4, 2016
1 parent 4450b50 commit f7589c3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Expand Up @@ -21,6 +21,8 @@
import java.util.Arrays;
import java.util.List;

import static hudson.plugins.promoted_builds.util.ItemListenerHelper.fireItemListeners;

public class KeepBuildForeverActionTest extends HudsonTestCase {

public void testCanMarkBuildKeepForever() throws Exception {
Expand All @@ -32,6 +34,9 @@ public void testCanMarkBuildKeepForever() throws Exception {
PromotionProcess promotionJob = createDownstreamSuccessPromotion(upJob, downJob);
promotionJob.getBuildSteps().add(new KeepBuildForeverAction());

// fire ItemListeners, this includes ArtifactArchiver,Migrator to make this test compatible with jenkins 1.575+
fireItemListeners();

FreeStyleBuild upBuild = assertBuildStatusSuccess(upJob.scheduleBuild2(0).get());
assertFalse(upBuild.isKeepLog());

Expand All @@ -50,6 +55,9 @@ public void testDoesNotMarkBuildIfPromotionNotGoodEnough() throws Exception {
promotionJob.getBuildSteps().add(new FixedResultBuilder(Result.FAILURE));
promotionJob.getBuildSteps().add(new KeepBuildForeverAction());

// fire ItemListeners, this includes ArtifactArchiver,Migrator to make this test compatible with jenkins 1.575+
fireItemListeners();

FreeStyleBuild upBuild = assertBuildStatusSuccess(upJob.scheduleBuild2(0).get());
assertFalse(upBuild.isKeepLog());

Expand All @@ -67,6 +75,9 @@ public void testDoesNotCareAboutResultOfOriginalBuild() throws Exception {
PromotionProcess promotionJob = createDownstreamSuccessPromotion(upJob, downJob);
promotionJob.getBuildSteps().add(new KeepBuildForeverAction());

// fire ItemListeners, this includes ArtifactArchiver,Migrator to make this test compatible with jenkins 1.575+
fireItemListeners();

FreeStyleBuild upBuild = assertBuildStatus(Result.FAILURE, upJob.scheduleBuild2(0).get());
assertFalse(upBuild.isKeepLog());

Expand All @@ -80,6 +91,9 @@ public void testDoesNotMarkBuildIfBuildNotPromotion() throws Exception {
job.getBuildersList().add(successfulBuilder());
job.getPublishersList().add(new KeepBuildForeverAction());

// fire ItemListeners, this includes ArtifactArchiver,Migrator to make this test compatible with jenkins 1.575+
fireItemListeners();

FreeStyleBuild build = assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
assertFalse(build.isKeepLog());
}
Expand Down
Expand Up @@ -23,6 +23,8 @@
import java.util.List;
import java.util.concurrent.Callable;

import static hudson.plugins.promoted_builds.util.ItemListenerHelper.fireItemListeners;

/**
* @author Kohsuke Kawaguchi
*/
Expand Down Expand Up @@ -53,9 +55,9 @@ public void test1() throws Exception {
"expr $BUILD_NUMBER % 2 - 1\n" // expr exits with non-zero status if result is zero
));
down.getPublishersList().replaceBy(recorders);
// TODO this and some tests in KeepBuildForeverActionTest will not work in 1.575+ because these ArtifactArchiver/Fingerprinter constructors are deprecated.
// Would only work if using @LocalData to prepare test configuration; or if we insert at this point a call to ArtifactArchiver.Migrator:
// for (ItemListener l : ItemListener.all()) {l.onLoaded();}

// fire ItemListeners, this includes ArtifactArchiver,Migrator to make this test compatible with jenkins 1.575+
fireItemListeners();

// not yet promoted while the downstream is failing
FreeStyleBuild up1 = assertBuildStatusSuccess(up.scheduleBuild2(0).get());
Expand Down
Expand Up @@ -18,6 +18,7 @@
import hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectRule;
import hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair;

import static hudson.plugins.promoted_builds.util.ItemListenerHelper.fireItemListeners;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -178,10 +179,15 @@ public void testPromotionEnvironmentShouldIncludeTargetParameters() throws Excep
JobPropertyImpl promotion = new JobPropertyImpl(inheritanceProjectPair.getBase());
inheritanceProjectPair.getBase().addProperty(promotion);

// TODO review this property asignment after https://issues.jenkins-ci.org/browse/JENKINS-34831 is fixed
inheritanceProjectPair.getBase().addProperty(new ParametersDefinitionProperty(new StringParameterDefinition(paramName, "")));
inheritanceProjectPair.getDerived().addProperty(new ParametersDefinitionProperty(new StringParameterDefinition(paramName, "")));
PromotionProcess promo1 = promotion.addProcess("promo1");
promo1.conditions.add(new SelfPromotionCondition(false));

// fire ItemListeners, this includes ArtifactArchiver,Migrator to make this test compatible with jenkins 1.575+
fireItemListeners();

String paramValue = "someString";
j.assertBuildStatusSuccess(inheritanceProjectPair.getDerived().scheduleBuild2(0, new Cause.UserCause(),
new ParametersAction(new StringParameterValue(paramName, paramValue))));
Expand Down
@@ -0,0 +1,19 @@
package hudson.plugins.promoted_builds.util;

import hudson.model.listeners.ItemListener;

/**
* Utility class to call ArtifactArchiver.Migrator ItemListener to make this code compatible with jenkins 1.575+
*/
public class ItemListenerHelper {
// TODO remove this class (and usages) after the baseline has been upgraded to Jenkins 1.575+

/**
* Executes all available ItemListeners
*/
public static void fireItemListeners() {
for (ItemListener l : ItemListener.all()) {
l.onLoaded();
}
}
}

0 comments on commit f7589c3

Please sign in to comment.