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

Conversation

mtellis2
Copy link

What does this PR do?

Updates the RunBigQueryContainer to optionally use the emulator's data-from-yaml option, allowing users to seed the container with data.

Why is it important?

This give the user of the RunBigQueryContainer the option to seed test data on the container creation/startup.

Related issues

@mtellis2 mtellis2 requested a review from a team as a code owner April 30, 2024 23:36
Copy link

netlify bot commented Apr 30, 2024

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 97cc547
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/663284109d8e6c0008c2e164
😎 Deploy Preview https://deploy-preview-2523--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

modules/gcloud/bigquery.go Show resolved Hide resolved
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/gcloud"
)

func ExampleRunBigQueryContainer() {
func TestBigQueryContainer(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

We would like to keep some testable examples, as they will land in the pkg.go.dev docs. If you have doubts on when creating a real test or a testable example, please use a testable example when you want to show case how to use the module. Else, a regular test is fine.

modules/gcloud/bigquery_test.go Outdated Show resolved Hide resolved
modules/gcloud/bigquery_test.go Outdated Show resolved Hide resolved
Comment on lines 30 to 35
for _, opt := range opts {
if err := opt.Customize(&req); err != nil {
return nil, err
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

@mdelapenya it wasn't working before without this and that appears to be due to needing to update
req.Cmd = []string{"--project", settings.ProjectID} to req.Cmd = append(req.Cmd, "--project", settings.ProjectID) so that the options added to the cmd are not removed

Copy link
Member

Choose a reason for hiding this comment

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

Let me test this locally, from this branch. Will come back to you later

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, with the addition of the YAML data file, now it's possible to have different settings contributing to the CMD. So it's fine if we append.

My only concern is now related to the usage of the gcloud options across all the emulators 🤔 Do you know if all of them would support passing this YAML file? Else, anybody could add this functional option to e.g. Firestore or Pubsub, and expect something else. I'm not an expert in those services, so you probably have more insights.

Copy link
Author

@mtellis2 mtellis2 May 2, 2024

Choose a reason for hiding this comment

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

Good point, I myself have not used the other emulators but would assume this data yaml functionality would and should only apply for the bigquery emulator. I'm currently not sure of a great way to prevent this in the other gcloud emulators. I think the data.yaml option should be ignored though due to how that CMD is being set.
req.Cmd = []string{...}

It looks like the spanner container might be the only one that differs. It does not have req.Cmd = []string{...}

modules/gcloud/go.mod Outdated Show resolved Hide resolved
Copy link
Collaborator

@stevenh stevenh left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I've done an initial pass but looks like this needs a rebase.

Comment on lines +92 to +94
if err != nil {
log.Fatalf("failed to run container: %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.

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)

Comment on lines +106 to +110
defer func() {
if err := bigQueryContainer.Terminate(ctx); err != nil {
log.Fatalf("failed to terminate container: %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.

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

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

@@ -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.

Comment on lines +74 to +75
// WithDataYamlFile seeds the Bigquery project for the GCloud container.
func WithDataYamlFile(dataYamlFile string) testcontainers.CustomizeRequestOption {
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


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants