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

feat: Add support to seed data when using RunBigQueryContainer #2523

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion docs/modules/gcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/modules/gcloud
## Usage example

!!!info
By default, the all the emulators use `gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators` as the default Docker image, except for the BigQuery emulator, which uses `ghcr.io/goccy/bigquery-emulator:0.4.3`, and Spanner, which uses `gcr.io/cloud-spanner-emulator/emulator:1.4.0`.
By default, the all the emulators use `gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators` as the default Docker image, except for the BigQuery emulator, which uses `ghcr.io/goccy/bigquery-emulator:0.6.1`, and Spanner, which uses `gcr.io/cloud-spanner-emulator/emulator:1.4.0`.

### BigQuery

Expand All @@ -28,6 +28,17 @@ go get github.com/testcontainers/testcontainers-go/modules/gcloud

It's important to set the `option.WithEndpoint()` option using the container's URI, as shown in the client example above.

#### Data Yaml (Seed File)

If you would like to do additional initialization in the BigQuery container, add a `data.yaml` file to the container request with the `WithDataYamlFile` function.
Those files will be copied after the container is created but before it's started. The startup command then used will look like `--project test --data-from-yaml /data.yaml`.

An example of a `data.yaml` file that seeds the BigQuery instance with datasets and tables is shown below:

<!--codeinclude-->
[Data Yaml content](../../modules/gcloud/testdata/data.yaml)
<!--/codeinclude-->

### BigTable

<!--codeinclude-->
Expand Down
10 changes: 5 additions & 5 deletions modules/gcloud/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func RunBigQueryContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*GCloudContainer, error) {
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "ghcr.io/goccy/bigquery-emulator:0.4.3",
Image: "ghcr.io/goccy/bigquery-emulator:0.6.1",
ExposedPorts: []string{"9050/tcp", "9060/tcp"},
WaitingFor: wait.ForHTTP("/discovery/v1/apis/bigquery/v2/rest").WithPort("9050/tcp").WithStartupTimeout(time.Second * 5),
},
Expand All @@ -25,20 +25,20 @@ func RunBigQueryContainer(ctx context.Context, opts ...testcontainers.ContainerC
return nil, err
}

req.Cmd = []string{"--project", settings.ProjectID}
req.Cmd = append(req.Cmd, "--project", settings.ProjectID)

container, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
return nil, err
}

spannerContainer, err := newGCloudContainer(ctx, 9050, container, settings)
bigqueryContainer, err := newGCloudContainer(ctx, 9050, container, settings)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this needs a rebase as there has been some work to ensure this correctly returns the container even on error.

if err != nil {
return nil, err
}

// always prepend http:// to the URI
spannerContainer.URI = "http://" + spannerContainer.URI
bigqueryContainer.URI = "http://" + bigqueryContainer.URI

return spannerContainer, nil
return bigqueryContainer, nil
mtellis2 marked this conversation as resolved.
Show resolved Hide resolved
}
73 changes: 71 additions & 2 deletions modules/gcloud/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"log"
"path/filepath"
"testing"

"cloud.google.com/go/bigquery"
"google.golang.org/api/iterator"
Expand All @@ -23,7 +25,7 @@ func ExampleRunBigQueryContainer() {

bigQueryContainer, err := gcloud.RunBigQueryContainer(
ctx,
testcontainers.WithImage("ghcr.io/goccy/bigquery-emulator:0.4.3"),
testcontainers.WithImage("ghcr.io/goccy/bigquery-emulator:0.6.1"),
gcloud.WithProjectID("bigquery-project"),
)
if err != nil {
Expand Down Expand Up @@ -78,8 +80,75 @@ func ExampleRunBigQueryContainer() {
}
}

fmt.Println(val)
fmt.Println(val[0])
// Output:
// 30
}

