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: support databend module #2779

Merged
merged 31 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0274491
feat: support databend module
hantmac Sep 13, 2024
1a51a84
chore: add project scaffolding
mdelapenya Sep 13, 2024
8473e23
chore: more file
mdelapenya Sep 13, 2024
6eddbaf
chore: run make lint
mdelapenya Sep 13, 2024
af447e3
fix: proper pinned version
mdelapenya Sep 13, 2024
4d3f405
fix comment
hantmac Sep 13, 2024
efd6342
add the detail documents
hantmac Sep 13, 2024
ff632e7
Update modules/databend/databend.go
hantmac Sep 14, 2024
1a09c9a
fix review comment
hantmac Sep 14, 2024
b281577
fix
hantmac Sep 14, 2024
77c4c42
default database
hantmac Sep 14, 2024
19fd929
Update modules/databend/databend_test.go
hantmac Sep 15, 2024
bbbc0b4
fix review comments
hantmac Sep 15, 2024
11fecd3
Merge branch 'feat/support-databend-module' of github.com:hantmac/tes…
hantmac Sep 15, 2024
f1bd4f0
Update modules/databend/databend.go
hantmac Sep 15, 2024
dd1a66c
Update modules/databend/databend.go
hantmac Sep 15, 2024
fdd7ad2
fix comment
hantmac Sep 15, 2024
4eebd6e
fix databend-go module
hantmac Sep 16, 2024
8b729bf
fix databend tests
hantmac Sep 16, 2024
0db9906
fix golangci-lint
hantmac Sep 17, 2024
2d9c1d4
Merge branch 'main' into feat/support-databend-module
hantmac Sep 17, 2024
6e2401c
Merge branch 'main' into feat/support-databend-module
hantmac Sep 18, 2024
b78a4cf
Update modules/databend/databend.go
hantmac Sep 18, 2024
97016db
Merge branch 'main' into feat/support-databend-module
hantmac Sep 18, 2024
74dead1
remove WithDatabase
hantmac Sep 19, 2024
3ff0b52
Merge branch 'feat/support-databend-module' of github.com:hantmac/tes…
hantmac Sep 19, 2024
e3e7489
Merge branch 'main' into feat/support-databend-module
hantmac Sep 19, 2024
7f7bcce
fix
hantmac Sep 19, 2024
5a54a32
Merge branch 'feat/support-databend-module' of github.com:hantmac/tes…
hantmac Sep 19, 2024
0922ab7
chore: rollback pinned version
mdelapenya Sep 19, 2024
7a24c55
Merge branch 'main' into feat/support-databend-module
hantmac Sep 20, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
matrix:
go-version: [1.22.x, 1.x]
platform: [ubuntu-latest]
module: [artemis, azurite, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, dolt, elasticsearch, gcloud, grafana-lgtm, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, valkey, vault, vearch, weaviate]
module: [artemis, azurite, cassandra, chroma, clickhouse, cockroachdb, compose, consul, couchbase, databend, dolt, elasticsearch, gcloud, grafana-lgtm, inbucket, influxdb, k3s, k6, kafka, localstack, mariadb, milvus, minio, mockserver, mongodb, mssql, mysql, nats, neo4j, ollama, openfga, openldap, opensearch, postgres, pulsar, qdrant, rabbitmq, redis, redpanda, registry, surrealdb, valkey, vault, vearch, weaviate]
uses: ./.github/workflows/ci-test-go.yml
with:
go-version: ${{ matrix.go-version }}
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.1
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
Expand Down
4 changes: 4 additions & 0 deletions .vscode/.testcontainers-go.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"name": "module / couchbase",
"path": "../modules/couchbase"
},
{
"name": "module / databend",
"path": "../modules/databend"
},
{
"name": "module / dolt",
"path": "../modules/dolt"
Expand Down
55 changes: 55 additions & 0 deletions docs/modules/databend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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>

!!!info
The `RunContainer(ctx, opts...)` function is deprecated and will be removed in the next major release of _Testcontainers for Go_.
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

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" %}

mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
### Container Methods

The Databend container exposes the following methods:
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ nav:
- modules/cockroachdb.md
- modules/consul.md
- modules/couchbase.md
- modules/databend.md
- modules/dolt.md
- modules/elasticsearch.md
- modules/gcloud.md
Expand Down
5 changes: 5 additions & 0 deletions modules/databend/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ../../commons-test.mk

.PHONY: test
test:
$(MAKE) test-databend
141 changes: 141 additions & 0 deletions modules/databend/databend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package databend

import (
"context"
"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
}

func WithDefaultCredentials() testcontainers.CustomizeRequestOption {
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
return func(req *testcontainers.GenericContainerRequest) error {
username := req.Env["QUERY_DEFAULT_USER"]
hantmac marked this conversation as resolved.
Show resolved Hide resolved
password := req.Env["QUERY_DEFAULT_PASSWORD"]
if len(password) == 0 {
password = defaultPassword

Check failure on line 32 in modules/databend/databend.go

View workflow job for this annotation

GitHub Actions / test-modules (1.22.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.22.x

ineffectual assignment to password (ineffassign)

Check failure on line 32 in modules/databend/databend.go

View workflow job for this annotation

GitHub Actions / test-modules (1.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.x

ineffectual assignment to password (ineffassign)
}
if len(username) == 0 {
username = defaultUser

Check failure on line 35 in modules/databend/databend.go

View workflow job for this annotation

GitHub Actions / test-modules (1.22.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.22.x

ineffectual assignment to username (ineffassign)

Check failure on line 35 in modules/databend/databend.go

View workflow job for this annotation

GitHub Actions / test-modules (1.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.x

ineffectual assignment to username (ineffassign)
}
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.ForLog("port: 8000 Databend Query Server"),
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
}

genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}

opts = append(opts, WithDefaultCredentials())

for _, opt := range opts {
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}

username, ok := req.Env["QUERY_DEFAULT_USER"]
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
username = defaultUser
}
password := req.Env["QUERY_DEFAULT_PASSWORD"]

if len(password) == 0 && password == "" && !strings.EqualFold(defaultUser, username) {
return nil, fmt.Errorf("empty password and user")
}
hantmac marked this conversation as resolved.
Show resolved Hide resolved

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.
Show resolved Hide 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 "", err
hantmac marked this conversation as resolved.
Show resolved Hide resolved
}

host, err := c.Host(ctx)
if err != nil {
return "", err
}

extraArgs := ""
if len(args) > 0 {
extraArgs = strings.Join(args, "&")
}
if extraArgs != "" {
extraArgs = "?" + extraArgs
}
hantmac marked this conversation as resolved.
Show resolved Hide resolved

// 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
}

func WithUsername(username string) testcontainers.CustomizeRequestOption {
hantmac marked this conversation as resolved.
Show resolved Hide resolved
return func(req *testcontainers.GenericContainerRequest) error {
req.Env["QUERY_DEFAULT_USER"] = username

return nil
}
}

func WithPassword(password string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Env["QUERY_DEFAULT_PASSWORD"] = password

return nil
}
}
80 changes: 80 additions & 0 deletions modules/databend/databend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package databend_test

import (
"context"
"database/sql"
"testing"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"

Check failure on line 9 in modules/databend/databend_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.22.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.22.x

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/testcontainers) (gci)

Check failure on line 9 in modules/databend/databend_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.x

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/testcontainers) (gci)

Check failure on line 10 in modules/databend/databend_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.22.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.22.x

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/testcontainers) (gci)

Check failure on line 10 in modules/databend/databend_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.x, ubuntu-latest, databend) / modules/databend/ubuntu-latest/1.x

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/testcontainers) (gci)
"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 TestDatabendWithNonRootUserAndEmptyPassword(t *testing.T) {
ctx := context.Background()

ctr, err := databend.Run(ctx,
"datafuselabs/databend:v1.2.615",
databend.WithUsername("databend"),
databend.WithPassword("databend"))
testcontainers.CleanupContainer(t, ctr)
require.EqualError(t, err, "empty password can be used only with the root user")
}

func TestDatabendWithRootUserAndEmptyPassword(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, _ := ctr.ConnectionString(ctx)
hantmac marked this conversation as resolved.
Show resolved Hide resolved

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" +
" PRIMARY KEY (`col_1`, `col_2`) \n" +
")")
require.NoError(t, err)
}
Loading
Loading