-
Notifications
You must be signed in to change notification settings - Fork 112
/
ReportManager.kt
261 lines (228 loc) · 9.09 KB
/
ReportManager.kt
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
package ftl.reports.util
import com.google.common.annotations.VisibleForTesting
import ftl.api.JUnitTest
import ftl.api.RemoteStorage
import ftl.api.ShardChunks
import ftl.api.downloadAsJunitXML
import ftl.api.generateJUnitTestResultFromApi
import ftl.api.parseJUnitLegacyTestResultFromFile
import ftl.api.parseJUnitTestResultFromFile
import ftl.api.uploadToRemoteStorage
import ftl.args.IArgs
import ftl.args.IgnoredTestCases
import ftl.args.IosArgs
import ftl.client.google.GcStorage
import ftl.config.FtlConstants
import ftl.domain.junit.merge
import ftl.domain.junit.mergeTestTimes
import ftl.domain.junit.removeStackTraces
import ftl.json.MatrixMap
import ftl.json.isAllSuccessful
import ftl.presentation.cli.firebase.test.reportmanager.ReportManagerState
import ftl.presentation.publish
import ftl.reports.CostReport
import ftl.reports.FullJUnitReport
import ftl.reports.HtmlErrorReport
import ftl.reports.JUnitReport
import ftl.reports.MatrixResultsReport
import ftl.reports.api.utcDateFormat
import ftl.reports.toXmlString
import ftl.run.common.getMatrixFilePath
import ftl.shard.createTestMethodDurationMap
import ftl.util.Artifacts
import ftl.util.getSmartFlankGCSPathAsFileReference
import ftl.util.resolveLocalRunPath
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.Date
object ReportManager {
/** Returns true if there were no test failures */
fun generate(
matrices: MatrixMap,
args: IArgs,
testShardChunks: ShardChunks,
ignoredTestCases: IgnoredTestCases = listOf()
) {
val testSuite: JUnitTest.Result? = parseTestSuite(matrices, args)
if (args.useLegacyJUnitResult) {
val useFlakyTests = args.flakyTestAttempts > 0
if (useFlakyTests) JUnitDedupe.modify(testSuite)
}
listOf(CostReport, MatrixResultsReport)
.map { it.run(matrices, testSuite, printToStdout = true, args = args) }
if (!matrices.isAllSuccessful()) {
listOf(
HtmlErrorReport
).map { it.run(matrices, testSuite, printToStdout = false, args = args) }
}
JUnitReport.run(
matrices,
testSuite?.apply {
if (ignoredTestCases.isNotEmpty()) {
testsuites?.add(ignoredTestCases.toJunitTestsResults())
}
},
printToStdout = false,
args = args
)
val testsResult = generateJUnitTestResultFromApi((args to matrices).toApiIdentity())
processJunitResults(args, matrices, testSuite, testShardChunks, testsResult)
uploadMatricesId(args, matrices)
}
private fun Pair<IArgs, MatrixMap>.toApiIdentity(): JUnitTest.Result.ApiIdentity {
val (args, matrices) = this
return JUnitTest.Result.ApiIdentity(
args = args,
matrixMap = matrices
)
}
@VisibleForTesting
internal fun processXmlFromFile(
matrices: MatrixMap,
args: IArgs,
parsingFunction: (File) -> JUnitTest.Result
): JUnitTest.Result? = findXmlFiles(matrices, args)
.map { xmlFile -> parsingFunction(xmlFile).updateWebLink(getWebLink(matrices, xmlFile)) }
.reduceOrNull { acc, result -> result.merge(acc) }
private fun findXmlFiles(
matrices: MatrixMap,
args: IArgs
) = File(resolveLocalRunPath(matrices, args))
.walk()
.filter { it.name.matches(Artifacts.testResultRgx) }
.fold(listOf<File>()) { xmlFiles, file -> xmlFiles + file }
private fun getWebLink(matrices: MatrixMap, xmlFile: File): String =
xmlFile.getMatrixPath(matrices.runPath)
?.findMatrixPath(matrices)
?: "".also { ReportManagerState.Warning("Matrix path not found in JSON.").publish() }
private fun String.findMatrixPath(matrices: MatrixMap) = matrices.map.values
.firstOrNull { savedMatrix -> savedMatrix.gcsPath.endsWithTextWithOptionalSlashAtTheEnd(this) }
?.webLink
?: "".also { ReportManagerState.Warning("Matrix path not found in JSON. $this").publish() }
@VisibleForTesting
internal fun String.endsWithTextWithOptionalSlashAtTheEnd(text: String) =
"($text)/*$".toRegex().containsMatchIn(this)
private fun JUnitTest.Result.updateWebLink(
webLink: String
) = apply {
testsuites?.forEach { testSuite ->
testSuite.testcases?.forEach { testCase ->
testCase.webLink = webLink
}
}
}
private fun parseTestSuite(matrices: MatrixMap, args: IArgs): JUnitTest.Result? = when {
// ios supports only legacy parsing
args is IosArgs -> processXmlFromFile(matrices, args, parseJUnitTestResultFromFile)
args.useLegacyJUnitResult -> processXmlFromFile(matrices, args, parseJUnitLegacyTestResultFromFile)
else -> generateJUnitTestResultFromApi((args to matrices).toApiIdentity()).removeStackTraces()
}
@VisibleForTesting
internal fun uploadReportResult(testResult: String, args: IArgs, fileName: String) {
if (args.resultsBucket.isBlank() || args.resultsDir.isBlank() || args.disableResultsUpload) return
uploadToRemoteStorage(
RemoteStorage.Dir(args.resultsBucket, args.resultsDir),
RemoteStorage.Data(fileName, testResult.toByteArray())
)
}
private fun processJunitResults(
args: IArgs,
matrices: MatrixMap,
testSuite: JUnitTest.Result?,
testShardChunks: ShardChunks,
testsResult: JUnitTest.Result
) {
when {
args.fullJUnitResult -> processFullJunitResult(args, matrices, testShardChunks, testsResult)
args.useLegacyJUnitResult -> processJunitXml(testSuite, args, testShardChunks)
else -> processJunitXml(testSuite, args, testShardChunks)
}
}
private fun IgnoredTestCases.toJunitTestsResults() = getSkippedJUnitTestSuite(
map {
JUnitTest.Case(
classname = it.split("#").first().replace("class ", ""),
name = it.split("#").last(),
time = "0.0",
skipped = null
)
}
)
@VisibleForTesting
internal fun getSkippedJUnitTestSuite(listOfJUnitTestCase: List<JUnitTest.Case>) = JUnitTest.Suite(
name = "junit-ignored",
tests = listOfJUnitTestCase.size.toString(),
errors = "0",
failures = "0",
skipped = listOfJUnitTestCase.size.toString(),
time = "0.0",
timestamp = utcDateFormat.format(Date()),
testcases = listOfJUnitTestCase.toMutableList()
)
private fun processFullJunitResult(
args: IArgs,
matrices: MatrixMap,
testShardChunks: ShardChunks,
testSuite: JUnitTest.Result
) {
FullJUnitReport.run(matrices, testSuite, printToStdout = false, args = args)
processJunitXml(testSuite, args, testShardChunks)
}
data class ShardEfficiency(
val shard: String,
val expectedTime: Double,
val finalTime: Double,
val timeDiff: Double
)
fun createShardEfficiencyList(
oldResult: JUnitTest.Result,
newResult: JUnitTest.Result,
args: IArgs,
testShardChunks: ShardChunks
): List<ShardEfficiency> {
val oldDurations = createTestMethodDurationMap(oldResult, args)
val newDurations = createTestMethodDurationMap(newResult, args)
return testShardChunks.mapIndexed { index, testSuite ->
var expectedTime = 0.0
var finalTime = 0.0
testSuite.forEach { testCase ->
expectedTime += oldDurations[testCase] ?: 0.0
finalTime += newDurations[testCase] ?: 0.0
}
val timeDiff = (finalTime - expectedTime)
ShardEfficiency("Shard $index", expectedTime, finalTime, timeDiff)
}
}
private fun printActual(
oldResult: JUnitTest.Result,
newResult: JUnitTest.Result,
args: IArgs,
testShardChunks: ShardChunks
) {
val list = createShardEfficiencyList(oldResult, newResult, args, testShardChunks)
ReportManagerState.ShardTimes(list).publish()
}
private fun processJunitXml(
newTestResult: JUnitTest.Result?,
args: IArgs,
testShardChunks: ShardChunks
) {
if (newTestResult == null || newTestResult.testsuites.isNullOrEmpty()) return
val oldTestResult = downloadAsJunitXML(args.getSmartFlankGCSPathAsFileReference())
if (args.useLegacyJUnitResult) {
newTestResult.mergeTestTimes(oldTestResult)
}
printActual(oldTestResult, newTestResult, args, testShardChunks)
GcStorage.uploadJunitXml(newTestResult.toXmlString(), args)
}
private fun uploadMatricesId(args: IArgs, matrixMap: MatrixMap) {
if (args.disableResultsUpload) return
val file = args.getMatrixFilePath(matrixMap).toString()
if (file.startsWith(FtlConstants.GCS_PREFIX)) return
uploadToRemoteStorage(
RemoteStorage.Dir(args.resultsBucket, args.resultsDir),
RemoteStorage.Data(file, Files.readAllBytes(Paths.get(file)))
)
}
}