func TestBigQueryWithDataYamlFile(t *testing.T) {
ctx := context.Background()

absPath, err := filepath.Abs(filepath.Join(".", "testdata", "data.yaml"))
if err != nil {
log.Fatalf("failed to run container: %v", err)
}
Comment on lines +92 to +94
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: use require.NoError to simplify, more below

Suggested change
if err != nil {
log.Fatalf("failed to run container: %v", err)
}
require.NoError(t, err)


bigQueryContainer, err := gcloud.RunBigQueryContainer(
ctx,
testcontainers.WithImage("ghcr.io/goccy/bigquery-emulator:0.6.1"),
gcloud.WithProjectID("test"),
gcloud.WithDataYamlFile(absPath),
)
if err != nil {
log.Fatalf("failed to run container: %v", err)
}

defer func() {
if err := bigQueryContainer.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %v", err)
}
}()
Comment on lines +106 to +110
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: use testcontainers.CleanupContainer to simplify and ensure correct cleanup.


projectID := bigQueryContainer.Settings.ProjectID

opts := []option.ClientOption{
option.WithEndpoint(bigQueryContainer.URI),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication(),
internaloption.SkipDialSettingsValidation(),
}

client, err := bigquery.NewClient(ctx, projectID, opts...)
if err != nil {
log.Fatalf("failed to create bigquery client: %v", err) // nolint:gocritic
}
defer client.Close()

selectQuery := client.Query("SELECT * FROM dataset1.table_a where name = @name")
selectQuery.QueryConfig.Parameters = []bigquery.QueryParameter{
{Name: "name", Value: "bob"},
}
it, err := selectQuery.Read(ctx)
if err != nil {
log.Fatalf("failed to read query: %v", err)
}

var val []bigquery.Value
for {
err := it.Next(&val)
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
log.Fatalf("failed to iterate: %v", err)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug: left over output comments below.


// Output:
// [30]
expectedValue := int64(30)
actualValue := val[0]
if expectedValue != actualValue {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: use require.Equal or EqualValue

t.Errorf("BigQuery value didn't match. \nExpected %v, \nbut got: %v", expectedValue, actualValue)
}
}
22 changes: 20 additions & 2 deletions modules/gcloud/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func newGCloudContainer(ctx context.Context, port int, c testcontainers.Containe
}

type options struct {
ProjectID string
ProjectID string
DataYamlFile string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug: this doesn't seem to be ever used.

}

func defaultOptions() options {
return options{
ProjectID: defaultProjectID,
ProjectID: defaultProjectID,
DataYamlFile: "/data.yaml",
}
}

Expand All @@ -69,6 +71,22 @@ func WithProjectID(projectID string) Option {
}
}

// WithDataYamlFile seeds the Bigquery project for the GCloud container.
func WithDataYamlFile(dataYamlFile string) testcontainers.CustomizeRequestOption {
Comment on lines +74 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: remove the duplication between function name and paramater

return func(req *testcontainers.GenericContainerRequest) error {
mtellis2 marked this conversation as resolved.
Show resolved Hide resolved
dataFile := testcontainers.ContainerFile{
HostFilePath: dataYamlFile,
ContainerFilePath: "/data.yaml",
FileMode: 0o755,
}

req.Files = append(req.Files, dataFile)
req.Cmd = append(req.Cmd, "--data-from-yaml", "/data.yaml")

return nil
}
}

// applyOptions applies the options to the container request and returns the settings.
func applyOptions(req *testcontainers.GenericContainerRequest, opts []testcontainers.ContainerCustomizer) (options, error) {
settings := defaultOptions()
Expand Down
1 change: 1 addition & 0 deletions modules/gcloud/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ require (
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
Expand Down
3 changes: 2 additions & 1 deletion modules/gcloud/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
20 changes: 20 additions & 0 deletions modules/gcloud/testdata/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
projects:
- id: test
datasets:
- id: dataset1
tables:
- id: table_a
columns:
- name: id
type: INTEGER
- name: name
type: STRING
- name: createdAt
type: TIMESTAMP
data:
- id: 1
name: alice
createdAt: "2022-10-21T00:00:00"
- id: 30
name: bob
createdAt: "2022-10-21T00:00:00"