Skip to content

Commit

Permalink
Miscellaneous test cleanup (jenkinsci#6097)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Dec 23, 2021
1 parent 8a5e309 commit e16c5eb
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 118 deletions.
56 changes: 14 additions & 42 deletions test/src/test/java/hudson/cli/ConsoleCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;

import hudson.Functions;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.Result;
import hudson.model.labels.LabelAtom;
import hudson.tasks.BatchFile;
import hudson.tasks.Shell;
Expand Down Expand Up @@ -204,23 +203,10 @@ public class ConsoleCommandTest {
project.getBuildersList().add(new Shell("echo start - ${BUILD_NUMBER}\nsleep 10s\n"
+ "echo after sleep - ${BUILD_NUMBER}"));
}
if (!project.scheduleBuild(0)) {
fail("Job wasn't scheduled properly");
}

// Wait until project is started (at least 1s)
while(!project.isBuilding()) {
System.out.println("Waiting for build to start and sleep 1s...");
Thread.sleep(1000);
}

// Wait for the first message
if(!project.getBuildByNumber(1).getLog().contains("start - 1")) {
Thread.sleep(1000);
}

assertThat(project.getBuildByNumber(1).getLog(), containsString("start - 1"));
assertThat(project.getBuildByNumber(1).getLog(), not(containsString("after sleep - 1")));
FreeStyleBuild build = project.scheduleBuild2(0).waitForStart();
j.waitForMessage("start - 1", build);
j.assertLogContains("start - 1", build);
j.assertLogNotContains("after sleep - 1", build);

CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ, Item.BUILD)
Expand All @@ -237,9 +223,8 @@ public class ConsoleCommandTest {
assertThat(result, succeeded());
assertThat(result.stdout(), containsString("after sleep - 1"));

assertThat(project.getBuildByNumber(1).isBuilding(), equalTo(false));
assertThat(project.getBuildByNumber(1).getResult(), equalTo(Result.SUCCESS));
assertThat(project.getBuildByNumber(1).getLog(), containsString("after sleep - 1"));
j.assertBuildStatusSuccess(j.waitForCompletion(build));
j.assertLogContains("after sleep - 1", build);
}

@Test public void consoleShouldSuccessWithLastNLines() throws Exception {
Expand Down Expand Up @@ -277,23 +262,11 @@ public class ConsoleCommandTest {
+ "echo 6\necho 7\necho 8\necho 9"));
}

if (!project.scheduleBuild(0)) {
fail("Job wasn't scheduled properly");
}

// Wait until project is started (at least 1s)
while(!project.isBuilding()) {
System.out.println("Waiting for build to start and sleep 1s...");
Thread.sleep(1000);
}
FreeStyleBuild build = project.scheduleBuild2(0).waitForStart();

// Wait for the first sleep
if(!project.getBuildByNumber(1).getLog().contains("echo 5")) {
Thread.sleep(1000);
}

assertThat(project.getBuildByNumber(1).getLog(), containsString("echo 5"));
assertThat(project.getBuildByNumber(1).getLog(), not(containsString("echo 6")));
j.waitForMessage("echo 5", build);
j.assertLogContains("echo 5", build);
j.assertLogNotContains("echo 6", build);

CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ, Item.BUILD)
Expand All @@ -305,9 +278,8 @@ public class ConsoleCommandTest {
assertThat(result.stdout(), containsString("echo 6"));
assertThat(result.stdout(), containsString("echo 9"));

assertThat(project.getBuildByNumber(1).isBuilding(), equalTo(false));
assertThat(project.getBuildByNumber(1).getResult(), equalTo(Result.SUCCESS));
assertThat(project.getBuildByNumber(1).getLog(), containsString("echo 9"));
j.assertBuildStatusSuccess(j.waitForCompletion(build));
j.assertLogContains("echo 9", build);
}

