Skip to content

Commit 3a36c2d

Browse files
committed
Add error cases in unit tests
1 parent 4dc1039 commit 3a36c2d

4 files changed

Lines changed: 115 additions & 6 deletions

File tree

xds/src/main/java/io/grpc/xds/XdsNameResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,7 @@ public void onUpdate(StatusOr<XdsConfig> updateOrStatus) {
677677
XdsConfig update = updateOrStatus.getValue();
678678
HttpConnectionManager httpConnectionManager = update.getListener().httpConnectionManager();
679679
if (httpConnectionManager == null) {
680-
String error = "API Listener: httpConnectionManager does not exist.";
681-
logger.log(XdsLogLevel.INFO, error);
680+
logger.log(XdsLogLevel.INFO, "API Listener: httpConnectionManager does not exist.");
682681
updateActiveFilters(null);
683682
cleanUpRoutes(updateOrStatus.getStatus());
684683
return;

xds/src/test/java/io/grpc/xds/GrpcXdsClientImplDataTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,39 @@ public void parseServerSideListener_useOriginalDst() throws ResourceInvalidExcep
26602660
listener,null, filterRegistry, null, getXdsResourceTypeArgs(true));
26612661
}
26622662

2663+
@Test
2664+
public void parseServerSideListener_emptyAddress() throws ResourceInvalidException {
2665+
Listener listener =
2666+
Listener.newBuilder()
2667+
.setName("listener1")
2668+
.setTrafficDirection(TrafficDirection.INBOUND)
2669+
.setAddress(Address.newBuilder()
2670+
.setSocketAddress(
2671+
SocketAddress.newBuilder()))
2672+
.build();
2673+
thrown.expect(ResourceInvalidException.class);
2674+
thrown.expectMessage("Invalid address: Empty address is not allowed.");
2675+
XdsListenerResource.parseServerSideListener(
2676+
listener,null, filterRegistry, null, getXdsResourceTypeArgs(true));
2677+
}
2678+
2679+
@Test
2680+
public void parseServerSideListener_namedPort() throws ResourceInvalidException {
2681+
Listener listener =
2682+
Listener.newBuilder()
2683+
.setName("listener1")
2684+
.setTrafficDirection(TrafficDirection.INBOUND)
2685+
.setAddress(Address.newBuilder()
2686+
.setSocketAddress(
2687+
SocketAddress.newBuilder()
2688+
.setAddress("172.14.14.5").setNamedPort("")))
2689+
.build();
2690+
thrown.expect(ResourceInvalidException.class);
2691+
thrown.expectMessage("NAMED_PORT is not supported in gRPC.");
2692+
XdsListenerResource.parseServerSideListener(
2693+
listener,null, filterRegistry, null, getXdsResourceTypeArgs(true));
2694+
}
2695+
26632696
@Test
26642697
public void parseServerSideListener_nonUniqueFilterChainMatch() throws ResourceInvalidException {
26652698
Filter filter1 = buildHttpConnectionManagerFilter(

xds/src/test/java/io/grpc/xds/XdsServerTestHelper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ static EnvoyServerProtoData.Listener buildTestListener(
130130
EnvoyServerProtoData.FilterChain defaultFilterChain = EnvoyServerProtoData.FilterChain.create(
131131
"filter-chain-bar", defaultFilterChainMatch, httpConnectionManager,
132132
tlsContextForDefaultFilterChain, tlsContextManager);
133-
EnvoyServerProtoData.Listener listener = EnvoyServerProtoData.Listener.create(
133+
return Listener.create(
134134
name, address, ImmutableList.of(filterChain1), defaultFilterChain, Protocol.TCP);
135-
return listener;
136135
}
137136

138137
static final class FakeXdsClientPoolFactory
@@ -291,6 +290,14 @@ private String awaitLdsResource(Duration timeout) {
291290
}
292291
}
293292

293+
void deliverLdsUpdateWithApiListener(long httpMaxStreamDurationNano,
294+
List<VirtualHost> virtualHosts) {
295+
execute(() -> {
296+
ldsWatcher.onChanged(LdsUpdate.forApiListener(HttpConnectionManager.forVirtualHosts(
297+
httpMaxStreamDurationNano, virtualHosts, null)));
298+
});
299+
}
300+
294301
void deliverLdsUpdate(LdsUpdate ldsUpdate) {
295302
execute(() -> ldsWatcher.onChanged(ldsUpdate));
296303
}

xds/src/test/java/io/grpc/xds/XdsServerWrapperTest.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,76 @@ public void run() {
580580
verify(listener, timeout(10000)).onNotServing(any());
581581
}
582582

583+
@Test
584+
public void onChanged_listenerIsNull()
585+
throws ExecutionException, InterruptedException, TimeoutException {
586+
587+
xdsServerWrapper = new XdsServerWrapper("10.1.2.3:1", mockBuilder, listener,
588+
selectorManager, new FakeXdsClientPoolFactory(xdsClient),
589+
filterRegistry, executor.getScheduledExecutorService());
590+
591+
final SettableFuture<Server> start = SettableFuture.create();
592+
Executors.newSingleThreadExecutor().execute(new Runnable() {
593+
@Override
594+
public void run() {
595+
try {
596+
start.set(xdsServerWrapper.start());
597+
} catch (Exception ex) {
598+
start.setException(ex);
599+
}
600+
}
601+
});
602+
String ldsResource = xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
603+
assertThat(ldsResource).isEqualTo("grpc/server?udpa.resource.listening_address=10.1.2.3:1");
604+
605+
VirtualHost virtualHost =
606+
VirtualHost.create(
607+
"virtual-host", Collections.singletonList("auth"), new ArrayList<Route>(),
608+
ImmutableMap.<String, FilterConfig>of());
609+
610+
xdsClient.deliverLdsUpdateWithApiListener(0L, Arrays.asList(virtualHost));
611+
verify(listener, timeout(10000)).onNotServing(any());
612+
}
613+
614+
@Test
615+
public void onChanged_listenerAddressPortMismatch()
616+
throws ExecutionException, InterruptedException, TimeoutException {
617+
618+
xdsServerWrapper = new XdsServerWrapper("10.1.2.3:1", mockBuilder, listener,
619+
selectorManager, new FakeXdsClientPoolFactory(xdsClient),
620+
filterRegistry, executor.getScheduledExecutorService());
621+
622+
final SettableFuture<Server> start = SettableFuture.create();
623+
Executors.newSingleThreadExecutor().execute(new Runnable() {
624+
@Override
625+
public void run() {
626+
try {
627+
start.set(xdsServerWrapper.start());
628+
} catch (Exception ex) {
629+
start.setException(ex);
630+
}
631+
}
632+
});
633+
String ldsResource = xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
634+
assertThat(ldsResource).isEqualTo("grpc/server?udpa.resource.listening_address=10.1.2.3:1");
635+
636+
VirtualHost virtualHost =
637+
VirtualHost.create(
638+
"virtual-host", Collections.singletonList("auth"), new ArrayList<Route>(),
639+
ImmutableMap.<String, FilterConfig>of());
640+
HttpConnectionManager httpConnectionManager = HttpConnectionManager.forVirtualHosts(
641+
0L, Collections.singletonList(virtualHost), new ArrayList<NamedFilterConfig>());
642+
EnvoyServerProtoData.FilterChain filterChain = EnvoyServerProtoData.FilterChain.create(
643+
"filter-chain-foo", createMatch(), httpConnectionManager, createTls(),
644+
mock(TlsContextManager.class));
645+
646+
LdsUpdate listenerUpdate = LdsUpdate.forTcpListener(
647+
Listener.create("listener", "10.1.2.3:2",
648+
ImmutableList.copyOf(Collections.singletonList(filterChain)), null, Protocol.TCP));
649+
xdsClient.deliverLdsUpdate(listenerUpdate);
650+
verify(listener, timeout(10000)).onNotServing(any());
651+
}
652+
583653
@Test
584654
public void discoverState_rds() throws Exception {
585655
final SettableFuture<Server> start = SettableFuture.create();
@@ -1853,7 +1923,7 @@ private static HttpConnectionManager createRds(String name) {
18531923
/**
18541924
* Returns the least-specific match-all Filter Chain Match.
18551925
*/
1856-
private static FilterChainMatch createMatch() {
1926+
static FilterChainMatch createMatch() {
18571927
return FilterChainMatch.create(
18581928
0,
18591929
ImmutableList.of(),
@@ -1909,7 +1979,7 @@ private static MethodDescriptor<Void, Void> createMethod(String path) {
19091979
.build();
19101980
}
19111981

1912-
private static EnvoyServerProtoData.DownstreamTlsContext createTls() {
1982+
static EnvoyServerProtoData.DownstreamTlsContext createTls() {
19131983
return CommonTlsContextTestsUtil.buildTestInternalDownstreamTlsContext("CERT1", "VA1");
19141984
}
19151985
}

0 commit comments

Comments
 (0)