-
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.
Merge branch 'master' into #848-auto-update-dependencies
- Loading branch information
Showing
4 changed files
with
168 additions
and
24 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
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
136 changes: 136 additions & 0 deletions
136
test_runner/src/test/kotlin/ftl/reports/outcome/BillableMinutesTest.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,136 @@ | ||
package ftl.reports.outcome | ||
|
||
import com.google.api.services.testing.model.AndroidModel | ||
import com.google.api.services.toolresults.model.Step | ||
import ftl.android.DeviceType | ||
import ftl.gc.GcTesting | ||
import ftl.http.executeWithRetry | ||
import io.mockk.every | ||
import io.mockk.mockkObject | ||
import io.mockk.unmockkAll | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.contrib.java.lang.system.SystemOutRule | ||
|
||
class BillableMinutesTest { | ||
|
||
@get:Rule | ||
val output: SystemOutRule = SystemOutRule().enableLog().muteForSuccessfulTests() | ||
|
||
private val androidModels = listOf<AndroidModel>( | ||
make { id = "NexusLowRes"; form = DeviceType.VIRTUAL.name }, | ||
make { id = "Nexus5"; form = DeviceType.VIRTUAL.name }, | ||
make { id = "g3"; form = DeviceType.PHYSICAL.name }, | ||
make { id = "sailfish"; form = DeviceType.PHYSICAL.name } | ||
) | ||
|
||
@Before | ||
fun setUp() { | ||
output.clearLog() | ||
mockkObject(GcTesting) | ||
every { | ||
GcTesting | ||
.get | ||
.testEnvironmentCatalog() | ||
.get(any()) | ||
.setProjectId(any()) | ||
.executeWithRetry() | ||
} returns make { | ||
androidDeviceCatalog = make { models = androidModels } | ||
} | ||
} | ||
|
||
@After | ||
fun tearDown() = unmockkAll() | ||
|
||
@Test | ||
fun `should return billing for virtual device only`() { | ||
val expected = 1L | ||
val step: Step = makeStep() | ||
|
||
val minutes = listOf(step).calculateAndroidBillableMinutes(projectId = "anyId", timeoutValue = 1000L) | ||
|
||
verifyBilling(minutes, expectedVirtual = expected) | ||
} | ||
|
||
@Test | ||
fun `should return billing for physical devices only`() { | ||
val expected = 1L | ||
val step: Step = makeStep(model = "g3") | ||
|
||
val minutes = listOf(step).calculateAndroidBillableMinutes(projectId = "anyId", timeoutValue = 1000L) | ||
|
||
verifyBilling(minutes, expectedPhysical = expected) | ||
} | ||
|
||
@Test | ||
fun `should return mixed billing`() { | ||
val expectedVirtual = 3L | ||
val expectedPhysical = 5L | ||
val step1: Step = makeStep(model = "nexuslowres", duration = 123) | ||
val step2: Step = makeStep(model = "SaIlFisH", duration = 245) | ||
|
||
val minutes = listOf(step1, step2).calculateAndroidBillableMinutes(projectId = "anyId", timeoutValue = 1000L) | ||
|
||
verifyBilling(minutes, expectedVirtual, expectedPhysical) | ||
} | ||
|
||
@Test | ||
fun `should return multiple mixed billing`() { | ||
val expectedVirtual = 9L | ||
val expectedPhysical = 16L | ||
val step1: Step = makeStep(model = "Nexus5", duration = 123) | ||
val step2: Step = makeStep(model = "SaIlFisH", duration = 245) | ||
val step3: Step = makeStep(model = "NEXUSLOWRES", duration = 352) | ||
val step4: Step = makeStep(model = "G3", duration = 657) | ||
|
||
val minutes = listOf(step1, step2, step3, step4).calculateAndroidBillableMinutes(projectId = "anyId", timeoutValue = 1000L) | ||
|
||
verifyBilling(minutes, expectedVirtual, expectedPhysical) | ||
} | ||
|
||
@Test | ||
fun `should return multiple mixed billing - timeouted`() { | ||
val timeout = 120L | ||
val expectedVirtual = 3L | ||
val expectedPhysical = 4L | ||
val step1: Step = makeStep(model = "Nexus5", duration = 23) | ||
val step2: Step = makeStep(model = "SaIlFisH", duration = 245) | ||
val step3: Step = makeStep(model = "NEXUSLOWRES", duration = 352) | ||
val step4: Step = makeStep(model = "G3", duration = 657) | ||
|
||
val minutes = listOf(step1, step2, step3, step4).calculateAndroidBillableMinutes(projectId = "anyId", timeoutValue = timeout) | ||
|
||
verifyBilling(minutes, expectedVirtual, expectedPhysical) | ||
} | ||
|
||
@Test | ||
fun `should return billing for physical device when unable to find device type`() { | ||
val expectedPhysical = 3L | ||
val modelId = "uncommon" | ||
val step: Step = makeStep(model = modelId, duration = 123) | ||
|
||
val minutes = listOf(step).calculateAndroidBillableMinutes(projectId = "anyId", timeoutValue = 1000L) | ||
|
||
verifyBilling(minutes, expectedPhysical = expectedPhysical) | ||
assertTrue(output.log.contains("Unable to find device type for $modelId. PHYSICAL used as fallback in cost calculations")) | ||
} | ||
} | ||
|
||
private fun makeStep(model: String = "NexusLowRes", duration: Long = 15L): Step = make { | ||
dimensionValue = listOf(make { key = "Model"; value = model }) | ||
testExecutionStep = make { | ||
testTiming = make { | ||
testProcessDuration = make { seconds = duration } | ||
} | ||
} | ||
} | ||
|
||
private fun verifyBilling(billing: BillableMinutes, expectedVirtual: Long = 0, expectedPhysical: Long = 0) { | ||
assertEquals("Physical devices minutes should be $expectedPhysical", expectedPhysical, billing.physical) | ||
assertEquals("Virtual devices minutes should be $expectedVirtual", expectedVirtual, billing.virtual) | ||
} |
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