|
| 1 | +package io.envoyproxy.controlplane.server; |
| 2 | + |
| 3 | +import static io.envoyproxy.controlplane.server.TestSnapshots.createSnapshot; |
| 4 | +import static io.restassured.RestAssured.given; |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; |
| 6 | +import static org.awaitility.Awaitility.await; |
| 7 | +import static org.hamcrest.Matchers.containsString; |
| 8 | + |
| 9 | +import envoy.api.v2.Discovery.DiscoveryRequest; |
| 10 | +import envoy.api.v2.Discovery.DiscoveryResponse; |
| 11 | +import io.envoyproxy.controlplane.cache.SimpleCache; |
| 12 | +import io.grpc.netty.NettyServerBuilder; |
| 13 | +import io.restassured.http.ContentType; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import org.junit.ClassRule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.rules.RuleChain; |
| 19 | +import org.testcontainers.containers.Network; |
| 20 | + |
| 21 | +public class DiscoveryServerAdsIT { |
| 22 | + |
| 23 | + private static final String CONFIG = "envoy/ads.config.yaml"; |
| 24 | + private static final String GROUP = "key"; |
| 25 | + private static final Integer LISTENER_PORT = 10000; |
| 26 | + |
| 27 | + private static final CountDownLatch onStreamOpenLatch = new CountDownLatch(1); |
| 28 | + private static final CountDownLatch onStreamRequestLatch = new CountDownLatch(1); |
| 29 | + private static final CountDownLatch onStreamResponseLatch = new CountDownLatch(1); |
| 30 | + |
| 31 | + private static final NettyGrpcServerRule ADS = new NettyGrpcServerRule() { |
| 32 | + @Override |
| 33 | + protected void configureServerBuilder(NettyServerBuilder builder) { |
| 34 | + final SimpleCache<String> cache = new SimpleCache<>(true, node -> GROUP); |
| 35 | + |
| 36 | + final DiscoveryServerCallbacks callbacks = new DiscoveryServerCallbacks() { |
| 37 | + @Override |
| 38 | + public void onStreamOpen(long streamId, String typeUrl) { |
| 39 | + onStreamOpenLatch.countDown(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onStreamRequest(long streamId, DiscoveryRequest request) { |
| 44 | + onStreamRequestLatch.countDown(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onStreamResponse(long streamId, DiscoveryRequest request, DiscoveryResponse response) { |
| 49 | + onStreamResponseLatch.countDown(); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + cache.setSnapshot( |
| 54 | + GROUP, |
| 55 | + createSnapshot("upstream", "upstream", EchoContainer.PORT, "listener0", LISTENER_PORT, "route0", "1")); |
| 56 | + |
| 57 | + DiscoveryServer server = new DiscoveryServer(callbacks, cache); |
| 58 | + |
| 59 | + builder.addService(server.getAggregatedDiscoveryServiceImpl()); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + private static final Network NETWORK = Network.newNetwork(); |
| 64 | + |
| 65 | + private static final EnvoyContainer ENVOY = new EnvoyContainer(CONFIG, () -> ADS.getServer().getPort()) |
| 66 | + .withExposedPorts(LISTENER_PORT) |
| 67 | + .withNetwork(NETWORK); |
| 68 | + |
| 69 | + private static final EchoContainer UPSTREAM = new EchoContainer() |
| 70 | + .withNetwork(NETWORK) |
| 71 | + .withNetworkAliases("upstream"); |
| 72 | + |
| 73 | + @ClassRule |
| 74 | + public static final RuleChain RULES = RuleChain.outerRule(UPSTREAM) |
| 75 | + .around(ADS) |
| 76 | + .around(ENVOY); |
| 77 | + |
| 78 | + @Test |
| 79 | + public void validateTestRequestToEchoServerViaEnvoy() throws InterruptedException { |
| 80 | + assertThat(onStreamOpenLatch.await(15, TimeUnit.SECONDS)).isTrue() |
| 81 | + .overridingErrorMessage("failed to open ADS stream"); |
| 82 | + |
| 83 | + assertThat(onStreamRequestLatch.await(15, TimeUnit.SECONDS)).isTrue() |
| 84 | + .overridingErrorMessage("failed to receive ADS request"); |
| 85 | + |
| 86 | + assertThat(onStreamResponseLatch.await(15, TimeUnit.SECONDS)).isTrue() |
| 87 | + .overridingErrorMessage("failed to send ADS response"); |
| 88 | + |
| 89 | + String baseUri = String.format("http://%s:%d", ENVOY.getContainerIpAddress(), ENVOY.getMappedPort(LISTENER_PORT)); |
| 90 | + |
| 91 | + await().atMost(5, TimeUnit.SECONDS).ignoreExceptions().untilAsserted( |
| 92 | + () -> given().baseUri(baseUri).contentType(ContentType.TEXT) |
| 93 | + .when().get("/") |
| 94 | + .then().statusCode(200) |
| 95 | + .and().body(containsString(UPSTREAM.response))); |
| 96 | + } |
| 97 | +} |
0 commit comments