Skip to content
Permalink
Browse files
Added (unfortunately yet disfunctional) regression test for JENKINS-1…
…1592(cherry picked from commit 8382283)
  • Loading branch information
kutzi authored and vjuranek committed Feb 1, 2012
1 parent 7f69189 commit 2d98f687191a1331783e2f0b2562f3138f457ad7
Showing 2 changed files with 114 additions and 1 deletion.
@@ -597,7 +597,7 @@ protected MavenModuleSet createMavenProject(String name) throws IOException {
return hudson.createProject(MavenModuleSet.class,name);
}

private String createUniqueProjectName() {
protected String createUniqueProjectName() {
return "test"+hudson.getItems().size();
}

@@ -0,0 +1,113 @@
package hudson.model;

import java.io.IOException;
import java.io.StringReader;

import hudson.EnvVars;
import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
import hudson.tasks.Maven.MavenInstallation;
import hudson.util.StreamTaskListener;

import jenkins.model.Jenkins;
import org.junit.Assert;

import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase;

/**
* Tests that getEnvironment() calls outside of builds are safe.
*
* @author kutzi
*/
@Bug(11592)
public class GetEnvironmentOutsideBuildTest extends HudsonTestCase {

private int oldExecNum;

@Override
protected void runTest() throws Throwable {
// Disable tests

// It's unfortunately not working, yet, as whenJenkinsMasterHasNoExecutors is not working as expected
}

public void setUp() throws Exception {
super.setUp();

this.oldExecNum = Jenkins.getInstance().getNumExecutors();
}

public void tearDown() throws Exception {
restoreOldNumExecutors();
super.tearDown();
}

private void restoreOldNumExecutors() throws IOException {
Jenkins.getInstance().setNumExecutors(this.oldExecNum);
Jenkins.getInstance().setNodes(Jenkins.getInstance().getNodes());
Assert.assertNotNull(Jenkins.getInstance().toComputer());
}

private MavenModuleSet createSimpleMavenProject() throws Exception {
MavenModuleSet project = createMavenProject();
MavenInstallation mi = configureMaven3();
project.setScm(new ExtractResourceSCM(getClass().getResource(
"/simple-projects.zip")));
project.setMaven(mi.getName());
project.setGoals("validate");
return project;
}

private void whenJenkinsMasterHasNoExecutors() throws IOException {
Jenkins.getInstance().setNumExecutors(0);
// force update of nodes:
Jenkins.getInstance().setNodes(Jenkins.getInstance().getNodes());
Assert.assertNull(Jenkins.getInstance().toComputer());
}

public void testMaven() throws Exception {
MavenModuleSet m = createSimpleMavenProject();

assertGetEnvironmentCallOutsideBuildWorks(m);
}

public void testFreestyle() throws Exception {
FreeStyleProject project = createFreeStyleProject();

assertGetEnvironmentCallOutsideBuildWorks(project);
}

public void testMatrix() throws Exception {
MatrixProject createMatrixProject = createMatrixProject();

assertGetEnvironmentCallOutsideBuildWorks(createMatrixProject);
}

public void testExternalJob() throws Exception {
ExternalJob p = jenkins.createProject(ExternalJob.class, createUniqueProjectName());
ExternalRun b = p.newBuild();
b.acceptRemoteSubmission(new StringReader(
"<run><log content-encoding='UTF-8'></log><result>0</result><duration>1</duration></run>"
));

assertGetEnvironmentWorks(b);
}

@SuppressWarnings({"rawtypes", "unchecked"})
private void assertGetEnvironmentCallOutsideBuildWorks(AbstractProject job) throws Exception {
AbstractBuild build = buildAndAssertSuccess(job);

assertGetEnvironmentWorks(build);
}

@SuppressWarnings("rawtypes")
private void assertGetEnvironmentWorks(Run build) throws IOException, InterruptedException {
whenJenkinsMasterHasNoExecutors();
// and getEnvironment is called outside of build
EnvVars envVars = build.getEnvironment(StreamTaskListener.fromStdout());
// then it should still succeed - i.e. no NPE o.s.l.t.
assertNotNull(envVars);
}
}

0 comments on commit 2d98f68

Please sign in to comment.