interop-testing,core: interop test to get remote address attributes

Adding interop-test for getting remote server address from client interceptor. Also added this feature to inprocess transport.
This commit is contained in:
ZHANG Dapeng 2019-12-02 13:30:55 -08:00 committed by GitHub
parent 7b6ab2558c
commit 8062406afc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 3 deletions

View File

@ -122,6 +122,8 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
this.attributes = Attributes.newBuilder()
.set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.PRIVACY_AND_INTEGRITY)
.set(GrpcAttributes.ATTR_CLIENT_EAG_ATTRS, eagAttrs)
.set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, new InProcessSocketAddress(name))
.set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, new InProcessSocketAddress(name))
.build();
logId = InternalLogId.allocate(getClass(), name);
}
@ -783,7 +785,7 @@ final class InProcessTransport implements ServerTransport, ConnectionClientTrans
@Override
public Attributes getAttributes() {
return Attributes.EMPTY;
return attributes;
}
@Override

View File

@ -153,6 +153,8 @@ public abstract class AbstractInteropTest {
private final AtomicReference<ServerCall<?, ?>> serverCallCapture =
new AtomicReference<>();
private final AtomicReference<ClientCall<?, ?>> clientCallCapture =
new AtomicReference<>();
private final AtomicReference<Metadata> requestHeadersCapture =
new AtomicReference<>();
private final AtomicReference<Context> contextCapture =
@ -1657,6 +1659,16 @@ public abstract class AbstractInteropTest {
}
}
/**
* Verifies remote server address and local client address are available from ClientCall
* Attributes via ClientInterceptor.
*/
@Test
public void getServerAddressAndLocalAddressFromClient() {
assertNotNull(obtainRemoteServerAddr());
assertNotNull(obtainLocalClientAddr());
}
/** Sends a large unary rpc with service account credentials. */
public void serviceAccountCreds(String jsonKey, InputStream credentialsStream, String authScope)
throws Exception {
@ -1829,8 +1841,19 @@ public abstract class AbstractInteropTest {
return serverCallCapture.get().getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
}
/** Helper for getting remote address from {@link io.grpc.ClientCall#getAttributes()} */
protected SocketAddress obtainRemoteServerAddr() {
TestServiceGrpc.TestServiceBlockingStub stub = blockingStub
.withInterceptors(recordClientCallInterceptor(clientCallCapture))
.withDeadlineAfter(5, TimeUnit.SECONDS);
stub.unaryCall(SimpleRequest.getDefaultInstance());
return clientCallCapture.get().getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
}
/** Helper for getting local address from {@link io.grpc.ServerCall#getAttributes()} */
protected SocketAddress obtainLocalClientAddr() {
protected SocketAddress obtainLocalServerAddr() {
TestServiceGrpc.TestServiceBlockingStub stub =
blockingStub.withDeadlineAfter(5, TimeUnit.SECONDS);
@ -1839,6 +1862,17 @@ public abstract class AbstractInteropTest {
return serverCallCapture.get().getAttributes().get(Grpc.TRANSPORT_ATTR_LOCAL_ADDR);
}
/** Helper for getting local address from {@link io.grpc.ClientCall#getAttributes()} */
protected SocketAddress obtainLocalClientAddr() {
TestServiceGrpc.TestServiceBlockingStub stub = blockingStub
.withInterceptors(recordClientCallInterceptor(clientCallCapture))
.withDeadlineAfter(5, TimeUnit.SECONDS);
stub.unaryCall(SimpleRequest.getDefaultInstance());
return clientCallCapture.get().getAttributes().get(Grpc.TRANSPORT_ATTR_LOCAL_ADDR);
}
/** Helper for asserting TLS info in SSLSession {@link io.grpc.ServerCall#getAttributes()} */
protected void assertX500SubjectDn(String tlsInfo) {
TestServiceGrpc.TestServiceBlockingStub stub =
@ -2155,6 +2189,23 @@ public abstract class AbstractInteropTest {
};
}
/**
* Captures the request attributes. Useful for testing ClientCalls.
* {@link ClientCall#getAttributes()}
*/
private static ClientInterceptor recordClientCallInterceptor(
final AtomicReference<ClientCall<?, ?>> clientCallCapture) {
return new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
ClientCall<ReqT, RespT> clientCall = next.newCall(method,callOptions);
clientCallCapture.set(clientCall);
return clientCall;
}
};
}
private static ServerInterceptor recordContextInterceptor(
final AtomicReference<Context> contextCapture) {
return new ServerInterceptor() {

View File

@ -89,7 +89,7 @@ public class Http2NettyTest extends AbstractInteropTest {
@Test
public void localAddr() throws Exception {
InetSocketAddress isa = (InetSocketAddress) obtainLocalClientAddr();
InetSocketAddress isa = (InetSocketAddress) obtainLocalServerAddr();
assertEquals(InetAddress.getLoopbackAddress(), isa.getAddress());
assertEquals(((InetSocketAddress) getListenAddress()).getPort(), isa.getPort());
}