/
AgentOptionsTest.java
360 lines (322 loc) · 16.9 KB
/
AgentOptionsTest.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.teamscale.jacoco.agent.options;
import com.teamscale.client.CommitDescriptor;
import com.teamscale.client.TeamscaleServer;
import com.teamscale.jacoco.agent.upload.artifactory.ArtifactoryConfig;
import com.teamscale.jacoco.agent.util.TestUtils;
import com.teamscale.report.util.CommandLineLogger;
import okhttp3.HttpUrl;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.function.Predicate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** Tests the {@link AgentOptions}. */
public class AgentOptionsTest {
@TempDir
public File testFolder;
@SuppressWarnings("ResultOfMethodCallIgnored")
@BeforeEach
public void setUp() throws IOException {
new File(testFolder, "file_with_manifest1.jar").createNewFile();
new File(testFolder, "plugins/inner").mkdirs();
new File(testFolder, "plugins/some_other_file.jar").createNewFile();
new File(testFolder, "plugins/file_with_manifest2.jar").createNewFile();
}
/** Tests include pattern matching. */
@Test
public void testIncludePatternMatching() throws Exception {
assertThat(includeFilter("com.*")).accepts("file.jar@com/foo/Bar.class", "file.jar@com/foo/Bar$Goo.class",
"file1.jar@goo/file2.jar@com/foo/Bar.class", "com/foo/Bar.class", "com.foo/Bar.class");
assertThat(includeFilter("com.*")).rejects("foo/com/Bar.class", "com.class", "file.jar@com.class",
"A$com$Bar.class");
assertThat(includeFilter("*com.*")).accepts("file.jar@com/foo/Bar.class", "file.jar@com/foo/Bar$Goo.class",
"file1.jar@goo/file2.jar@com/foo/Bar.class", "com/foo/Bar.class", "foo/com/goo/Bar.class",
"A$com$Bar.class", "src/com/foo/Bar.class");
assertThat(includeFilter("*com.*;*de.*"))
.accepts("file.jar@com/foo/Bar.class", "file.jar@de/foo/Bar$Goo.class");
assertThat(excludeFilter("*com.*;*de.*"))
.rejects("file.jar@com/foo/Bar.class", "file.jar@de/foo/Bar$Goo.class");
assertThat(includeFilter("*com.customer.*")).accepts(
"C:\\client-daily\\client\\plugins\\com.customer.something.client_1.2.3.4.1234566778.jar@com/customer/something/SomeClass.class");
}
/** Interval options test. */
@Test
public void testIntervalOptions() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("");
assertThat(agentOptions.getDumpIntervalInMinutes()).isEqualTo(480);
agentOptions = getAgentOptionsParserWithDummyLogger().parse("interval=0");
assertThat(agentOptions.shouldDumpInIntervals()).isEqualTo(false);
agentOptions = getAgentOptionsParserWithDummyLogger().parse("interval=30");
assertThat(agentOptions.shouldDumpInIntervals()).isEqualTo(true);
assertThat(agentOptions.getDumpIntervalInMinutes()).isEqualTo(30);
}
/** Tests the options for uploading coverage to teamscale. */
@Test
public void testTeamscaleUploadOptions() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("" +
"teamscale-server-url=127.0.0.1," +
"teamscale-project=test," +
"teamscale-user=build," +
"teamscale-access-token=token," +
"teamscale-partition=\"Unit Tests\"," +
"teamscale-commit=default:HEAD," +
"teamscale-message=\"This is my message\"");
TeamscaleServer teamscaleServer = agentOptions.getTeamscaleServerOptions();
assertThat(teamscaleServer.url.toString()).isEqualTo("http://127.0.0.1/");
assertThat(teamscaleServer.project).isEqualTo("test");
assertThat(teamscaleServer.userName).isEqualTo("build");
assertThat(teamscaleServer.userAccessToken).isEqualTo("token");
assertThat(teamscaleServer.partition).isEqualTo("Unit Tests");
assertThat(teamscaleServer.commit.toString()).isEqualTo("default:HEAD");
assertThat(teamscaleServer.getMessage()).isEqualTo("This is my message");
}
/** Tests the options for the Test Impact mode. */
@Test
public void testHttpServerOptions() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("mode=TESTWISE,class-dir=.," +
"http-server-port=8081");
assertThat(agentOptions.getHttpServerPort()).isEqualTo(8081);
}
/** Tests the options http-server-port option for normal mode. */
@Test
public void testHttpServerOptionsForNormalMode() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("http-server-port=8081");
assertThat(agentOptions.getHttpServerPort()).isEqualTo(8081);
}
/** Tests the options for the Test Impact mode. */
@Test
public void testEnvironmentVariableOptions() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("mode=TESTWISE,class-dir=.," +
"test-env=TEST");
assertThat(agentOptions.getTestEnvironmentVariableName()).isEqualTo("TEST");
}
/** Tests the options for the Test Impact mode. */
@Test
public void testHttpServerOptionsWithCoverageViaHttp() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("mode=TESTWISE,class-dir=.," +
"http-server-port=8081,tia-mode=http");
assertThat(agentOptions.getHttpServerPort()).isEqualTo(8081);
assertThat(agentOptions.getTestwiseCoverageMode()).isEqualTo(ETestwiseCoverageMode.HTTP);
}
/** Tests setting ignore-uncovered-classes works. */
@Test
public void testIgnoreUncoveredClasses() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("ignore-uncovered-classes=true");
assertTrue(agentOptions.shouldIgnoreUncoveredClasses());
}
/** Tests default for ignore-uncovered-classes is false. */
@Test
public void testIgnoreUncoveredClassesDefault() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("");
assertFalse(agentOptions.shouldIgnoreUncoveredClasses());
}
/** Tests default for ignore-uncovered-classes is false. */
@Test
public void shouldAllowMinusForEnumConstants() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("tia-mode=exec-file");
assertThat(agentOptions.getTestwiseCoverageMode()).isEqualTo(ETestwiseCoverageMode.EXEC_FILE);
}
/** Tests that supplying both revision and commit info is forbidden. */
@Test
public void testBothRevisionAndCommitSupplied() throws URISyntaxException {
String message = "'teamscale-revision' and 'teamscale-revision-manifest-jar' are incompatible with "
+ "'teamscale-commit' and 'teamscale-commit-manifest-jar'.";
File jar = new File(getClass().getResource("manifest-and-git-properties.jar").toURI());
assertThatThrownBy(
() -> getAgentOptionsParserWithDummyLogger().parse(
"teamscale-revision=1234,teamscale-commit=master:1000"))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(message);
assertThatThrownBy(
() -> getAgentOptionsParserWithDummyLogger().parse(
"teamscale-revision=1234,teamscale-commit-manifest-jar=" + jar.getAbsolutePath()))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(message);
assertThatThrownBy(
() -> getAgentOptionsParserWithDummyLogger().parse(
"teamscale-revision-manifest-jar=" + jar.getAbsolutePath() + ",teamscale-commit=master:1000"))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(message);
assertThatThrownBy(
() -> getAgentOptionsParserWithDummyLogger().parse(
"teamscale-revision-manifest-jar=" + jar.getAbsolutePath() + ",teamscale-commit-manifest-jar=" + jar.getAbsolutePath()))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(message);
}
/** Tests the 'teamscale-revision-manifest-jar' option correctly parses the 'Git_Commit' field in the manifest. */
@Test
public void testTeamscaleRevisionManifestJarOption() throws Exception {
File jar = new File(getClass().getResource("manifest-with-git-commit-revision.jar").toURI());
AgentOptions options = getAgentOptionsParserWithDummyLogger().parse(
"teamscale-revision-manifest-jar=" + jar.getAbsolutePath() + ",teamscale-server-url=ts.com,teamscale-user=u,teamscale-access-token=t,teamscale-project=p,teamscale-partition=p");
assertThat(options.getTeamscaleServerOptions().revision).isEqualTo("f364d58dc4966ca856260185e46a90f80ee5e9c6");
}
/**
* Tests that an exception is thrown when the user attempts to specify the commit/revision when 'teamscale-project'
* is not specified. The absence of the `teamscale-project` implies a multi-project upload, so the commit/revision
* have to be provided individually via the 'git.properties' file.
*/
@Test
public void testNoCommitOrRevisionGivenWhenProjectNull() throws Exception {
String message = "You tried to provide a commit to upload to directly." +
" This is not possible, since you did not provide the 'teamscale-project' to upload to";
assertThatThrownBy(
() -> getAgentOptionsParserWithDummyLogger().parse(
"teamscale-server-url=127.0.0.1," +
"teamscale-user=build," +
"teamscale-access-token=token," +
"teamscale-partition=\"Unit Tests\"," +
"teamscale-commit=default:HEAD," +
"teamscale-message=\"This is my message\""))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(message);
assertThatThrownBy(
() -> getAgentOptionsParserWithDummyLogger().parse(
"teamscale-server-url=127.0.0.1," +
"teamscale-user=build," +
"teamscale-access-token=token," +
"teamscale-partition=\"Unit Tests\"," +
"teamscale-revision=1234ABCD," +
"teamscale-message=\"This is my message\""))
.isInstanceOf(AgentOptionParseException.class).hasMessageContaining(message);
}
/**
* Test that agent continues to run if the user provided an invalid path via the
* {@link AgentOptions#GIT_PROPERTIES_JAR_OPTION} jar option.
*/
@Test
public void testGitPropertiesJarOptionWithNonExistentFileDoesNotFailBadly() throws Exception {
File jarFile = new File(getClass().getResource("nested-jar.war").getFile());
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse(
AgentOptions.GIT_PROPERTIES_JAR_OPTION + "=doesNotExist" + File.separator + jarFile.getAbsolutePath());
assertThat(agentOptions.gitPropertiesJar).isNull();
}
/** Test that the {@link AgentOptions#GIT_PROPERTIES_JAR_OPTION} option can be parsed correctly */
@Test
public void testGitPropertiesJarOptionParsedCorrectly() throws Exception {
File jarFile = new File(getClass().getResource("nested-jar.war").getFile());
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse(
AgentOptions.GIT_PROPERTIES_JAR_OPTION + "=" + jarFile.getAbsolutePath());
assertThat(agentOptions.gitPropertiesJar).isNotNull();
}
/**
* Test that agent continues to run if the user provided a folder via the
* {@link AgentOptions#GIT_PROPERTIES_JAR_OPTION} jar option.
*/
@Test
public void testGitPropertiesJarDoesNotAcceptFolders() throws Exception {
File jarFile = new File(getClass().getResource("nested-jar.war").getFile());
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse(
AgentOptions.GIT_PROPERTIES_JAR_OPTION + "=" + jarFile.getParent());
assertThat(agentOptions.gitPropertiesJar).isNull();
}
/** Tests that supplying version info is supported in Testwise mode. */
@Test
public void testVersionInfosInTestwiseMode() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("mode=TESTWISE,class-dir=.," +
"http-server-port=8081,teamscale-revision=1234,teamscale-server-url=ts.com,teamscale-user=u,teamscale-access-token=t,teamscale-project=p,teamscale-partition=p");
assertThat(agentOptions.getTeamscaleServerOptions().revision).isEqualTo("1234");
agentOptions = getAgentOptionsParserWithDummyLogger().parse("mode=TESTWISE,class-dir=.," +
"http-server-port=8081,teamscale-commit=branch:1234,teamscale-server-url=ts.com,teamscale-user=u,teamscale-access-token=t,teamscale-project=p,teamscale-partition=p");
assertThat(agentOptions.getTeamscaleServerOptions().commit).isEqualTo(CommitDescriptor.parse("branch:1234"));
}
/** Tests the options for azure file storage upload. */
@Test
public void testAzureFileStorageOptions() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("" +
"azure-url=https://mrteamscaleshdev.file.core.windows.net/tstestshare/," +
"azure-key=Ut0BQ2OEvgQXGnNJEjxnaEULAYgBpAK9+HukeKSzAB4CreIQkl2hikIbgNe4i+sL0uAbpTrFeFjOzh3bAtMMVg==");
assertThat(agentOptions.azureFileStorageConfig.url.toString())
.isEqualTo("https://mrteamscaleshdev.file.core.windows.net/tstestshare/");
assertThat(agentOptions.azureFileStorageConfig.accessKey).isEqualTo(
"Ut0BQ2OEvgQXGnNJEjxnaEULAYgBpAK9+HukeKSzAB4CreIQkl2hikIbgNe4i+sL0uAbpTrFeFjOzh3bAtMMVg==");
}
/** Tests the options for SAP NWDI applications. */
@Test
public void testValidSapNwdiOptions() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("" +
"teamscale-server-url=http://your.teamscale.url," +
"teamscale-user=your-user-name," +
"teamscale-access-token=your-access-token," +
"teamscale-partition=Manual Tests," +
"sap-nwdi-applications=com.example:project;com.test:p2");
assertThat(agentOptions.sapNetWeaverJavaApplications)
.isNotNull()
.satisfies(sap -> {
assertThat(sap).hasSize(2);
assertThat(sap).element(0).satisfies(application -> {
assertThat(application.markerClass).isEqualTo("com.example");
assertThat(application.teamscaleProject).isEqualTo("project");
});
assertThat(sap).element(1).satisfies(application -> {
assertThat(application.markerClass).isEqualTo("com.test");
assertThat(application.teamscaleProject).isEqualTo("p2");
});
});
assertThat(agentOptions.teamscaleServer.isConfiguredForMultiProjectUpload()).isTrue();
assertThat(agentOptions.teamscaleServer.isConfiguredForSingleProjectTeamscaleUpload()).isFalse();
}
/**
* Tests successful parsing of the {@link ArtifactoryConfig#ARTIFACTORY_API_KEY_OPTION}
*/
@Test
public void testArtifactoryApiKeyOptionIsCorrectlyParsed() throws Exception {
String someArtifactoryApiKey = "some_api_key";
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse(
String.format("%s=%s,%s=%s,%s=%s", ArtifactoryConfig.ARTIFACTORY_URL_OPTION, "http://some_url",
ArtifactoryConfig.ARTIFACTORY_API_KEY_OPTION, someArtifactoryApiKey,
ArtifactoryConfig.ARTIFACTORY_PARTITION, "partition"));
assertThat(agentOptions.artifactoryConfig.apiKey).isEqualTo(someArtifactoryApiKey);
}
/**
* Tests that setting {@link ArtifactoryConfig#ARTIFACTORY_USER_OPTION} and
* {@link ArtifactoryConfig#ARTIFACTORY_PASSWORD_OPTION} (along with
* {@link ArtifactoryConfig#ARTIFACTORY_URL_OPTION}) passes the AgentOptions' validity check.
*/
@Test
public void testArtifactoryBasicAuthSetPassesValiditiyCheck() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("");
agentOptions.artifactoryConfig.url = HttpUrl.get("http://some_url");
agentOptions.artifactoryConfig.user = "user";
agentOptions.artifactoryConfig.password = "password";
agentOptions.artifactoryConfig.partition = "partition";
assertThat(agentOptions.getValidator().isValid()).isTrue();
}
/**
* Tests that setting {@link ArtifactoryConfig#ARTIFACTORY_USER_OPTION} and
* {@link ArtifactoryConfig#ARTIFACTORY_API_KEY_OPTION} (along with
* {@link ArtifactoryConfig#ARTIFACTORY_URL_OPTION}) passes the AgentOptions' validity check.
*/
@Test
public void testArtifactoryApiKeySetPassesValidityCheck() throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger().parse("");
agentOptions.artifactoryConfig.url = HttpUrl.get("http://some_url");
agentOptions.artifactoryConfig.apiKey = "api_key";
agentOptions.artifactoryConfig.partition = "partition";
assertThat(agentOptions.getValidator().isValid()).isTrue();
}
/** Returns the include filter predicate for the given filter expression. */
private static Predicate<String> includeFilter(String filterString) throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger()
.parse("includes=" + filterString);
return string -> agentOptions.getLocationIncludeFilter().isIncluded(string);
}
/** Returns the include filter predicate for the given filter expression. */
private static Predicate<String> excludeFilter(String filterString) throws Exception {
AgentOptions agentOptions = getAgentOptionsParserWithDummyLogger()
.parse("excludes=" + filterString);
return string -> agentOptions.getLocationIncludeFilter().isIncluded(string);
}
private static AgentOptionsParser getAgentOptionsParserWithDummyLogger() {
return new AgentOptionsParser(new CommandLineLogger(), null, null, null);
}
/**
* Delete created coverage folders
*/
@AfterAll
public static void teardown() throws IOException {
TestUtils.cleanAgentCoverageDirectory();
}
}