From e16c5eb2ca034f11c198fa8471a9c1b6a0c42e2e Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 23 Dec 2021 08:19:52 -0800 Subject: [PATCH] Miscellaneous test cleanup (#6097) --- .../java/hudson/cli/ConsoleCommandTest.java | 56 +++++-------------- .../hudson/cli/DeleteBuildsCommandTest.java | 3 +- .../java/hudson/cli/RunRangeCommand2Test.java | 17 ++---- .../test/java/hudson/model/ExecutorTest.java | 15 +++-- .../LabelLoadStatisticsQueueLengthTest.java | 9 +-- .../src/test/java/hudson/model/QueueTest.java | 32 ++++++----- .../src/test/java/hudson/tasks/MavenTest.java | 48 ++++++---------- .../java/hudson/triggers/SCMTriggerTest.java | 9 +-- 8 files changed, 71 insertions(+), 118 deletions(-) diff --git a/test/src/test/java/hudson/cli/ConsoleCommandTest.java b/test/src/test/java/hudson/cli/ConsoleCommandTest.java index ea99d6508f09..e3a232b4ad4b 100644 --- a/test/src/test/java/hudson/cli/ConsoleCommandTest.java +++ b/test/src/test/java/hudson/cli/ConsoleCommandTest.java @@ -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; @@ -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) @@ -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 { @@ -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) @@ -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 { @@ -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)); diff --git a/test/src/test/java/hudson/cli/DeleteBuildsCommandTest.java b/test/src/test/java/hudson/cli/DeleteBuildsCommandTest.java index 366f8e981839..9d8c5d7f44f3 100644 --- a/test/src/test/java/hudson/cli/DeleteBuildsCommandTest.java +++ b/test/src/test/java/hudson/cli/DeleteBuildsCommandTest.java @@ -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; @@ -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)); diff --git a/test/src/test/java/hudson/cli/RunRangeCommand2Test.java b/test/src/test/java/hudson/cli/RunRangeCommand2Test.java index c9330304e2ec..894e10c95148 100644 --- a/test/src/test/java/hudson/cli/RunRangeCommand2Test.java +++ b/test/src/test/java/hudson/cli/RunRangeCommand2Test.java @@ -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; @@ -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)); diff --git a/test/src/test/java/hudson/model/ExecutorTest.java b/test/src/test/java/hudson/model/ExecutorTest.java index 3ef9cf3a4d24..b511caf16332 100644 --- a/test/src/test/java/hudson/model/ExecutorTest.java +++ b/test/src/test/java/hudson/model/ExecutorTest.java @@ -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 @@ -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") diff --git a/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java b/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java index 0e29a791ca13..d977a8ed64a2 100644 --- a/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java +++ b/test/src/test/java/hudson/model/LabelLoadStatisticsQueueLengthTest.java @@ -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; @@ -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. @@ -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. diff --git a/test/src/test/java/hudson/model/QueueTest.java b/test/src/test/java/hudson/model/QueueTest.java index 78a7137328ec..5e8d9eb2057b 100644 --- a/test/src/test/java/hudson/model/QueueTest.java +++ b/test/src/test/java/hudson/model/QueueTest.java @@ -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)); @@ -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"))); @@ -277,10 +277,12 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen }); Future b1 = p.scheduleBuild2(0); + assertNotNull(b1); seq.phase(1); // and make sure we have one build under way // get another going Future b2 = p.scheduleBuild2(0); + assertNotNull(b2); q.scheduleMaintenance().get(); Queue.Item[] items = q.getItems(); @@ -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 fb = project.scheduleBuild2(0, new UserIdCause()); + final Future 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(); @@ -1123,7 +1125,7 @@ private void checkCancelOperationUsingUrl(Function 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)); diff --git a/test/src/test/java/hudson/tasks/MavenTest.java b/test/src/test/java/hudson/tasks/MavenTest.java index 96f15a57bdef..5276b52381f8 100644 --- a/test/src/test/java/hudson/tasks/MavenTest.java +++ b/test/src/test/java/hudson/tasks/MavenTest.java @@ -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 @@ -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 { @@ -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()); } @@ -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") diff --git a/test/src/test/java/hudson/triggers/SCMTriggerTest.java b/test/src/test/java/hudson/triggers/SCMTriggerTest.java index faa14acfe940..e5f8a6f3f107 100644 --- a/test/src/test/java/hudson/triggers/SCMTriggerTest.java +++ b/test/src/test/java/hudson/triggers/SCMTriggerTest.java @@ -82,6 +82,7 @@ public void simultaneousPollAndBuild() throws Exception { p.setScm(new TestSCM(checkoutStarted)); Future build = p.scheduleBuild2(0, new Cause.UserCause()); + assertNotNull(build); checkoutStarted.block(); assertFalse("SCM-poll after build has started should wait until that build finishes SCM-update", p.pollSCMChanges(StreamTaskListener.fromStdout())); build.get(); // let mock build finish @@ -155,15 +156,15 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen p.addTrigger(t); // Start one build to block others - assertTrue(p.scheduleBuild(new Cause.UserCause())); + p.scheduleBuild2(0, new Cause.UserCause()).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 fb = p.scheduleBuild2(0, new Cause.UserCause()); assertNotNull(fb); - assertTrue(p.scheduleBuild(new SCMTriggerCause("First poll"))); - assertTrue(p.scheduleBuild(new SCMTriggerCause("Second poll"))); - assertTrue(p.scheduleBuild(new SCMTriggerCause("Third poll"))); + assertNotNull(p.scheduleBuild2(0, new SCMTriggerCause("First poll"))); + assertNotNull(p.scheduleBuild2(0, new SCMTriggerCause("Second poll"))); + assertNotNull(p.scheduleBuild2(0, new SCMTriggerCause("Third poll"))); // Wait for 2nd build to finish buildShouldComplete.signal();