-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate ImpactedTestEngine mocks to kotlin mocks
- Loading branch information
1 parent
c7c9ee5
commit 4d962e2
Showing
9 changed files
with
156 additions
and
184 deletions.
There are no files selected for viewing
121 changes: 0 additions & 121 deletions
121
...gine/src/main/java/com/teamscale/test_impacted/engine/executor/ImpactedTestsProvider.java
This file was deleted.
Oops, something went wrong.
105 changes: 105 additions & 0 deletions
105
...gine/src/main/kotlin/com/teamscale/test_impacted/engine/executor/ImpactedTestsProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.teamscale.test_impacted.engine.executor | ||
|
||
import com.teamscale.client.ClusteredTestDetails | ||
import com.teamscale.client.CommitDescriptor | ||
import com.teamscale.client.PrioritizableTestCluster | ||
import com.teamscale.client.TeamscaleClient | ||
import com.teamscale.test_impacted.commons.LoggerUtils.createLogger | ||
import retrofit2.Response | ||
import java.io.IOException | ||
import java.util.logging.Level | ||
|
||
/** | ||
* Class for retrieving the impacted [PrioritizableTestCluster]s corresponding to [ClusteredTestDetails] | ||
* available for test execution. | ||
*/ | ||
open class ImpactedTestsProvider( | ||
private val client: TeamscaleClient, | ||
private val baseline: String, | ||
private val baselineRevision: String, | ||
private val endCommit: CommitDescriptor, | ||
private val endRevision: String, | ||
private val repository: String, | ||
@JvmField val partition: String, | ||
private val includeNonImpacted: Boolean, | ||
private val includeAddedTests: Boolean, | ||
private val includeFailedAndSkipped: Boolean | ||
) { | ||
private val logger = createLogger() | ||
|
||
/** Queries Teamscale for impacted tests. */ | ||
fun getImpactedTestsFromTeamscale( | ||
availableTestDetails: List<ClusteredTestDetails> | ||
): List<PrioritizableTestCluster> { | ||
try { | ||
logger.info { "Getting impacted tests..." } | ||
val response = client | ||
.getImpactedTests( | ||
availableTestDetails, baseline, baselineRevision, endCommit, endRevision, repository, | ||
listOf(partition), | ||
includeNonImpacted, includeAddedTests, includeFailedAndSkipped | ||
) | ||
|
||
if (response.isSuccessful) { | ||
val testClusters = response.body() | ||
if (testClusters != null && testCountIsPlausible(testClusters, availableTestDetails)) { | ||
return testClusters | ||
} | ||
logger.severe( | ||
""" | ||
Teamscale was not able to determine impacted tests: | ||
${response.body()} | ||
""".trimIndent() | ||
) | ||
} else { | ||
logger.severe( | ||
"Retrieval of impacted tests failed: ${response.code()} ${response.message()}\n${ | ||
getErrorBody(response) | ||
}" | ||
) | ||
} | ||
} catch (e: IOException) { | ||
logger.log( | ||
Level.SEVERE, e | ||
) { "Retrieval of impacted tests failed." } | ||
} | ||
return emptyList() | ||
} | ||
|
||
/** | ||
* Checks that the number of tests returned by Teamscale matches the number of available tests when running with | ||
* [.includeNonImpacted]. | ||
*/ | ||
private fun testCountIsPlausible( | ||
testClusters: List<PrioritizableTestCluster>, | ||
availableTestDetails: List<ClusteredTestDetails> | ||
): Boolean { | ||
val returnedTests = testClusters.stream().mapToLong { | ||
it.tests!!.size.toLong() | ||
}.sum() | ||
if (!includeNonImpacted) { | ||
logger.info { "Received $returnedTests impacted tests of ${availableTestDetails.size} available tests." } | ||
return true | ||
} | ||
if (returnedTests == availableTestDetails.size.toLong()) { | ||
return true | ||
} else { | ||
logger.severe { | ||
"Retrieved $returnedTests tests from Teamscale, but expected ${availableTestDetails.size}." | ||
} | ||
return false | ||
} | ||
} | ||
|
||
companion object { | ||
@Throws(IOException::class) | ||
private fun getErrorBody(response: Response<*>): String { | ||
response.errorBody().use { error -> | ||
if (error != null) { | ||
return error.string() | ||
} | ||
} | ||
return "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.