Quarkus 环境变量与测试容器的问题
使用测试容器开发集成测试。 application.properties 文件中只有几个字段作为环境变量(例如:将其作为 quarkus.datasource.username=${SER_DB_USERNAME:postgres} 传递)。
通过测试容器设置环境字段时,
GenericContainer<?> someService = new GenericContainer<>(img)
.withEnv("SER_DB_USERNAME", DataLayer.DB_USERNAME)
测试容器已成功获取该值,但
对于以下环境变量, 在 application.properties 文件中定义的 app.security.enabled=${SER_SEC_ENABLE:true}
@IfBuildProperty(name = "app.security.enabled", stringValue = "true")
环境变量是通过 cmd 提示符使用 -DSER_SEC_ENABLED=true 设置的,但是当尝试在测试容器中传递相同的值时,它始终为空。
GenericContainer<?> someService = new GenericContainer<>(img)
.withEnv("SER_SEC_ENABLE", "true")
Developed the integration tests using Test container. Have few fields as environment variables(Eg: passing it as quarkus.datasource.username=${SER_DB_USERNAME:postgres}
) in application.properties file.
When setting environment field through test container
GenericContainer<?> someService = new GenericContainer<>(img)
.withEnv("SER_DB_USERNAME", DataLayer.DB_USERNAME)
This value is being successfully taken with test containers but
For the below environment variable,app.security.enabled=${SER_SEC_ENABLE:true}
defined in application.properties file
@IfBuildProperty(name = "app.security.enabled", stringValue = "true")
the environment variable is setting through cmd prompt using -DSER_SEC_ENABLED=true, but when trying to pass the same value in test containers, it's always null.
GenericContainer<?> someService = new GenericContainer<>(img)
.withEnv("SER_SEC_ENABLE", "true")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有更多的项目上下文,我至少可以观察到,app.security.enabled 是一个构建属性而不是运行时属性,因此它可能已经在构建时进行了评估。如果您使用已构建的映像/应用程序启动容器,则环境变量很可能不起作用。
此外,使用
-D
标志在 JVM 上设置属性不会产生环境变量,这明确是 JVM 上的系统属性。Without having more context of the project, I can at least observe, that
app.security.enabled
is a build property rather than a runtime property, so it might be evaluated at build time already. If you start the container with an already built image/application, it is very likely, that the environment variable has no effect.Furthermore, setting a property on the JVM using the
-D
flag does not result in an environment variable, this is explicitly a system property on the JVM.