diff --git a/docs/modules/gcloud.md b/docs/modules/gcloud.md index 2344ebc393..92135102ff 100644 --- a/docs/modules/gcloud.md +++ b/docs/modules/gcloud.md @@ -42,7 +42,7 @@ An example of a `data.yaml` file that seeds the BigQuery instance with datasets !!!warning - This feature is only available for the `BigQuery` container, and if you pass multiple `WithDataYAML` options, only the last one is used. + This feature is only available for the `BigQuery` container, and if you pass multiple `WithDataYAML` options, an error is returned. ### BigTable diff --git a/modules/gcloud/bigquery_test.go b/modules/gcloud/bigquery_test.go index a9b73271ed..7a09276a95 100644 --- a/modules/gcloud/bigquery_test.go +++ b/modules/gcloud/bigquery_test.go @@ -160,47 +160,5 @@ func TestBigQueryWithDataYamlFile_multiple(t *testing.T) { gcloud.WithDataYAML(r2), // last file will be used ) testcontainers.CleanupContainer(t, bigQueryContainer) - require.NoError(t, err) - - 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...) - require.NoError(t, err) - defer client.Close() - - t.Run("select/dataset1-not-found", func(t *testing.T) { - selectQuery := client.Query("SELECT * FROM dataset1.table_a where name = @name") - selectQuery.QueryConfig.Parameters = []bigquery.QueryParameter{ - {Name: "name", Value: "bob"}, - } - _, err := selectQuery.Read(ctx) - require.Error(t, err) - }) - - t.Run("select/dataset2", func(t *testing.T) { - selectQuery := client.Query("SELECT * FROM dataset2.table_b where name = @name") - selectQuery.QueryConfig.Parameters = []bigquery.QueryParameter{ - {Name: "name", Value: "naomi"}, - } - it, err := selectQuery.Read(ctx) - require.NoError(t, err) - - var val []bigquery.Value - for { - err := it.Next(&val) - if errors.Is(err, iterator.Done) { - break - } - require.NoError(t, err) - } - - require.Equal(t, int64(60), val[0]) - }) + require.Error(t, err) } diff --git a/modules/gcloud/gcloud.go b/modules/gcloud/gcloud.go index 1f03c6ed30..281e6067d5 100644 --- a/modules/gcloud/gcloud.go +++ b/modules/gcloud/gcloud.go @@ -2,6 +2,7 @@ package gcloud import ( "context" + "errors" "fmt" "io" @@ -59,7 +60,7 @@ func defaultOptions() options { var _ testcontainers.ContainerCustomizer = (*Option)(nil) // Option is an option for the GCloud container. -type Option func(*options) +type Option func(*options) error // Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface. func (o Option) Customize(*testcontainers.GenericContainerRequest) error { @@ -69,8 +70,9 @@ func (o Option) Customize(*testcontainers.GenericContainerRequest) error { // WithProjectID sets the project ID for the GCloud container. func WithProjectID(projectID string) Option { - return func(o *options) { + return func(o *options) error { o.ProjectID = projectID + return nil } } @@ -81,8 +83,13 @@ func WithProjectID(projectID string) Option { // Other GCloud containers will ignore this option. // If this option is passed multiple times, the last added will be used. func WithDataYAML(r io.Reader) Option { - return func(o *options) { + return func(o *options) error { + if o.bigQueryDataYaml != nil { + return errors.New("data yaml file already exists") + } + o.bigQueryDataYaml = r + return nil } } @@ -91,7 +98,9 @@ func applyOptions(req *testcontainers.GenericContainerRequest, opts []testcontai settings := defaultOptions() for _, opt := range opts { if apply, ok := opt.(Option); ok { - apply(&settings) + if err := apply(&settings); err != nil { + return options{}, err + } } if err := opt.Customize(req); err != nil { return options{}, err