Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #146 from jglick/standard-test-utils-JENKINS-26399
Browse files Browse the repository at this point in the history
[JENKINS-26399] Standard test utils
  • Loading branch information
jglick committed Jul 20, 2015
2 parents d5f5355 + 4b528c5 commit 2f6a9d2
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 255 deletions.

This file was deleted.

Expand Up @@ -27,8 +27,6 @@
import hudson.EnvVars;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Node.Mode;
import hudson.model.Run;
import hudson.model.Slave;
import hudson.slaves.CommandLauncher;
import hudson.slaves.NodeProperty;
Expand All @@ -47,31 +45,6 @@
*/
public class JenkinsRuleExt {

// TODO use standard version 1.607+

/**
* Waits for a build to complete.
* @return the same build, once done
*/
public static <R extends Run<?,?>> R waitForCompletion(R r) throws InterruptedException {
// Could be using com.jayway.awaitility:awaitility but it seems like overkill here.
while (r.isBuilding()) {
Thread.sleep(100);
}
return r;
}

/**
* Waits for a build log to contain a specified string.
* @return the same build, once it does
*/
public static <R extends Run<?,?>> R waitForMessage(String message, R r) throws IOException, InterruptedException {
while (!JenkinsRule.getLog(r).contains(message)) {
Thread.sleep(100);
}
return r;
}

/**
* Akin to {@link JenkinsRule#createSlave(String, String, EnvVars)} but allows {@link Computer#getEnvironment} to be controlled rather than directly modifying launchers.
* @param env variables to override in {@link Computer#getEnvironment}; null values will get unset even if defined in the test environment
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

Expand Down
Expand Up @@ -52,6 +52,7 @@
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;
Expand Down Expand Up @@ -228,7 +229,7 @@ public void failedToStartRun() throws Exception {
assertNotNull(p);
WorkflowRun b = p.getLastBuild();
assertNotNull(b);
r.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
r.assertBuildStatusSuccess(r.waitForCompletion(b));
}

}
Expand Up @@ -116,7 +116,7 @@ public void evaluate() throws Throwable {
}
liveness();
SemaphoreStep.success("wait/1", null);
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
}
});
}
Expand Down Expand Up @@ -162,7 +162,7 @@ private void liveness() {
rebuildContext(story.j);
assertThatWorkflowIsSuspended();
SemaphoreStep.success("wait/1", null);
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
}
});
}
Expand Down Expand Up @@ -198,7 +198,7 @@ private void liveness() {
rebuildContext(story.j);
assertThatWorkflowIsSuspended();
SemaphoreStep.success("wait/1", null);
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));

story.j.assertLogContains("before=demo", b);
story.j.assertLogContains("ONSLAVE=true", b);
Expand Down Expand Up @@ -232,7 +232,7 @@ private void liveness() {
@Override public void evaluate() throws Throwable {
rebuildContext(story.j);
SemaphoreStep.success("wait/1", null);
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
}
});
}
Expand Down Expand Up @@ -300,7 +300,7 @@ private void startJnlpProc() throws Exception {
while (f2.isFile()) {
Thread.sleep(100);
}
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
story.j.assertLogContains("finished waiting", b);
story.j.assertLogContains("OK, done", b);
killJnlpProc();
Expand Down Expand Up @@ -349,7 +349,7 @@ private void startJnlpProc() throws Exception {
while (f2.isFile()) {
Thread.sleep(100);
}
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
story.j.assertLogContains("finished waiting", b); // TODO sometimes is not printed to log, despite f2 having been removed
story.j.assertLogContains("OK, done", b);
killJnlpProc();
Expand Down Expand Up @@ -472,7 +472,7 @@ private void startJnlpProc() throws Exception {
SemaphoreStep.success("wait/2", null);
SemaphoreStep.success("wait/3", null);

story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
assertTrue(e.programPromise.get().closures.isEmpty());
}
});
Expand All @@ -490,21 +490,21 @@ private void startJnlpProc() throws Exception {
startBuilding();
waitForWorkflowToSuspend();
assertTrue(b.isBuilding());
JenkinsRuleExt.waitForMessage("running as someone", b);
story.j.waitForMessage("running as someone", b);
CheckAuth.finish(false);
waitForWorkflowToSuspend();
assertTrue(b.isBuilding());
JenkinsRuleExt.waitForMessage("still running as someone", b);
story.j.waitForMessage("still running as someone", b);
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
assertEquals(JenkinsRule.DummySecurityRealm.class, jenkins().getSecurityRealm().getClass());
rebuildContext(story.j);
assertThatWorkflowIsSuspended();
JenkinsRuleExt.waitForMessage("again running as someone", b);
story.j.waitForMessage("again running as someone", b);
CheckAuth.finish(true);
story.j.assertLogContains("finally running as someone", story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b)));
story.j.assertLogContains("finally running as someone", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)));
}
});
}
Expand Down Expand Up @@ -586,7 +586,7 @@ public static void finish(final boolean terminate) {
rebuildContext(story.j);
assertThatWorkflowIsSuspended();
SemaphoreStep.success("env/1", null);
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
story.j.assertLogContains("tag=jenkins-demo-1 PERMACHINE=set", b);
story.j.assertLogContains("tag2=custom", b);
story.j.assertLogContains("tag3=custom2 stuff=more", b);
Expand All @@ -608,15 +608,15 @@ public static void finish(final boolean terminate) {
p = jenkins().createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition("node('special') {echo 'OK ran'}"));
startBuilding();
JenkinsRuleExt.waitForMessage("Still waiting to schedule task", b);
story.j.waitForMessage("Still waiting to schedule task", b);
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
story.j.createSlave("special", null);
rebuildContext(story.j);
// TODO JENKINS-27532 sometimes two copies of the WorkflowRun are loaded
story.j.assertLogContains("OK ran", story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b)));
story.j.assertLogContains("OK ran", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)));
}
});
}
Expand Down
Expand Up @@ -30,14 +30,14 @@
import hudson.tasks.junit.TestResultAction;
import java.util.List;
import javax.mail.internet.InternetAddress;
import org.jenkinsci.plugins.workflow.BuildWatcher;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.mock_javamail.Mailbox;

Expand Down
Expand Up @@ -42,7 +42,6 @@
import java.util.Locale;
import java.util.Map;
import jenkins.tasks.SimpleBuildWrapper;
import org.jenkinsci.plugins.workflow.BuildWatcher;
import org.jenkinsci.plugins.workflow.JenkinsRuleExt;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand All @@ -54,6 +53,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.TestExtension;
Expand Down Expand Up @@ -83,7 +83,7 @@ public class CoreWrapperStepTest {
SemaphoreStep.success("restarting/1", null);
WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
story.j.assertBuildStatusSuccess(JenkinsRuleExt.waitForCompletion(b));
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
String expected = "/home/jenkins/extra/bin:/usr/bin:/bin";
story.j.assertLogContains("groovy PATH=" + expected + ":", b);
story.j.assertLogContains("shell PATH=" + expected + ":", b);
Expand Down

0 comments on commit 2f6a9d2

Please sign in to comment.