Skip to content

Commit

Permalink
WIP Re-enable accidentially disabled integration tests
Browse files Browse the repository at this point in the history
The culprit is commit 9580862 when switching
from HudsonTestCase to JenkinsRule. The rule has to be used as a JUnit 4 rule
inside a test. It is different to JUnit 3 and cannot be used as a base class.
As no JUnit 3 base class is used anymore the JUnit 4 runner kicks in and only
detects methods annotated with `@Test` as test methods. JUnit 3 was using the
prefix "test" in method names to detect test methods.

The temporary folder has to be provided as absolute path.
Otherwise `Jenkins::checkRawBuildsDir` fails with a message like this:
"jenkins.model.InvalidBuildsDir: ${ITEM_ROOTDIR}/builds does not resolve to an absolute path"

`WebClient::goTo` now takes a relative instead of an absolute URL.

Triggering the "change" event on HTML form inputs now requires a bit
more plumbing.

Many tests fail because running the Javascript code fails reporting
a missing "each" function.
  • Loading branch information
darxriggs committed Oct 26, 2021
1 parent c540200 commit 6da487e
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
*
* @author mfriedenhagen
*/
public abstract class AbstractHudsonTestCaseDeletingInstanceDir
extends
JenkinsRule {
class JenkinsRuleWithDeletingInstanceDir extends JenkinsRule {

/** Our logger. */
private static final Logger LOG = Logger.getLogger(
AbstractHudsonTestCaseDeletingInstanceDir.class.getName());
private static final Logger LOG = Logger.getLogger(JenkinsRuleWithDeletingInstanceDir.class.getName());

static {
new File("target/tmp").mkdir();
System.setProperty("java.io.tmpdir", "target/tmp");
File tmpDir = new File("target/tmp");
tmpDir.mkdir();
System.setProperty("java.io.tmpdir", tmpDir.getAbsolutePath());
// Possible workaround for
// https://issues.jenkins-ci.org/browse/JENKINS-19244
// jenkins random hang during startup - Solaris and Linux
System.setProperty("hudson.model.Hudson.parallelLoad", "false");
}

/** {@inheritDoc} */
@Override
public void after() throws Exception {
super.after();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import org.acegisecurity.context.SecurityContextHolder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.recipes.LocalData;
import org.jvnet.hudson.test.recipes.PresetData;
import org.jvnet.hudson.test.recipes.PresetData.DataSet;
Expand All @@ -13,49 +18,51 @@
import hudson.model.Result;
import jenkins.model.Jenkins;

public class JobConfigBadgeActionIT
extends
AbstractHudsonTestCaseDeletingInstanceDir {
public class JobConfigBadgeActionIT {

@Rule
public JenkinsRule j = new JenkinsRuleWithDeletingInstanceDir();

private WebClient webClient;
private static final int SLEEP_TIME = 1100;

@Override
public void before() throws Throwable {
super.before();
webClient = new WebClient();
@Before
public void before() {
webClient = j.createWebClient();
}

@Test
public void testBadgeAction() throws Exception {
final String jobName = "newjob";
final String description = "a description";
final FreeStyleProject project = createFreeStyleProject(jobName);
final FreeStyleProject project = j.createFreeStyleProject(jobName);

assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
HtmlPage htmlPage = webClient.goTo("job/" + jobName);
Assert.assertFalse("Page should not contain build badge",
htmlPage.asXml().contains("buildbadge.png"));

assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
htmlPage = (HtmlPage) htmlPage.refresh();
Assert.assertFalse("Page should still not contain build badge",
htmlPage.asXml().contains("buildbadge.png"));

project.setDescription(description);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());

htmlPage = (HtmlPage) htmlPage.refresh();
Assert.assertTrue("Page should contain build badge",
htmlPage.asXml().contains("buildbadge.png"));
}

@Test
public void testBadgeAfterRename() throws Exception {
final String oldName = "firstjobname";
final String newName = "secondjobname";

final FreeStyleProject project = createFreeStyleProject(oldName);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
final FreeStyleProject project = j.createFreeStyleProject(oldName);
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
Thread.sleep(SLEEP_TIME);

project.renameTo(newName);
Expand All @@ -73,15 +80,16 @@ public void testBadgeAfterRename() throws Exception {
showDiffPage.asText().contains("No lines changed"));
}

@Test
public void testCorrectLinkTargetsAfterRename() throws Exception {
final String oldName = "jobname1";
final String newName = "jobname2";
final String oldDescription = "first description";
final String newDescription = "second description";

final FreeStyleProject project = createFreeStyleProject(oldName);
final FreeStyleProject project = j.createFreeStyleProject(oldName);
project.setDescription(oldDescription);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
Thread.sleep(SLEEP_TIME);

project.setDescription(newDescription);
Expand Down Expand Up @@ -109,59 +117,62 @@ public void testCorrectLinkTargetsAfterRename() throws Exception {
showDiffPage2.asText().contains("Older"));
}

@Test
public void testProjectWithConfigsButMissingBuilds() throws Exception {
final FreeStyleProject project = createFreeStyleProject();
final FreeStyleProject project = j.createFreeStyleProject();
Thread.sleep(SLEEP_TIME);
project.setDescription("bla");
Thread.sleep(SLEEP_TIME);
project.updateNextBuildNumber(5);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
}

@LocalData
@Test
public void testBuildWithoutHistoryDir() throws Exception {
final FreeStyleProject project = (FreeStyleProject) jenkins
.getItem("Test1");
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
final FreeStyleProject project = (FreeStyleProject) j.jenkins.getItem("Test1");
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
}

@LocalData
@Test
public void testBuildWithoutHistoryEntries() throws Exception {
final FreeStyleProject project = (FreeStyleProject) jenkins
.getItem("Test2");
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
final FreeStyleProject project = (FreeStyleProject) j.jenkins.getItem("Test2");
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
}

@PresetData(DataSet.ANONYMOUS_READONLY)
@Test
public void testBadgeConfigurationAnonymous() throws Exception {
final String jobName = "newjob";
final String description = "a description";
final FreeStyleProject project = createFreeStyleProject(jobName);
final FreeStyleProject project = j.createFreeStyleProject(jobName);

assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
Thread.sleep(SLEEP_TIME);
project.setDescription(description);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());

jenkins.setSecurityRealm(createDummySecurityRealm());
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
SecurityContextHolder.getContext().setAuthentication(Jenkins.ANONYMOUS);
shouldPageContainBadge("anonymous");
}

@LocalData
@Test
public void testBadgeConfigurationWithPermissions() throws Exception {
final String jobName = "newjob";
final String description = "a description";
final FreeStyleProject project = createFreeStyleProject(jobName);
final FreeStyleProject project = j.createFreeStyleProject(jobName);

assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
Thread.sleep(SLEEP_TIME);
project.setDescription(description);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());

jenkins.setSecurityRealm(createDummySecurityRealm());
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
webClient.login("configUser");
shouldPageContainBadge("configUser");

Expand All @@ -170,7 +181,7 @@ public void testBadgeConfigurationWithPermissions() throws Exception {
}

private void shouldPageContainBadge(String user) throws Exception {
final JobConfigHistory jch = jenkins.getPlugin(JobConfigHistory.class);
final JobConfigHistory jch = j.jenkins.getPlugin(JobConfigHistory.class);
HtmlPage htmlPage = webClient.goTo("job/newjob");

// default = always
Expand Down Expand Up @@ -205,29 +216,29 @@ private void shouldPageContainBadge(String user) throws Exception {
}
}

@Test
public void testCorrectShowDiffLinkWithSingleChange() throws Exception {
final String jobName = "testjob";
final FreeStyleProject project = createFreeStyleProject(jobName);
final FreeStyleProject project = j.createFreeStyleProject(jobName);
project.setDescription("first description");
Thread.sleep(SLEEP_TIME);

final String secondDescription = "second description";
project.setDescription(secondDescription);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
Thread.sleep(SLEEP_TIME);

final String lastDescription = "last description";
project.setDescription(lastDescription);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());

HtmlPage htmlPage = webClient.goTo("job/" + jobName);
Assert.assertTrue("Page should contain build badge",
htmlPage.asXml().contains("buildbadge.png"));

final HtmlAnchor showDiffLink = (HtmlAnchor) htmlPage
.getElementById("showDiff");
final HtmlAnchor showDiffLink = (HtmlAnchor) htmlPage.getElementById("showDiff");
final HtmlPage showDiffPage = showDiffLink.click();
final String page = showDiffPage.asText();
Assert.assertTrue("ShowDiffFiles page should be reached now",
Expand All @@ -238,16 +249,17 @@ public void testCorrectShowDiffLinkWithSingleChange() throws Exception {
page.contains(lastDescription));
}

@Test
public void testCorrectShowDiffLinkWithMultipleChanges() throws Exception {
final String jobName = "testjob";

final FreeStyleProject project = createFreeStyleProject(jobName);
final FreeStyleProject project = j.createFreeStyleProject(jobName);
project.setDescription("first description");
Thread.sleep(SLEEP_TIME);
final String secondDescription = "second description";
project.setDescription(secondDescription);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
Thread.sleep(SLEEP_TIME);

for (int i = 3; i < 6; i++) {
Expand All @@ -257,7 +269,7 @@ public void testCorrectShowDiffLinkWithMultipleChanges() throws Exception {
final String lastDescription = "last description";
project.setDescription(lastDescription);
Thread.sleep(SLEEP_TIME);
assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());
j.assertBuildStatus(Result.SUCCESS, project.scheduleBuild2(0).get());

HtmlPage htmlPage = webClient.goTo("job/" + jobName);
final HtmlAnchor showDiffLink = (HtmlAnchor) htmlPage
Expand Down
Loading

0 comments on commit 6da487e

Please sign in to comment.