interop-testing: Modernize stress test channel creation

OverrideAuthority() is the modern alternative to rewriting the
InetAddress, and can also work if we change the channel creation to use
target strings instead. We can use test CAs now without resorting to the
Netty-specific APIs. After this change, only 4 classes in
interop-testing depend on Netty.
This commit is contained in:
Eric Anderson 2023-08-25 10:29:10 -07:00
parent 92174be3df
commit d15daed5e4
2 changed files with 30 additions and 43 deletions

View File

@ -32,24 +32,22 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.TlsChannelCredentials;
import io.grpc.stub.StreamObserver;
import io.grpc.testing.TlsTesting;
import io.netty.handler.ssl.SslContext;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@ -296,19 +294,9 @@ public class StressTestClient {
List<InetSocketAddress> addresses = new ArrayList<>();
for (List<String> namePort : parseCommaSeparatedTuples(addressesStr)) {
InetAddress address;
String name = namePort.get(0);
int port = Integer.valueOf(namePort.get(1));
try {
address = InetAddress.getByName(name);
if (serverHostOverride != null) {
// Force the hostname to match the cert the server uses.
address = InetAddress.getByAddress(serverHostOverride, address.getAddress());
}
} catch (UnknownHostException ex) {
throw new RuntimeException(ex);
}
addresses.add(new InetSocketAddress(address, port));
addresses.add(new InetSocketAddress(name, port));
}
return addresses;
@ -341,19 +329,28 @@ public class StressTestClient {
}
private ManagedChannel createChannel(InetSocketAddress address) {
SslContext sslContext = null;
ChannelCredentials channelCredentials;
if (useTls) {
if (useTestCa) {
try {
sslContext = GrpcSslContexts.forClient().trustManager(
TlsTesting.loadCert("ca.pem")).build();
channelCredentials = TlsChannelCredentials.newBuilder()
.trustManager(TlsTesting.loadCert("ca.pem"))
.build();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} else {
channelCredentials = TlsChannelCredentials.create();
}
return NettyChannelBuilder.forAddress(address)
.negotiationType(useTls ? NegotiationType.TLS : NegotiationType.PLAINTEXT)
.sslContext(sslContext)
.build();
} else {
channelCredentials = InsecureChannelCredentials.create();
}
ManagedChannelBuilder<?> builder = Grpc.newChannelBuilderForAddress(
address.getHostString(), address.getPort(), channelCredentials);
if (serverHostOverride != null) {
builder.overrideAuthority(serverHostOverride);
}
return builder.build();
}
private static String serverAddressesToString(List<InetSocketAddress> addresses) {

View File

@ -32,8 +32,6 @@ import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
@ -103,27 +101,19 @@ public class StressTestClientTest {
assertEquals(9090, client.metricsPort());
}
@Test
public void serverHostOverrideShouldBeApplied() {
StressTestClient client = new StressTestClient();
client.parseArgs(new String[] {
"--server_addresses=localhost:8080",
"--server_host_override=foo.test.google.fr",
});
assertEquals("foo.test.google.fr", client.addresses().get(0).getHostName());
}
@Test
public void gaugesShouldBeExported() throws Exception {
TestServiceServer server = new TestServiceServer();
server.parseArgs(new String[]{"--port=" + 0, "--use_tls=false"});
server.parseArgs(new String[]{"--port=" + 0, "--use_tls=true"});
server.start();
StressTestClient client = new StressTestClient();
client.parseArgs(new String[] {"--test_cases=empty_unary:1",
"--server_addresses=localhost:" + server.getPort(), "--metrics_port=" + 0,
"--server_host_override=foo.test.google.fr",
"--use_tls=true",
"--use_test_ca=true",
"--num_stubs_per_channel=2"});
client.startMetricsService();
client.runStressTest();
@ -142,7 +132,7 @@ public class StressTestClientTest {
List<GaugeResponse> allGauges =
ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
while (allGauges.size() < gaugeNames.size()) {
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100));
Thread.sleep(100);
allGauges = ImmutableList.copyOf(stub.getAllGauges(EmptyMessage.getDefaultInstance()));
}