@@ -11,10 +11,10 @@ import (
1111)
1212
1313const (
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
2829var _ testcontainers.ContainerCustomizer = (* DatabendOption )(nil )
2930
31+ // Deprecated: use testcontainers.ContainerCustomizer instead
3032// DatabendOption is an option for the Databend container.
3133type DatabendOption func (* DatabendContainer )
3234
35+ // Deprecated: use testcontainers.ContainerCustomizer instead
3336// Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.
3437func (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
4043func 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
0 commit comments