Skip to content

Commit

Permalink
Refactor ftl.run (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-goral authored Mar 10, 2020
1 parent 71314ac commit 480cb74
Show file tree
Hide file tree
Showing 36 changed files with 762 additions and 648 deletions.
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/args/AndroidTestShard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.coroutines.runBlocking
object AndroidTestShard {

// computed properties not specified in yaml
fun getTestShardChunks(args: AndroidArgs, testApk: String): List<List<String>> {
fun getTestShardChunks(args: AndroidArgs, testApk: String): ShardChunks {
// Download test APK if necessary so it can be used to validate test methods
var testLocalApk = testApk
if (testApk.startsWith(FtlConstants.GCS_PREFIX)) {
Expand Down
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ object ArgsHelper {
return ArgsFileVisitor("glob:$filePath").walk(searchDir)
}

fun calculateShards(filteredTests: List<FlankTestMethod>, args: IArgs): List<List<String>> {
fun calculateShards(filteredTests: List<FlankTestMethod>, args: IArgs): ShardChunks {
val shards = if (args.disableSharding) {
mutableListOf(filteredTests.map { it.testName } as MutableList<String>)
} else {
Expand Down
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/args/IosArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class IosArgs(
val testTargets = cli?.testTargets ?: iosFlank.testTargets.filterNotNull()

// computed properties not specified in yaml
val testShardChunks: List<List<String>> by lazy {
val testShardChunks: ShardChunks by lazy {
if (disableSharding) return@lazy listOf(emptyList<String>())

val validTestMethods = Xctestrun.findTestNames(xctestrunFile)
Expand Down
3 changes: 3 additions & 0 deletions test_runner/src/main/kotlin/ftl/args/ShardChunks.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ftl.args

typealias ShardChunks = List<List<String>>
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/cli/firebase/CancelCommand.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ftl.cli.firebase

import ftl.args.AndroidArgs
import ftl.run.TestRunner
import ftl.run.cancelLastRun
import picocli.CommandLine

@CommandLine.Command(
Expand All @@ -20,7 +20,7 @@ Reads in the matrix_ids.json file. Cancels any incomplete matrices.
)
class CancelCommand : Runnable {
override fun run() {
TestRunner.cancelLastRun(AndroidArgs.default())
cancelLastRun(AndroidArgs.default())
}

@CommandLine.Option(names = ["-h", "--help"], usageHelp = true, description = ["Prints this help message"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ftl.cli.firebase

import ftl.args.AndroidArgs
import ftl.run.TestRunner
import ftl.run.refreshLastRun
import kotlinx.coroutines.runBlocking
import picocli.CommandLine.Command
import picocli.CommandLine.Option
Expand All @@ -23,7 +23,10 @@ Reads in the matrix_ids.json file. Refreshes any incomplete matrices.
class RefreshCommand : Runnable {
override fun run() {
runBlocking {
TestRunner.refreshLastRun(AndroidArgs.default(), emptyList())
refreshLastRun(
currentArgs = AndroidArgs.default(),
testShardChunks = emptyList()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.cli.firebase.test.android

import ftl.args.AndroidArgs
import ftl.args.AndroidTestShard
import ftl.args.ShardChunks
import ftl.args.yml.AppTestPair
import ftl.config.Device
import ftl.config.FtlConstants
Expand All @@ -10,13 +11,14 @@ import ftl.config.FtlConstants.defaultAndroidVersion
import ftl.config.FtlConstants.defaultLocale
import ftl.config.FtlConstants.defaultOrientation
import ftl.mock.MockServer
import ftl.run.TestRunner
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.system.exitProcess
import ftl.run.common.prettyPrint
import ftl.run.newTestRun
import kotlinx.coroutines.runBlocking
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.system.exitProcess

@Command(
name = "run",
Expand All @@ -43,16 +45,16 @@ class AndroidRunCommand : Runnable {
val config = AndroidArgs.load(Paths.get(configPath), cli = this)

if (dumpShards) {
val testShardChunks = AndroidTestShard.getTestShardChunks(config, config.testApk)
val testShardChunksJson = TestRunner.gson.toJson(testShardChunks)
val testShardChunks: ShardChunks = AndroidTestShard.getTestShardChunks(config, config.testApk)
val testShardChunksJson: String = prettyPrint.toJson(testShardChunks)

Files.write(Paths.get(shardFile), testShardChunksJson.toByteArray())
println("Saved shards to $shardFile")
exitProcess(0)
}

runBlocking {
TestRunner.newRun(config)
newTestRun(config)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import ftl.config.FtlConstants
import ftl.config.FtlConstants.defaultIosModel
import ftl.config.FtlConstants.defaultIosVersion
import ftl.mock.MockServer
import ftl.run.TestRunner
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.system.exitProcess
import ftl.run.common.prettyPrint
import ftl.run.newTestRun
import kotlinx.coroutines.runBlocking
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.system.exitProcess

@Command(
name = "run",
Expand All @@ -38,14 +39,14 @@ class IosRunCommand : Runnable {
val config = IosArgs.load(Paths.get(configPath), cli = this)

if (dumpShards) {
val testShardChunksJson = TestRunner.gson.toJson(config.testShardChunks)
val testShardChunksJson: String = prettyPrint.toJson(config.testShardChunks)
Files.write(Paths.get(shardFile), testShardChunksJson.toByteArray())
println("Saved shards to $shardFile")
exitProcess(0)
}

runBlocking {
TestRunner.newRun(config)
newTestRun(config)
}
}

Expand Down
5 changes: 3 additions & 2 deletions test_runner/src/main/kotlin/ftl/gc/GcAndroidTestMatrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.google.api.services.testing.model.TestSpecification
import com.google.api.services.testing.model.TestTargetsForShard
import com.google.api.services.testing.model.ToolResultsHistory
import ftl.args.AndroidArgs
import ftl.args.ShardChunks
import ftl.util.fatalError
import ftl.util.join
import ftl.util.testTimeoutToSeconds
Expand All @@ -35,7 +36,7 @@ object GcAndroidTestMatrix {
testApkGcsPath: String,
runGcsPath: String,
androidDeviceList: AndroidDeviceList,
testTargets: List<List<String>>,
testShards: ShardChunks,
args: AndroidArgs,
toolResultsHistory: ToolResultsHistory
): Testing.Projects.TestMatrices.Create {
Expand All @@ -46,7 +47,7 @@ object GcAndroidTestMatrix {
val matrixGcsPath = join(args.resultsBucket, runGcsPath)

// ShardingOption().setUniformSharding(UniformSharding().setNumShards())
val testTargetsForShard: List<TestTargetsForShard> = testTargets.map {
val testTargetsForShard: List<TestTargetsForShard> = testShards.map {
TestTargetsForShard().setTestTargets(it)
}
val manualSharding = ManualSharding().setTestTargetsForShard(testTargetsForShard)
Expand Down
9 changes: 5 additions & 4 deletions test_runner/src/main/kotlin/ftl/reports/util/ReportManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.reports.util

import ftl.args.IArgs
import ftl.args.IosArgs
import ftl.args.ShardChunks
import ftl.gc.GcStorage
import ftl.json.MatrixMap
import ftl.reports.CostReport
Expand Down Expand Up @@ -91,7 +92,7 @@ object ReportManager {
}

/** Returns true if there were no test failures */
fun generate(matrices: MatrixMap, args: IArgs, testShardChunks: List<List<String>>): Int {
fun generate(matrices: MatrixMap, args: IArgs, testShardChunks: ShardChunks): Int {
val testSuite = parseTestSuite(matrices, args)

val useFlakyTests = args.flakyTestAttempts > 0
Expand Down Expand Up @@ -127,7 +128,7 @@ object ReportManager {
oldResult: JUnitTestResult,
newResult: JUnitTestResult,
args: IArgs,
testShardChunks: List<List<String>>
testShardChunks: ShardChunks
):
List<ShardEfficiency> {
val oldDurations = Shard.createTestMethodDurationMap(oldResult, args)
Expand All @@ -154,7 +155,7 @@ object ReportManager {
oldResult: JUnitTestResult,
newResult: JUnitTestResult,
args: IArgs,
testShardChunks: List<List<String>>
testShardChunks: ShardChunks
) {
val list = createShardEfficiencyList(oldResult, newResult, args, testShardChunks)

Expand All @@ -166,7 +167,7 @@ object ReportManager {
private fun processJunitXml(
newTestResult: JUnitTestResult?,
args: IArgs,
testShardChunks: List<List<String>>
testShardChunks: ShardChunks
) {
if (newTestResult == null) return

Expand Down
80 changes: 0 additions & 80 deletions test_runner/src/main/kotlin/ftl/run/AndroidTestRunner.kt

This file was deleted.

45 changes: 45 additions & 0 deletions test_runner/src/main/kotlin/ftl/run/CancelLastRun.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ftl.run

import ftl.args.IArgs
import ftl.config.FtlConstants
import ftl.gc.GcTestMatrix
import ftl.json.MatrixMap
import ftl.run.common.getLastArgs
import ftl.run.common.getLastMatrices
import ftl.util.MatrixState
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

// used to cancel and update results from an async run
fun cancelLastRun(args: IArgs) {
val matrixMap = getLastMatrices(args)
val lastArgs = getLastArgs(args)

cancelMatrices(matrixMap, lastArgs)
}

/** Cancel all in progress matrices in parallel **/
private fun cancelMatrices(matrixMap: MatrixMap, args: IArgs) {
println("CancelMatrices")

val map = matrixMap.map
var matrixCount = 0

runBlocking {
map.forEach { matrix ->
// Only cancel unfinished
if (MatrixState.inProgress(matrix.value.state)) {
matrixCount += 1
launch { GcTestMatrix.cancel(matrix.key, args) }
}
}
}

if (matrixCount == 0) {
println(FtlConstants.indent + "No matrices to cancel")
} else {
println(FtlConstants.indent + "Cancelling ${matrixCount}x matrices")
}

println()
}
Loading

0 comments on commit 480cb74

Please sign in to comment.