Skip to content

Commit a642d65

Browse files
mdelapenyaclaude
andauthored
chore(openldap): use Run function (#3422)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 25897bd commit a642d65

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

modules/openldap/openldap.go

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"strings"
89

910
"github.com/testcontainers/testcontainers-go"
1011
"github.com/testcontainers/testcontainers-go/wait"
@@ -50,44 +51,32 @@ func (c *OpenLDAPContainer) LoadLdif(ctx context.Context, ldif []byte) error {
5051
// WithAdminUsername sets the initial admin username to be created when the container starts
5152
// It is used in conjunction with WithAdminPassword to set a username and its password.
5253
// It will create the specified user with admin power.
53-
func WithAdminUsername(username string) testcontainers.CustomizeRequestOption {
54-
return func(req *testcontainers.GenericContainerRequest) error {
55-
req.Env["LDAP_ADMIN_USERNAME"] = username
56-
57-
return nil
58-
}
54+
func WithAdminUsername(username string) testcontainers.ContainerCustomizer {
55+
return testcontainers.WithEnv(map[string]string{"LDAP_ADMIN_USERNAME": username})
5956
}
6057

6158
// WithAdminPassword sets the initial admin password of the user to be created when the container starts
6259
// It is used in conjunction with WithAdminUsername to set a username and its password.
6360
// It will set the admin password for OpenLDAP.
64-
func WithAdminPassword(password string) testcontainers.CustomizeRequestOption {
65-
return func(req *testcontainers.GenericContainerRequest) error {
66-
req.Env["LDAP_ADMIN_PASSWORD"] = password
67-
68-
return nil
69-
}
61+
func WithAdminPassword(password string) testcontainers.ContainerCustomizer {
62+
return testcontainers.WithEnv(map[string]string{"LDAP_ADMIN_PASSWORD": password})
7063
}
7164

7265
// WithRoot sets the root of the OpenLDAP instance
73-
func WithRoot(root string) testcontainers.CustomizeRequestOption {
74-
return func(req *testcontainers.GenericContainerRequest) error {
75-
req.Env["LDAP_ROOT"] = root
76-
77-
return nil
78-
}
66+
func WithRoot(root string) testcontainers.ContainerCustomizer {
67+
return testcontainers.WithEnv(map[string]string{"LDAP_ROOT": root})
7968
}
8069

8170
// WithInitialLdif sets the initial ldif file to be loaded into the OpenLDAP container
8271
func WithInitialLdif(ldif string) testcontainers.CustomizeRequestOption {
8372
return func(req *testcontainers.GenericContainerRequest) error {
84-
req.Files = append(req.Files, testcontainers.ContainerFile{
73+
cf := testcontainers.ContainerFile{
8574
HostFilePath: ldif,
8675
ContainerFilePath: "/initial_ldif.ldif",
8776
FileMode: 0o644,
88-
})
77+
}
8978

90-
req.LifecycleHooks = append(req.LifecycleHooks, testcontainers.ContainerLifecycleHooks{
79+
hook := testcontainers.ContainerLifecycleHooks{
9180
PostReadies: []testcontainers.ContainerHook{
9281
func(ctx context.Context, container testcontainers.Container) error {
9382
username := req.Env["LDAP_ADMIN_USERNAME"]
@@ -104,9 +93,13 @@ func WithInitialLdif(ldif string) testcontainers.CustomizeRequestOption {
10493
return nil
10594
},
10695
},
107-
})
96+
}
10897

109-
return nil
98+
if err := testcontainers.WithFiles(cf)(req); err != nil {
99+
return err
100+
}
101+
102+
return testcontainers.WithAdditionalLifecycleHooks(hook)(req)
110103
}
111104
}
112105

@@ -118,49 +111,57 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
118111

119112
// Run creates an instance of the OpenLDAP container type
120113
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*OpenLDAPContainer, error) {
121-
req := testcontainers.ContainerRequest{
122-
Image: img,
123-
Env: map[string]string{
114+
moduleOpts := []testcontainers.ContainerCustomizer{
115+
testcontainers.WithEnv(map[string]string{
124116
"LDAP_ADMIN_USERNAME": defaultUser,
125117
"LDAP_ADMIN_PASSWORD": defaultPassword,
126118
"LDAP_ROOT": defaultRoot,
127-
},
128-
ExposedPorts: []string{"1389/tcp"},
129-
WaitingFor: wait.ForAll(
119+
}),
120+
testcontainers.WithExposedPorts("1389/tcp"),
121+
testcontainers.WithWaitStrategy(wait.ForAll(
130122
wait.ForLog("** Starting slapd **"),
131123
wait.ForListeningPort("1389/tcp"),
132-
),
133-
LifecycleHooks: []testcontainers.ContainerLifecycleHooks{
134-
{
135-
PostReadies: []testcontainers.ContainerHook{},
136-
},
137-
},
124+
)),
138125
}
139126

140-
genericContainerReq := testcontainers.GenericContainerRequest{
141-
ContainerRequest: req,
142-
Started: true,
143-
}
144-
145-
for _, opt := range opts {
146-
if err := opt.Customize(&genericContainerReq); err != nil {
147-
return nil, err
148-
}
149-
}
127+
moduleOpts = append(moduleOpts, opts...)
150128

151-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
129+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
152130
var c *OpenLDAPContainer
153-
if container != nil {
131+
if ctr != nil {
154132
c = &OpenLDAPContainer{
155-
Container: container,
156-
adminUsername: req.Env["LDAP_ADMIN_USERNAME"],
157-
adminPassword: req.Env["LDAP_ADMIN_PASSWORD"],
158-
rootDn: req.Env["LDAP_ROOT"],
133+
Container: ctr,
134+
adminUsername: defaultUser,
135+
adminPassword: defaultPassword,
136+
rootDn: defaultRoot,
159137
}
160138
}
161139

162140
if err != nil {
163-
return c, fmt.Errorf("generic container: %w", err)
141+
return c, fmt.Errorf("run openldap: %w", err)
142+
}
143+
144+
// Retrieve the actual env vars set on the container
145+
inspect, err := ctr.Inspect(ctx)
146+
if err != nil {
147+
return c, fmt.Errorf("inspect openldap: %w", err)
148+
}
149+
150+
var foundUser, foundPass, foundRoot bool
151+
for _, env := range inspect.Config.Env {
152+
if v, ok := strings.CutPrefix(env, "LDAP_ADMIN_USERNAME="); ok {
153+
c.adminUsername, foundUser = v, true
154+
}
155+
if v, ok := strings.CutPrefix(env, "LDAP_ADMIN_PASSWORD="); ok {
156+
c.adminPassword, foundPass = v, true
157+
}
158+
if v, ok := strings.CutPrefix(env, "LDAP_ROOT="); ok {
159+
c.rootDn, foundRoot = v, true
160+
}
161+
162+
if foundUser && foundPass && foundRoot {
163+
break
164+
}
164165
}
165166

166167
return c, nil

0 commit comments

Comments
 (0)