Skip to content

Commit b7e70d0

Browse files
mdelapenyaclaude
andauthored
chore(postgres): use Run function (#3425)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 86bebae commit b7e70d0

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

modules/postgres/postgres.go

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,19 @@ func WithConfigFile(cfg string) testcontainers.CustomizeRequestOption {
7070
FileMode: 0o755,
7171
}
7272

73-
req.Files = append(req.Files, cfgFile)
74-
req.Cmd = append(req.Cmd, "-c", "config_file=/etc/postgresql.conf")
73+
if err := testcontainers.WithFiles(cfgFile)(req); err != nil {
74+
return err
75+
}
7576

76-
return nil
77+
return testcontainers.WithCmdArgs("-c", "config_file=/etc/postgresql.conf")(req)
7778
}
7879
}
7980

8081
// WithDatabase sets the initial database to be created when the container starts
8182
// It can be used to define a different name for the default database that is created when the image is first started.
8283
// If it is not specified, then the value of WithUser will be used.
83-
func WithDatabase(dbName string) testcontainers.CustomizeRequestOption {
84-
return func(req *testcontainers.GenericContainerRequest) error {
85-
req.Env["POSTGRES_DB"] = dbName
86-
87-
return nil
88-
}
84+
func WithDatabase(dbName string) testcontainers.ContainerCustomizer {
85+
return testcontainers.WithEnv(map[string]string{"POSTGRES_DB": dbName})
8986
}
9087

9188
// WithInitScripts sets the init scripts to be run when the container starts.
@@ -124,28 +121,19 @@ func WithOrderedInitScripts(scripts ...string) testcontainers.CustomizeRequestOp
124121
// WithPassword sets the initial password of the user to be created when the container starts
125122
// It is required for you to use the PostgreSQL image. It must not be empty or undefined.
126123
// This environment variable sets the superuser password for PostgreSQL.
127-
func WithPassword(password string) testcontainers.CustomizeRequestOption {
128-
return func(req *testcontainers.GenericContainerRequest) error {
129-
req.Env["POSTGRES_PASSWORD"] = password
130-
131-
return nil
132-
}
124+
func WithPassword(password string) testcontainers.ContainerCustomizer {
125+
return testcontainers.WithEnv(map[string]string{"POSTGRES_PASSWORD": password})
133126
}
134127

135128
// WithUsername sets the initial username to be created when the container starts
136129
// It is used in conjunction with WithPassword to set a user and its password.
137130
// It will create the specified user with superuser power and a database with the same name.
138131
// If it is not specified, then the default user of postgres will be used.
139-
func WithUsername(user string) testcontainers.CustomizeRequestOption {
140-
return func(req *testcontainers.GenericContainerRequest) error {
141-
if user == "" {
142-
user = defaultUser
143-
}
144-
145-
req.Env["POSTGRES_USER"] = user
146-
147-
return nil
132+
func WithUsername(user string) testcontainers.ContainerCustomizer {
133+
if user == "" {
134+
user = defaultUser
148135
}
136+
return testcontainers.WithEnv(map[string]string{"POSTGRES_USER": user})
149137
}
150138

151139
// Deprecated: use Run instead
@@ -156,48 +144,64 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
156144

157145
// Run creates an instance of the Postgres container type
158146
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*PostgresContainer, error) {
159-
req := testcontainers.ContainerRequest{
160-
Image: img,
161-
Env: map[string]string{
162-
"POSTGRES_USER": defaultUser,
163-
"POSTGRES_PASSWORD": defaultPassword,
164-
"POSTGRES_DB": defaultUser, // defaults to the user name
165-
},
166-
ExposedPorts: []string{"5432/tcp"},
167-
Cmd: []string{"postgres", "-c", "fsync=off"},
168-
}
169-
170-
genericContainerReq := testcontainers.GenericContainerRequest{
171-
ContainerRequest: req,
172-
Started: true,
173-
}
174-
175147
// Gather all config options (defaults and then apply provided options)
176148
settings := defaultOptions()
177149
for _, opt := range opts {
178150
if apply, ok := opt.(Option); ok {
179151
apply(&settings)
180152
}
181-
if err := opt.Customize(&genericContainerReq); err != nil {
182-
return nil, err
183-
}
184153
}
185154

186-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
155+
moduleOpts := []testcontainers.ContainerCustomizer{
156+
testcontainers.WithEnv(map[string]string{
157+
"POSTGRES_USER": defaultUser,
158+
"POSTGRES_PASSWORD": defaultPassword,
159+
"POSTGRES_DB": defaultUser, // defaults to the user name
160+
}),
161+
testcontainers.WithExposedPorts("5432/tcp"),
162+
testcontainers.WithCmd("postgres", "-c", "fsync=off"),
163+
}
164+
165+
moduleOpts = append(moduleOpts, opts...)
166+
167+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
187168
var c *PostgresContainer
188-
if container != nil {
169+
if ctr != nil {
189170
c = &PostgresContainer{
190-
Container: container,
191-
dbName: req.Env["POSTGRES_DB"],
192-
password: req.Env["POSTGRES_PASSWORD"],
193-
user: req.Env["POSTGRES_USER"],
171+
Container: ctr,
172+
dbName: defaultUser,
173+
password: defaultPassword,
174+
user: defaultUser,
194175
sqlDriverName: settings.SQLDriverName,
195176
snapshotName: settings.Snapshot,
196177
}
197178
}
198179

199180
if err != nil {
200-
return c, fmt.Errorf("generic container: %w", err)
181+
return c, fmt.Errorf("run postgres: %w", err)
182+
}
183+
184+
// Retrieve the actual env vars set on the container
185+
inspect, err := ctr.Inspect(ctx)
186+
if err != nil {
187+
return c, fmt.Errorf("inspect postgres: %w", err)
188+
}
189+
190+
var foundDB, foundUser, foundPass bool
191+
for _, env := range inspect.Config.Env {
192+
if v, ok := strings.CutPrefix(env, "POSTGRES_DB="); ok {
193+
c.dbName, foundDB = v, true
194+
}
195+
if v, ok := strings.CutPrefix(env, "POSTGRES_USER="); ok {
196+
c.user, foundUser = v, true
197+
}
198+
if v, ok := strings.CutPrefix(env, "POSTGRES_PASSWORD="); ok {
199+
c.password, foundPass = v, true
200+
}
201+
202+
if foundDB && foundUser && foundPass {
203+
break
204+
}
201205
}
202206

203207
return c, nil
@@ -228,7 +232,7 @@ func WithSSLCert(caCertFile string, certFile string, keyFile string) testcontain
228232
return func(req *testcontainers.GenericContainerRequest) error {
229233
const entrypointPath = "/usr/local/bin/docker-entrypoint-ssl.bash"
230234

231-
req.Files = append(req.Files,
235+
if err := testcontainers.WithFiles(
232236
testcontainers.ContainerFile{
233237
HostFilePath: caCertFile,
234238
ContainerFilePath: "/tmp/testcontainers-go/postgres/ca_cert.pem",
@@ -249,10 +253,11 @@ func WithSSLCert(caCertFile string, certFile string, keyFile string) testcontain
249253
ContainerFilePath: entrypointPath,
250254
FileMode: defaultPermission,
251255
},
252-
)
253-
req.Entrypoint = []string{"sh", entrypointPath}
256+
)(req); err != nil {
257+
return err
258+
}
254259

255-
return nil
260+
return testcontainers.WithEntrypoint("sh", entrypointPath)(req)
256261
}
257262
}
258263

0 commit comments

Comments
 (0)