-
Notifications
You must be signed in to change notification settings - Fork 51
/
TestJenkinsfile.java
180 lines (162 loc) · 8.24 KB
/
TestJenkinsfile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.groovy.runtime.MethodClosure;
import org.junit.Before;
import org.junit.Test;
import com.cloudbees.groovy.cps.impl.CpsCallableInvocation;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS;
import com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration;
import groovy.lang.Closure;
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 java.lang.Boolean.TRUE;
import static org.assertj.core.api.Assertions.assertThat;
// try 'extends BasePipelineTest' for debugging in case of weird Groovy exceptions
public class TestJenkinsfile extends BasePipelineTestCPS {
private static final String LIB_PATH = "target/libs";
@Before
@Override
public void setUp() throws Exception {
super.setUp();
LibraryConfiguration library = library()
.name("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 (Note: built-ins are in BasePipelineTest.setUp)
getHelper().registerAllowedMethod("archive", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("archive", ImmutableList.of(Object.class), null);
getHelper().registerAllowedMethod("hipchatSend", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("junit", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("lock", ImmutableList.of(Map.class, Closure.class), null);
getHelper().registerAllowedMethod("lock", ImmutableList.of(String.class, Closure.class), null);
getHelper().registerAllowedMethod("milestone", ImmutableList.of(), null);
getHelper().registerAllowedMethod("stash", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("timestamps", ImmutableList.of(Closure.class), null);
getHelper().registerAllowedMethod("unstash", ImmutableList.of(Map.class), null);
getHelper().registerAllowedMethod("unstash", ImmutableList.of(String.class), null);
getHelper().registerAllowedMethod("withEnv", ImmutableList.of(List.class, Closure.class), null);
getHelper().registerAllowedMethod("wrap", ImmutableList.of(Map.class, Closure.class), null);
getHelper().registerAllowedMethod(method("findFiles", Map.class), args -> {
String glob = ((Map)args).get("glob").toString();
if (glob.equals("**/target/surefire-reports/TEST-*.xml")) {
return new File[]{new File("server/zanata-war/target/surefire-reports/TEST-org.zanata.xml.StreamSerializerTest.xml")};
}
if (glob.equals("**/target/failsafe-reports/TEST-*.xml")) {
return new File[]{new File("server/functional-test/target/failsafe-reports/TEST-org.zanata.feature.testharness.BasicAcceptanceTestSuite.xml")};
}
throw new RuntimeException("Unmocked invocation");
});
getHelper().registerAllowedMethod(method("sh", Map.class),
SH.INSTANCE);
// PipelineUnit(withCredentialsInterceptor) can't handle a List<Map>
// TODO for some reason the steps inside closure.call() are not shown as nested
getHelper().registerAllowedMethod("withCredentials",
ImmutableList.of(List.class, Closure.class),
new Closure(null) {
@Override
public Object call(Object... args) {
Closure closure = (Closure) args[1];
return closure.call();
}
});
// 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("BRANCH_NAME", "PR-456");
env.put("BUILD_NUMBER", "123");
env.put("EXECUTOR_NUMBER", "1");
env.put("DEFAULT_NODE", "master");
env.put("NODE_NAME", "jenkins-pipeline-unit");
// these steps will be passed by reference to library methods
Map<String, Closure> steps = new HashMap<>();
steps.put("hipchatSend", Closure.IDENTITY);
steps.put("echo", Closure.IDENTITY);
steps.put("emailext", Closure.IDENTITY);
steps.put("emailextrecipients", Closure.IDENTITY);
steps.put("library", Closure.IDENTITY);
steps.put("sh", SH.INSTANCE);
steps.put("step", Closure.IDENTITY);
// we need this for CPS mode
MethodClosure.ALLOW_RESOLVE = true;
// global variables
getBinding().setProperty("env", env);
getBinding().setProperty("steps", steps);
getBinding().setProperty("params", ImmutableMap.of("LABEL", "master"));
getBinding().setProperty("LABEL", "master");
// these objects are just used as parameters
getBinding().setProperty("scm", ImmutableMap.of());
getBinding().setProperty("manager", ImmutableMap.of());
}
static class SH<T, R> extends Closure<R> {
static final Closure INSTANCE = new SH();
SH() {
super(null);
}
@SuppressWarnings("unused")
protected Object doCall(T args) {
if (args instanceof String) return 0;
@SuppressWarnings("unchecked")
Map<String, ?> a = (Map<String, ?>) args;
if (TRUE.equals(a.get("returnStdout"))) {
String script = a.get("script").toString();
if (script.endsWith("allocate-jboss-ports")) {
return "JBOSS_HTTP_PORT=51081\nSMTP_PORT=34765\n";
}
if (script.startsWith("git ls-remote")) {
// ScmGit.init in zanata-pipeline-library uses these:
if (script.endsWith("refs/pull/*/head")) {
return "1234567890123456789012345678901234567890 refs/pull/123/head";
} else if (script.endsWith("refs/heads/*")) {
return "fc2b7c527e4401c03bcaf2833739d16e77698ab6 refs/heads/master\n" +
"b0d3e2ff4696f2702f4b4fbac3b59b6cf9a76790 refs/heads/ZNTA-2234-tag" ;
} else {
// Notifier.groovy in zanata-pipeline-library uses this:
return "1234567890123456789012345678901234567890 abcdef\n";
}
}
}
if (TRUE.equals(a.get("returnStatus"))) {
return 0;
}
return 0;
}
}
@Test
public void shouldExecuteWithoutErrors() throws Exception {
try {
// load and execute the Jenkinsfile
runScript("../Jenkinsfile");
printCallStack();
assertJobStatusSuccess();
boolean verified = getHelper().getCallStack()
.stream()
.filter(it -> it.getMethodName().equals("sh") && it.argsToString().contains("mvn"))
// snoop on mvn commands in the stream
// .peek(it -> System.out.printf("%s\n\n", it.argsToString()))
.anyMatch(it -> {
String args = it.argsToString();
return args.contains("-Dappserver=") && args.contains("install");
});
assertThat(verified).isTrue();
} 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;
}
}
}