Skip to content

Commit

Permalink
PROM-5262 | debugging: evaluating CEL
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinFalkowski committed Feb 17, 2025
1 parent 29e0199 commit 5f53595
Showing 1 changed file with 52 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.micrometer.core.instrument.MeterRegistry
import okhttp3.Response
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.api.ObjectAssert
import org.assertj.core.api.StringAssert
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand All @@ -32,6 +33,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.Health
import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.SnapshotDebugResponse
import pl.allegro.tech.servicemesh.envoycontrol.config.service.GenericServiceExtension
import pl.allegro.tech.servicemesh.envoycontrol.config.service.HttpsEchoContainer
import pl.allegro.tech.servicemesh.envoycontrol.routing.Assertions.failsAtStartupWithError
import pl.allegro.tech.servicemesh.envoycontrol.services.ServicesState
import java.lang.reflect.AnnotatedElement
import java.lang.reflect.Method
Expand Down Expand Up @@ -84,6 +86,7 @@ private val baseConfig = ConfigYaml("/envoy/debug/config_base.yaml")

class CELTest {


companion object {
private val `baseConfig with node metadata default_service_tag_preference` = baseConfig.modify {
//language=yaml
Expand All @@ -93,6 +96,9 @@ class CELTest {
default_service_tag_preference: lvte1|vte2|global
""".trimIndent()
}

private val `CEL failure log` =
"[critical].*error initializing config .* Not supported field in StreamInfo: CEL".toRegex()
}

@Nested
Expand Down Expand Up @@ -126,22 +132,12 @@ class CELTest {
// given
val envoy = staticEnvoyExtension(config = `config with CEL in accessLog`).asClosable()

val expectedFailureLog =
"[critical].*error initializing config .* Not supported field in StreamInfo: CEL".toRegex()
envoy.extension.container.logRecorder.recordLogs {
expectedFailureLog.containsMatchIn(it)
// [2025-02-14 19:27:20.111][134][critical][main] [source/server/server.cc:414] error initializing config ' /tmp/tmp.ZXJQoHVwcp/envoy.yaml': Not supported field in StreamInfo: CEL
}

// expects
assertThatThrownBy { envoy.use { it.start() } }

val failureLogs = envoy.extension.container.logRecorder.getRecordedLogs()
assertThat(envoy).failsAtStartupWithError(`CEL failure log`)

assertThat(failureLogs, StringAssert::class.java)
.first().containsPattern(expectedFailureLog.pattern)
}


@Test
fun `use %CEL()% in access log with explicitly enabled CEL formatter works`() {
// given
Expand Down Expand Up @@ -203,40 +199,25 @@ class CELTest {
val envoy = staticEnvoyExtension(config = `config with request_headers_to_add with %CEL()%`)
.asClosable()

// [2025-02-16 23:19:24.339][134][critical][main] [source/server/server.cc:414] error initializing config ' /tmp/tmp.5YgxPJYgps/envoy.yaml': Not supported field in StreamInfo: CEL

// expects
assertThatThrownBy { envoy.use { it.start() } }


// then
assertThat(3).isEqualTo(2)
assertThat(envoy).failsAtStartupWithError(`CEL failure log`)
}
}
}

private object Assertions {
fun ObjectAssert<ClosableEnvoyExtension>.failsAtStartupWithError(expectedFailureLog: Regex) = satisfies({
it.extension.container.logRecorder.recordLogs { log ->
expectedFailureLog.containsMatchIn(log)
}

assertThatThrownBy { it.use(ClosableEnvoyExtension::start) }

val failureLogs = it.extension.container.logRecorder.getRecordedLogs()

// @Nested
// inner class Zupa : DDTest()
//
// @Test
// fun buga() {
// assertThat("").isEqualTo("fwef")
// }
//
// open class DDTest {
//
// companion object {
// @JvmField
// @RegisterExtension
// val echoService = GenericServiceExtension(HttpsEchoContainer())
// }
//
// @Test
// fun ogaBoga() {
// assertThat(echoService.container().ipAddress()).isEqualTo("122")
// assertThat(3).isEqualTo(5)
// }
// }
assertThat(failureLogs, StringAssert::class.java)
.first().containsPattern(expectedFailureLog.pattern)
})
}

private class ConfigYaml private constructor(private val config: ObjectNode) {
Expand Down Expand Up @@ -407,36 +388,6 @@ private class ConfigYaml private constructor(private val config: ObjectNode) {
}
}

class TempYamlLoader {

val yaml = Yaml()
val json = ObjectMapper()

@Test
fun debug() {

val baseConfigYaml: Map<Any, Any>? = javaClass.getResourceAsStream("/envoy/debug/config_base.yaml")
.let { requireNotNull(it) { "file not found" } }
.use { yaml.load(it) }

val baseConfigJson: ObjectNode = json.valueToTree(baseConfigYaml)

val someList = baseConfigJson.requiredAt("/some/list").let { it as ArrayNode }

val newFirstElement = json.createObjectNode().apply {
put("name", "inserted at index 0, yeah")
put("val", 333)
}
someList.insert(0, newFirstElement)

val out = json.convertValue(baseConfigJson, Map::class.java)
val outYaml = yaml.dump(out)

val outJson = json.writeValueAsString(baseConfigJson)
assertThat(out).isEqualTo("dupa")
}
}

private fun staticEnvoyExtension(
config: ConfigYaml,
localService: GenericServiceExtension<HttpsEchoContainer>? = null
Expand Down Expand Up @@ -599,3 +550,33 @@ val contextMock = object : ExtensionContext {
TODO("Not yet implemented")
}
}

class TempYamlLoader {

val yaml = Yaml()
val json = ObjectMapper()

@Test
fun debug() {

val baseConfigYaml: Map<Any, Any>? = javaClass.getResourceAsStream("/envoy/debug/config_base.yaml")
.let { requireNotNull(it) { "file not found" } }
.use { yaml.load(it) }

val baseConfigJson: ObjectNode = json.valueToTree(baseConfigYaml)

val someList = baseConfigJson.requiredAt("/some/list").let { it as ArrayNode }

val newFirstElement = json.createObjectNode().apply {
put("name", "inserted at index 0, yeah")
put("val", 333)
}
someList.insert(0, newFirstElement)

val out = json.convertValue(baseConfigJson, Map::class.java)
val outYaml = yaml.dump(out)

val outJson = json.writeValueAsString(baseConfigJson)
assertThat(out).isEqualTo("dupa")
}
}

0 comments on commit 5f53595

Please sign in to comment.