/
CIConfigIntegrationTests.kt
300 lines (261 loc) · 13.3 KB
/
CIConfigIntegrationTests.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
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
import common.JvmVendor
import common.JvmVersion
import common.Os
import common.VersionedSettingsBranch
import configurations.BaseGradleBuildType
import configurations.GitHubMergeQueueCheckPass
import jetbrains.buildServer.configs.kotlin.DslContext
import jetbrains.buildServer.configs.kotlin.buildSteps.GradleBuildStep
import jetbrains.buildServer.configs.kotlin.failureConditions.BuildFailureOnText
import model.ALL_CROSS_VERSION_BUCKETS
import model.CIBuildModel
import model.DefaultFunctionalTestBucketProvider
import model.JsonBasedGradleSubprojectProvider
import model.QUICK_CROSS_VERSION_BUCKETS
import model.TestCoverage
import model.TestType
import model.ignoredSubprojects
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import projects.CheckProject
import projects.FunctionalTestProject
import projects.StageProject
import java.io.File
class CIConfigIntegrationTests {
init {
DslContext.initForTest()
}
private val subprojectProvider = JsonBasedGradleSubprojectProvider(File("../.teamcity/subprojects.json"))
private val model = CIBuildModel(
projectId = "Check",
branch = VersionedSettingsBranch.fromDslContext(),
buildScanTags = listOf("Check"),
subprojects = subprojectProvider
)
private val gradleBuildBucketProvider = DefaultFunctionalTestBucketProvider(model, File("./test-buckets.json").absoluteFile)
private val rootProject = CheckProject(model, gradleBuildBucketProvider)
@Test
fun configurationTreeCanBeGenerated() {
assertEquals(rootProject.subProjects.size, model.stages.size)
assertEquals(rootProject.buildTypes.size, model.stages.size + 1) // +1 for the GitHubMergeQueueCheckPass
}
@Test
fun configurationsHaveDependencies() {
val stagePassConfigs = rootProject.buildTypes.filter { it !is GitHubMergeQueueCheckPass }
assertEquals(model.stages.size, stagePassConfigs.size)
stagePassConfigs.forEach {
val stageNumber = stagePassConfigs.indexOf(it) + 1
val stage = model.stages[stageNumber - 1]
val prevStage = if (stageNumber > 1) model.stages[stageNumber - 2] else null
if (stage.runsIndependent) {
return@forEach
}
assertEquals(
stage.specificBuilds.size + stage.functionalTests.size + stage.performanceTests.size + stage.docsTests.size + (if (prevStage != null) 1 else 0),
it.dependencies.items.size, stage.stageName.stageName
)
}
}
private
val largeSubProjectRegex = """\((\w+(_\d+))\)""".toRegex()
/**
* Test Coverage - AllVersionsCrossVersion Java8 Oracle Linux (core_2) -> core_2
*/
private
fun BaseGradleBuildType.getSubProjectSplitName() = largeSubProjectRegex.find(this.name)!!.groupValues[1]
private
fun BaseGradleBuildType.getGradleTasks(): String {
val runnerStep = this.steps.items.find { it.name == "GRADLE_RUNNER" } as GradleBuildStep
return runnerStep.tasks!!
}
private
fun BaseGradleBuildType.getGradleParams(): String {
val runnerStep = this.steps.items.find { it.name == "GRADLE_RUNNER" } as GradleBuildStep
return runnerStep.gradleParams!!
}
@Test
fun canSplitLargeProjects() {
fun assertAllSplitsArePresent(subProjectName: String, functionalTests: List<BaseGradleBuildType>) {
val splitSubProjectNames = functionalTests.map { it.getSubProjectSplitName() }.toSet()
val expectedProjectNames = (1..functionalTests.size).map { "${subProjectName}_$it" }.toSet()
assertEquals(expectedProjectNames, splitSubProjectNames)
}
fun assertCorrectParameters(subProjectName: String, functionalTests: List<BaseGradleBuildType>) {
functionalTests.forEach { assertTrue(it.getGradleTasks().startsWith("clean $subProjectName")) }
if (functionalTests.size == 1) {
assertFalse(functionalTests[0].getGradleParams().contains("-PincludeTestClasses"))
assertFalse(functionalTests[0].getGradleParams().contains("-PexcludeTestClasses"))
} else {
functionalTests.forEachIndexed { index, it ->
if (index == functionalTests.size - 1) {
assertTrue(it.getGradleParams().contains("-PexcludeTestClasses"))
} else {
assertTrue(it.getGradleParams().contains("-PincludeTestClasses"))
}
}
}
}
fun assertProjectAreSplitByClassesCorrectly(functionalTests: List<BaseGradleBuildType>) {
val functionalTestsWithSplit: Map<String, List<BaseGradleBuildType>> =
functionalTests.filter { largeSubProjectRegex.containsMatchIn(it.name) }.groupBy { it.getSubProjectSplitName().substringBefore('_') }
functionalTestsWithSplit.forEach {
assertAllSplitsArePresent(it.key, it.value)
assertCorrectParameters(it.key, it.value)
}
}
fun assertProjectAreSplitByGradleVersionCorrectly(buckets: List<List<String>>, testType: TestType, functionalTests: List<BaseGradleBuildType>) {
buckets.forEachIndexed { index: Int, startEndVersion: List<String> ->
assertTrue(functionalTests[index].name.contains("(${startEndVersion[0]} <= gradle <${startEndVersion[1]})"))
assertEquals("clean ${testType}Test", functionalTests[index].getGradleTasks())
assertTrue(
functionalTests[index].getGradleParams().apply {
println(this)
}.contains("-PonlyTestGradleVersion=${startEndVersion[0]}-${startEndVersion[1]}")
)
}
}
for (stageProject in rootProject.subProjects.filterIsInstance<StageProject>()) {
for (functionalTestProject in stageProject.subProjects.filterIsInstance<FunctionalTestProject>()) {
when {
functionalTestProject.name.contains("AllVersionsCrossVersion") -> {
assertProjectAreSplitByGradleVersionCorrectly(ALL_CROSS_VERSION_BUCKETS, TestType.allVersionsCrossVersion, functionalTestProject.functionalTests)
}
functionalTestProject.name.contains("QuickFeedbackCrossVersion") -> {
assertProjectAreSplitByGradleVersionCorrectly(QUICK_CROSS_VERSION_BUCKETS, TestType.quickFeedbackCrossVersion, functionalTestProject.functionalTests)
}
else -> {
assertProjectAreSplitByClassesCorrectly(functionalTestProject.functionalTests)
}
}
}
}
}
@Test
fun buildsContainFailureConditionForPotentialCredentialsLeaks() {
val allBuildTypes = rootProject.subProjects.flatMap { it.buildTypes }
allBuildTypes.forEach {
val credentialLeakCondition = it.failureConditions.items.find { it.type.equals("BuildFailureOnMessage") } as BuildFailureOnText
assertTrue(credentialLeakCondition.enabled)
assertTrue(credentialLeakCondition.stopBuildOnFailure!!)
}
}
private fun subProjectFolderList(): List<File> {
val subprojectRoots = File("../platforms").listFiles(File::isDirectory).plus(File("../subprojects"))
val subProjectFolders = subprojectRoots.map { it.listFiles(File::isDirectory).asList() }.flatten().filter { dir ->
// filter out the directories that have only a `build` subdirectory - this usually happens after branch switching
dir.listFiles { _: File, name: String -> name != "build" }!!.isNotEmpty()
}
assertFalse(subProjectFolders.isEmpty())
return subProjectFolders
}
@Test
fun allSubprojectsAreListed() {
val knownSubprojectDirs = model.subprojects.subprojects.map { File("../", it.path) }
subProjectFolderList().forEach {
assertTrue(
it in knownSubprojectDirs,
"Not defined: $it"
)
}
}
@Test
fun uuidsAreUnique() {
val uuidList = model.stages.flatMap { it.functionalTests.map { ft -> ft.uuid } }
assertEquals(uuidList.distinct(), uuidList)
}
@Test
fun testsAreCorrectlyConfiguredForAllSubProjects() {
model.subprojects.subprojects.filter {
!ignoredSubprojects.contains(it.name)
}.forEach {
val dir = File("..", it.path)
assertEquals(it.unitTests, File(dir, "src/test").isDirectory, "${it.name}'s unitTests is wrong!")
assertEquals(it.functionalTests, File(dir, "src/integTest").isDirectory, "${it.name}'s functionalTests is wrong!")
assertEquals(it.crossVersionTests, File(dir, "src/crossVersionTest").isDirectory, "${it.name}'s crossVersionTests is wrong!")
}
}
@Test
fun allSubprojectsDefineTheirUnitTestPropertyCorrectly() {
val projectDirsWithUnitTests = model.subprojects.subprojects
.filter { it.unitTests }
.map { File("..", it.path) }
val projectFoldersWithUnitTests = subProjectFolderList().filter {
File(it, "src/test").exists() &&
it.name != "architecture-test" // architecture-test:test is part of Sanity Check
}
assertFalse(projectFoldersWithUnitTests.isEmpty())
projectFoldersWithUnitTests.forEach {
assertTrue(projectDirsWithUnitTests.contains(it), "Contains unit tests: $it")
}
}
private fun containsSrcFileWithString(srcRoot: File, content: String, exceptions: List<String>): Boolean {
srcRoot.walkTopDown().forEach { file ->
if (file.extension == "groovy" || file.extension == "java") {
val originalText = file.readText()
val text = originalText.lineSequence()
.filterNot { it.trim().startsWith("//") }
.joinToString("\n")
if (text.contains(content) && exceptions.all { !text.contains(it) }) {
println("Found suspicious test file: $file")
return true
}
}
}
return false
}
@Test
fun allSubprojectsDefineTheirFunctionTestPropertyCorrectly() {
val projectDirsWithFunctionalTests = model.subprojects.subprojects
.filter { it.functionalTests }
.map { File("..", it.path) }
val projectFoldersWithFunctionalTests = subProjectFolderList().filter {
File(it, "src/integTest").exists() &&
it.name != "distributions-integ-tests" && // distributions:integTest is part of Build Distributions
it.name != "soak" // soak tests have their own test category
}
assertFalse(projectFoldersWithFunctionalTests.isEmpty())
projectFoldersWithFunctionalTests.forEach {
assertTrue(projectDirsWithFunctionalTests.contains(it), "Contains functional tests: $it")
}
}
@Test
fun allSubprojectsDefineTheirCrossVersionTestPropertyCorrectly() {
val projectDirsWithCrossVersionTests = model.subprojects.subprojects
.filter { it.crossVersionTests }
.map { File("..", it.path) }
val projectFoldersWithCrossVersionTests = subProjectFolderList()
.filter { File(it, "src/crossVersionTest").exists() }
assertFalse(projectFoldersWithCrossVersionTests.isEmpty())
projectFoldersWithCrossVersionTests.forEach {
assertTrue(projectDirsWithCrossVersionTests.contains(it), "Contains cross-version tests: $it")
}
}
@Test
fun integTestFolderDoesNotContainCrossVersionTests() {
val projectFoldersWithFunctionalTests = subProjectFolderList().filter { File(it, "src/integTest").exists() }
assertFalse(projectFoldersWithFunctionalTests.isEmpty())
projectFoldersWithFunctionalTests.forEach {
assertFalse(containsSrcFileWithString(File(it, "src/integTest"), "CrossVersion", listOf("package org.gradle.testkit", "CrossVersionPerformanceTest")))
}
}
@Test
fun long_ids_are_shortened() {
val testCoverage = TestCoverage(1, TestType.quickFeedbackCrossVersion, Os.WINDOWS, JvmVersion.java11, JvmVendor.oracle)
val shortenedId = testCoverage.asConfigurationId(model, "veryLongSubprojectNameLongerThanEverythingWeHave")
assertTrue(shortenedId.length < 80)
assertEquals("Check_QckFdbckCrssVrsn_1_vryLngSbprjctNmLngrThnEvrythngWHv", shortenedId)
assertEquals("Check_QuickFeedbackCrossVersion_1_iIntegT", testCoverage.asConfigurationId(model, "internalIntegTesting"))
assertEquals("Check_QuickFeedbackCrossVersion_1_buildCache", testCoverage.asConfigurationId(model, "buildCache"))
assertEquals("Check_QuickFeedbackCrossVersion_1_0", testCoverage.asConfigurationId(model))
}
@Test
fun allVersionsAreIncludedInCrossVersionTests() {
assertEquals("0.0", ALL_CROSS_VERSION_BUCKETS[0][0])
assertEquals("99.0", ALL_CROSS_VERSION_BUCKETS[ALL_CROSS_VERSION_BUCKETS.size - 1][1])
(1 until ALL_CROSS_VERSION_BUCKETS.size).forEach {
assertEquals(ALL_CROSS_VERSION_BUCKETS[it - 1][1], ALL_CROSS_VERSION_BUCKETS[it][0])
}
}
}