@Test public void consoleShouldFailIfTheBuildIsStuckInTheQueue() throws Exception {
Expand All @@ -316,7 +288,7 @@ public class ConsoleCommandTest {
project.getBuildersList().add(new Shell("echo 1\nsleep 10s"));
project.setAssignedLabel(new LabelAtom("never_created"));

assertThat("Job wasn't scheduled properly", project.scheduleBuild(0), equalTo(true));
assertNotNull(project.scheduleBuild2(0));
Thread.sleep(1000);
assertThat("Job wasn't scheduled properly - it isn't in the queue", project.isInQueue(), equalTo(true));
assertThat("Job wasn't scheduled properly - it is running on non-exist node", project.isBuilding(), equalTo(false));
Expand Down
3 changes: 2 additions & 1 deletion test/src/test/java/hudson/cli/DeleteBuildsCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeFalse;

import hudson.Functions;
Expand Down Expand Up @@ -155,7 +156,7 @@ public class DeleteBuildsCommandTest {
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.getBuildersList().add(new Shell("echo 1"));
project.setAssignedLabel(new LabelAtom("never_created"));
assertThat("Job wasn't scheduled properly", project.scheduleBuild(0), equalTo(true));
assertNotNull(project.scheduleBuild2(0));
Thread.sleep(1000);
assertThat("Job wasn't scheduled properly - it isn't in the queue", project.isInQueue(), equalTo(true));
assertThat("Job wasn't scheduled properly - it is running on non-exist node", project.isBuilding(), equalTo(false));
Expand Down
17 changes: 4 additions & 13 deletions test/src/test/java/hudson/cli/RunRangeCommand2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;

import hudson.Functions;
import hudson.model.FreeStyleBuild;
Expand Down Expand Up @@ -85,31 +86,21 @@ public class RunRangeCommand2Test {
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo 1\r\nping -n 10 127.0.0.1 >nul") : new Shell("echo 1\nsleep 10s"));
FreeStyleBuild build = project.scheduleBuild2(0).waitForStart();

// Wait until classProject is started (at least 1s)
while(!project.isBuilding()) {
System.out.println("Waiting for build to start and sleep 1s...");
Thread.sleep(1000);
}

// Wait for the first sleep
if(!project.getBuildByNumber(1).getLog().contains("echo 1")) {
Thread.sleep(1000);
}
j.waitForMessage("echo 1", build);

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ)
.invokeWithArgs("aProject", "1");
assertThat(result, succeeded());
assertThat(result.stdout(), containsString("Builds: 1" + System.lineSeparator()));
j.waitForCompletion(build);
j.assertBuildStatusSuccess(j.waitForCompletion(build));
}

@Test public void dummyRangeShouldSuccessEvenTheBuildIsStuckInTheQueue() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("aProject");
project.getBuildersList().add(new Shell("echo 1\nsleep 10s"));
project.setAssignedLabel(new LabelAtom("never_created"));
assertThat("Job wasn't scheduled properly", project.scheduleBuild(0), equalTo(true));
assertNotNull(project.scheduleBuild2(0));
Thread.sleep(1000);
assertThat("Job wasn't scheduled properly - it isn't in the queue",
project.isInQueue(), equalTo(true));
Expand Down
15 changes: 7 additions & 8 deletions test/src/test/java/hudson/model/ExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public void abortCause() throws Exception {
FreeStyleBuild b = r.get();

// make sure this information is recorded
assertEquals(Result.FAILURE, b.getResult());
j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b));
InterruptedBuildAction iba = b.getAction(InterruptedBuildAction.class);
assertEquals(1,iba.getCauses().size());
assertEquals(((UserInterruption) iba.getCauses().get(0)).getUser(), johnny);

// make sure it shows up in the log
assertTrue(b.getLog().contains(johnny.getId()));
j.assertLogContains(johnny.getId(), b);
}

@Test
Expand All @@ -114,12 +114,11 @@ public void disconnectCause() throws Exception {

FreeStyleBuild b = r.get();

String log = b.getLog();
assertEquals(Result.FAILURE, b.getResult());
assertThat(log, containsString("Finished: FAILURE"));
assertThat(log, containsString("Build step 'BlockingBuilder' marked build as failure"));
assertThat(log, containsString("Agent went offline during the build"));
assertThat(log, containsString("Disconnected by Johnny : Taking offline to break your build"));
j.assertBuildStatus(Result.FAILURE, j.waitForCompletion(b));
j.assertLogContains("Finished: FAILURE", b);
j.assertLogContains("Build step 'BlockingBuilder' marked build as failure", b);
j.assertLogContains("Agent went offline during the build", b);
j.assertLogContains("Disconnected by Johnny : Taking offline to break your build", b);
}

@Issue("SECURITY-611")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -90,9 +91,9 @@ public void queueLengthReflectsBuildableItemsAssignedLabel()

// Add the job to the build queue several times with an assigned label.
for (int i = 0; i < 3; i++) {
project.scheduleBuild(0, CAUSE, new LabelAssignmentActionImpl(),
assertNotNull(project.scheduleBuild2(0, CAUSE, new LabelAssignmentActionImpl(),
new ParametersAction(new StringParameterValue(
PARAMETER_NAME, String.valueOf(i))));
PARAMETER_NAME, String.valueOf(i)))));
}

// Verify that the real queue length is 3.
Expand Down Expand Up @@ -143,9 +144,9 @@ public void queueLengthReflectsJobsAssignedLabel() throws Exception {

// Add the job to the build queue several times.
for (int i = 0; i < 3; i++) {
project.scheduleBuild(0, CAUSE,
assertNotNull(project.scheduleBuild2(0, CAUSE,
new ParametersAction(new StringParameterValue(
PARAMETER_NAME, String.valueOf(i))));
PARAMETER_NAME, String.valueOf(i)))));
}

