Skip to content

Commit

Permalink
Test and fix for [JENKINS-51290] - properties step error about job no…
Browse files Browse the repository at this point in the history
…t yet started if could not obtain execution for previous run
  • Loading branch information
svanoort committed May 17, 2018
1 parent 74368e4 commit 0a06168
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Expand Up @@ -70,7 +70,7 @@ THE SOFTWARE.
<workflow-step-api.version>2.13</workflow-step-api.version>
<workflow-scm-step-plugin.version>2.4</workflow-scm-step-plugin.version>
<workflow-support.version>2.17</workflow-support.version>
<workflow-cps.version>2.43</workflow-cps.version>
<workflow-cps.version>2.53</workflow-cps.version>
<git-plugin.version>3.7.0</git-plugin.version>
</properties>
<dependencies>
Expand All @@ -82,7 +82,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.20</version>
<version>2.21</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -92,7 +92,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.10</version>
<version>1.14</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -123,7 +123,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.25</version>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -133,7 +133,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.39</version>
<version>1.42</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Expand Up @@ -24,6 +24,7 @@

package org.jenkinsci.plugins.workflow.multibranch;

import com.google.common.util.concurrent.ListenableFuture;
import hudson.AbortException;
import hudson.BulkChange;
import hudson.Extension;
Expand All @@ -39,6 +40,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;

import hudson.model.TaskListener;
Expand All @@ -51,6 +54,7 @@
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousStepExecution;
Expand All @@ -77,6 +81,8 @@ public List<JobProperty> getProperties() {
return properties;
}

private static final Logger LOGGER = Logger.getLogger(JobPropertyStep.class.getName());

public Map<JobPropertyDescriptor,JobProperty> getPropertiesMap() {
return Descriptor.toMap((List) properties);
}
Expand All @@ -101,10 +107,15 @@ public static class Execution extends AbstractSynchronousStepExecution<Void> {
FlowExecutionOwner owner = ((FlowExecutionOwner.Executable) previousRun).asFlowExecutionOwner();

if (owner != null) {
FlowExecution execution = owner.get();
if (execution != null) {
previousHadStep = new DepthFirstScanner().findFirstMatch(execution,
new NodeStepTypePredicate(step.getDescriptor())) != null;
try {
FlowExecution execution = owner.get();
if (execution != null) {
previousHadStep = new DepthFirstScanner().findFirstMatch(execution,
new NodeStepTypePredicate(step.getDescriptor())) != null;
}
} catch (Exception ex) {
// May happen legitimately due to owner.get() throwing IOException when previous execution was nulled
LOGGER.log(Level.FINE, "Could not search for JobPropertyStep execution: previous run either had null execution due to legitimate error and shows as not-yet-started, or threw exception", ex);
}
}
}
Expand Down
Expand Up @@ -35,6 +35,7 @@
import hudson.model.queue.QueueTaskFuture;
import hudson.tasks.LogRotator;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -77,6 +78,7 @@
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -138,6 +140,24 @@ public void resetStartsAndStops() {
assertEquals(Collections.emptyList(), removeTriggerProperty(emptyInput));
}

@Issue("JENKINS-51290")
@Test
public void testPreviousBuildFailedHard() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");

// First we simulate a build that has failed so hard it has a null execution
p.setDefinition(new CpsFlowDefinition("echo 'Not doing anything'", true));
WorkflowRun run = r.buildAndAssertSuccess(p);
Field f = run.getClass().getDeclaredField("execution");
f.setAccessible(true);
f.set(run, null);
Assert.assertNull(run.getExecution());

// Verify build runs cleanly
p.setDefinition(new CpsFlowDefinition("properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '1']]])", true));
r.buildAndAssertSuccess(p);
}

@SuppressWarnings("rawtypes")
@Test public void configRoundTripBuildDiscarder() throws Exception {
List<JobProperty> properties = Collections.<JobProperty>singletonList(new BuildDiscarderProperty(new LogRotator(1, 2, -1, 3)));
Expand Down

0 comments on commit 0a06168

Please sign in to comment.