-
Notifications
You must be signed in to change notification settings - Fork 136
/
TckRunner.java
250 lines (219 loc) · 11.1 KB
/
TckRunner.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.tests.tck.ant;
import java.io.File;
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.glassfish.main.tests.tck.ant.io.ZipResolver;
import org.glassfish.main.tests.tck.ant.junit.generated.Error;
import org.glassfish.main.tests.tck.ant.junit.generated.Failure;
import org.glassfish.main.tests.tck.ant.junit.generated.Testcase;
import org.glassfish.main.tests.tck.ant.junit.generated.Testsuite;
import org.glassfish.main.tests.tck.ant.xml.JUnitResultsParser;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import static java.util.concurrent.TimeUnit.MINUTES;
/**
* TCK Runner prepares or resets the workspace, runs the test and stops all servers after
* the test.
*
* @author David Matejcek
*/
public class TckRunner {
private static final Logger LOG = System.getLogger(TckRunner.class.getName());
private static final int MAIL_SMTP_PORT = 3025;
private static final int MAIL_IMAP_PORT = 3143;
private final TckConfiguration cfg;
private GenericContainer<?> mailServer;
private File glassfishZip;
/**
* Just creates this instance.
*
* @param cfg
*/
public TckRunner(final TckConfiguration cfg) {
this.cfg = cfg;
}
/**
* Unpacks the TCK and resolves glassFish zip as maven dependencies.
*/
public void prepareWorkspace() {
LOG.log(Level.INFO, "Preparing workspace at {0}", cfg.getTargetDir());
ZipResolver zipResolver = new ZipResolver(cfg.getTargetDir());
if (cfg.getJakartaeeDir().exists()) {
LOG.log(Level.INFO, "Jakarta EE was already installed, unzipping to {0} skipped.", cfg.getJakartaeeDir());
} else {
zipResolver.unzipDependency("org.glassfish.main.tests.tck", "jakarta-ant-based-tck", cfg.getTckVersion());
cfg.getJakartaeetckCommand().toFile().setExecutable(true);
}
glassfishZip = zipResolver.getZipFile("org.glassfish.main.distributions", "glassfish", cfg.getGlassFishVersion());
}
/**
* Starts the TestContainer's docker container providing a mail server for tests.
* The server can be stopped with other servers by the {@link #stopServers()} method.
*/
// stopped in #stopServers
@SuppressWarnings("resource")
public void startMailServer() {
mailServer = new GenericContainer<>(DockerImageName.parse("greenmail/standalone:1.6.10"))
.waitingFor(Wait.forLogMessage(".*Starting GreenMail standalone.*", 1))
.withEnv("GREENMAIL_OPTS", "-Dgreenmail.setup.test.all -Dgreenmail.hostname=0.0.0.0 -Dgreenmail.auth.disabled")
.withExposedPorts(MAIL_SMTP_PORT, 3110, MAIL_IMAP_PORT, 3465, 3993, 3995, 8080);
mailServer.start();
}
/**
* Starts tests. Expects that the workspace was initialized by the {@link #prepareWorkspace()}
* method or reset by {@link #deleteGeneratedWorkspaceDirs()}, all servers managed by the TCK
* are stopped, mail server was started if it is required by the test by the
* {@link #startMailServer()} method.
* <p>
* Call of this method may take minutes to hours, so be patient.
* <p>
* You should stop all servers by the {@link #stopServers()} method after the test.
*
* @param modulePath path to selected tests
*/
public void start(Path modulePath) throws IOException, InterruptedException {
LOG.log(Level.INFO, "Starting tests of module {0} ...", modulePath);
Process process = startBash(this.cfg.getJakartaeetckCommand().toAbsolutePath() + " " + modulePath);
if (process.waitFor() != 0) {
throw new IllegalStateException("TCK execution ended with exit code " + process.exitValue());
}
final File testReport = toTestReportFile(modulePath);
JUnitResultsParser resultsParser = new JUnitResultsParser();
Testsuite testsuite = resultsParser.parse(testReport);
if (testsuite.getErrors() > 0 || testsuite.getFailures() > 0) {
throw new IllegalStateException(toReport(testsuite));
}
}
/**
* Fixes Jakarta EE issue with stopping what it started - after some types of errors the script
* doesn't stop GlassFish and Derby instances.
* <p>
* Also stops the mailserver if it was started by the {@link #startMailServer()} method.
*/
public void stopServers() throws InterruptedException, IOException {
if (mailServer != null) {
mailServer.stop();
mailServer.close();
mailServer = null;
}
// FIXME: The rest of the method can be removed after merging https://github.com/eclipse-ee4j/jakartaee-tck/pull/1118
Path riPath = cfg.getTargetDir().toPath().resolve("ri");
Path viPath = cfg.getTargetDir().toPath().resolve("vi");
// We don't care about the result here
startBash(viPath.resolve(Path.of("glassfish7", "bin", "asadmin")) + " stop-database").waitFor(1, MINUTES);
startBash(viPath.resolve(Path.of("glassfish7", "bin", "asadmin")) + " stop-domain").waitFor(1, MINUTES);
startBash(riPath.resolve(Path.of("glassfish7", "bin", "asadmin")) + " stop-domain").waitFor(1, MINUTES);
Path derbyDir = viPath.resolve(Path.of("glassfish7", "javadb"));
startBash(cfg.getJdkDirectory().toPath().resolve(Path.of("bin", "java"))
+ " -Dderby.system.home=" + derbyDir.resolve("databases")
+ " -classpath " + derbyDir.resolve(Path.of("lib", "derbynet.jar"))
+ ":" + derbyDir.resolve(Path.of("lib", "derby.jar"))
+ ":" + derbyDir.resolve(Path.of("lib", "derbyshared.jar"))
+ ":" + derbyDir.resolve(Path.of("lib", "derbytools.jar"))
+ " org.apache.derby.drda.NetworkServerControl -h localhost -p 1527 shutdown")
.waitFor(1, MINUTES);
}
/**
* Results are already gzipped and we need to run another module, so we remove generated files
* from previous executions.
*/
public void deleteGeneratedWorkspaceDirs() throws IOException {
deleteDirectory(cfg.getTargetDir().toPath().resolve("ri"));
deleteDirectory(cfg.getTargetDir().toPath().resolve("vi"));
deleteDirectory(new File("/tmp/DerbyDB").toPath());
deleteDirectory(new File(cfg.getTargetDir(), "jakartaeetck-work").toPath());
}
private void deleteDirectory(final Path directory) throws IOException {
if (!directory.toFile().exists()) {
return;
}
LOG.log(Level.INFO, "Deleting directory: {0}", directory);
Files.walk(directory).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
private Process startBash(final String command) throws IOException {
ProcessBuilder bash = new ProcessBuilder("/bin/bash", "-c", command).inheritIO().directory(cfg.getTargetDir());
configureEnvironment(bash.environment());
return bash.start();
}
private void configureEnvironment(Map<String, String> env) {
env.put("LC_ALL", "en_US.UTF-8");
env.put("WORKSPACE", cfg.getTargetDir().getAbsolutePath());
env.put("JAVA_HOME", cfg.getJdkDirectory().getAbsolutePath());
env.put("JDK17_HOME", cfg.getJdkDirectory().getAbsolutePath());
env.put("JDK", "JDK17");
if (mailServer != null) {
env.put("SMTP_PORT", String.valueOf(mailServer.getMappedPort(MAIL_SMTP_PORT)));
env.put("IMAP_PORT", String.valueOf(mailServer.getMappedPort(MAIL_IMAP_PORT)));
}
env.put("HARNESS_DEBUG", String.valueOf(cfg.isHarnessLoggingEnabled()));
env.put("AS_DEBUG", String.valueOf(cfg.isAsadminLoggingEnabled()));
env.put("PATH", cfg.getJdkDirectory().getAbsolutePath() + "/bin:" + cfg.getAntDirectory().getAbsolutePath()
+ "/bin/:/usr/bin");
env.put("CTS_HOME", cfg.getTargetDir().getAbsolutePath());
env.put("TS_HOME", cfg.getJakartaeeDir().getAbsolutePath());
env.put("GF_BUNDLE_ZIP", this.glassfishZip.getAbsolutePath());
env.put("GF_VI_BUNDLE_ZIP", this.glassfishZip.getAbsolutePath());
env.put("GF_HOME_RI", cfg.getTargetDir().getAbsolutePath() + "/ri/glassfish7");
env.put("GF_HOME_VI", cfg.getTargetDir().getAbsolutePath() + "/vi/glassfish7");
env.put("GF_LOGGING_CFG_RI", cfg.getServerLoggingProperties().getAbsolutePath());
env.put("GF_LOGGING_CFG_VI", cfg.getServerLoggingProperties().getAbsolutePath());
env.put("DATABASE", "JavaDB");
env.put("CLIENT_LOGGING_CFG", cfg.getClientLoggingProperties().getAbsolutePath());
LOG.log(Level.DEBUG, "Configured environment: \n{0}", env);
}
private File toTestReportFile(Path modulePath) {
StringBuilder name = new StringBuilder(64);
for (Path path : modulePath) {
if (name.length() > 0) {
name.append('_');
}
name.append(path);
}
name.append("-junit-report.xml");
return new File(cfg.getTargetDir().toPath().resolve(Path.of("results", "junitreports")).toFile(),
name.toString());
}
private String toReport(Testsuite suite) {
StringBuilder report = new StringBuilder();
report.append("Test suite ").append(suite.getName());
report.append(" failed with ").append(suite.getFailures()).append(" failures and ");
report.append(suite.getErrors()).append(" errors.");
List<Testcase> tests = suite.getTestcase();
if (suite.getFailures() > 0) {
Testcase test = tests.stream().filter(tc -> !tc.getFailure().isEmpty()).findFirst().get();
Failure failure = test.getFailure().get(0);
report.append("\nThe first failure is in class ").append(test.getClassname());
report.append(" with this message: \n").append(failure.getMessage());
}
if (suite.getErrors() > 0) {
Testcase test = tests.stream().filter(tc -> !tc.getError().isEmpty()).findFirst().get();
Error error = test.getError().get(0);
report.append("\nThe first error is in class ").append(test.getClassname());
report.append(" with this message: \n").append(error.getMessage());
}
report.append("\nSee logs for more.");
return report.toString();
}
}