Skip to content
New issue

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

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

Already on GitHub? Sign in 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 all commits
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
)
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 @@ -3,11 +3,11 @@ package ftl.domain
import flank.common.logLn
import ftl.api.Platform
import ftl.api.fetchIpBlocks
import ftl.api.fetchNetworkProfiles
import ftl.api.fetchOrientation
import ftl.args.AndroidArgs
import ftl.client.google.AndroidCatalog
import ftl.environment.common.toCliTable
import ftl.environment.networkConfigurationAsTable
import ftl.environment.providedSoftwareAsTable
import ftl.environment.toCliTable
import java.nio.file.Paths
Expand All @@ -22,7 +22,7 @@ fun DescribeAndroidTestEnvironment.invoke() {
logLn(AndroidCatalog.supportedVersionsAsTable(projectId))
logLn(AndroidCatalog.localesAsTable(projectId))
logLn(providedSoftwareAsTable())
logLn(networkConfigurationAsTable())
logLn(fetchNetworkProfiles().toCliTable())
// TODO move toCliTable() to presentation layer during refactor of presentation after #1728
logLn(fetchOrientation(projectId, Platform.ANDROID).toCliTable())
logLn(fetchIpBlocks().toCliTable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ftl.domain
import flank.common.logLn
import ftl.api.Platform
import ftl.api.fetchIpBlocks
import ftl.api.fetchNetworkProfiles
import ftl.api.fetchOrientation
import ftl.args.IosArgs
import ftl.environment.common.toCliTable
import ftl.environment.networkConfigurationAsTable
import ftl.environment.providedSoftwareAsTable
import ftl.environment.toCliTable
import ftl.ios.IosCatalog
Expand All @@ -22,7 +22,7 @@ operator fun DescribeIosTestEnvironment.invoke() {
logLn(IosCatalog.softwareVersionsAsTable(projectId))
logLn(IosCatalog.localesAsTable(projectId))
logLn(providedSoftwareAsTable())
logLn(networkConfigurationAsTable())
logLn(fetchNetworkProfiles().toCliTable())
// TODO move toCliTable() and printing presentation layer during refactor of presentation after #1728
logLn(fetchOrientation(projectId, Platform.IOS).toCliTable())
logLn(fetchIpBlocks().toCliTable())
Expand Down
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>.toCliTable() = 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
@@ -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
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ftl.cli.firebase.test.networkprofiles

import ftl.environment.networkConfigurationAsTable
import ftl.gc.GcTesting
import ftl.api.NetworkProfile
import ftl.api.fetchNetworkProfiles
import ftl.environment.common.toCliTable
import ftl.presentation.cli.firebase.test.networkprofiles.NetworkProfilesListCommand
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
Expand All @@ -17,9 +17,8 @@ class NetworkProfilesListCommandTest {

@Before
fun setUp() {
mockkStatic(
"ftl.environment.NetworkConfigurationCatalogKt"
)
mockkStatic(List<NetworkProfile>::toCliTable)
mockkStatic(::fetchNetworkProfiles)
}

@After
Expand All @@ -29,12 +28,13 @@ class NetworkProfilesListCommandTest {

@Test
fun run() {
mockkObject(GcTesting) {
every {
networkConfigurationAsTable()
} returns ""
CommandLine(NetworkProfilesListCommand()).execute()
verify { networkConfigurationAsTable() }
}
every {
fetchNetworkProfiles()
} returns emptyList()
every {
any<List<NetworkProfile>>().toCliTable()
} returns ""
CommandLine(NetworkProfilesListCommand()).execute()
verify { any<List<NetworkProfile>>().toCliTable() }
}
}