-
-
Notifications
You must be signed in to change notification settings - Fork 605
feat: support databend module #2779
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
mdelapenya
merged 31 commits into
testcontainers:main
from
hantmac:feat/support-databend-module
Sep 20, 2024
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0274491
feat: support databend module
hantmac 1a51a84
chore: add project scaffolding
mdelapenya 8473e23
chore: more file
mdelapenya 6eddbaf
chore: run make lint
mdelapenya af447e3
fix: proper pinned version
mdelapenya 4d3f405
fix comment
hantmac efd6342
add the detail documents
hantmac ff632e7
Update modules/databend/databend.go
hantmac 1a09c9a
fix review comment
hantmac b281577
fix
hantmac 77c4c42
default database
hantmac 19fd929
Update modules/databend/databend_test.go
hantmac bbbc0b4
fix review comments
hantmac 11fecd3
Merge branch 'feat/support-databend-module' of github.com:hantmac/tes…
hantmac f1bd4f0
Update modules/databend/databend.go
hantmac dd1a66c
Update modules/databend/databend.go
hantmac fdd7ad2
fix comment
hantmac 4eebd6e
fix databend-go module
hantmac 8b729bf
fix databend tests
hantmac 0db9906
fix golangci-lint
hantmac 2d9c1d4
Merge branch 'main' into feat/support-databend-module
hantmac 6e2401c
Merge branch 'main' into feat/support-databend-module
hantmac b78a4cf
Update modules/databend/databend.go
hantmac 97016db
Merge branch 'main' into feat/support-databend-module
hantmac 74dead1
remove WithDatabase
hantmac 3ff0b52
Merge branch 'feat/support-databend-module' of github.com:hantmac/tes…
hantmac e3e7489
Merge branch 'main' into feat/support-databend-module
hantmac 7f7bcce
fix
hantmac 5a54a32
Merge branch 'feat/support-databend-module' of github.com:hantmac/tes…
hantmac 0922ab7
chore: rollback pinned version
mdelapenya 7a24c55
Merge branch 'main' into feat/support-databend-module
hantmac 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
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Databend | ||
|
|
||
| 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> | ||
|
|
||
| ## Introduction | ||
|
|
||
| The Testcontainers module for Databend. | ||
|
|
||
| ## Adding this module to your project dependencies | ||
|
|
||
| Please run the following command to add the Databend module to your Go dependencies: | ||
|
|
||
| ``` | ||
| go get github.com/testcontainers/testcontainers-go/modules/databend | ||
| ``` | ||
|
|
||
| ## Usage example | ||
|
|
||
| <!--codeinclude--> | ||
| [Creating a Databend container](../../modules/databend/examples_test.go) inside_block:runDatabendContainer | ||
| <!--/codeinclude--> | ||
|
|
||
| ## Module Reference | ||
|
|
||
| ### Run function | ||
|
|
||
| - 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> | ||
|
|
||
| The Databend module exposes one entrypoint function to create the Databend container, and this function receives three parameters: | ||
|
|
||
| ```golang | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DatabendContainer, error) | ||
| ``` | ||
|
|
||
| - `context.Context`, the Go context. | ||
| - `string`, the Docker image to use. | ||
| - `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
|
||
| ### Container Options | ||
|
|
||
| When starting the Databend container, you can pass options in a variadic way to configure it. | ||
|
|
||
| #### Image | ||
|
|
||
| If you need to set a different Databend Docker image, you can set a valid Docker image as the second argument in the `Run` function. | ||
| E.g. `Run(context.Background(), "datafuselabs/databend:v1.2.615")`. | ||
|
|
||
| {% include "../features/common_functional_options.md" %} | ||
|
|
||
| #### Set username, password | ||
|
|
||
| If you need to set a different user/password/database, you can use `WithUsername`, `WithPassword` options. | ||
|
|
||
| !!!info | ||
| The default values for the username is `databend`, for password is `databend` and for the default database name is `default`. | ||
|
|
||
| ### Container Methods | ||
|
|
||
| The Databend container exposes the following methods: | ||
|
|
||
| #### ConnectionString | ||
|
|
||
| This method returns the connection string to connect to the Databend container, using the default `8000` port. | ||
| It's possible to pass extra parameters to the connection string, e.g. `sslmode=disable`. | ||
|
|
||
| <!--codeinclude--> | ||
| [Get connection string](../../modules/databend/databend_test.go) inside_block:connectionString | ||
| <!--/codeinclude--> | ||
|
|
||
| #### MustGetConnectionString | ||
|
|
||
| `MustConnectionString` panics if the address cannot be determined. | ||
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| include ../../commons-test.mk | ||
|
|
||
| .PHONY: test | ||
| test: | ||
| $(MAKE) test-databend |
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package databend | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/wait" | ||
| ) | ||
|
|
||
| const ( | ||
| databendUser = "databend" | ||
| defaultUser = "databend" | ||
| defaultPassword = "databend" | ||
| defaultDatabaseName = "default" | ||
| ) | ||
|
|
||
| // DatabendContainer represents the Databend container type used in the module | ||
| type DatabendContainer struct { | ||
| testcontainers.Container | ||
| username string | ||
| password string | ||
| database string | ||
| } | ||
|
|
||
| var _ testcontainers.ContainerCustomizer = (*DatabendOption)(nil) | ||
|
|
||
| // DatabendOption is an option for the Databend container. | ||
| type DatabendOption func(*DatabendContainer) | ||
|
|
||
| // Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface. | ||
| func (o DatabendOption) Customize(*testcontainers.GenericContainerRequest) error { | ||
| // NOOP to satisfy interface. | ||
| return nil | ||
| } | ||
|
|
||
| // Run creates an instance of the Databend container type | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DatabendContainer, error) { | ||
| req := testcontainers.ContainerRequest{ | ||
| Image: img, | ||
| ExposedPorts: []string{"8000/tcp"}, | ||
| Env: map[string]string{ | ||
| "QUERY_DEFAULT_USER": defaultUser, | ||
| "QUERY_DEFAULT_PASSWORD": defaultPassword, | ||
| }, | ||
| WaitingFor: wait.ForListeningPort("8000/tcp"), | ||
| } | ||
|
|
||
| genericContainerReq := testcontainers.GenericContainerRequest{ | ||
| ContainerRequest: req, | ||
| Started: true, | ||
| } | ||
|
|
||
| for _, opt := range opts { | ||
| if err := opt.Customize(&genericContainerReq); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| username := req.Env["QUERY_DEFAULT_USER"] | ||
| password := req.Env["QUERY_DEFAULT_PASSWORD"] | ||
| if password == "" && username == "" { | ||
| return nil, errors.New("empty password and user") | ||
| } | ||
|
|
||
| container, err := testcontainers.GenericContainer(ctx, genericContainerReq) | ||
| var c *DatabendContainer | ||
| if container != nil { | ||
| c = &DatabendContainer{ | ||
| Container: container, | ||
| password: password, | ||
| username: username, | ||
| database: defaultDatabaseName, | ||
|
stevenh marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| if err != nil { | ||
| return c, fmt.Errorf("generic container: %w", err) | ||
| } | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| // MustConnectionString panics if the address cannot be determined. | ||
| func (c *DatabendContainer) MustConnectionString(ctx context.Context, args ...string) string { | ||
| addr, err := c.ConnectionString(ctx, args...) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return addr | ||
| } | ||
|
|
||
| func (c *DatabendContainer) ConnectionString(ctx context.Context, args ...string) (string, error) { | ||
| containerPort, err := c.MappedPort(ctx, "8000/tcp") | ||
| if err != nil { | ||
| return "", fmt.Errorf("mapped port: %w", err) | ||
| } | ||
|
|
||
| host, err := c.Host(ctx) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| extraArgs := "" | ||
| if len(args) > 0 { | ||
| extraArgs = "?" + strings.Join(args, "&") | ||
| } | ||
| if c.database == "" { | ||
|
stevenh marked this conversation as resolved.
|
||
| return "", errors.New("database name is empty") | ||
| } | ||
|
|
||
| // databend://databend:databend@localhost:8000/default?sslmode=disable | ||
| connectionString := fmt.Sprintf("databend://%s:%s@%s:%s/%s%s", c.username, c.password, host, containerPort.Port(), c.database, extraArgs) | ||
| return connectionString, nil | ||
| } | ||
|
|
||
| // WithUsername sets the username for the Databend container. | ||
| // WithUsername is [Run] option that configures the default query user by setting | ||
| // the `QUERY_DEFAULT_USER` container environment variable. | ||
| func WithUsername(username string) testcontainers.CustomizeRequestOption { | ||
|
hantmac marked this conversation as resolved.
|
||
| return func(req *testcontainers.GenericContainerRequest) error { | ||
| req.Env["QUERY_DEFAULT_USER"] = username | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithPassword sets the password for the Databend container. | ||
| func WithPassword(password string) testcontainers.CustomizeRequestOption { | ||
| return func(req *testcontainers.GenericContainerRequest) error { | ||
| req.Env["QUERY_DEFAULT_PASSWORD"] = password | ||
| return nil | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package databend_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "testing" | ||
|
|
||
| _ "github.com/datafuselabs/databend-go" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/modules/databend" | ||
| ) | ||
|
|
||
| func TestDatabend(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| ctr, err := databend.Run(ctx, "datafuselabs/databend:v1.2.615") | ||
| testcontainers.CleanupContainer(t, ctr) | ||
| require.NoError(t, err) | ||
|
|
||
| // perform assertions | ||
| // connectionString { | ||
| connectionString, err := ctr.ConnectionString(ctx, "sslmode=disable") | ||
| // } | ||
| require.NoError(t, err) | ||
|
|
||
| mustConnectionString := ctr.MustConnectionString(ctx, "sslmode=disable") | ||
| require.Equal(t, connectionString, mustConnectionString) | ||
|
|
||
| db, err := sql.Open("databend", connectionString) | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
|
|
||
| err = db.Ping() | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = db.Exec("CREATE TABLE IF NOT EXISTS a_table ( \n" + | ||
| " `col_1` VARCHAR(128) NOT NULL, \n" + | ||
| " `col_2` VARCHAR(128) NOT NULL \n" + | ||
| ")") | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestDatabendWithDefaultUserAndPassword(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| ctr, err := databend.Run(ctx, | ||
| "datafuselabs/databend:v1.2.615", | ||
| databend.WithUsername("databend")) | ||
| testcontainers.CleanupContainer(t, ctr) | ||
| require.NoError(t, err) | ||
|
|
||
| // perform assertions | ||
| connectionString, err := ctr.ConnectionString(ctx, "sslmode=disable") | ||
| require.NoError(t, err) | ||
|
|
||
| db, err := sql.Open("databend", connectionString) | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
| err = db.Ping() | ||
| require.NoError(t, err) | ||
|
|
||
| var i int | ||
| row := db.QueryRow("select 1") | ||
| err = row.Scan(&i) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = db.Exec("CREATE TABLE IF NOT EXISTS a_table ( \n" + | ||
| " `col_1` VARCHAR(128) NOT NULL, \n" + | ||
| " `col_2` VARCHAR(128) NOT NULL \n" + | ||
| ")") | ||
| require.NoError(t, err) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.