Skip to content

Commit

Permalink
Clean up tests (#215)
Browse files Browse the repository at this point in the history
* Refactor the TestUtils and ProcessDestroyer test helper classes into a new SwarmClientRule class that extends JUnit's ExternalResource class to allow cleanup to occur more reliably when tests fail.
* Add a thread that tails the Swarm client's log file as the test is running to make it easier to do postmortem debugging of test failures.
* Update the Jenkinsfile to remove unnecessary test branches.
  • Loading branch information
basil authored Jun 3, 2020
1 parent 72c2374 commit 332c63b
Show file tree
Hide file tree
Showing 8 changed files with 687 additions and 558 deletions.
11 changes: 3 additions & 8 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ buildPlugin(configurations: [
[ platform: 'linux', jdk: '8', jenkins: null ],

// Test the common case (i.e., a recent LTS release) on both Linux and Windows.
[ platform: 'linux', jdk: '8', jenkins: '2.222.3', javaLevel: '8' ],
[ platform: 'windows', jdk: '8', jenkins: '2.222.3', javaLevel: '8' ],

// Test with the first version of Jenkins that supports the WebSocket protocol on both Linux and
// Windows. This can be removed once WebSocket support is shipped in an LTS release.
[ platform: 'linux', jdk: '8', jenkins: '2.229', javaLevel: '8' ],
[ platform: 'windows', jdk: '8', jenkins: '2.229', javaLevel: '8' ],
[ platform: 'linux', jdk: '8', jenkins: '2.222.4', javaLevel: '8' ],
[ platform: 'windows', jdk: '8', jenkins: '2.222.4', javaLevel: '8' ],

// Test the bleeding edge of the compatibility spectrum (i.e., the latest supported Java runtime).
[ platform: 'linux', jdk: '11', jenkins: '2.222.3', javaLevel: '8' ],
[ platform: 'linux', jdk: '11', jenkins: '2.222.4', javaLevel: '8' ],
])
1 change: 0 additions & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
</plugins>
</build>
<properties>
<surefire.rerunFailingTestsCount>1</surefire.rerunFailingTestsCount>
<workflow-support-plugin.version>3.3</workflow-support-plugin.version>
</properties>
<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package hudson.plugins.swarm;

import static org.junit.Assert.assertNotNull;

import hudson.model.Node;
import hudson.plugins.swarm.test.SwarmClientRule;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class PipelineJobRestartTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();

/** Static so that it can be used with {@link #swarmClientRule}. */
private static final RestartableJenkinsRule holder =
new RestartableJenkinsRule.Builder().withReusedPort().build();

@Rule public RestartableJenkinsRule story = holder;

/** A {@link ClassRule} for compatibility with {@link RestartableJenkinsRule}. */
@ClassRule(order = 20)
public static TemporaryFolder temporaryFolder =
TemporaryFolder.builder().assureDeletion().build();

/** A {@link ClassRule} for compatibility with {@link RestartableJenkinsRule}. */
@ClassRule(order = 30)
public static SwarmClientRule swarmClientRule =
new SwarmClientRule(() -> holder.j, temporaryFolder);

/**
* Starts a Jenkins job on a Swarm agent, restarts Jenkins while the job is running, and
* verifies that the job continues running on the same agent after Jenkins has been restarted.
*/
@Test
public void buildShellScriptAfterRestart() {
story.then(
s -> {
// "-deleteExistingClients" is needed so that the Swarm Client can connect
// after the restart.
Node node = swarmClientRule.createSwarmClient("-deleteExistingClients");

WorkflowJob project = story.j.createProject(WorkflowJob.class, "test");
project.setConcurrentBuild(false);
project.setDefinition(
new CpsFlowDefinition(PipelineJobTest.getFlow(node, 1), true));

WorkflowRun build = project.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("wait-0/1", build);
});
story.then(
s -> {
SemaphoreStep.success("wait-0/1", null);
WorkflowJob project =
story.j.jenkins.getItemByFullName("test", WorkflowJob.class);
assertNotNull(project);
WorkflowRun build = project.getBuildByNumber(1);
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(build));
story.j.assertLogContains("ON_SWARM_CLIENT=true", build);
});
}
}
Loading

0 comments on commit 332c63b

Please sign in to comment.