Skip to content

Commit

Permalink
PROM-5262 | debugging: evaluating ENVIRONMENT
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinFalkowski committed Feb 17, 2025
1 parent 583441c commit 8152c16
Showing 1 changed file with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@file:Suppress("DANGEROUS_CHARACTERS", "PrivatePropertyName", "ObjectPropertyName", "ObjectPrivatePropertyName")
@file:Suppress("DANGEROUS_CHARACTERS", "PrivatePropertyName", "ObjectPropertyName", "ObjectPrivatePropertyName",
"ClassName"
)

package pl.allegro.tech.servicemesh.envoycontrol.routing

Expand Down Expand Up @@ -41,6 +43,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.routing.CELTest.Companion.Config
import pl.allegro.tech.servicemesh.envoycontrol.routing.CELTest.Companion.Modifications.`access log stdout config`
import pl.allegro.tech.servicemesh.envoycontrol.routing.CELTest.Companion.Modifications.`add CELL formatter to access log`
import pl.allegro.tech.servicemesh.envoycontrol.routing.EchoExtension.echoService
import pl.allegro.tech.servicemesh.envoycontrol.routing.HeaderFromEnvironmentTest.`environment variable present test`.Companion
import pl.allegro.tech.servicemesh.envoycontrol.services.ServicesState
import java.io.ObjectInputFilter.Config
import java.lang.reflect.AnnotatedElement
Expand Down Expand Up @@ -247,33 +250,77 @@ class CELTest {

class HeaderFromEnvironmentTest {

@Test
fun `%ENVIRONMENT() in header works`() {
// given
val config = baseConfig.modify {
companion object {
private val config = baseConfig.modify {
listeners {
egress {
//language=yaml
http.at("/route_config/request_headers_to_add") += """
- header:
key: x-service-tag-preference-from-env
value: "%ENVIRONMENT(DEFAULT_SERVICE_TAG_PREFERENCE)%"
append_action: ADD_IF_ABSENT
""".trimIndent()
}
}
}
val envoy = staticEnvoyExtension(config = config, localService = echoService)
.apply { container.withEnv("DEFAULT_SERVICE_TAG_PREFERENCE", "lvte2|vte3|global") }
.asClosable()
}


// when
val response = envoy.use {
it.start()
it.extension.egressOperations.callService("echo").asHttpsEchoResponse()
@Nested
inner class `environment variable present`: `environment variable present test`()
open class `environment variable present test` {

companion object {
@JvmField
@RegisterExtension
val envoy = staticEnvoyExtension(config = config, localService = echoService)
.apply { container.withEnv("DEFAULT_SERVICE_TAG_PREFERENCE", "lvte2|vte3|global") }
}

@Test
fun `%ENVIRONMENT()% in header works`() {

// when
val response = envoy.egressOperations.callService("echo").asHttpsEchoResponse()

// then
assertThat(response.requestHeaders).containsEntry("x-service-tag-preference-from-env", "lvte2|vte3|global")
}

@Test
fun `should not override header from request`() {
// when
val response = envoy.egressOperations.callService(
service = "echo",
headers = mapOf("x-service-tag-preference-from-env" to "override-in-request")
).asHttpsEchoResponse()

// then
assertThat(response.requestHeaders).containsEntry("x-service-tag-preference-from-env", "override-in-request")
}
}


@Nested
inner class `environment variable not set` : `environment variable not set test`()
open class `environment variable not set test` {

companion object {
@JvmField
@RegisterExtension
val envoy = staticEnvoyExtension(config = config, localService = echoService)
}

@Test
fun `it sets the header to '-' value`() {
// when
val response = envoy.egressOperations.callService("echo").asHttpsEchoResponse()

// then
assertThat(response.requestHeaders).containsEntry("x-service-tag-preference-from-env", "-")
}

// then
assertThat(response.requestHeaders).containsEntry("x-service-tag-preference-from-env", "lvte2|vte3|global")
}
}

Expand All @@ -292,12 +339,6 @@ private object Assertions {
})
}

class A : (Int) -> Unit {
override fun invoke(p1: Int) {
TODO("Not yet implemented")
}
}

private class ConfigYaml private constructor(private val config: ObjectNode) {
private object S {
val yaml = Yaml()
Expand Down

0 comments on commit 8152c16

Please sign in to comment.