-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
333 additions
and
11 deletions.
There are no files selected for viewing
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
95 changes: 95 additions & 0 deletions
95
...omain/src/main/kotlin/flank/corellium/domain/run/test/android/device/task/ExecuteTests.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,95 @@ | ||
package flank.corellium.domain.run.test.android.device.task | ||
|
||
import flank.corellium.api.AndroidTestPlan | ||
import flank.corellium.domain.RunTestCorelliumAndroid | ||
import flank.corellium.domain.RunTestCorelliumAndroid.Authorize | ||
import flank.corellium.domain.RunTestCorelliumAndroid.Device | ||
import flank.corellium.domain.RunTestCorelliumAndroid.ExecuteTests | ||
import flank.corellium.domain.RunTestCorelliumAndroid.InstallApks | ||
import flank.corellium.domain.RunTestCorelliumAndroid.ParseApkInfo | ||
import flank.exection.parallel.from | ||
import flank.exection.parallel.using | ||
import flank.instrument.command.formatAmInstrumentCommand | ||
import flank.instrument.log.Instrument | ||
import flank.instrument.log.parseAdbInstrumentLog | ||
import flank.log.Output | ||
import flank.shard.Shard | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.buffer | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.take | ||
import java.io.File | ||
|
||
/** | ||
* The step is executing tests on previously invoked devices, and returning the test results. | ||
* | ||
* The side effect is console logs from `am instrument` saved inside [RunTestCorelliumAndroid.ExecuteTests.ADB_LOG] output subdirectory. | ||
* | ||
* The optional parsing errors are sent through [RunTestCorelliumAndroid.Context.out]. | ||
*/ | ||
internal val executeTests = ExecuteTests from setOf( | ||
ParseApkInfo, | ||
Authorize, | ||
InstallApks, | ||
) using Device.context { | ||
val outputDir = File(args.outputDir, ExecuteTests.ADB_LOG).apply { mkdir() } | ||
val testPlan: AndroidTestPlan.Config = prepareTestPlan() | ||
coroutineScope { | ||
ExecuteTests.Plan(testPlan).out() | ||
api.executeTest(testPlan).map { (id, flow) -> | ||
async { flow.collectResults(id, shard.size, outputDir, out) } | ||
}.awaitAll() | ||
} | ||
} | ||
|
||
/** | ||
* Prepare [AndroidTestPlan.Config] for test execution. | ||
* It is just mapping and formatting the data collected in state. | ||
*/ | ||
private fun Device.Context.prepareTestPlan(): AndroidTestPlan.Config = | ||
AndroidTestPlan.Config( | ||
mapOf( | ||
instance.id to shard.flatMap { app: Shard.App -> | ||
app.tests.map { test -> | ||
formatAmInstrumentCommand( | ||
packageName = info.packageNames.getValue(test.name), | ||
testRunner = info.testRunners.getValue(test.name), | ||
testCases = test.cases.map { case -> "class " + case.name } | ||
) | ||
} | ||
} | ||
) | ||
) | ||
|
||
private suspend fun Flow<String>.collectResults( | ||
id: String, | ||
expectedResults: Int, | ||
dir: File, | ||
out: Output, | ||
): List<Instrument> { | ||
var read = 0 | ||
var parsed = 0 | ||
val file = dir.resolve(id) | ||
val results = mutableListOf<Instrument>() | ||
this.onEach { file.appendText(it + "\n") } | ||
.buffer(100) | ||
.flowOn(Dispatchers.IO) | ||
.onEach { ++read } | ||
.parseAdbInstrumentLog() | ||
.onEach { parsed = read } | ||
.onEach { result -> results += result } | ||
.onEach { result -> ExecuteTests.Status(id, result).out() } | ||
.catch { cause -> ExecuteTests.Error(id, cause, file.path, ++parsed..read).out() } | ||
.filterIsInstance<Instrument.Result>() | ||
.take(expectedResults) | ||
.collect() | ||
return results | ||
} |
30 changes: 30 additions & 0 deletions
30
...domain/src/main/kotlin/flank/corellium/domain/run/test/android/device/task/InstallApks.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,30 @@ | ||
package flank.corellium.domain.run.test.android.device.task | ||
|
||
import flank.corellium.api.AndroidApps | ||
import flank.corellium.domain.RunTestCorelliumAndroid.Authorize | ||
import flank.corellium.domain.RunTestCorelliumAndroid.Device | ||
import flank.corellium.domain.RunTestCorelliumAndroid.InstallApks | ||
import flank.exection.parallel.from | ||
import flank.exection.parallel.using | ||
import kotlinx.coroutines.flow.collect | ||
|
||
/** | ||
* The step is installing required software on android instances. | ||
*/ | ||
internal val installApks = InstallApks from setOf( | ||
Authorize | ||
) using Device.context { | ||
val apks = prepareApkToInstall() | ||
api.installAndroidApps(listOf(apks)).collect { event -> | ||
InstallApks.Status(event).out() | ||
} | ||
// If tests will be executed too fast just after the | ||
// app installed, the instrumentation will fail | ||
// delay(4_000) TODO: for verification | ||
} | ||
|
||
private fun Device.Context.prepareApkToInstall() = | ||
AndroidApps( | ||
instanceId = instance.id, | ||
paths = shard.flatMap { app -> app.tests.map { test -> test.name } + app.name } | ||
) |
16 changes: 16 additions & 0 deletions
16
...m/domain/src/main/kotlin/flank/corellium/domain/run/test/android/step/AvailableDevices.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,16 @@ | ||
package flank.corellium.domain.run.test.android.step | ||
|
||
import flank.corellium.domain.RunTestCorelliumAndroid | ||
import flank.corellium.domain.RunTestCorelliumAndroid.AvailableDevices | ||
import flank.exection.parallel.from | ||
import flank.exection.parallel.using | ||
import kotlinx.coroutines.channels.Channel | ||
|
||
/** | ||
* Prepare channel for providing devices ready to use. | ||
*/ | ||
val availableDevices = AvailableDevices from setOf( | ||
RunTestCorelliumAndroid.PrepareShards | ||
) using RunTestCorelliumAndroid.context { | ||
Channel(shards.size) | ||
} |
19 changes: 19 additions & 0 deletions
19
...ium/domain/src/main/kotlin/flank/corellium/domain/run/test/android/step/DispatchShards.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,19 @@ | ||
package flank.corellium.domain.run.test.android.step | ||
|
||
import flank.corellium.domain.RunTestCorelliumAndroid | ||
import flank.corellium.domain.RunTestCorelliumAndroid.DispatchShards | ||
import flank.exection.parallel.from | ||
import flank.exection.parallel.using | ||
import flank.shard.InstanceShard | ||
import kotlinx.coroutines.channels.Channel | ||
|
||
/** | ||
* Prepare channel for dispatching shards and fill the buffer with initial elements. | ||
*/ | ||
val dispatchShards = DispatchShards from setOf( | ||
RunTestCorelliumAndroid.PrepareShards | ||
) using RunTestCorelliumAndroid.context { | ||
Channel<InstanceShard>(shards.size).apply { | ||
shards.forEach { shard -> send(shard) } | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...lium/domain/src/main/kotlin/flank/corellium/domain/run/test/android/step/DispatchTests.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,75 @@ | ||
package flank.corellium.domain.run.test.android.step | ||
|
||
import flank.corellium.domain.RunTestCorelliumAndroid.Authorize | ||
import flank.corellium.domain.RunTestCorelliumAndroid.AvailableDevices | ||
import flank.corellium.domain.RunTestCorelliumAndroid.Device | ||
import flank.corellium.domain.RunTestCorelliumAndroid.DispatchShards | ||
import flank.corellium.domain.RunTestCorelliumAndroid.ExecuteTests | ||
import flank.corellium.domain.RunTestCorelliumAndroid.ParseApkInfo | ||
import flank.corellium.domain.RunTestCorelliumAndroid.context | ||
import flank.exection.parallel.ParallelState | ||
import flank.exection.parallel.from | ||
import flank.exection.parallel.invoke | ||
import flank.exection.parallel.select | ||
import flank.exection.parallel.using | ||
import flank.exection.parallel.verify | ||
import flank.shard.InstanceShard | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.flow.last | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* Dispatch each test shard execution into first best matching device. | ||
*/ | ||
val dispatchTests = ExecuteTests from setOf( | ||
ParseApkInfo, | ||
Authorize, | ||
AvailableDevices, | ||
DispatchShards, | ||
) using context { state -> | ||
coroutineScope { | ||
var count = 0 | ||
channelFlow { | ||
for (shard in dispatch) { | ||
val index = ++count | ||
val instance = devices.maxBy { instance -> | ||
(shard.apks intersect instance.apks).size | ||
} | ||
val seed = mapOf( | ||
Device.Shard to shard, | ||
Device.Instance to instance, | ||
) | ||
launch { | ||
val result = Device | ||
.execute(state + seed) | ||
.last() | ||
.verify() | ||
.select(ExecuteTests) | ||
send(result) | ||
devices.send(instance) | ||
if (index == count) | ||
dispatch.close() | ||
} | ||
} | ||
}.toList().reduce { accumulator, value -> | ||
accumulator + value | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Flatten into the list of apk. | ||
*/ | ||
private val InstanceShard.apks: List<String> | ||
get() = flatMap { app -> app.tests.map { test -> test.name } + app.name } | ||
|
||
private suspend fun <T> Channel<T>.maxBy(select: (T) -> Int): T = | ||
mutableListOf<T>().apply { | ||
add(receive()) | ||
while (!isEmpty) add(receive()) | ||
sortByDescending(select) | ||
drop(1).forEach { send(it) } | ||
}.first() |
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
11 changes: 11 additions & 0 deletions
11
...lium/domain/src/main/kotlin/flank/corellium/domain/run/test/android/step/HandleResults.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,11 @@ | ||
package flank.corellium.domain.run.test.android.step | ||
|
||
import flank.corellium.domain.RunTestCorelliumAndroid.HandleResults | ||
import flank.exection.parallel.from | ||
import flank.exection.parallel.using | ||
|
||
val handleResults = HandleResults from setOf( | ||
HandleResults.Init | ||
) using { | ||
TODO() | ||
} |
Oops, something went wrong.