Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor: Refactor data scratch network profiles #1819

Merged
merged 5 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/NetworkProfileFetch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ftl.adapter

import ftl.adapter.google.toApiModel
import ftl.api.NetworkProfile
import ftl.client.google.getGoogleNetworkConfiguration

object NetworkProfileFetch :
NetworkProfile.Fetch,
() -> List<NetworkProfile> by {
getGoogleNetworkConfiguration().toApiModel()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ftl.adapter.google

import com.google.testing.model.NetworkConfiguration
import com.google.testing.model.TrafficRule
import ftl.api.NetworkProfile

internal fun List<NetworkConfiguration>.toApiModel() = map {
NetworkProfile(
it.id,
it.downRule.toDataRule(),
it.upRule.toDataRule()
)
}

private fun TrafficRule.toDataRule() = NetworkProfile.Rule(
bandwidth,
delay,
packetLossRatio,
packetDuplicationRatio,
burst
)
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/android/AndroidCatalog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ftl.config.Device
import ftl.environment.android.asPrintableTable
import ftl.environment.android.getDescription
import ftl.environment.asPrintableTable
import ftl.environment.common.asPrintableTable
import ftl.environment.common.toCliTable
import ftl.environment.getLocaleDescription
import ftl.gc.GcTesting
import ftl.http.executeWithRetry
Expand Down Expand Up @@ -44,7 +44,7 @@ object AndroidCatalog {

private fun getVersionsList(projectId: String) = deviceCatalog(projectId).versions

fun supportedOrientationsAsTable(projectId: String) = deviceCatalog(projectId).runtimeConfiguration.orientations.asPrintableTable()
fun supportedOrientationsAsTable(projectId: String) = deviceCatalog(projectId).runtimeConfiguration.orientations.toCliTable()

fun localesAsTable(projectId: String) = getLocales(projectId).asPrintableTable()

Expand Down
21 changes: 21 additions & 0 deletions test_runner/src/main/kotlin/ftl/api/NetworkProfile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ftl.api

import ftl.adapter.NetworkProfileFetch

val fetchNetworkProfiles: NetworkProfile.Fetch get() = NetworkProfileFetch

data class NetworkProfile(
val id: String,
val downRule: Rule,
val upRule: Rule
) {
data class Rule(
val bandwidth: Float?,
val delay: String?,
val packetLossRatio: Float?,
val packetDuplicationRatio: Float?,
val burst: Float?
)

interface Fetch : () -> List<NetworkProfile>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ftl.client.google

import ftl.gc.GcTesting
import ftl.http.executeWithRetry

fun getGoogleNetworkConfiguration() = GcTesting.get.testEnvironmentCatalog()
.get("NETWORK_CONFIGURATION")
.executeWithRetry()
?.networkConfigurationCatalog
?.configurations
.orEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ftl.domain

import flank.common.logLn
import ftl.android.AndroidCatalog
import ftl.api.fetchNetworkProfiles
import ftl.args.AndroidArgs
import ftl.environment.common.toCliTable
import ftl.environment.ipBlocksListAsTable
import ftl.environment.networkConfigurationAsTable
import ftl.environment.providedSoftwareAsTable
import java.nio.file.Paths

Expand All @@ -18,7 +19,8 @@ fun DescribeAndroidTestEnvironment.invoke() {
logLn(AndroidCatalog.supportedVersionsAsTable(projectId))
logLn(AndroidCatalog.localesAsTable(projectId))
logLn(providedSoftwareAsTable())
logLn(networkConfigurationAsTable())
// TODO move toCliTable() and printing presentation layer during refactor of presentation after #1728
logLn(fetchNetworkProfiles().toCliTable())
logLn(AndroidCatalog.supportedOrientationsAsTable(projectId))
logLn(ipBlocksListAsTable())
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ftl.domain

import flank.common.logLn
import ftl.api.fetchNetworkProfiles
import ftl.args.IosArgs
import ftl.environment.common.toCliTable
import ftl.environment.ipBlocksListAsTable
import ftl.environment.networkConfigurationAsTable
import ftl.environment.providedSoftwareAsTable
import ftl.ios.IosCatalog
import java.nio.file.Paths
Expand All @@ -18,7 +19,8 @@ operator fun DescribeIosTestEnvironment.invoke() {
logLn(IosCatalog.softwareVersionsAsTable(projectId))
logLn(IosCatalog.localesAsTable(projectId))
logLn(providedSoftwareAsTable())
logLn(networkConfigurationAsTable())
// TODO move toCliTable() and printing presentation layer during refactor of presentation after #1728
logLn(fetchNetworkProfiles().toCliTable())
logLn(IosCatalog.supportedOrientationsAsTable(projectId))
logLn(ipBlocksListAsTable())
}
6 changes: 4 additions & 2 deletions test_runner/src/main/kotlin/ftl/domain/ListNetworkProfiles.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ftl.domain

import flank.common.logLn
import ftl.environment.networkConfigurationAsTable
import ftl.api.fetchNetworkProfiles
import ftl.environment.common.toCliTable

interface ListNetworkProfiles

operator fun ListNetworkProfiles.invoke() {
// TODO move toCliTable() and printing presentation layer during refactor of presentation after #1728
logLn("fetching available network profiles...")
logLn(networkConfigurationAsTable())
logLn(fetchNetworkProfiles().toCliTable())
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package ftl.environment

import com.google.testing.model.NetworkConfiguration
import com.google.testing.model.TrafficRule
import ftl.api.NetworkProfile
import ftl.api.fetchNetworkProfiles

fun networkProfileDescription(profile: String) = getNetworkConfiguration()
.find { it.id?.toUpperCase() == profile.toUpperCase() }
fun networkProfileDescription(profile: String) = fetchNetworkProfiles()
.find { it.id.equals(profile, ignoreCase = true) }
.toNullProof()
.prepareDescription()
?: "Unable to fetch profile [$profile] description"

private fun NetworkConfiguration?.toNullProof() = this?.run {
private fun NetworkProfile?.toNullProof() = this?.run {
NetworkConfigurationWrapper(
downRule = wrappedOrEmpty(downRule),
upRule = wrappedOrEmpty(upRule),
id = id.toStringOrUnableToFetch()
)
}

private fun wrappedOrEmpty(rule: TrafficRule?) = rule?.let {
private fun wrappedOrEmpty(rule: NetworkProfile.Rule?) = rule?.let {
Rule(
bandwidth = it.bandwidth.toStringOrUnableToFetch(),
delay = it.delay.toStringOrUnableToFetch(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ftl.environment.common

import com.google.testing.model.NetworkConfiguration
import ftl.api.NetworkProfile
import ftl.environment.TestEnvironmentInfo
import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.util.TableStyle
import ftl.util.buildTable

fun List<NetworkConfiguration>.asPrintableTable() = createConfigurationDetails().createConfigurationsTable()
fun List<NetworkProfile>.toCliTable() = createConfigurationDetails().createConfigurationsTable()

private fun List<NetworkConfiguration>.createConfigurationDetails() = fold(mutableMapOf<String, MutableList<String>>()) { networkInfo, networkConfiguration ->
private fun List<NetworkProfile>.createConfigurationDetails() = fold(mutableMapOf<String, MutableList<String>>()) { networkInfo, networkConfiguration ->
networkInfo.apply {
getOrCreateList(PROFILE_ID).add(" ")
getOrCreateList(PROFILE_ID).add(networkConfiguration.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ftl.environment.tagToSystemOutColorMapper
import ftl.util.applyColorsUsing
import ftl.util.buildTable

fun List<Orientation>.asPrintableTable() = createOrientationsDetails().createOrientationsTable()
fun List<Orientation>.toCliTable() = createOrientationsDetails().createOrientationsTable()

private fun List<Orientation>.createOrientationsDetails() = fold(mutableMapOf<String, MutableList<String>>()) { orientationInfo, orientation ->
orientationInfo.apply {
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/ios/IosCatalog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ftl.ios
import com.google.testing.model.IosDeviceCatalog
import ftl.config.Device
import ftl.environment.asPrintableTable
import ftl.environment.common.asPrintableTable
import ftl.environment.common.toCliTable
import ftl.environment.getLocaleDescription
import ftl.environment.ios.asPrintableTable
import ftl.environment.ios.getDescription
Expand Down Expand Up @@ -39,7 +39,7 @@ object IosCatalog {
private fun getLocales(projectId: String) = iosDeviceCatalog(projectId).runtimeConfiguration.locales

fun supportedOrientationsAsTable(projectId: String) =
iosDeviceCatalog(projectId).runtimeConfiguration.orientations.asPrintableTable()
iosDeviceCatalog(projectId).runtimeConfiguration.orientations.toCliTable()

fun supportedXcode(version: String, projectId: String) = xcodeVersions(projectId).contains(version)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ftl.cli.firebase.test.networkprofiles

import com.google.testing.model.NetworkConfiguration
import com.google.testing.model.TrafficRule
import ftl.environment.getNetworkConfiguration
import ftl.api.NetworkProfile
import ftl.api.fetchNetworkProfiles
import ftl.presentation.cli.firebase.test.networkprofiles.NetworkProfilesDescribeCommand
import ftl.run.exception.FlankConfigurationError
import ftl.test.util.assertThrowsWithMessage
Expand All @@ -24,7 +23,7 @@ class NetworkProfileDescribeTest {

@Before
fun setup() {
mockkStatic("ftl.environment.NetworkConfigurationCatalogKt")
mockkStatic("ftl.api.NetworkProfileKt")
}

@After
Expand All @@ -40,18 +39,18 @@ class NetworkProfileDescribeTest {
@Test
fun `should print profile description if data exists`() {
val configs = listOf(
NetworkConfiguration().apply {
downRule = makeRule(0.5f)
upRule = makeRule(0.8f)
NetworkProfile(
downRule = makeRule(0.5f),
upRule = makeRule(0.8f),
id = "ANY"
},
NetworkConfiguration().apply {
downRule = makeRule(0.1f)
upRule = makeRule(0.2f)
),
NetworkProfile(
downRule = makeRule(0.1f),
upRule = makeRule(0.2f),
id = "DIFFERENT"
}
)
)
every { getNetworkConfiguration() } returns configs
every { fetchNetworkProfiles() } returns configs

systemOutRule.clearLog()
runMainCommand("network-profiles", "describe", "ANY")
Expand All @@ -74,7 +73,7 @@ class NetworkProfileDescribeTest {

@Test
fun `should handle case when API answers with null for configuration request`() {
every { getNetworkConfiguration() } returns emptyList()
every { fetchNetworkProfiles() } returns emptyList()

systemOutRule.clearLog()
runMainCommand("network-profiles", "describe", "NON-EXISTING")
Expand All @@ -88,23 +87,23 @@ class NetworkProfileDescribeTest {
@Test
fun `should print message if unable to find provided profile`() {
val configs = listOf(
NetworkConfiguration().apply {
downRule = makeRule(0.456f)
id = "ANY_1"
NetworkProfile(
downRule = makeRule(0.456f),
id = "ANY_1",
upRule = makeRule(0.111f)
},
NetworkConfiguration().apply {
downRule = makeRule(0.0976f)
id = "ANY_2"
),
NetworkProfile(
downRule = makeRule(0.0976f),
id = "ANY_2",
upRule = makeRule(0.234f)
},
NetworkConfiguration().apply {
downRule = makeRule(0.1f)
id = "ANY_3"
),
NetworkProfile(
downRule = makeRule(0.1f),
id = "ANY_3",
upRule = makeRule(0.11233f)
}
)
)
every { getNetworkConfiguration() } returns configs
every { fetchNetworkProfiles() } returns configs

systemOutRule.clearLog()
runMainCommand("network-profiles", "describe", "NON-EXISTING")
Expand All @@ -118,13 +117,19 @@ class NetworkProfileDescribeTest {
@Test
fun `should handle possible null values`() {
val configs = listOf(
NetworkConfiguration().apply {
downRule = makeRule(0.456f)
id = "WITH_NULLS"
upRule = TrafficRule().apply { packetLossRatio = 0.123f }
}
NetworkProfile(
downRule = makeRule(0.456f),
id = "WITH_NULLS",
upRule = NetworkProfile.Rule(
bandwidth = null,
delay = null,
packetLossRatio = 0.123f,
packetDuplicationRatio = 0F,
burst = 0F
)
)
)
every { getNetworkConfiguration() } returns configs
every { fetchNetworkProfiles() } returns configs

systemOutRule.clearLog()
runMainCommand("network-profiles", "describe", "WITH_NULLS")
Expand All @@ -146,8 +151,10 @@ class NetworkProfileDescribeTest {
}
}

private fun makeRule(ratio: Float) = TrafficRule().apply {
bandwidth = ratio
delay = "${ratio}s"
packetLossRatio = ratio
}
private fun makeRule(ratio: Float) = NetworkProfile.Rule(
bandwidth = ratio,
delay = "${ratio}s",
packetLossRatio = ratio,
packetDuplicationRatio = ratio,
burst = ratio
)
Loading