// Verify that the real queue length is 3.
Expand Down
32 changes: 17 additions & 15 deletions test/src/test/java/hudson/model/QueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public class QueueTest {
r.jenkins.setNumExecutors(0);

FreeStyleProject testProject = r.createFreeStyleProject("test");
testProject.scheduleBuild(new UserIdCause());
assertNotNull(testProject.scheduleBuild2(0, new UserIdCause()));
q.save();

System.out.println(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "queue.xml"), StandardCharsets.UTF_8));
Expand Down Expand Up @@ -222,7 +222,7 @@ public void recover_from_legacy_list() {
r.jenkins.setNumExecutors(0);

FreeStyleProject testProject = r.createFreeStyleProject("test");
testProject.scheduleBuild(new UserIdCause());
assertNotNull(testProject.scheduleBuild2(0, new UserIdCause()));
q.save();

System.out.println(FileUtils.readFileToString(new File(r.jenkins.getRootDir(), "queue.xml")));
Expand Down Expand Up @@ -277,10 +277,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
});

Future<FreeStyleBuild> b1 = p.scheduleBuild2(0);
assertNotNull(b1);
seq.phase(1); // and make sure we have one build under way

// get another going
Future<FreeStyleBuild> b2 = p.scheduleBuild2(0);
assertNotNull(b2);

q.scheduleMaintenance().get();
Queue.Item[] items = q.getItems();
Expand Down Expand Up @@ -361,22 +363,22 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
});

// Start one build to block others
assertTrue(project.scheduleBuild(new UserIdCause()));
project.scheduleBuild2(0, new UserIdCause()).waitForStart();
buildStarted.block(); // wait for the build to really start

// Schedule a new build, and trigger it many ways while it sits in queue
Future<FreeStyleBuild> fb = project.scheduleBuild2(0, new UserIdCause());
final Future<FreeStyleBuild> fb = project.scheduleBuild2(0, new UserIdCause());
assertNotNull(fb);
assertTrue(project.scheduleBuild(new SCMTriggerCause("")));
assertTrue(project.scheduleBuild(new UserIdCause()));
assertTrue(project.scheduleBuild(new TimerTriggerCause()));
assertTrue(project.scheduleBuild(new RemoteCause("1.2.3.4", "test")));
assertTrue(project.scheduleBuild(new RemoteCause("4.3.2.1", "test")));
assertTrue(project.scheduleBuild(new SCMTriggerCause("")));
assertTrue(project.scheduleBuild(new RemoteCause("1.2.3.4", "test")));
assertTrue(project.scheduleBuild(new RemoteCause("1.2.3.4", "foo")));
assertTrue(project.scheduleBuild(new SCMTriggerCause("")));
assertTrue(project.scheduleBuild(new TimerTriggerCause()));
assertNotNull(project.scheduleBuild2(0, new SCMTriggerCause("")));
assertNotNull(project.scheduleBuild2(0, new UserIdCause()));
assertNotNull(project.scheduleBuild2(0, new TimerTriggerCause()));
assertNotNull(project.scheduleBuild2(0, new RemoteCause("1.2.3.4", "test")));
assertNotNull(project.scheduleBuild2(0, new RemoteCause("4.3.2.1", "test")));
assertNotNull(project.scheduleBuild2(0, new SCMTriggerCause("")));
assertNotNull(project.scheduleBuild2(0, new RemoteCause("1.2.3.4", "test")));
assertNotNull(project.scheduleBuild2(0, new RemoteCause("1.2.3.4", "foo")));
assertNotNull(project.scheduleBuild2(0, new SCMTriggerCause("")));
assertNotNull(project.scheduleBuild2(0, new TimerTriggerCause()));

// Wait for 2nd build to finish
buildShouldComplete.signal();
Expand Down Expand Up @@ -1123,7 +1125,7 @@ private void checkCancelOperationUsingUrl(Function<Queue.Item, String> urlProvid
assertThat(q.getItems().length, equalTo(0));

FreeStyleProject testProject = r.createFreeStyleProject("test");
testProject.scheduleBuild(new UserIdCause());
assertNotNull(testProject.scheduleBuild2(0, new UserIdCause()));

Queue.Item[] items = q.getItems();
assertThat(items.length, equalTo(1));
Expand Down
48 changes: 17 additions & 31 deletions test/src/test/java/hudson/tasks/MavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,8 @@ private void verify() throws Exception {
project.setScm(new ExtractResourceSCM(getClass().getResource("maven-empty.zip")));
project.getBuildersList().add(new Maven("clean package",null));

FreeStyleBuild build = project.scheduleBuild2(0).get();
@SuppressWarnings("deprecation")
String buildLog = build.getLog();
assertNotNull(buildLog);
System.out.println(buildLog);
assertFalse(buildLog.contains("-Dpassword=12345"));
FreeStyleBuild build = j.waitForCompletion(project.scheduleBuild2(0).waitForStart());
j.assertLogNotContains("-Dpassword=12345", build);
}

@Test
Expand All @@ -222,19 +218,9 @@ public void parametersReferencedFromPropertiesShouldRetainBackslashes() throws E
new EnvironmentVariablesNodeProperty(envVar)
));

