Skip to content

Commit 7ed7f28

Browse files
jespinoona-agent
andcommitted
fix(mysql): use Inspect() to get container environment variables
Use container.Inspect() instead of non-existent Env() method to extract environment variables from the running container, following the same pattern as other migrated modules. Co-authored-by: Ona <no-reply@ona.com>
1 parent 7f3474c commit 7ed7f28

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

modules/mysql/mysql.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,39 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
6868
container, err := testcontainers.Run(ctx, img, moduleOpts...)
6969
var c *MySQLContainer
7070
if container != nil {
71-
// Extract configuration from the container environment
72-
env, err := container.Env(ctx)
73-
if err != nil {
74-
return nil, fmt.Errorf("get container env: %w", err)
75-
}
71+
c = &MySQLContainer{Container: container}
72+
}
7673

77-
username, ok := env["MYSQL_USER"]
78-
if !ok {
79-
username = rootUser
80-
}
81-
password := env["MYSQL_PASSWORD"]
74+
if err != nil {
75+
return c, fmt.Errorf("run mysql: %w", err)
76+
}
8277

83-
if len(password) == 0 && password == "" && !strings.EqualFold(rootUser, username) {
84-
return nil, errors.New("empty password can be used only with the root user")
85-
}
78+
// Extract configuration from the container environment
79+
inspect, err := container.Inspect(ctx)
80+
if err != nil {
81+
return c, fmt.Errorf("inspect mysql: %w", err)
82+
}
8683

87-
c = &MySQLContainer{
88-
Container: container,
89-
password: password,
90-
username: username,
91-
database: env["MYSQL_DATABASE"],
84+
env := make(map[string]string)
85+
for _, envVar := range inspect.Config.Env {
86+
if key, value, found := strings.Cut(envVar, "="); found {
87+
env[key] = value
9288
}
9389
}
9490

95-
if err != nil {
96-
return c, fmt.Errorf("run mysql: %w", err)
91+
username, ok := env["MYSQL_USER"]
92+
if !ok {
93+
username = rootUser
9794
}
95+
password := env["MYSQL_PASSWORD"]
96+
97+
if len(password) == 0 && password == "" && !strings.EqualFold(rootUser, username) {
98+
return nil, errors.New("empty password can be used only with the root user")
99+
}
100+
101+
c.password = password
102+
c.username = username
103+
c.database = env["MYSQL_DATABASE"]
98104

99105
return c, nil
100106
}

0 commit comments

Comments
 (0)