Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding some tests. Fails without a fix of JENKINS-26552.
Originally-Committed-As: 74b51c42dba52952b84b73b978cb562a1bb63ad5
  • Loading branch information
jglick committed Jan 22, 2015
1 parent c37265b commit a3c5129
Showing 1 changed file with 105 additions and 0 deletions.
@@ -0,0 +1,105 @@
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildWrapperDescriptor;
import java.io.File;
import java.io.IOException;
import jenkins.tasks.SimpleBuildWrapper;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;

public class CoreWrapperStepTest {

@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();

@Test public void useWrapper() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node {wrap([$class: 'MockWrapper']) {semaphore 'restarting'; echo \"groovy PATH=${env.PATH}\"; sh 'echo shell PATH=$PATH'}}"));
WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("restarting/1", b);
}
});
story.addStep(new Statement() {
@SuppressWarnings("SleepWhileInLoop")
@Override public void evaluate() throws Throwable {
SemaphoreStep.success("restarting/1", null);
WorkflowJob p = story.j.jenkins.getItemByFullName("p", WorkflowJob.class);
WorkflowRun b = p.getLastBuild();
while (b.isBuilding()) { // TODO JENKINS-26399
Thread.sleep(100);
}
story.j.assertBuildStatusSuccess(b);
String expected = story.j.jenkins.getWorkspaceFor(p).child("bin").getRemote() + File.pathSeparatorChar;
story.j.assertLogContains("groovy PATH=" + expected, b);
story.j.assertLogContains("shell PATH=" + expected, b);
story.j.assertLogContains("ran DisposerImpl", b);
}
});
}

public static class MockWrapper extends SimpleBuildWrapper {
@DataBoundConstructor public MockWrapper() {}
@Override public void setUp(Context context, Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
assertNotNull(initialEnvironment.toString(), initialEnvironment.get("PATH"));
context.env("PATH+STUFF", workspace.child("bin").getRemote());
context.setDisposer(new DisposerImpl());
}
private static final class DisposerImpl extends Disposer {
private static final long serialVersionUID = 1;
@Override public void tearDown(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
listener.getLogger().println("ran DisposerImpl");
}
}
@TestExtension public static class DescriptorImpl extends BuildWrapperDescriptor {
@Override public String getDisplayName() {
return "MockWrapper";
}
@Override public boolean isApplicable(AbstractProject<?,?> item) {
return true;
}
}

}


}

0 comments on commit a3c5129

Please sign in to comment.