FreeStyleBuild build = project.scheduleBuild2(0).get();
@SuppressWarnings("deprecation")
String buildLog = build.getLog();

assertNotNull(buildLog);
assertTrue(
"Parameter my.path should preserve backslashes in:\n" + buildLog,
buildLog.contains("-Dmy.path=C:\\Windows\\Dir")
);
assertTrue(
"Parameter global.path should preserve backslashes in:\n" + buildLog,
buildLog.contains("-Dglobal.path=D:\\Jenkins")
);
FreeStyleBuild build = j.waitForCompletion(project.scheduleBuild2(0).waitForStart());
j.assertLogContains("-Dmy.path=C:\\Windows\\Dir", build);
j.assertLogContains("-Dglobal.path=D:\\Jenkins", build);
}

@Test public void defaultSettingsProvider() throws Exception {
Expand Down Expand Up @@ -314,21 +300,21 @@ public void parametersReferencedFromPropertiesShouldRetainBackslashes() throws E

FreeStyleProject p = j.createFreeStyleProject();
p.updateByXml((Source) new StreamSource(getClass().getResourceAsStream("MavenTest/doPassBuildVariablesOptionally.xml")));
String log = j.buildAndAssertSuccess(p).getLog();
FreeStyleBuild build = j.buildAndAssertSuccess(p);
assertTrue("Build variables injection should be enabled by default when loading from XML", p.getBuildersList().get(Maven.class).isInjectBuildVariables());
assertTrue("Build variables should be injected by default when loading from XML", log.contains("-DNAME=VALUE"));
j.assertLogContains("-DNAME=VALUE", build);

p.getBuildersList().clear();
p.getBuildersList().add(new Maven("--help", maven.getName(), null, null, null, false, null, null, false/*do not inject*/));

log = j.buildAndAssertSuccess(p).getLog();
assertFalse("Build variables should not be injected", log.contains("-DNAME=VALUE"));
build = j.buildAndAssertSuccess(p);
j.assertLogNotContains("-DNAME=VALUE", build);

p.getBuildersList().clear();
p.getBuildersList().add(new Maven("--help", maven.getName(), null, null, null, false, null, null, true/*do inject*/));

log = j.buildAndAssertSuccess(p).getLog();
assertTrue("Build variables should be injected", log.contains("-DNAME=VALUE"));
build = j.buildAndAssertSuccess(p);
j.assertLogContains("-DNAME=VALUE", build);

assertFalse("Build variables injection should be disabled by default", new Maven("", "").isInjectBuildVariables());
}
Expand All @@ -341,16 +327,16 @@ public void parametersReferencedFromPropertiesShouldRetainBackslashes() throws E

p.getBuildersList().add(new Maven("--help", maven.getName(), null, properties, null, false, null,
null, false/*do not inject build variables*/));
String log = j.buildAndAssertSuccess(p).getLog();
assertTrue("Properties should always be injected, even when build variables injection is disabled",
log.contains("-DTEST_PROP1=VAL1") && log.contains("-DTEST_PROP2=VAL2"));
FreeStyleBuild build = j.buildAndAssertSuccess(p);
j.assertLogContains("-DTEST_PROP1=VAL1", build);
j.assertLogContains("-DTEST_PROP2=VAL2", build);

p.getBuildersList().clear();
p.getBuildersList().add(new Maven("--help", maven.getName(), null, properties, null, false, null,
null, true/*do inject build variables*/));
log = j.buildAndAssertSuccess(p).getLog();
assertTrue("Properties should always be injected, even when build variables injection is enabled",
log.contains("-DTEST_PROP1=VAL1") && log.contains("-DTEST_PROP2=VAL2"));
build = j.buildAndAssertSuccess(p);
j.assertLogContains("-DTEST_PROP1=VAL1", build);
j.assertLogContains("-DTEST_PROP2=VAL2", build);
}

@Issue("JENKINS-34138")
Expand Down
Loading

0 comments on commit e16c5eb

Please sign in to comment.