[英]How can I connect a test container in Quarkus to the Docker network of DevServices?
In my @QuarkusIntegrationTest
I want to use a MockServer testcontainer that simulates the responses to requests from my application.在我的
@QuarkusIntegrationTest
我想使用一个MockServer 测试容器来模拟对来自我的应用程序的请求的响应。 To start the MockServer I use testcontainers.要启动 MockServer,我使用 testcontainers。 The application uses http://localhost:port , where port is mockserverContainer.getMappedPort ().
应用程序使用http://localhost:port ,其中 port 是 mockserverContainer.getMappedPort ()。 It all works.
这一切都有效。
When I test the application with -Dquarkus.container-image.build=true
, I get the following problem: Quarkus creates a Docker network and connects the container with the application and the MongoDb test container to this network via the DevServices.当我使用
-Dquarkus.container-image.build=true
测试应用程序时,我遇到以下问题:Quarkus 创建一个 Docker 网络并通过 DevServices 将容器与应用程序和 MongoDb 测试容器连接到该网络。 I would also like to bring the MockServer test container into this network, but I can't.我也想将 MockServer 测试容器带入这个网络,但我不能。
Network.SHARED
that is available in the test is not the network that Quarkus uses for the DevServices.测试中可用的
Network.SHARED
不是 Quarkus 用于 DevServices 的网络。 Of course, I can use the host's IP address to access the MockServer from the application that is being tested, which complicates the build scripts because the host's IP address is not that easy to determine in every environment.当然,我可以使用主机的 IP 地址从正在测试的应用程序访问 MockServer,这会使构建脚本复杂化,因为主机的 IP 地址在每个环境中都不太容易确定。
Is there any way that the testcontainer can connect to the Docker network used by the Quarkus DevService? testcontainer 有什么办法可以连接到 Quarkus DevService 使用的 Docker 网络吗?
(Quarkus Version 2.3.0) (Quarkus 2.3.0 版)
The following example works with the -Dquarkus.container-image.build=false
option, ie if the application itself is not running in a container.以下示例使用
-Dquarkus.container-image.build=false
选项,即如果应用程序本身未在容器中运行。
@QuarkusIntegrationTest
@Testcontainers
public class JobTest {
@Container
public static MockServerContainer serverContainer = new MockServerContainer()
@BeforeAll
public static initMockServer() {
String json = ...
new MockServerClient(serverContainer.getHost(), serverContainer.getServerPort())
.when(request()
.withMethod("GET")
.withPath(".*"))
.respond(response()
.withStatusCode(Response.Status.OK.getStatusCode())
.withHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON)
.withBody(json));
}
@Test
@DisplayName("import recipe from website")
public void importRecipe() {
// The URL that the application wants to get data from. Is simulated by the MockServer.
var recipeUrl = String.format("http://localhost:%d/lasagne.html", serverContainer.getServerPort());
// The URL of the application that triggered the import function.
var jobUrl = String.format("http://localhost:%s/job", System.getProperty("quarkus.http.port"));
RestAssured
.given()
.body(createJob(recipeUrl))
.contentType(ContentType.JSON)
.when()
.post(jobUrl)
.then()
.statusCode(Response.Status.CREATED.getStatusCode())
.header("Location", r -> equalTo(url + "/" + r.path("jobId")));
}
}
As of Quarkus 2.5
, you will be able to do something like:从 Quarkus
2.5
,您将能够执行以下操作:
public class CustomResource implements QuarkusTestResourceLifecycleManager, DevServicesContext.ContextAware {
private Optional<String> containerNetworkId = Optional.empty();
@Override
public Map<String, String> start() {
// start a container here using containerNetworkId
return someMap;
}
@Override
public void stop() {
// close the container
}
@Override
public void setIntegrationTestContext(DevServicesContext context) {
containerNetworkId = context.containerNetworkId();
}
}
and your test would be:你的测试将是:
@QuarkusIntegrationTest
@QuarkusTestResource(CustomResource.class)
public class JobTest {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.