@@ -52,53 +52,69 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
5252
5353// Run creates an instance of the MySQL container type
5454func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* MySQLContainer , error ) {
55- req := testcontainers.ContainerRequest {
56- Image : img ,
57- ExposedPorts : []string {"3306/tcp" , "33060/tcp" },
58- Env : map [string ]string {
55+ moduleOpts := []testcontainers.ContainerCustomizer {
56+ testcontainers .WithExposedPorts ("3306/tcp" , "33060/tcp" ),
57+ testcontainers .WithEnv (map [string ]string {
5958 "MYSQL_USER" : defaultUser ,
6059 "MYSQL_PASSWORD" : defaultPassword ,
6160 "MYSQL_DATABASE" : defaultDatabaseName ,
62- },
63- WaitingFor : wait .ForLog ("port: 3306 MySQL Community Server" ),
61+ }) ,
62+ testcontainers . WithWaitStrategy ( wait .ForLog ("port: 3306 MySQL Community Server" ) ),
6463 }
6564
66- genericContainerReq := testcontainers.GenericContainerRequest {
67- ContainerRequest : req ,
68- Started : true ,
69- }
70-
71- opts = append (opts , WithDefaultCredentials ())
65+ moduleOpts = append (moduleOpts , opts ... )
66+ moduleOpts = append (moduleOpts , WithDefaultCredentials ())
7267
73- for _ , opt := range opts {
74- if err := opt .Customize (& genericContainerReq ); err != nil {
75- return nil , err
68+ // Validate credentials after applying all options
69+ validateCreds := func (req * testcontainers.GenericContainerRequest ) error {
70+ username , ok := req .Env ["MYSQL_USER" ]
71+ if ! ok {
72+ username = rootUser
7673 }
77- }
74+ password := req . Env [ "MYSQL_PASSWORD" ]
7875
79- username , ok := req .Env ["MYSQL_USER" ]
80- if ! ok {
81- username = rootUser
76+ if len (password ) == 0 && password == "" && ! strings .EqualFold (rootUser , username ) {
77+ return errors .New ("empty password can be used only with the root user" )
78+ }
79+ return nil
8280 }
83- password := req .Env ["MYSQL_PASSWORD" ]
8481
85- if len (password ) == 0 && password == "" && ! strings .EqualFold (rootUser , username ) {
86- return nil , errors .New ("empty password can be used only with the root user" )
87- }
82+ moduleOpts = append (moduleOpts , testcontainers .CustomizeRequestOption (validateCreds ))
8883
89- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
84+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
9085 var c * MySQLContainer
91- if container != nil {
86+ if ctr != nil {
9287 c = & MySQLContainer {
93- Container : container ,
94- password : password ,
95- username : username ,
96- database : req .Env ["MYSQL_DATABASE" ],
88+ Container : ctr ,
89+ username : rootUser , // default to root, will be overridden if MYSQL_USER is set
9790 }
9891 }
9992
10093 if err != nil {
101- return c , fmt .Errorf ("generic container: %w" , err )
94+ return c , fmt .Errorf ("run mysql: %w" , err )
95+ }
96+
97+ // Retrieve credentials from container environment
98+ inspect , err := ctr .Inspect (ctx )
99+ if err != nil {
100+ return c , fmt .Errorf ("inspect mysql: %w" , err )
101+ }
102+
103+ var foundUser , foundPass , foundDB bool
104+ for _ , env := range inspect .Config .Env {
105+ if v , ok := strings .CutPrefix (env , "MYSQL_USER=" ); ok {
106+ c .username , foundUser = v , true
107+ }
108+ if v , ok := strings .CutPrefix (env , "MYSQL_PASSWORD=" ); ok {
109+ c .password , foundPass = v , true
110+ }
111+ if v , ok := strings .CutPrefix (env , "MYSQL_DATABASE=" ); ok {
112+ c .database , foundDB = v , true
113+ }
114+
115+ if foundUser && foundPass && foundDB {
116+ break
117+ }
102118 }
103119
104120 return c , nil
0 commit comments