Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added (unfortunately yet disfunctional) regression test for JENKINS-1…
- Loading branch information
Showing
2 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
test/src/test/java/hudson/model/GetEnvironmentOutsideBuildTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |