-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expose gRPC endpoint from BigQueryEmulator #10779
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
Merged
eddumelendez
merged 7 commits into
testcontainers:main
from
Eliassen-Steinar:expose-grpc-endpoint
Sep 24, 2025
+155
−1
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
16b5244
Expose gRPC endpoint
Eliassen-Steinar c72b623
Merge branch 'testcontainers:main' into expose-grpc-endpoint
Eliassen-Steinar b9dad59
Modify exposing endpoint to exposing port, only port is required
Eliassen-Steinar ca5fbd7
Expand test to include a proper table we can connect to, and validate…
Eliassen-Steinar 97213e8
Merge branch 'main' into expose-grpc-endpoint
Eliassen-Steinar ba27f37
Polish
eddumelendez 93e0911
Merge branch 'main' into expose-grpc-endpoint
eddumelendez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,32 @@ | ||
| package org.testcontainers.containers; | ||
|
|
||
| import com.google.api.gax.core.NoCredentialsProvider; | ||
| import com.google.api.gax.grpc.GrpcTransportChannel; | ||
| import com.google.api.gax.rpc.FixedTransportChannelProvider; | ||
| import com.google.cloud.NoCredentials; | ||
| import com.google.cloud.bigquery.BigQuery; | ||
| import com.google.cloud.bigquery.BigQueryOptions; | ||
| import com.google.cloud.bigquery.DatasetId; | ||
| import com.google.cloud.bigquery.DatasetInfo; | ||
| import com.google.cloud.bigquery.Field; | ||
| import com.google.cloud.bigquery.QueryJobConfiguration; | ||
| import com.google.cloud.bigquery.Schema; | ||
| import com.google.cloud.bigquery.StandardSQLTypeName; | ||
| import com.google.cloud.bigquery.StandardTableDefinition; | ||
| import com.google.cloud.bigquery.TableDefinition; | ||
| import com.google.cloud.bigquery.TableId; | ||
| import com.google.cloud.bigquery.TableInfo; | ||
| import com.google.cloud.bigquery.TableResult; | ||
| import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient; | ||
| import com.google.cloud.bigquery.storage.v1.BigQueryWriteSettings; | ||
| import com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest; | ||
| import com.google.cloud.bigquery.storage.v1.TableName; | ||
| import com.google.cloud.bigquery.storage.v1.WriteStream; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import org.threeten.bp.Duration; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
@@ -15,8 +35,19 @@ | |
|
|
||
| class BigQueryEmulatorContainerTest { | ||
|
|
||
| private BigQuery getBigQuery(BigQueryEmulatorContainer container) { | ||
| String url = container.getEmulatorHttpEndpoint(); | ||
| return BigQueryOptions | ||
| .newBuilder() | ||
| .setProjectId(container.getProjectId()) | ||
| .setHost(url) | ||
| .setLocation(url) | ||
| .setCredentials(NoCredentials.getInstance()) | ||
| .build().getService(); | ||
| } | ||
|
|
||
| @Test | ||
| void test() throws Exception { | ||
| void testHttpEndpoint() throws Exception { | ||
| try ( | ||
| // emulatorContainer { | ||
| BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.4.3") | ||
|
|
@@ -25,15 +56,7 @@ void test() throws Exception { | |
| container.start(); | ||
|
|
||
| // bigQueryClient { | ||
| String url = container.getEmulatorHttpEndpoint(); | ||
| BigQueryOptions options = BigQueryOptions | ||
| .newBuilder() | ||
| .setProjectId(container.getProjectId()) | ||
| .setHost(url) | ||
| .setLocation(url) | ||
| .setCredentials(NoCredentials.getInstance()) | ||
| .build(); | ||
| BigQuery bigQuery = options.getService(); | ||
| BigQuery bigQuery = getBigQuery(container); | ||
| // } | ||
|
|
||
| String fn = | ||
|
|
@@ -51,4 +74,61 @@ void test() throws Exception { | |
| assertThat(values).containsOnly(BigDecimal.valueOf(30)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testGrcpEndpoint() throws IOException { | ||
|
||
| try (BigQueryEmulatorContainer container = new BigQueryEmulatorContainer("ghcr.io/goccy/bigquery-emulator:0.6.5")) { | ||
| container.start(); | ||
|
|
||
| // Test setup. | ||
| // Create a table the "regular" way. We need this to verify we can connect a writestream | ||
eddumelendez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BigQuery bigQuery = getBigQuery(container); | ||
| String tableName = "test-table"; | ||
| String datasetName = "test-dataset"; | ||
|
|
||
| bigQuery.create(DatasetInfo.of(DatasetId.of(container.getProjectId(), datasetName))); | ||
|
|
||
| Schema schema = Schema.of( | ||
| Field.of("name", StandardSQLTypeName.STRING) | ||
| ); | ||
|
|
||
| TableId tableId = TableId.of(datasetName, tableName); | ||
| TableDefinition tableDefinition = StandardTableDefinition.of(schema); | ||
| TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); | ||
|
|
||
| bigQuery.create(tableInfo); | ||
|
|
||
| // Actual test. | ||
| // BigQueryWriteSettings requires a HTTP/2 connection, not provided by the originally exposed endpoint. | ||
eddumelendez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BigQueryWriteSettings.Builder bigQueryWriteSettingsBuilder = BigQueryWriteSettings.newBuilder(); | ||
|
|
||
| bigQueryWriteSettingsBuilder.createWriteStreamSettings() | ||
| .setRetrySettings(bigQueryWriteSettingsBuilder.createWriteStreamSettings() | ||
| .getRetrySettings() | ||
| .toBuilder() | ||
| .setTotalTimeout(Duration.ofSeconds(60)) | ||
| .build()); | ||
|
|
||
| // Use the now exposed grpcPort to get a working connection. | ||
eddumelendez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create( | ||
| bigQueryWriteSettingsBuilder.setTransportChannelProvider(FixedTransportChannelProvider.create(GrpcTransportChannel.create( | ||
| ManagedChannelBuilder.forAddress(container.getHost(), container.getEmulatorGrpcPort()).usePlaintext().build()))) | ||
| .setCredentialsProvider(NoCredentialsProvider.create()) | ||
| .build() | ||
| ); | ||
|
|
||
| TableName parentTable = TableName.of(container.getProjectId(), datasetName, tableName); | ||
| CreateWriteStreamRequest createWriteStreamRequest = CreateWriteStreamRequest.newBuilder() | ||
| .setParent(parentTable.toString()) | ||
| .setWriteStream(WriteStream.newBuilder().setType(WriteStream.Type.PENDING)) | ||
| .build(); | ||
|
|
||
| // Validate that we can successfully create a write stream. This would not work with http endpoint | ||
eddumelendez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| bigQueryWriteClient.createWriteStream(createWriteStreamRequest); | ||
|
|
||
| bigQueryWriteClient.shutdown(); | ||
| bigQueryWriteClient.close(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this method to the end of the file