/
AgentOptionsParserTest.java
251 lines (220 loc) · 11.4 KB
/
AgentOptionsParserTest.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
package com.teamscale.jacoco.agent.options;
import com.teamscale.client.JsonUtils;
import com.teamscale.client.ProfilerConfiguration;
import com.teamscale.client.ProfilerRegistration;
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 okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** Tests parsing of the agent's command line options. */
public class AgentOptionsParserTest {
private TeamscaleCredentials teamscaleCredentials;
private final AgentOptionsParser parser = new AgentOptionsParser(new CommandLineLogger(), null, null, null);
private Path configFile;
/** The mock server to run requests against. */
protected MockWebServer mockWebServer;
/** Starts the mock server. */
@BeforeEach
public void setup() throws Exception {
configFile = Paths.get(getClass().getResource("agent.properties").toURI());
mockWebServer = new MockWebServer();
mockWebServer.start();
teamscaleCredentials = new TeamscaleCredentials(mockWebServer.url("/"), "user", "key");
}
/** Shuts down the mock server. */
@AfterEach
public void cleanup() throws Exception {
mockWebServer.shutdown();
}
@Test
public void testUploadMethodRecognition() throws Exception {
assertThat(parser.parse(null).determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.LOCAL_DISK);
assertThat(parser.parse("azure-url=azure.com,azure-key=key").determineUploadMethod()).isEqualTo(
AgentOptions.EUploadMethod.AZURE_FILE_STORAGE);
assertThat(parser.parse(
String.format("%s=%s,%s=%s,%s=%s", ArtifactoryConfig.ARTIFACTORY_URL_OPTION, "http://some_url",
ArtifactoryConfig.ARTIFACTORY_API_KEY_OPTION, "apikey",
ArtifactoryConfig.ARTIFACTORY_PARTITION, "partition")
).determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.ARTIFACTORY);
String basicTeamscaleOptions = "teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p";
assertThat(parser.parse(basicTeamscaleOptions)
.determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.TEAMSCALE_MULTI_PROJECT);
assertThat(parser.parse(basicTeamscaleOptions + ",teamscale-project=proj")
.determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.TEAMSCALE_SINGLE_PROJECT);
assertThat(parser.parse(
basicTeamscaleOptions + ",sap-nwdi-applications=com.package.MyClass:projectId;com.company.Main:project")
.determineUploadMethod())
.isEqualTo(AgentOptions.EUploadMethod.SAP_NWDI_TEAMSCALE);
}
@Test
public void testUploadMethodRecognitionWithTeamscaleProperties() throws Exception {
TeamscaleCredentials credentials = new TeamscaleCredentials(HttpUrl.get("http://localhost"), "user", "key");
AgentOptionsParser parser = new AgentOptionsParser(new CommandLineLogger(), null, null, credentials);
assertThat(parser.parse(null).determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.LOCAL_DISK);
assertThat(parser.parse("azure-url=azure.com,azure-key=key").determineUploadMethod()).isEqualTo(
AgentOptions.EUploadMethod.AZURE_FILE_STORAGE);
assertThat(parser.parse(
String.format("%s=%s,%s=%s,%s=%s", ArtifactoryConfig.ARTIFACTORY_URL_OPTION, "http://some_url",
ArtifactoryConfig.ARTIFACTORY_API_KEY_OPTION, "apikey",
ArtifactoryConfig.ARTIFACTORY_PARTITION, "partition")
).determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.ARTIFACTORY);
String basicTeamscaleOptions = "teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p";
assertThat(parser.parse(basicTeamscaleOptions)
.determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.TEAMSCALE_MULTI_PROJECT);
assertThat(parser.parse(basicTeamscaleOptions + ",teamscale-project=proj")
.determineUploadMethod()).isEqualTo(AgentOptions.EUploadMethod.TEAMSCALE_SINGLE_PROJECT);
assertThat(parser.parse(
basicTeamscaleOptions + ",sap-nwdi-applications=com.package.MyClass:projectId;com.company.Main:project")
.determineUploadMethod())
.isEqualTo(AgentOptions.EUploadMethod.SAP_NWDI_TEAMSCALE);
}
@Test
public void environmentConfigIdOverridesCommandLineOptions() throws Exception {
ProfilerRegistration registration = new ProfilerRegistration();
registration.profilerId = UUID.randomUUID().toString();
registration.profilerConfiguration = new ProfilerConfiguration();
registration.profilerConfiguration.configurationId = "my-config";
registration.profilerConfiguration.configurationOptions = "teamscale-partition=foo";
mockWebServer.enqueue(new MockResponse().setBody(JsonUtils.serialize(registration)));
AgentOptionsParser parser = new AgentOptionsParser(new CommandLineLogger(), "my-config",
null, teamscaleCredentials);
AgentOptions options = parser.parse("teamscale-partition=bar");
assertThat(options.teamscaleServer.partition).isEqualTo("foo");
}
@Test
public void environmentConfigFileOverridesCommandLineOptions() throws Exception {
AgentOptionsParser parser = new AgentOptionsParser(new CommandLineLogger(), null, configFile.toString(),
teamscaleCredentials);
AgentOptions options = parser.parse("teamscale-partition=from-command-line");
assertThat(options.teamscaleServer.partition).isEqualTo("from-config-file");
}
@Test
public void environmentConfigFileOverridesConfigId() throws Exception {
ProfilerRegistration registration = new ProfilerRegistration();
registration.profilerId = UUID.randomUUID().toString();
registration.profilerConfiguration = new ProfilerConfiguration();
registration.profilerConfiguration.configurationId = "my-config";
registration.profilerConfiguration.configurationOptions = "teamscale-partition=from-config-id";
mockWebServer.enqueue(new MockResponse().setBody(JsonUtils.serialize(registration)));
AgentOptionsParser parser = new AgentOptionsParser(new CommandLineLogger(), "my-config", configFile.toString(),
teamscaleCredentials);
AgentOptions options = parser.parse("teamscale-partition=from-command-line");
assertThat(options.teamscaleServer.partition).isEqualTo("from-config-file");
}
@Test
public void notAllRequiredTeamscaleOptionsSet() {
assertThatCode(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p,teamscale-project=proj")
).doesNotThrowAnyException();
assertThatCode(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p")
).doesNotThrowAnyException();
assertThatCode(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token")
).doesNotThrowAnyException();
assertThatThrownBy(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-project=proj")
).hasMessageContaining("You configured a 'teamscale-project' but no 'teamscale-partition' to upload to.");
assertThatThrownBy(
() -> parser.parse("teamscale-server-url=teamscale.com")
).hasMessageContaining("not all required ones");
assertThatThrownBy(
() -> parser.parse("teamscale-server-url=teamscale.com,teamscale-user=user")
).hasMessageContaining("not all required ones");
assertThatThrownBy(
() -> parser.parse("teamscale-server-url=teamscale.com,teamscale-access-token=token")
).hasMessageContaining("not all required ones");
assertThatThrownBy(
() -> parser.parse("teamscale-user=user,teamscale-access-token=token")
).hasMessageContaining("not all required ones");
assertThatThrownBy(
() -> parser.parse("teamscale-revision=1234")
).hasMessageContaining("not all required ones");
assertThatThrownBy(
() -> parser.parse("teamscale-commit=master:1234")
).hasMessageContaining("not all required ones");
}
@Test
public void sapNwdiRequiresAllTeamscaleOptionsExceptProject() {
assertThatThrownBy(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,sap-nwdi-applications=com.package.MyClass:projectId;com.company.Main:project")
).hasMessageContaining(
"You provided an SAP NWDI applications config, but the 'teamscale-' upload options are incomplete");
assertThatThrownBy(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p,teamscale-project=proj,sap-nwdi-applications=com.package.MyClass:projectId;com.company.Main:project")
).hasMessageContaining(
"The project must be specified via sap-nwdi-applications");
assertThatThrownBy(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p,teamscale-project=proj,sap-nwdi-applications=com.package.MyClass:projectId;com.company.Main:project")
).hasMessageContaining(
"You provided an SAP NWDI applications config and a teamscale-project")
.hasMessageNotContaining("the 'teamscale-' upload options are incomplete");
}
@Test
public void revisionOrCommitRequireProject() {
assertThatThrownBy(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p,teamscale-revision=12345")
).hasMessageContaining("you did not provide the 'teamscale-project'");
assertThatThrownBy(
() -> parser.parse(
"teamscale-server-url=teamscale.com,teamscale-user=user,teamscale-access-token=token,teamscale-partition=p,teamscale-commit=master:HEAD")
).hasMessageContaining("you did not provide the 'teamscale-project'");
}
@Test
public void environmentConfigIdDoesNotExist() {
mockWebServer.enqueue(new MockResponse().setResponseCode(404).setBody("invalid-config-id does not exist"));
assertThatThrownBy(
() -> new AgentOptionsParser(new CommandLineLogger(), "invalid-config-id", null,
teamscaleCredentials).parse(
"")
).isInstanceOf(AgentOptionParseException.class).hasMessageContaining("invalid-config-id does not exist");
}
@Test
public void notGivingAnyOptionsShouldBeOK() throws Exception {
parser.parse("");
parser.parse(null);
}
@Test
public void mustPreserveDefaultExcludes() throws Exception {
assertThat(parser.parse("").jacocoExcludes).isEqualTo(AgentOptions.DEFAULT_EXCLUDES);
assertThat(parser.parse("excludes=**foo**").jacocoExcludes)
.isEqualTo("**foo**:" + AgentOptions.DEFAULT_EXCLUDES);
}
@Test
public void teamscalePropertiesCredentialsUsedAsDefaultButOverridable() throws Exception {
AgentOptionsParser parser = new AgentOptionsParser(new CommandLineLogger(), null, null, teamscaleCredentials);
assertThat(parser.parse("teamscale-project=p,teamscale-partition=p").teamscaleServer.userName).isEqualTo(
"user");
assertThat(parser.parse(
"teamscale-project=p,teamscale-partition=p,teamscale-user=user2").teamscaleServer.userName).isEqualTo(
"user2");
}
/**
* Delete created coverage folders
*/
@AfterAll
public static void teardown() throws IOException {
TestUtils.cleanAgentCoverageDirectory();
}
}