@@ -27,7 +27,14 @@ type DoltContainer struct {
2727 database string
2828}
2929
30+ // Deprecated: this function will be removed in the next major release.
3031func WithDefaultCredentials () testcontainers.CustomizeRequestOption {
32+ return withDefaultCredentials ()
33+ }
34+
35+ // withDefaultCredentials is the function that applies the default credentials to the container request.
36+ // In case the provided username is the root user, the credentials will be removed.
37+ func withDefaultCredentials () testcontainers.CustomizeRequestOption {
3138 return func (req * testcontainers.GenericContainerRequest ) error {
3239 username := req .Env ["DOLT_USER" ]
3340 if strings .EqualFold (rootUser , username ) {
@@ -47,56 +54,59 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
4754
4855// Run creates an instance of the Dolt container type
4956func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* DoltContainer , error ) {
50- req := testcontainers.ContainerRequest {
51- Image : img ,
52- ExposedPorts : []string {"3306/tcp" , "33060/tcp" },
53- Env : map [string ]string {
57+ moduleOpts := []testcontainers.ContainerCustomizer {
58+ testcontainers .WithExposedPorts ("3306/tcp" , "33060/tcp" ),
59+ testcontainers .WithEnv (map [string ]string {
5460 "DOLT_USER" : defaultUser ,
5561 "DOLT_PASSWORD" : defaultPassword ,
5662 "DOLT_DATABASE" : defaultDatabaseName ,
57- },
58- WaitingFor : wait .ForLog ("Server ready. Accepting connections." ),
63+ }) ,
64+ testcontainers . WithWaitStrategy ( wait .ForLog ("Server ready. Accepting connections." ) ),
5965 }
6066
61- genericContainerReq := testcontainers.GenericContainerRequest {
62- ContainerRequest : req ,
63- Started : true ,
67+ moduleOpts = append (moduleOpts , opts ... )
68+ moduleOpts = append (moduleOpts , WithDefaultCredentials ())
69+
70+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
71+ var dc * DoltContainer
72+ if ctr != nil {
73+ dc = & DoltContainer {Container : ctr , username : defaultUser , password : defaultPassword , database : defaultDatabaseName }
74+ }
75+ if err != nil {
76+ return dc , fmt .Errorf ("run dolt: %w" , err )
6477 }
6578
66- opts = append (opts , WithDefaultCredentials ())
79+ // refresh the credentials from the environment variables
80+ inspect , err := ctr .Inspect (ctx )
81+ if err != nil {
82+ return dc , fmt .Errorf ("inspect dolt: %w" , err )
83+ }
6784
68- for _ , opt := range opts {
69- if err := opt .Customize (& genericContainerReq ); err != nil {
70- return nil , err
85+ foundUser := false
86+ for _ , env := range inspect .Config .Env {
87+ if v , ok := strings .CutPrefix (env , "DOLT_USER=" ); ok {
88+ dc .username , foundUser = v , true
89+ }
90+ if v , ok := strings .CutPrefix (env , "DOLT_PASSWORD=" ); ok {
91+ dc .password = v
92+ }
93+ if v , ok := strings .CutPrefix (env , "DOLT_DATABASE=" ); ok {
94+ dc .database = v
7195 }
7296 }
7397
7498 createUser := true
75- username , ok := req .Env ["DOLT_USER" ]
76- if ! ok {
77- username = rootUser
99+ if ! foundUser {
100+ // withCredentials found the root user
101+ dc .username = rootUser
102+ dc .password = ""
78103 createUser = false
79104 }
80- password := req .Env ["DOLT_PASSWORD" ]
81-
82- database := req .Env ["DOLT_DATABASE" ]
83- if database == "" {
84- database = defaultDatabaseName
85- }
86105
87- if len (password ) == 0 && password == "" && ! strings .EqualFold (rootUser , username ) {
106+ if len (dc . password ) == 0 && dc . password == "" && ! strings .EqualFold (rootUser , dc . username ) {
88107 return nil , errors .New ("empty password can be used only with the root user" )
89108 }
90109
91- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
92- var dc * DoltContainer
93- if container != nil {
94- dc = & DoltContainer {Container : container , username : username , password : password , database : database }
95- }
96- if err != nil {
97- return dc , err
98- }
99-
100110 // dolthub/dolt-sql-server does not create user or database, so we do so here
101111 if err = dc .initialize (ctx , createUser ); err != nil {
102112 return dc , fmt .Errorf ("initialize: %w" , err )
0 commit comments