|
56 | 56 | import java.util.concurrent.Executor;
|
57 | 57 | import java.util.concurrent.ScheduledExecutorService;
|
58 | 58 | import java.util.concurrent.ScheduledThreadPoolExecutor;
|
| 59 | +import java.util.logging.Handler; |
| 60 | +import java.util.logging.LogRecord; |
| 61 | +import java.util.stream.Collectors; |
59 | 62 | import javax.annotation.Nullable;
|
60 | 63 | import org.junit.Test;
|
61 | 64 | import org.junit.runner.RunWith;
|
@@ -503,4 +506,70 @@ protected Object getMtlsObjectFromTransportChannel(MtlsProvider provider)
|
503 | 506 | .build();
|
504 | 507 | return channelProvider.createMtlsChannelCredentials();
|
505 | 508 | }
|
| 509 | + |
| 510 | + @Test |
| 511 | + public void testLogDirectPathMisconfigAttrempDirectPathNotSet() { |
| 512 | + FakeLogHandler logHandler = new FakeLogHandler(); |
| 513 | + InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler); |
| 514 | + InstantiatingGrpcChannelProvider provider = |
| 515 | + InstantiatingGrpcChannelProvider.newBuilder().setAttemptDirectPathXds().build(); |
| 516 | + assertThat(logHandler.getAllMessages()) |
| 517 | + .contains( |
| 518 | + "DirectPath is misconfigured. Please set the attemptDirectPath option along with the" |
| 519 | + + " attemptDirectPathXds option."); |
| 520 | + InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler); |
| 521 | + } |
| 522 | + |
| 523 | + @Test |
| 524 | + public void testLogDirectPathMisconfigWrongCredential() { |
| 525 | + FakeLogHandler logHandler = new FakeLogHandler(); |
| 526 | + InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler); |
| 527 | + InstantiatingGrpcChannelProvider provider = |
| 528 | + InstantiatingGrpcChannelProvider.newBuilder() |
| 529 | + .setAttemptDirectPathXds() |
| 530 | + .setAttemptDirectPath(true) |
| 531 | + .build(); |
| 532 | + assertThat(logHandler.getAllMessages()) |
| 533 | + .contains( |
| 534 | + "DirectPath is misconfigured. Please make sure the credential is an instance of" |
| 535 | + + " com.google.auth.oauth2.ComputeEngineCredentials ."); |
| 536 | + InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler); |
| 537 | + } |
| 538 | + |
| 539 | + @Test |
| 540 | + public void testLogDirectPathMisconfigNotOnGCE() { |
| 541 | + FakeLogHandler logHandler = new FakeLogHandler(); |
| 542 | + InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler); |
| 543 | + InstantiatingGrpcChannelProvider provider = |
| 544 | + InstantiatingGrpcChannelProvider.newBuilder() |
| 545 | + .setAttemptDirectPathXds() |
| 546 | + .setAttemptDirectPath(true) |
| 547 | + .setAllowNonDefaultServiceAccount(true) |
| 548 | + .build(); |
| 549 | + if (!InstantiatingGrpcChannelProvider.isOnComputeEngine()) { |
| 550 | + assertThat(logHandler.getAllMessages()) |
| 551 | + .contains( |
| 552 | + "DirectPath is misconfigured. DirectPath is only available in a GCE environment."); |
| 553 | + } |
| 554 | + InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler); |
| 555 | + } |
| 556 | + |
| 557 | + private static class FakeLogHandler extends Handler { |
| 558 | + List<LogRecord> records = new ArrayList<>(); |
| 559 | + |
| 560 | + @Override |
| 561 | + public void publish(LogRecord record) { |
| 562 | + records.add(record); |
| 563 | + } |
| 564 | + |
| 565 | + @Override |
| 566 | + public void flush() {} |
| 567 | + |
| 568 | + @Override |
| 569 | + public void close() throws SecurityException {} |
| 570 | + |
| 571 | + List<String> getAllMessages() { |
| 572 | + return records.stream().map(LogRecord::getMessage).collect(Collectors.toList()); |
| 573 | + } |
| 574 | + } |
506 | 575 | }
|
0 commit comments