|
21 | 21 | import static org.mockito.Mockito.verify; |
22 | 22 | import static org.mockito.Mockito.when; |
23 | 23 |
|
24 | | -import com.google.common.collect.ImmutableList; |
25 | | -import com.google.common.collect.ImmutableMap; |
26 | 24 | import com.google.common.collect.Iterables; |
27 | 25 | import io.grpc.ChannelLogger; |
28 | 26 | import io.grpc.MetricRecorder; |
|
46 | 44 | import java.nio.charset.StandardCharsets; |
47 | 45 | import java.util.Arrays; |
48 | 46 | import java.util.HashMap; |
49 | | -import java.util.List; |
50 | 47 | import java.util.Map; |
51 | 48 | import java.util.Random; |
52 | 49 | import java.util.concurrent.Executor; |
@@ -103,6 +100,8 @@ public void close(Executor instance) {} |
103 | 100 |
|
104 | 101 | private final NameResolverRegistry nsRegistry = new NameResolverRegistry(); |
105 | 102 | private final Map<String, NameResolver> delegatedResolver = new HashMap<>(); |
| 103 | + private final Map<String, URI> delegatedUri = new HashMap<>(); |
| 104 | + private final Map<String, Uri> delegatedRfcUri = new HashMap<>(); |
106 | 105 |
|
107 | 106 | @Mock |
108 | 107 | private NameResolver.Listener2 mockListener; |
@@ -187,57 +186,125 @@ public void onGcpAndNoProvidedBootstrap_DelegateToXds() { |
187 | 186 | verify(Iterables.getOnlyElement(delegatedResolver.values())).start(mockListener); |
188 | 187 | } |
189 | 188 |
|
190 | | - @SuppressWarnings("unchecked") |
191 | 189 | @Test |
192 | | - public void generateBootstrap_ipv6() throws IOException { |
193 | | - Map<String, ?> bootstrap = GoogleCloudToProdNameResolver.generateBootstrap(); |
194 | | - Map<String, ?> node = (Map<String, ?>) bootstrap.get("node"); |
195 | | - assertThat(node).containsExactly( |
196 | | - "id", "C2P-991614323", |
197 | | - "locality", ImmutableMap.of("zone", ZONE), |
198 | | - "metadata", ImmutableMap.of("TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE", true)); |
199 | | - Map<String, ?> server = Iterables.getOnlyElement( |
200 | | - (List<Map<String, ?>>) bootstrap.get("xds_servers")); |
201 | | - assertThat(server).containsExactly( |
202 | | - "server_uri", "directpath-pa.googleapis.com", |
203 | | - "channel_creds", ImmutableList.of(ImmutableMap.of("type", "google_default")), |
204 | | - "server_features", ImmutableList.of("xds_v3", "ignore_resource_deletion")); |
205 | | - Map<String, ?> authorities = (Map<String, ?>) bootstrap.get("authorities"); |
206 | | - assertThat(authorities).containsExactly( |
207 | | - "traffic-director-c2p.xds.googleapis.com", |
208 | | - ImmutableMap.of("xds_servers", ImmutableList.of(server))); |
| 190 | + public void notOnGcpButForceXds_DelegateToXds() { |
| 191 | + GoogleCloudToProdNameResolver.isOnGcp = false; |
| 192 | + String target = TARGET_URI + "?force-xds"; |
| 193 | + resolver = |
| 194 | + enableRfc3986UrisParam |
| 195 | + ? new GoogleCloudToProdNameResolver( |
| 196 | + Uri.create(target), args, fakeExecutorResource, nsRegistry.asFactory()) |
| 197 | + : new GoogleCloudToProdNameResolver( |
| 198 | + URI.create(target), args, fakeExecutorResource, nsRegistry.asFactory()); |
| 199 | + resolver.start(mockListener); |
| 200 | + fakeExecutor.runDueTasks(); |
| 201 | + assertThat(delegatedResolver.keySet()).containsExactly("xds"); |
| 202 | + |
| 203 | + if (enableRfc3986UrisParam) { |
| 204 | + Uri delegatedRfcUriValue = delegatedRfcUri.get("xds"); |
| 205 | + assertThat(delegatedRfcUriValue).isNotNull(); |
| 206 | + assertThat(delegatedRfcUriValue.getRawQuery()).isNull(); |
| 207 | + } else { |
| 208 | + URI delegatedUriValue = delegatedUri.get("xds"); |
| 209 | + assertThat(delegatedUriValue).isNotNull(); |
| 210 | + assertThat(delegatedUriValue.getQuery()).isNull(); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + @Test |
| 215 | + public void notOnGcpButForceXds_KeyValueTrue_DelegateToXds() { |
| 216 | + GoogleCloudToProdNameResolver.isOnGcp = false; |
| 217 | + String target = TARGET_URI + "?force-xds=true"; |
| 218 | + resolver = enableRfc3986UrisParam |
| 219 | + ? new GoogleCloudToProdNameResolver( |
| 220 | + Uri.create(target), args, fakeExecutorResource, nsRegistry.asFactory()) |
| 221 | + : new GoogleCloudToProdNameResolver( |
| 222 | + URI.create(target), args, fakeExecutorResource, nsRegistry.asFactory()); |
| 223 | + resolver.start(mockListener); |
| 224 | + fakeExecutor.runDueTasks(); |
| 225 | + assertThat(delegatedResolver.keySet()).containsExactly("xds"); |
| 226 | + |
| 227 | + if (enableRfc3986UrisParam) { |
| 228 | + Uri delegatedRfcUriValue = delegatedRfcUri.get("xds"); |
| 229 | + assertThat(delegatedRfcUriValue).isNotNull(); |
| 230 | + assertThat(delegatedRfcUriValue.getRawQuery()).isNull(); |
| 231 | + } else { |
| 232 | + URI delegatedUriValue = delegatedUri.get("xds"); |
| 233 | + assertThat(delegatedUriValue).isNotNull(); |
| 234 | + assertThat(delegatedUriValue.getQuery()).isNull(); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + @Test |
| 240 | + public void notOnGcpButForceXds_WithMultipleParams_DelegateToXds() { |
| 241 | + GoogleCloudToProdNameResolver.isOnGcp = false; |
| 242 | + String target = TARGET_URI + "?foo=bar&force-xds&baz=qux"; |
| 243 | + resolver = enableRfc3986UrisParam |
| 244 | + ? new GoogleCloudToProdNameResolver( |
| 245 | + Uri.create(target), args, fakeExecutorResource, nsRegistry.asFactory()) |
| 246 | + : new GoogleCloudToProdNameResolver( |
| 247 | + URI.create(target), args, fakeExecutorResource, nsRegistry.asFactory()); |
| 248 | + resolver.start(mockListener); |
| 249 | + fakeExecutor.runDueTasks(); |
| 250 | + assertThat(delegatedResolver.keySet()).containsExactly("xds"); |
| 251 | + |
| 252 | + if (enableRfc3986UrisParam) { |
| 253 | + Uri delegatedRfcUriValue = delegatedRfcUri.get("xds"); |
| 254 | + assertThat(delegatedRfcUriValue).isNotNull(); |
| 255 | + assertThat(delegatedRfcUriValue.getRawQuery()).isEqualTo("foo=bar&baz=qux"); |
| 256 | + } else { |
| 257 | + URI delegatedUriValue = delegatedUri.get("xds"); |
| 258 | + assertThat(delegatedUriValue).isNotNull(); |
| 259 | + assertThat(delegatedUriValue.getQuery()).isEqualTo("foo=bar&baz=qux"); |
| 260 | + } |
209 | 261 | } |
210 | 262 |
|
211 | | - @SuppressWarnings("unchecked") |
212 | 263 | @Test |
213 | | - public void generateBootstrap_noIpV6() throws IOException { |
214 | | - responseToIpV6 = null; |
215 | | - Map<String, ?> bootstrap = GoogleCloudToProdNameResolver.generateBootstrap(); |
216 | | - Map<String, ?> node = (Map<String, ?>) bootstrap.get("node"); |
217 | | - assertThat(node).containsExactly( |
218 | | - "id", "C2P-991614323", |
219 | | - "locality", ImmutableMap.of("zone", ZONE)); |
220 | | - Map<String, ?> server = Iterables.getOnlyElement( |
221 | | - (List<Map<String, ?>>) bootstrap.get("xds_servers")); |
222 | | - assertThat(server).containsExactly( |
223 | | - "server_uri", "directpath-pa.googleapis.com", |
224 | | - "channel_creds", ImmutableList.of(ImmutableMap.of("type", "google_default")), |
225 | | - "server_features", ImmutableList.of("xds_v3", "ignore_resource_deletion")); |
226 | | - Map<String, ?> authorities = (Map<String, ?>) bootstrap.get("authorities"); |
227 | | - assertThat(authorities).containsExactly( |
228 | | - "traffic-director-c2p.xds.googleapis.com", |
229 | | - ImmutableMap.of("xds_servers", ImmutableList.of(server))); |
| 264 | + public void notOnGcpButForceXds_WithEncodedAmpersand_DelegateToXds() { |
| 265 | + GoogleCloudToProdNameResolver.isOnGcp = false; |
| 266 | + String target = TARGET_URI + "?force-xds&foo=bar%26baz"; |
| 267 | + resolver = enableRfc3986UrisParam |
| 268 | + ? new GoogleCloudToProdNameResolver( |
| 269 | + Uri.create(target), args, fakeExecutorResource, nsRegistry.asFactory()) |
| 270 | + : new GoogleCloudToProdNameResolver( |
| 271 | + URI.create(target), args, fakeExecutorResource, nsRegistry.asFactory()); |
| 272 | + resolver.start(mockListener); |
| 273 | + fakeExecutor.runDueTasks(); |
| 274 | + assertThat(delegatedResolver.keySet()).containsExactly("xds"); |
| 275 | + |
| 276 | + if (enableRfc3986UrisParam) { |
| 277 | + Uri delegatedRfcUriValue = delegatedRfcUri.get("xds"); |
| 278 | + assertThat(delegatedRfcUriValue).isNotNull(); |
| 279 | + assertThat(delegatedRfcUriValue.getRawQuery()).isEqualTo("foo=bar%26baz"); |
| 280 | + } else { |
| 281 | + URI delegatedUriValue = delegatedUri.get("xds"); |
| 282 | + assertThat(delegatedUriValue).isNotNull(); |
| 283 | + assertThat(delegatedUriValue.getRawQuery()).isEqualTo("foo=bar%26baz"); |
| 284 | + } |
230 | 285 | } |
231 | 286 |
|
232 | | - @SuppressWarnings("unchecked") |
233 | 287 | @Test |
234 | | - public void emptyResolverMeetadataValue() throws IOException { |
235 | | - responseToIpV6 = ""; |
236 | | - Map<String, ?> bootstrap = GoogleCloudToProdNameResolver.generateBootstrap(); |
237 | | - Map<String, ?> node = (Map<String, ?>) bootstrap.get("node"); |
238 | | - assertThat(node).containsExactly( |
239 | | - "id", "C2P-991614323", |
240 | | - "locality", ImmutableMap.of("zone", ZONE)); |
| 288 | + public void notOnGcpButForceXds_CaseSensitive_DelegateToDns() { |
| 289 | + GoogleCloudToProdNameResolver.isOnGcp = false; |
| 290 | + String target = TARGET_URI + "?FORCE-XDS"; |
| 291 | + resolver = enableRfc3986UrisParam |
| 292 | + ? new GoogleCloudToProdNameResolver( |
| 293 | + Uri.create(target), args, fakeExecutorResource, nsRegistry.asFactory()) |
| 294 | + : new GoogleCloudToProdNameResolver( |
| 295 | + URI.create(target), args, fakeExecutorResource, nsRegistry.asFactory()); |
| 296 | + resolver.start(mockListener); |
| 297 | + assertThat(delegatedResolver.keySet()).containsExactly("dns"); |
| 298 | + |
| 299 | + if (enableRfc3986UrisParam) { |
| 300 | + Uri delegatedRfcUriValue = delegatedRfcUri.get("dns"); |
| 301 | + assertThat(delegatedRfcUriValue).isNotNull(); |
| 302 | + assertThat(delegatedRfcUriValue.getRawQuery()).isEqualTo("FORCE-XDS"); |
| 303 | + } else { |
| 304 | + URI delegatedUriValue = delegatedUri.get("dns"); |
| 305 | + assertThat(delegatedUriValue).isNotNull(); |
| 306 | + assertThat(delegatedUriValue.getQuery()).isEqualTo("FORCE-XDS"); |
| 307 | + } |
241 | 308 | } |
242 | 309 |
|
243 | 310 | @Test |
@@ -270,6 +337,18 @@ private FakeNsProvider(String scheme) { |
270 | 337 | @Override |
271 | 338 | public NameResolver newNameResolver(URI targetUri, Args args) { |
272 | 339 | if (scheme.equals(targetUri.getScheme())) { |
| 340 | + delegatedUri.put(scheme, targetUri); |
| 341 | + NameResolver resolver = mock(NameResolver.class); |
| 342 | + delegatedResolver.put(scheme, resolver); |
| 343 | + return resolver; |
| 344 | + } |
| 345 | + return null; |
| 346 | + } |
| 347 | + |
| 348 | + @Override |
| 349 | + public NameResolver newNameResolver(Uri targetUri, Args args) { |
| 350 | + if (scheme.equals(targetUri.getScheme())) { |
| 351 | + delegatedRfcUri.put(scheme, targetUri); |
273 | 352 | NameResolver resolver = mock(NameResolver.class); |
274 | 353 | delegatedResolver.put(scheme, resolver); |
275 | 354 | return resolver; |
|
0 commit comments