|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2026 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | +package io.vertx.tests.grpc; |
| 12 | + |
| 13 | +import com.github.dockerjava.api.DockerClient; |
| 14 | +import com.github.dockerjava.api.command.WaitContainerCmd; |
| 15 | +import com.github.dockerjava.api.command.WaitContainerResultCallback; |
| 16 | +import io.netty.util.internal.PlatformDependent; |
| 17 | +import io.vertx.core.Vertx; |
| 18 | +import io.vertx.core.http.HttpClient; |
| 19 | +import io.vertx.core.http.HttpClientOptions; |
| 20 | +import io.vertx.core.http.HttpServer; |
| 21 | +import io.vertx.core.http.HttpServerOptions; |
| 22 | +import io.vertx.core.net.SocketAddress; |
| 23 | +import io.vertx.httpproxy.HttpProxy; |
| 24 | +import io.vertx.httpproxy.ProxyOptions; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | +import org.testcontainers.containers.GenericContainer; |
| 30 | +import org.testcontainers.containers.InternetProtocol; |
| 31 | +import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy; |
| 32 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 33 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
| 34 | + |
| 35 | +import java.time.Duration; |
| 36 | + |
| 37 | +import static org.junit.Assert.assertEquals; |
| 38 | +import static org.junit.Assume.assumeFalse; |
| 39 | + |
| 40 | +public class GrpcProxyIntegrationTest { |
| 41 | + |
| 42 | + private static final int GRPC_SERVER_PORT = 50051; |
| 43 | + private static final int PROXY_PORT = 8080; |
| 44 | + |
| 45 | + private Vertx vertx; |
| 46 | + private HttpServer proxyServer; |
| 47 | + private HttpClient httpClient; |
| 48 | + private ServerContainer<?> grpcServerContainer; |
| 49 | + private GenericContainer<?> grpcClientContainer; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void beforeClass() throws Exception { |
| 53 | + assumeFalse("Cannot run Linux containers on Windows", PlatformDependent.isWindows()); |
| 54 | + } |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() throws Exception { |
| 58 | + vertx = Vertx.vertx(); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void tearDownContainers() { |
| 63 | + if (grpcClientContainer != null) { |
| 64 | + grpcClientContainer.stop(); |
| 65 | + } |
| 66 | + if (grpcServerContainer != null) { |
| 67 | + grpcServerContainer.stop(); |
| 68 | + } |
| 69 | + if (proxyServer != null) { |
| 70 | + proxyServer.close().await(); |
| 71 | + } |
| 72 | + if (httpClient != null) { |
| 73 | + httpClient.close().await(); |
| 74 | + } |
| 75 | + if (vertx != null) { |
| 76 | + vertx.close().await(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testGrpcThroughProxy() throws Exception { |
| 82 | + startGrpcServer(); |
| 83 | + startProxy(); |
| 84 | + int exitCode = runGrpcClient(); |
| 85 | + assertEquals("gRPC client tests should pass", 0, exitCode); |
| 86 | + } |
| 87 | + |
| 88 | + private void startProxy() { |
| 89 | + httpClient = vertx.createHttpClient(new HttpClientOptions() |
| 90 | + .setProtocolVersion(io.vertx.core.http.HttpVersion.HTTP_2) |
| 91 | + .setHttp2ClearTextUpgrade(false)); |
| 92 | + |
| 93 | + SocketAddress backend = SocketAddress.inetSocketAddress(grpcServerContainer.getMappedPort(GRPC_SERVER_PORT), grpcServerContainer.getHost()); |
| 94 | + HttpProxy proxy = HttpProxy.reverseProxy(new ProxyOptions(), httpClient).origin(backend); |
| 95 | + |
| 96 | + proxyServer = vertx.createHttpServer(new HttpServerOptions() |
| 97 | + .setPort(PROXY_PORT) |
| 98 | + .setHost("0.0.0.0") |
| 99 | + .setHttp2ClearTextEnabled(true)) |
| 100 | + .requestHandler(proxy) |
| 101 | + .listen() |
| 102 | + .await(); |
| 103 | + } |
| 104 | + |
| 105 | + private void startGrpcServer() throws Exception { |
| 106 | + grpcServerContainer = new ServerContainer<>(new ImageFromDockerfile("vertx-http-proxy-grpc-server", false) |
| 107 | + .withFileFromClasspath("Dockerfile", "grpc/server/Dockerfile") |
| 108 | + .withFileFromClasspath("server.js", "grpc/server/server.js") |
| 109 | + .withFileFromClasspath("package.json", "grpc/package.json") |
| 110 | + .withFileFromClasspath("test.proto", "grpc/test.proto")); |
| 111 | + if (System.getProperties().containsKey("containerFixedPort")) { |
| 112 | + grpcServerContainer.withFixedExposedPort(GRPC_SERVER_PORT, GRPC_SERVER_PORT); |
| 113 | + } else { |
| 114 | + grpcServerContainer.withExposedPorts(GRPC_SERVER_PORT); |
| 115 | + } |
| 116 | + grpcServerContainer |
| 117 | + .withEnv("GRPC_PORT", String.valueOf(GRPC_SERVER_PORT)) |
| 118 | + .withEnv("GRPC_HOST", "0.0.0.0") |
| 119 | + .waitingFor(Wait.forLogMessage(".*gRPC server listening.*", 1)); |
| 120 | + |
| 121 | + grpcServerContainer.start(); |
| 122 | + } |
| 123 | + |
| 124 | + private int runGrpcClient() throws Exception { |
| 125 | + grpcClientContainer = new GenericContainer<>( |
| 126 | + new ImageFromDockerfile("vertx-http-proxy-grpc-client", false) |
| 127 | + .withFileFromClasspath("Dockerfile", "grpc/client/Dockerfile") |
| 128 | + .withFileFromClasspath("client.js", "grpc/client/client.js") |
| 129 | + .withFileFromClasspath("package.json", "grpc/package.json") |
| 130 | + .withFileFromClasspath("test.proto", "grpc/test.proto")) |
| 131 | + .withNetworkMode("host") |
| 132 | + .withEnv("GRPC_SERVER", String.format("localhost:%d", PROXY_PORT)) |
| 133 | + .withStartupCheckStrategy(new OneShotStartupCheckStrategy()) |
| 134 | + .withStartupTimeout(Duration.ofMinutes(3)); |
| 135 | + |
| 136 | + grpcClientContainer.start(); |
| 137 | + |
| 138 | + DockerClient dockerClient = grpcClientContainer.getDockerClient(); |
| 139 | + try (WaitContainerCmd cmd = dockerClient.waitContainerCmd(grpcClientContainer.getContainerId())) { |
| 140 | + return cmd.exec(new WaitContainerResultCallback()) |
| 141 | + .awaitStatusCode(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static class ServerContainer<SELF extends ServerContainer<SELF>> extends GenericContainer<SELF> { |
| 146 | + |
| 147 | + public ServerContainer(java.util.concurrent.Future<String> dockerImageName) { |
| 148 | + super(dockerImageName); |
| 149 | + } |
| 150 | + |
| 151 | + public SELF withFixedExposedPort(int hostPort, int containerPort) { |
| 152 | + super.addFixedExposedPort(hostPort, containerPort, InternetProtocol.TCP); |
| 153 | + return self(); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments