Skip to content

Commit

Permalink
chore: pass a reader to WithDataYAML option
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Dec 11, 2024
1 parent 2956a10 commit cd8324c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/modules/gcloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ 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)
#### Data YAML (Seed File)

- Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>

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`.
If you would like to do additional initialization in the BigQuery container, add a `data.yaml` file represented by an `io.Reader` to the container request with the `WithDataYAML` function.
That file is copied after the container is created but before it's started. The startup command then used will look like `--project test --data-from-yaml /testcontainers-data.yaml`.

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

Expand All @@ -42,7 +42,7 @@ An example of a `data.yaml` file that seeds the BigQuery instance with datasets
<!--/codeinclude-->

!!!warning
This feature is only available for the `BigQuery` container, and if you pass multiple `WithDataYamlFile` options, the last file is used.
This feature is only available for the `BigQuery` container, and if you pass multiple `WithDataYAML` options, only the last one is used.

### BigTable

Expand Down
9 changes: 4 additions & 5 deletions modules/gcloud/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gcloud

import (
"context"
"path/filepath"
"time"

"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -35,15 +34,15 @@ func RunBigQuery(ctx context.Context, img string, opts ...testcontainers.Contain
req.Cmd = append(req.Cmd, "--project", settings.ProjectID)

// Process data yaml file only for the BigQuery container.
if settings.bigQueryDataYamlFile != "" {
containerPath := "/" + filepath.Base(settings.bigQueryDataYamlFile)
if settings.bigQueryDataYaml != nil {
containerPath := "/testcontainers-data.yaml"

req.Cmd = append(req.Cmd, "--data-from-yaml", containerPath)

req.Files = append(req.Files, testcontainers.ContainerFile{
HostFilePath: settings.bigQueryDataYamlFile,
Reader: settings.bigQueryDataYaml,
ContainerFilePath: containerPath,
FileMode: 0o755,
FileMode: 0o644,
})
}

Expand Down
16 changes: 13 additions & 3 deletions modules/gcloud/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -95,11 +96,14 @@ func TestBigQueryWithDataYamlFile(t *testing.T) {
testDataPath, err := filepath.Abs(filepath.Join(".", "testdata"))
require.NoError(t, err)

r, err := os.Open(filepath.Join(testDataPath, "data.yaml"))
require.NoError(t, err)

bigQueryContainer, err := gcloud.RunBigQuery(
ctx,
"ghcr.io/goccy/bigquery-emulator:0.6.1",
gcloud.WithProjectID("test"),
gcloud.WithDataYamlFile(filepath.Join(testDataPath, "data.yaml")),
gcloud.WithDataYAML(r),
)
testcontainers.CleanupContainer(t, bigQueryContainer)
require.NoError(t, err)
Expand Down Expand Up @@ -142,12 +146,18 @@ func TestBigQueryWithDataYamlFile_multiple(t *testing.T) {
testDataPath, err := filepath.Abs(filepath.Join(".", "testdata"))
require.NoError(t, err)

r1, err := os.Open(filepath.Join(testDataPath, "data.yaml"))
require.NoError(t, err)

r2, err := os.Open(filepath.Join(testDataPath, "data2.yaml"))
require.NoError(t, err)

bigQueryContainer, err := gcloud.RunBigQuery(
ctx,
"ghcr.io/goccy/bigquery-emulator:0.6.1",
gcloud.WithProjectID("test"),
gcloud.WithDataYamlFile(filepath.Join(testDataPath, "data.yaml")),
gcloud.WithDataYamlFile(filepath.Join(testDataPath, "data2.yaml")), // last file will be used
gcloud.WithDataYAML(r1),
gcloud.WithDataYAML(r2), // last file will be used
)
testcontainers.CleanupContainer(t, bigQueryContainer)
require.NoError(t, err)
Expand Down
17 changes: 9 additions & 8 deletions modules/gcloud/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcloud
import (
"context"
"fmt"
"io"

"github.com/docker/go-connections/nat"

Expand Down Expand Up @@ -44,8 +45,8 @@ func newGCloudContainer(ctx context.Context, req testcontainers.GenericContainer
}

type options struct {
ProjectID string
bigQueryDataYamlFile string
ProjectID string
bigQueryDataYaml io.Reader
}

func defaultOptions() options {
Expand Down Expand Up @@ -73,15 +74,15 @@ func WithProjectID(projectID string) Option {
}
}

// WithDataYamlFile seeds the Bigquery project for the GCloud container
// with the local path to the data yaml file, which is used to copy the file to the container,
// and then process the file to seed the Bigquery project.
// WithDataYAML seeds the Bigquery project for the GCloud container with an io.Reader representing
// the data yaml file, which is used to copy the file to the container, and then processed to seed
// the Bigquery project.
//
// Other GCloud containers will ignore this option.
// If this option is passed multiple times, the last file will be used.
func WithDataYamlFile(f string) Option {
// If this option is passed multiple times, the last added will be used.
func WithDataYAML(r io.Reader) Option {
return func(o *options) {
o.bigQueryDataYamlFile = f
o.bigQueryDataYaml = r
}
}

Expand Down

0 comments on commit cd8324c

Please sign in to comment.