Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 40 additions & 28 deletions modules/databend/databend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

const (
databendUser = "databend"
defaultUser = "databend"
defaultPassword = "databend"
defaultDatabaseName = "default"
defaultPort = "8000/tcp"
)

// DatabendContainer represents the Databend container type used in the module
Expand All @@ -25,11 +25,14 @@ type DatabendContainer struct {
database string
}

// Deprecated: use testcontainers.ContainerCustomizer instead
var _ testcontainers.ContainerCustomizer = (*DatabendOption)(nil)

// Deprecated: use testcontainers.ContainerCustomizer instead
// DatabendOption is an option for the Databend container.
type DatabendOption func(*DatabendContainer)

// Deprecated: use testcontainers.ContainerCustomizer instead
// Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.
func (o DatabendOption) Customize(*testcontainers.GenericContainerRequest) error {
// NOOP to satisfy interface.
Expand All @@ -38,46 +41,55 @@ func (o DatabendOption) Customize(*testcontainers.GenericContainerRequest) error

// 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{
moduleOpts := []testcontainers.ContainerCustomizer{
testcontainers.WithExposedPorts(defaultPort),
testcontainers.WithEnv(map[string]string{
"QUERY_DEFAULT_USER": defaultUser,
"QUERY_DEFAULT_PASSWORD": defaultPassword,
},
WaitingFor: wait.ForListeningPort("8000/tcp"),
}),
testcontainers.WithWaitStrategy(wait.ForListeningPort(defaultPort)),
}

genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
moduleOpts = append(moduleOpts, opts...)

for _, opt := range opts {
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
var c *DatabendContainer
if ctr != nil {
// set default credentials
c = &DatabendContainer{
Container: ctr,
password: defaultPassword,
username: defaultUser,
database: defaultDatabaseName,
}
}

username := req.Env["QUERY_DEFAULT_USER"]
password := req.Env["QUERY_DEFAULT_PASSWORD"]
if password == "" && username == "" {
return nil, errors.New("empty password and user")
if err != nil {
return c, fmt.Errorf("run databend: %w", err)
}

container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
var c *DatabendContainer
if container != nil {
c = &DatabendContainer{
Container: container,
password: password,
username: username,
database: defaultDatabaseName,
// refresh the credentials from the environment variables
inspect, err := ctr.Inspect(ctx)
if err != nil {
return c, fmt.Errorf("inspect databend: %w", err)
}

foundUser, foundPass := false, false
for _, env := range inspect.Config.Env {
if v, ok := strings.CutPrefix(env, "QUERY_DEFAULT_USER="); ok {
c.username, foundUser = v, true
}
if v, ok := strings.CutPrefix(env, "QUERY_DEFAULT_PASSWORD="); ok {
c.password, foundPass = v, true
}

if foundUser && foundPass {
break
}
}

if err != nil {
return c, fmt.Errorf("generic container: %w", err)
if c.username == "" && c.password == "" {
return c, errors.New("empty password and user")
}

return c, nil
Expand Down
11 changes: 11 additions & 0 deletions modules/databend/databend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ func TestDatabendWithDefaultUserAndPassword(t *testing.T) {
")")
require.NoError(t, err)
}

func TestDatabendWithEmptyCredentials(t *testing.T) {
ctx := context.Background()

ctr, err := databend.Run(ctx,
"datafuselabs/databend:v1.2.615",
databend.WithUsername(""),
databend.WithPassword(""))
testcontainers.CleanupContainer(t, ctr)
require.Error(t, err)
}
Loading