-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
28 deletions.
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
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
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,85 @@ | ||
import com.cloudbees.groovy.cps.impl.CpsCallableInvocation; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.lesfurets.jenkins.unit.BasePipelineTest; | ||
import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS; | ||
import com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration; | ||
import groovy.lang.Closure; | ||
import groovy.lang.Script; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.lesfurets.jenkins.unit.MethodSignature.method; | ||
import static com.lesfurets.jenkins.unit.global.lib.GitSource.gitSource; | ||
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library; | ||
import static com.lesfurets.jenkins.unit.global.lib.LocalSource.localSource; | ||
|
||
public class TestJenkinsfile extends BasePipelineTestCPS { | ||
//public class TestJenkinsfile extends BasePipelineTest { | ||
|
||
private static final String LIB_PATH = "target/libs"; | ||
|
||
@Before | ||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
LibraryConfiguration library = library() | ||
.name("github.com/zanata/zanata-pipeline-library") | ||
.retriever(gitSource("https://github.com/zanata/zanata-pipeline-library")) | ||
// uncomment to use already-downloaded (perhaps modified) copy instead of git: | ||
// .retriever(localSource(LIB_PATH)) | ||
.targetPath(LIB_PATH) | ||
.defaultVersion("master") | ||
.allowOverride(true) | ||
.implicit(false) | ||
.build(); | ||
getHelper().registerSharedLibrary(library); | ||
|
||
// set up mock methods | ||
getHelper().registerAllowedMethod("timestamps", ImmutableList.of(Closure.class), null); | ||
getHelper().registerAllowedMethod("wrap", ImmutableList.of(Map.class, Closure.class), null); | ||
getHelper().registerAllowedMethod("hipchatSend", ImmutableList.of(Map.class), null); | ||
getHelper().registerAllowedMethod("junit", ImmutableList.of(Map.class), null); | ||
getHelper().registerAllowedMethod("archive", ImmutableList.of(Map.class), null); | ||
getHelper().registerAllowedMethod("archive", ImmutableList.of(Object.class), null); | ||
getHelper().registerAllowedMethod("stash", ImmutableList.of(Map.class), null); | ||
getHelper().registerAllowedMethod("unstash", ImmutableList.of(Map.class), null); | ||
getHelper().registerAllowedMethod("unstash", ImmutableList.of(String.class), null); | ||
getHelper().registerAllowedMethod(method("findFiles", Map.class), args -> { | ||
return ImmutableList.of(); | ||
}); | ||
|
||
// environment variables | ||
Map<String, String> env = new HashMap<>(); | ||
env.put("BUILD_URL", "http://example.com/job/JobName/123"); | ||
env.put("JOB_NAME", "JobName"); | ||
env.put("BUILD_NUMBER", "123"); | ||
env.put("DEFAULT_NODE", "master"); | ||
env.put("NODE_NAME", "jenkins-pipeline-unit"); | ||
|
||
// global variables | ||
getBinding().setProperty("env", env); | ||
getBinding().setProperty("LABEL", "master"); | ||
getBinding().setProperty("scm", ImmutableMap.of()); | ||
getBinding().setProperty("currentBuild", new HashMap<>()); | ||
} | ||
|
||
@Test | ||
public void should_execute_without_errors() throws Exception { | ||
|
||
try { | ||
Script script = loadScript("../Jenkinsfile"); | ||
printCallStack(); | ||
} catch (CpsCallableInvocation e) { | ||
// if the script fails, we need the call stack to tell us where the problem is | ||
// (CpsCallableInvocation tells us very little) | ||
System.err.println("CPS call stack:"); | ||
getHelper().getCallStack().forEach(System.err::println); | ||
throw e; | ||
} | ||
} | ||
} |