Skip to content

Commit 8350eb9

Browse files
authored
chore(databend): use Run function (#3402)
* chore(databend): use Run function * chore: deprecate DatabendOption as is no longer needed/used
1 parent e3c6250 commit 8350eb9

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

modules/databend/databend.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
)
1212

1313
const (
14-
databendUser = "databend"
1514
defaultUser = "databend"
1615
defaultPassword = "databend"
1716
defaultDatabaseName = "default"
17+
defaultPort = "8000/tcp"
1818
)
1919

2020
// DatabendContainer represents the Databend container type used in the module
@@ -25,11 +25,14 @@ type DatabendContainer struct {
2525
database string
2626
}
2727

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

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

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

3942
// Run creates an instance of the Databend container type
4043
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*DatabendContainer, error) {
41-
req := testcontainers.ContainerRequest{
42-
Image: img,
43-
ExposedPorts: []string{"8000/tcp"},
44-
Env: map[string]string{
44+
moduleOpts := []testcontainers.ContainerCustomizer{
45+
testcontainers.WithExposedPorts(defaultPort),
46+
testcontainers.WithEnv(map[string]string{
4547
"QUERY_DEFAULT_USER": defaultUser,
4648
"QUERY_DEFAULT_PASSWORD": defaultPassword,
47-
},
48-
WaitingFor: wait.ForListeningPort("8000/tcp"),
49+
}),
50+
testcontainers.WithWaitStrategy(wait.ForListeningPort(defaultPort)),
4951
}
5052

51-
genericContainerReq := testcontainers.GenericContainerRequest{
52-
ContainerRequest: req,
53-
Started: true,
54-
}
53+
moduleOpts = append(moduleOpts, opts...)
5554

56-
for _, opt := range opts {
57-
if err := opt.Customize(&genericContainerReq); err != nil {
58-
return nil, err
55+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
56+
var c *DatabendContainer
57+
if ctr != nil {
58+
// set default credentials
59+
c = &DatabendContainer{
60+
Container: ctr,
61+
password: defaultPassword,
62+
username: defaultUser,
63+
database: defaultDatabaseName,
5964
}
6065
}
6166

62-
username := req.Env["QUERY_DEFAULT_USER"]
63-
password := req.Env["QUERY_DEFAULT_PASSWORD"]
64-
if password == "" && username == "" {
65-
return nil, errors.New("empty password and user")
67+
if err != nil {
68+
return c, fmt.Errorf("run databend: %w", err)
6669
}
6770

68-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
69-
var c *DatabendContainer
70-
if container != nil {
71-
c = &DatabendContainer{
72-
Container: container,
73-
password: password,
74-
username: username,
75-
database: defaultDatabaseName,
71+
// refresh the credentials from the environment variables
72+
inspect, err := ctr.Inspect(ctx)
73+
if err != nil {
74+
return c, fmt.Errorf("inspect databend: %w", err)
75+
}
76+
77+
foundUser, foundPass := false, false
78+
for _, env := range inspect.Config.Env {
79+
if v, ok := strings.CutPrefix(env, "QUERY_DEFAULT_USER="); ok {
80+
c.username, foundUser = v, true
81+
}
82+
if v, ok := strings.CutPrefix(env, "QUERY_DEFAULT_PASSWORD="); ok {
83+
c.password, foundPass = v, true
84+
}
85+
86+
if foundUser && foundPass {
87+
break
7688
}
7789
}
7890

79-
if err != nil {
80-
return c, fmt.Errorf("generic container: %w", err)
91+
if c.username == "" && c.password == "" {
92+
return c, errors.New("empty password and user")
8193
}
8294

8395
return c, nil

modules/databend/databend_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,14 @@ func TestDatabendWithDefaultUserAndPassword(t *testing.T) {
7272
")")
7373
require.NoError(t, err)
7474
}
75+
76+
func TestDatabendWithEmptyCredentials(t *testing.T) {
77+
ctx := context.Background()
78+
79+
ctr, err := databend.Run(ctx,
80+
"datafuselabs/databend:v1.2.615",
81+
databend.WithUsername(""),
82+
databend.WithPassword(""))
83+
testcontainers.CleanupContainer(t, ctr)
84+
require.Error(t, err)
85+
}

0 commit comments

Comments
 (0)