Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KSmigielski committed Nov 11, 2022
1 parent e8a3971 commit 9fd2ce3
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MetricsDiscoveryServerCallbacks(private val meterRegistry: MeterRegistry)

meterRegistry.gauge("grpc.all-connections", connections)
connectionsByType.forEach { (type, typeConnections) ->
meterRegistry.gauge("grpc.connections.${type.name.toLowerCase()}", typeConnections)
meterRegistry.gauge("grpc.connections.${type.name.lowercase()}", typeConnections)
}
}

Expand All @@ -51,15 +51,15 @@ class MetricsDiscoveryServerCallbacks(private val meterRegistry: MeterRegistry)
}

override fun onV3StreamRequest(streamId: Long, request: V3DiscoveryRequest) {
meterRegistry.counter("grpc.requests.${StreamType.fromTypeUrl(request.typeUrl).name.toLowerCase()}")
meterRegistry.counter("grpc.requests.${StreamType.fromTypeUrl(request.typeUrl).name.lowercase()}")
.increment()
}

override fun onV3StreamDeltaRequest(
streamId: Long,
request: V3DeltaDiscoveryRequest
) {
meterRegistry.counter("grpc.requests.${StreamType.fromTypeUrl(request.typeUrl).name.toLowerCase()}.delta")
meterRegistry.counter("grpc.requests.${StreamType.fromTypeUrl(request.typeUrl).name.lowercase()}.delta")
.increment()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class EnvoySnapshotFactory(
routes.add(
egressRoutesFactory.createEgressDomainRoutes(
it.value,
it.key.port.toString().toLowerCase()
it.key.port.toString().lowercase()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class EnvoyClustersFactory(
thresholdsBuilder.maxPendingRequests = UInt32Value.of(threshold.maxPendingRequests)
thresholdsBuilder.maxRequests = UInt32Value.of(threshold.maxRequests)
thresholdsBuilder.maxRetries = UInt32Value.of(threshold.maxRetries)
when (threshold.priority.toUpperCase()) {
when (threshold.priority.uppercase()) {
"DEFAULT" -> thresholdsBuilder.priority = RoutingPriority.DEFAULT
"HIGH" -> thresholdsBuilder.priority = RoutingPriority.HIGH
else -> thresholdsBuilder.priority = RoutingPriority.UNRECOGNIZED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package pl.allegro.tech.servicemesh.envoycontrol.snapshot.resource.listeners.fil
import com.google.protobuf.BoolValue
import com.google.protobuf.Duration
import com.google.protobuf.util.Durations
import io.envoyproxy.envoy.config.core.v3.ApiConfigSource
import io.envoyproxy.envoy.config.core.v3.AggregatedConfigSource
import io.envoyproxy.envoy.config.core.v3.ApiVersion
import io.envoyproxy.envoy.config.core.v3.ConfigSource
import io.envoyproxy.envoy.config.core.v3.GrpcService
import io.envoyproxy.envoy.config.core.v3.Http1ProtocolOptions
import io.envoyproxy.envoy.config.core.v3.HttpProtocolOptions
import io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
Expand Down Expand Up @@ -34,7 +33,6 @@ class HttpConnectionManagerFactory(
snapshotProperties.dynamicForwardProxy
).filter

private val defaultApiConfigSourceV3: ApiConfigSource = apiConfigSource()
private val accessLogFilter = AccessLogFilter(snapshotProperties)

@SuppressWarnings("LongParameterList")
Expand Down Expand Up @@ -93,7 +91,7 @@ class HttpConnectionManagerFactory(
connectionManagerBuilder.addAccessLog(
accessLogFilter.createFilter(
listenersConfig.accessLogPath,
direction.name.toLowerCase(),
direction.name.lowercase(),
listenersConfig.accessLogFilterSettings
)
)
Expand All @@ -106,19 +104,16 @@ class HttpConnectionManagerFactory(
private fun setupRds(
rdsInitialFetchTimeout: Duration,
routeConfigName: String
): Rds {
val configSource = ConfigSource.newBuilder()
.setInitialFetchTimeout(rdsInitialFetchTimeout)
.setResourceApiVersion(ApiVersion.V3)
.setApiConfigSource(defaultApiConfigSourceV3)

return Rds.newBuilder()
): Rds = Rds.newBuilder()
.setRouteConfigName(routeConfigName)
.setConfigSource(
configSource.build()
ConfigSource.newBuilder()
.setInitialFetchTimeout(rdsInitialFetchTimeout)
.setResourceApiVersion(ApiVersion.V3)
.setAds(AggregatedConfigSource.getDefaultInstance())
.build()
)
.build()
}

private fun ingressHttp1ProtocolOptions(serviceName: String): Http1ProtocolOptions? {
return Http1ProtocolOptions.newBuilder()
Expand All @@ -127,25 +122,6 @@ class HttpConnectionManagerFactory(
.build()
}

private fun apiConfigSource(): ApiConfigSource {
return ApiConfigSource.newBuilder()
.setApiType(
if (snapshotProperties.deltaXdsEnabled) {
ApiConfigSource.ApiType.DELTA_GRPC
} else {
ApiConfigSource.ApiType.GRPC
}
)
.setTransportApiVersion(ApiVersion.V3)
.addGrpcServices(
GrpcService.newBuilder()
.setEnvoyGrpc(
GrpcService.EnvoyGrpc.newBuilder()
.setClusterName(snapshotProperties.xdsClusterName)
)
).build()
}

private fun addHttpFilters(
connectionManagerBuilder: HttpConnectionManager.Builder,
filterFactories: List<HttpFilterFactory>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,8 @@ class NodeMetadataValidatorTest {
// expects
assertThatExceptionOfType(ServiceNameNotProvidedException::class.java)
.isThrownBy { requireServiceNameValidator.onV3StreamRequest(streamId = 123, request = request) }
.satisfies {
assertThat(it.status.description).isEqualTo(
"Service name has not been provided."
)
assertThat(it.status.code).isEqualTo(Status.Code.INVALID_ARGUMENT)
}
.matches { it.status.description == "Service name has not been provided." }
.matches { it.status.code == Status.Code.INVALID_ARGUMENT }
}

@Test
Expand Down Expand Up @@ -187,12 +183,8 @@ class NodeMetadataValidatorTest {
// then
assertThatExceptionOfType(RateLimitIncorrectValidationException::class.java)
.isThrownBy { validator.onV3StreamRequest(123, request = request) }
.satisfies {
assertThat(it.status.description).isEqualTo(
"Rate limit value: 0/j is incorrect."
)
assertThat(it.status.code).isEqualTo(Status.Code.INVALID_ARGUMENT)
}
.matches { it.status.description == "Rate limit value: 0/j is incorrect." }
.matches { it.status.code == Status.Code.INVALID_ARGUMENT }
}

private fun createIncomingPermissions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ open class EndpointMetadataMergingTests {
protected open fun callEchoServiceRepeatedly(
repeat: Int,
tag: String,
assertNoErrors: Boolean = true
): CallStats {
val stats = CallStats(listOf(service))
envoy.egressOperations.callServiceRepeatedly(
Expand All @@ -64,7 +63,7 @@ open class EndpointMetadataMergingTests {
minRepeat = repeat,
maxRepeat = repeat,
headers = mapOf("x-service-tag" to tag),
assertNoErrors = assertNoErrors
assertNoErrors = true
)
return stats
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import pl.allegro.tech.servicemesh.envoycontrol.config.envoy.EnvoyExtension
import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.EnvoyControlExtension
import pl.allegro.tech.servicemesh.envoycontrol.config.service.EchoServiceExtension

class AdsEnvoyControlHttp2Test : EnvoyControlHttp2Test {
class EnvoyControlHttp2Test {

companion object {

@JvmField
Expand All @@ -37,63 +38,40 @@ class AdsEnvoyControlHttp2Test : EnvoyControlHttp2Test {
val secondEnvoy = EnvoyExtension(envoyControl, service, config = Ads)
}

override fun consul() = consul

override fun envoyControl() = envoyControl

override fun service() = service

override fun envoy() = envoy

override fun secondEnvoy() = secondEnvoy
}

interface EnvoyControlHttp2Test {

fun consul(): ConsulExtension

fun envoyControl(): EnvoyControlExtension

fun service(): EchoServiceExtension

fun envoy(): EnvoyExtension

fun secondEnvoy(): EnvoyExtension

@Test
fun `should establish http2 connection between envoys`() {
// given
consul().server.operations.registerService(
consul.server.operations.registerService(
name = "proxy1",
address = secondEnvoy().container.ipAddress(),
address = secondEnvoy.container.ipAddress(),
port = EnvoyContainer.INGRESS_LISTENER_CONTAINER_PORT,
tags = listOf("envoy")
)

untilAsserted {
// when
val response = envoy().egressOperations.callService("proxy1")
val response = envoy.egressOperations.callService("proxy1")
assertThat(response).isOk()

// then
assertDidNotUseHttp1(envoy())
assertUsedHttp2(envoy())
assertDidNotUseHttp1(envoy)
assertUsedHttp2(envoy)
}
}

@Test
fun `should establish http1 connection between envoy and a service by default`() {
// given
consul().server.operations.registerService(service(), name = "echo")
consul.server.operations.registerService(service, name = "echo")

untilAsserted {
// when
val response = envoy().egressOperations.callService("echo")
assertThat(response).isOk().isFrom(service())
val response = envoy.egressOperations.callService("echo")
assertThat(response).isOk().isFrom(service)

// then
assertUsedHttp1(envoy())
assertDidNotUseHttp2(envoy())
assertUsedHttp1(envoy)
assertDidNotUseHttp2(envoy)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.allegro.tech.servicemesh.envoycontrol

import io.micrometer.core.instrument.MeterRegistry
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
Expand All @@ -19,6 +18,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.server.callbacks.MetricsDiscover
import pl.allegro.tech.servicemesh.envoycontrol.server.callbacks.MetricsDiscoveryServerCallbacks.StreamType.RDS
import pl.allegro.tech.servicemesh.envoycontrol.server.callbacks.MetricsDiscoveryServerCallbacks.StreamType.SDS
import pl.allegro.tech.servicemesh.envoycontrol.server.callbacks.MetricsDiscoveryServerCallbacks.StreamType.UNKNOWN
import java.time.Duration

class AdsMetricsDiscoveryServerCallbackTest : MetricsDiscoveryServerCallbacksTest {
companion object {
Expand Down Expand Up @@ -59,20 +59,20 @@ class AdsMetricsDiscoveryServerCallbackTest : MetricsDiscoveryServerCallbacksTes
)

override fun expectedGrpcRequestsCounterValues() = mapOf(
CDS.name.toLowerCase() to isGreaterThanZero(),
EDS.name.toLowerCase() to isGreaterThanZero(),
LDS.name.toLowerCase() to isGreaterThanZero(),
RDS.name.toLowerCase() to isGreaterThanZero(),
SDS.name.toLowerCase() to isNull(),
ADS.name.toLowerCase() to isNull(),
UNKNOWN.name.toLowerCase() to isNull(),
"${CDS.name.toLowerCase()}.delta" to isNull(),
"${EDS.name.toLowerCase()}.delta" to isNull(),
"${LDS.name.toLowerCase()}.delta" to isNull(),
"${RDS.name.toLowerCase()}.delta" to isNull(),
"${SDS.name.toLowerCase()}.delta" to isNull(),
"${ADS.name.toLowerCase()}.delta" to isNull(),
"${UNKNOWN.name.toLowerCase()}.delta" to isNull()
CDS.name.lowercase() to isGreaterThanZero(),
EDS.name.lowercase() to isGreaterThanZero(),
LDS.name.lowercase() to isGreaterThanZero(),
RDS.name.lowercase() to isGreaterThanZero(),
SDS.name.lowercase() to isNull(),
ADS.name.lowercase() to isNull(),
UNKNOWN.name.lowercase() to isNull(),
"${CDS.name.lowercase()}.delta" to isNull(),
"${EDS.name.lowercase()}.delta" to isNull(),
"${LDS.name.lowercase()}.delta" to isNull(),
"${RDS.name.lowercase()}.delta" to isNull(),
"${SDS.name.lowercase()}.delta" to isNull(),
"${ADS.name.lowercase()}.delta" to isNull(),
"${UNKNOWN.name.lowercase()}.delta" to isNull()
)
}

Expand Down Expand Up @@ -105,7 +105,7 @@ class DeltaAdsMetricsDiscoveryServerCallbackTest : MetricsDiscoveryServerCallbac
override fun envoy() = envoy

override fun expectedGrpcConnectionsGaugeValues() = mapOf(
CDS to 0,
CDS to 1,
EDS to 0,
LDS to 0,
RDS to 0,
Expand All @@ -115,20 +115,20 @@ class DeltaAdsMetricsDiscoveryServerCallbackTest : MetricsDiscoveryServerCallbac
)

override fun expectedGrpcRequestsCounterValues() = mapOf(
CDS.name.toLowerCase() to isNull(),
EDS.name.toLowerCase() to isNull(),
LDS.name.toLowerCase() to isNull(),
RDS.name.toLowerCase() to isNull(),
SDS.name.toLowerCase() to isNull(),
ADS.name.toLowerCase() to isNull(),
UNKNOWN.name.toLowerCase() to isNull(),
"${CDS.name.toLowerCase()}.delta" to isGreaterThanZero(),
"${EDS.name.toLowerCase()}.delta" to isGreaterThanZero(),
"${LDS.name.toLowerCase()}.delta" to isGreaterThanZero(),
"${RDS.name.toLowerCase()}.delta" to isGreaterThanZero(),
"${SDS.name.toLowerCase()}.delta" to isNull(),
"${ADS.name.toLowerCase()}.delta" to isNull(),
"${UNKNOWN.name.toLowerCase()}.delta" to isNull()
CDS.name.lowercase() to isNull(),
EDS.name.lowercase() to isNull(),
LDS.name.lowercase() to isNull(),
RDS.name.lowercase() to isNull(),
SDS.name.lowercase() to isNull(),
ADS.name.lowercase() to isNull(),
UNKNOWN.name.lowercase() to isNull(),
"${CDS.name.lowercase()}.delta" to isGreaterThanZero(),
"${EDS.name.lowercase()}.delta" to isGreaterThanZero(),
"${LDS.name.lowercase()}.delta" to isGreaterThanZero(),
"${RDS.name.lowercase()}.delta" to isGreaterThanZero(),
"${SDS.name.lowercase()}.delta" to isNull(),
"${ADS.name.lowercase()}.delta" to isNull(),
"${UNKNOWN.name.lowercase()}.delta" to isNull()
)
}

Expand All @@ -146,9 +146,7 @@ interface MetricsDiscoveryServerCallbacksTest {

fun expectedGrpcRequestsCounterValues(): Map<String, (Int?) -> Boolean>

fun MeterRegistry.counterValue(name: String) = this.find(name).counter()?.count()?.toInt()

fun isGreaterThanZero() = { x: Int? -> x!! > 0 }
fun isGreaterThanZero() = { x: Int? -> x != null && x > 0 }

fun isNull() = { x: Int? -> x == null }

Expand All @@ -159,11 +157,14 @@ interface MetricsDiscoveryServerCallbacksTest {
consul().server.operations.registerService(service(), name = "echo")

// expect
untilAsserted {
untilAsserted(wait = Duration.ofSeconds(5)) {
expectedGrpcConnectionsGaugeValues().forEach { (type, value) ->
val metric = "grpc.connections.${type.name.toLowerCase()}"
assertThat(meterRegistry.find(metric).gauge()).isNotNull
assertThat(meterRegistry.get(metric).gauge().value().toInt()).isEqualTo(value)
val metric = "grpc.connections.${type.name.lowercase()}"
assertThat(meterRegistry.find(metric).gauge())
.withFailMessage("Metric $metric should not be null")
.isNotNull
.withFailMessage("Value of metric $metric should be $value")
.matches { it.value().toInt() == value }
}
}
}
Expand All @@ -175,11 +176,12 @@ interface MetricsDiscoveryServerCallbacksTest {
consul().server.operations.registerService(service(), name = "echo")

// expect
untilAsserted {
untilAsserted(wait = Duration.ofSeconds(5)) {
expectedGrpcRequestsCounterValues().forEach { (type, condition) ->
val counterValue = meterRegistry.counterValue("grpc.requests.$type")
println("$type $counterValue")
assertThat(counterValue).satisfies { condition(it) }
val metric = "grpc.requests.$type"
assertThat(meterRegistry.find(metric).counter()?.count()?.toInt())
.withFailMessage("Metric $metric does not meet the condition")
.matches(condition)
}
}
}
Expand Down
Loading

0 comments on commit 9fd2ce3

Please sign in to comment.