-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTestEventHandlerStrategyBase.java
More file actions
143 lines (127 loc) · 6.47 KB
/
TestEventHandlerStrategyBase.java
File metadata and controls
143 lines (127 loc) · 6.47 KB
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
package com.teamscale.jacoco.agent.testimpact;
import com.teamscale.client.ClusteredTestDetails;
import com.teamscale.client.HttpUtils;
import com.teamscale.client.PrioritizableTestCluster;
import com.teamscale.client.TeamscaleClient;
import com.teamscale.jacoco.agent.JacocoRuntimeController;
import com.teamscale.jacoco.agent.logging.LoggingUtils;
import com.teamscale.jacoco.agent.options.AgentOptions;
import com.teamscale.jacoco.agent.upload.teamscale.TeamscaleConfig;
import com.teamscale.report.testwise.jacoco.cache.CoverageGenerationException;
import com.teamscale.report.testwise.model.TestExecution;
import com.teamscale.report.testwise.model.TestInfo;
import org.slf4j.Logger;
import retrofit2.Response;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/** Base class for strategies to handle test events. */
public abstract class TestEventHandlerStrategyBase {
private final Logger logger = LoggingUtils.getLogger(this);
/** Controls the JaCoCo runtime. */
protected final JacocoRuntimeController controller;
/** The timestamp at which the /test/start endpoint has been called last time. */
private long startTimestamp = -1;
/** The options the user has configured for the agent. */
protected final AgentOptions agentOptions;
/** May be null if the user did not configure Teamscale. */
protected final TeamscaleClient teamscaleClient;
protected TestEventHandlerStrategyBase(AgentOptions agentOptions, JacocoRuntimeController controller) {
this.controller = controller;
this.agentOptions = agentOptions;
this.teamscaleClient = agentOptions.createTeamscaleClient(true);
}
/** Called when test test with the given name is about to start. */
public void testStart(String test) {
logger.debug("Test {} started", test);
// Reset coverage so that we only record coverage that belongs to this particular test case.
controller.reset();
controller.setSessionId(test);
startTimestamp = System.currentTimeMillis();
}
/**
* Called when the test with the given name finished.
*
* @param test Uniform path of the test
* @param testExecution A test execution object holding the test result and error message. May be null if none is
* given in the request.
* @return The body of the response. <code>null</code> indicates "204 No content". Non-null results will be treated
* as a json response.
*/
public TestInfo testEnd(String test,
TestExecution testExecution) throws JacocoRuntimeController.DumpException, CoverageGenerationException {
if (testExecution != null) {
testExecution.uniformPath = test;
if (startTimestamp != -1) {
long endTimestamp = System.currentTimeMillis();
testExecution.setDurationMillis(endTimestamp - startTimestamp);
}
}
logger.debug("Test {} ended with test execution {}", test, testExecution);
return null;
}
/**
* Retrieves impacted tests from Teamscale, if a {@link #teamscaleClient} has been configured.
*
* @param availableTests List of all available tests that could be run or null if the user does not want to
* provide one.
* @param includeNonImpactedTests If this is true, only performs prioritization, no selection.
* @param baseline Optional baseline for the considered changes.
* @throws IOException if the request to Teamscale failed.
* @throws UnsupportedOperationException if the user did not properly configure the {@link #teamscaleClient}.
*/
public List<PrioritizableTestCluster> testRunStart(List<ClusteredTestDetails> availableTests,
boolean includeNonImpactedTests,
boolean includeAddedTests, boolean includeFailedAndSkipped,
String baseline, String baselineRevision) throws IOException {
int availableTestCount = 0;
if (availableTests != null) {
availableTestCount = availableTests.size();
}
logger.debug("Test run started with {} available tests. baseline = {}, includeNonImpactedTests = {}",
availableTestCount, baseline, includeNonImpactedTests);
validateConfiguration();
Response<List<PrioritizableTestCluster>> response = teamscaleClient
.getImpactedTests(availableTests, baseline, baselineRevision,
agentOptions.getTeamscaleServerOptions().commit,
agentOptions.getTeamscaleServerOptions().revision,
agentOptions.getTeamscaleServerOptions().repository,
Collections.singletonList(agentOptions.getTeamscaleServerOptions().partition),
includeNonImpactedTests, includeAddedTests, includeFailedAndSkipped);
if (response.isSuccessful()) {
List<PrioritizableTestCluster> prioritizableTestClusters = response.body();
logger.debug("Teamscale suggested these tests: {}", prioritizableTestClusters);
return prioritizableTestClusters;
} else {
String responseBody = HttpUtils.getErrorBodyStringSafe(response);
throw new IOException(
"Request to Teamscale to get impacted tests failed with HTTP status " + response.code() +
" " + response.message() + ". Response body: " + responseBody);
}
}
private void validateConfiguration() {
if (teamscaleClient == null) {
throw new UnsupportedOperationException("You did not configure a connection to Teamscale in the agent." +
" Thus, you cannot use the agent to retrieve impacted tests via the testrun/start REST endpoint." +
" Please use the 'teamscale-' agent parameters to configure a Teamscale connection.");
}
if (!agentOptions.getTeamscaleServerOptions().hasCommitOrRevision()) {
throw new UnsupportedOperationException(
"You must provide a revision or commit via the agent's '" + TeamscaleConfig.TEAMSCALE_REVISION_OPTION + "', '" +
TeamscaleConfig.TEAMSCALE_REVISION_MANIFEST_JAR_OPTION + "', '" + TeamscaleConfig.TEAMSCALE_COMMIT_OPTION +
"', '" + TeamscaleConfig.TEAMSCALE_COMMIT_MANIFEST_JAR_OPTION + "' or '" +
AgentOptions.GIT_PROPERTIES_JAR_OPTION + "' option." +
" Auto-detecting the git.properties does not work since we need the commit before any code" +
" has been profiled in order to obtain the prioritized test cases from the TIA.");
}
}
/**
* Signals that the test run has ended. Strategies that support this can upload a report via the
* {@link #teamscaleClient} here.
*/
public void testRunEnd(boolean partial) throws IOException, CoverageGenerationException {
throw new UnsupportedOperationException("You configured the agent in a mode that does not support uploading " +
"reports to Teamscale. Please configure 'tia-mode=teamscale-upload' or simply don't call" +
"POST /testrun/end.");
}
}