Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: clean up stub creation in EnhancedBigtableStubTest #2439

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import com.google.cloud.bigtable.data.v2.models.TableId;
import com.google.cloud.bigtable.data.v2.models.sql.ResultSetMetadata;
import com.google.cloud.bigtable.data.v2.models.sql.Statement;
import com.google.cloud.bigtable.data.v2.stub.metrics.NoopMetricsProvider;
import com.google.cloud.bigtable.data.v2.stub.sql.ExecuteQueryCallable;
import com.google.cloud.bigtable.data.v2.stub.sql.SqlServerStream;
import com.google.common.collect.ImmutableMap;
Expand All @@ -97,7 +98,6 @@
import io.grpc.CallOptions;
import io.grpc.Context;
import io.grpc.Deadline;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
Expand Down Expand Up @@ -172,6 +172,7 @@ public void setUp() throws IOException, IllegalAccessException, InstantiationExc
.setInstanceId(INSTANCE_ID)
.setAppProfileId(APP_PROFILE_ID)
.setCredentialsProvider(NoCredentialsProvider.create())
.setMetricsProvider(NoopMetricsProvider.INSTANCE)
.build()
.getStubSettings();

Expand All @@ -187,9 +188,6 @@ public void tearDown() {
@Test
public void testJwtAudience()
throws InterruptedException, IOException, NoSuchAlgorithmException, ExecutionException {
// close default stub - need to create custom one
enhancedBigtableStub.close();

// Create fake jwt creds
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyGen.genKeyPair();
Expand All @@ -210,9 +208,10 @@ public void testJwtAudience()
.setJwtAudienceMapping(ImmutableMap.of("localhost", expectedAudience))
.setCredentialsProvider(FixedCredentialsProvider.create(jwtCreds))
.build();
enhancedBigtableStub = EnhancedBigtableStub.create(settings);
try (EnhancedBigtableStub stub = EnhancedBigtableStub.create(settings)) {
stub.readRowCallable().futureCall(Query.create("fake-table")).get();
}
// Send rpc and grab the credentials sent
enhancedBigtableStub.readRowCallable().futureCall(Query.create("fake-table")).get();
Metadata metadata = metadataInterceptor.headers.take();

String authValue = metadata.get(Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER));
Expand All @@ -226,9 +225,6 @@ public void testJwtAudience()
@Test
public void testBatchJwtAudience()
throws InterruptedException, IOException, NoSuchAlgorithmException, ExecutionException {
// close default stub - need to create custom one
enhancedBigtableStub.close();

// Create fake jwt creds
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyGen.genKeyPair();
Expand All @@ -241,31 +237,30 @@ public void testBatchJwtAudience()
.setPrivateKeyId("fake-private-key")
.build();

// Create a fixed channel that will ignore the default endpoint and connect to the emulator
ManagedChannel emulatorChannel =
ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext().build();
EnhancedBigtableStubSettings settings =
EnhancedBigtableStubSettings.newBuilder()
.setProjectId("fake-project")
.setInstanceId("fake-instance")
.setEndpoint("batch-bigtable.googleapis.com:443")
.setCredentialsProvider(FixedCredentialsProvider.create(jwtCreds))
.setMetricsProvider(NoopMetricsProvider.INSTANCE)
// Use a fixed channel that will ignore the default endpoint and connect to the emulator
.setTransportChannelProvider(
FixedTransportChannelProvider.create(
GrpcTransportChannel.create(
ManagedChannelBuilder.forAddress("localhost", server.getPort())
.usePlaintext()
.build())))
// Channel refreshing doesn't work with FixedTransportChannelProvider. Disable it for
// the test
.setRefreshingChannel(false)
.build();

Metadata metadata;
try {
EnhancedBigtableStubSettings settings =
EnhancedBigtableStubSettings.newBuilder()
.setProjectId("fake-project")
.setInstanceId("fake-instance")
.setEndpoint("batch-bigtable.googleapis.com:443")
.setCredentialsProvider(FixedCredentialsProvider.create(jwtCreds))
.setTransportChannelProvider(
FixedTransportChannelProvider.create(
GrpcTransportChannel.create(emulatorChannel)))
// Channel refreshing doesn't work with FixedTransportChannelProvider. Disable it for
// the test
.setRefreshingChannel(false)
.build();
enhancedBigtableStub = EnhancedBigtableStub.create(settings);
try (EnhancedBigtableStub stub = EnhancedBigtableStub.create(settings)) {
// Send rpc and grab the credentials sent
enhancedBigtableStub.readRowCallable().futureCall(Query.create("fake-table")).get();
stub.readRowCallable().futureCall(Query.create("fake-table")).get();
metadata = metadataInterceptor.headers.take();
} finally {
emulatorChannel.shutdown();
}

String authValue = metadata.get(Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER));
Expand All @@ -278,7 +273,6 @@ public void testBatchJwtAudience()

@Test
public void testFeatureFlags() throws InterruptedException, IOException, ExecutionException {

enhancedBigtableStub.readRowCallable().futureCall(Query.create("fake-table")).get();
Metadata metadata = metadataInterceptor.headers.take();

Expand Down
Loading