|
| 1 | +package example.springframework.boot.xds.cloudconfig; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.awaitility.Awaitility.await; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.concurrent.atomic.AtomicReference; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.AfterAll; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.boot.web.server.context.WebServerApplicationContext; |
| 16 | +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; |
| 17 | +import org.springframework.context.ApplicationContext; |
| 18 | +import org.springframework.context.ConfigurableApplicationContext; |
| 19 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 20 | +import org.springframework.core.env.MapPropertySource; |
| 21 | + |
| 22 | +import com.linecorp.armeria.client.WebClient; |
| 23 | +import com.linecorp.armeria.common.AggregatedHttpResponse; |
| 24 | +import com.linecorp.armeria.common.HttpStatus; |
| 25 | +import com.linecorp.armeria.xds.ClusterSnapshot; |
| 26 | +import com.linecorp.armeria.xds.XdsBootstrap; |
| 27 | + |
| 28 | +import example.springframework.boot.xds.cloudconfig.client.ClientMain; |
| 29 | +import example.springframework.boot.xds.cloudconfig.server.ConfigServerMain; |
| 30 | + |
| 31 | +@SpringBootTest( |
| 32 | + classes = ClientMain.class, |
| 33 | + properties = { |
| 34 | + "spring.config.import=configserver:", |
| 35 | + "spring.cloud.config.enabled=true" |
| 36 | + }) |
| 37 | +class SpringCloudConfigXdsExampleTest { |
| 38 | + |
| 39 | + private static ConfigurableApplicationContext server; |
| 40 | + private static int configPort; |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + static void startConfigServer() { |
| 44 | + server = ConfigServerMain.createApplication().run("--server.port=0"); |
| 45 | + |
| 46 | + configPort = ((WebServerApplicationContext) server).getWebServer().getPort(); |
| 47 | + System.setProperty("spring.cloud.config.uri", "http://localhost:" + configPort); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterAll |
| 51 | + static void stopConfigServer() { |
| 52 | + System.clearProperty("spring.cloud.config.uri"); |
| 53 | + if (server != null) { |
| 54 | + server.close(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Autowired |
| 59 | + WebClient xdsWebClient; |
| 60 | + |
| 61 | + @Autowired |
| 62 | + XdsBootstrap xdsBootstrap; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + ConfigurableEnvironment environment; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + ApplicationContext applicationContext; |
| 69 | + |
| 70 | + @Test |
| 71 | + void webClientViaXdsPreprocessor() { |
| 72 | + // Verify the initial cluster snapshot loaded from config server (port 8888) |
| 73 | + final AtomicReference<ClusterSnapshot> snapshotRef = new AtomicReference<>(); |
| 74 | + xdsBootstrap.clusterRoot("test-cluster") |
| 75 | + .addSnapshotWatcher((snapshot, error) -> { |
| 76 | + if (snapshot != null) { |
| 77 | + snapshotRef.set(snapshot); |
| 78 | + } |
| 79 | + }); |
| 80 | + await().untilAsserted(() -> { |
| 81 | + assertThat(snapshotRef.get()).isNotNull(); |
| 82 | + assertThat(endpointPort(snapshotRef.get())).isEqualTo(8888); |
| 83 | + }); |
| 84 | + |
| 85 | + // Override the cluster endpoint to point at the config server's actual port |
| 86 | + environment.getPropertySources() |
| 87 | + .addFirst(new MapPropertySource("test", |
| 88 | + Map.of("armeria.xds.test-cluster", clusterYaml(configPort)))); |
| 89 | + applicationContext.publishEvent( |
| 90 | + new EnvironmentChangeEvent(Set.of("armeria.xds.test-cluster"))); |
| 91 | + |
| 92 | + // Wait for the xDS update to propagate |
| 93 | + await().untilAsserted(() -> |
| 94 | + assertThat(endpointPort(snapshotRef.get())).isEqualTo(configPort)); |
| 95 | + |
| 96 | + // Call the config server's actuator health endpoint via the xDS-resolved client |
| 97 | + final AggregatedHttpResponse response = |
| 98 | + xdsWebClient.get("/actuator/health").aggregate().join(); |
| 99 | + assertThat(response.status()).isEqualTo(HttpStatus.OK); |
| 100 | + } |
| 101 | + |
| 102 | + private static int endpointPort(ClusterSnapshot snapshot) { |
| 103 | + return snapshot.xdsResource().resource() |
| 104 | + .getLoadAssignment() |
| 105 | + .getEndpoints(0) |
| 106 | + .getLbEndpoints(0) |
| 107 | + .getEndpoint() |
| 108 | + .getAddress() |
| 109 | + .getSocketAddress() |
| 110 | + .getPortValue(); |
| 111 | + } |
| 112 | + |
| 113 | + private static String clusterYaml(int port) { |
| 114 | + return """ |
| 115 | + name: test-cluster |
| 116 | + type: STATIC |
| 117 | + load_assignment: |
| 118 | + cluster_name: test-cluster |
| 119 | + endpoints: |
| 120 | + - lb_endpoints: |
| 121 | + - endpoint: |
| 122 | + address: |
| 123 | + socket_address: |
| 124 | + address: 127.0.0.1 |
| 125 | + port_value: %d |
| 126 | + """.formatted(port); |
| 127 | + } |
| 128 | +} |
0 commit comments