Skip to content

Commit d256b4b

Browse files
mdelapenyaclaude
andauthored
chore(registry): use Run function (#3431)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 7ae3814 commit d256b4b

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

modules/registry/options.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ const (
2020
// The dataPath must have the same structure as the registry data directory.
2121
func WithData(dataPath string) testcontainers.CustomizeRequestOption {
2222
return func(req *testcontainers.GenericContainerRequest) error {
23-
req.Files = append(req.Files, testcontainers.ContainerFile{
23+
if err := testcontainers.WithFiles(testcontainers.ContainerFile{
2424
HostFilePath: dataPath,
2525
ContainerFilePath: containerDataPath,
26-
})
26+
})(req); err != nil {
27+
return err
28+
}
2729

28-
req.Env["REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY"] = containerDataPath
29-
return nil
30+
return testcontainers.WithEnv(map[string]string{
31+
"REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY": containerDataPath,
32+
})(req)
3033
}
3134
}
3235

@@ -60,16 +63,18 @@ func WithHtpasswd(credentials string) testcontainers.CustomizeRequestOption {
6063
// thanks to the REGISTRY_AUTH_HTPASSWD_PATH environment variable.
6164
func WithHtpasswdFile(htpasswdPath string) testcontainers.CustomizeRequestOption {
6265
return func(req *testcontainers.GenericContainerRequest) error {
63-
req.Files = append(req.Files, testcontainers.ContainerFile{
66+
if err := testcontainers.WithFiles(testcontainers.ContainerFile{
6467
HostFilePath: htpasswdPath,
6568
ContainerFilePath: containerHtpasswdPath,
6669
FileMode: 0o644,
67-
})
70+
})(req); err != nil {
71+
return err
72+
}
6873

69-
req.Env["REGISTRY_AUTH"] = "htpasswd"
70-
req.Env["REGISTRY_AUTH_HTPASSWD_REALM"] = "Registry"
71-
req.Env["REGISTRY_AUTH_HTPASSWD_PATH"] = containerHtpasswdPath
72-
req.Env["REGISTRY_AUTH_HTPASSWD_PATH"] = containerHtpasswdPath
73-
return nil
74+
return testcontainers.WithEnv(map[string]string{
75+
"REGISTRY_AUTH": "htpasswd",
76+
"REGISTRY_AUTH_HTPASSWD_REALM": "Registry",
77+
"REGISTRY_AUTH_HTPASSWD_PATH": containerHtpasswdPath,
78+
})(req)
7479
}
7580
}

modules/registry/registry.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -273,36 +273,26 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
273273

274274
// Run creates an instance of the Registry container type
275275
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*RegistryContainer, error) {
276-
req := testcontainers.ContainerRequest{
277-
Image: img,
278-
ExposedPorts: []string{registryPort},
279-
Env: map[string]string{
276+
moduleOpts := []testcontainers.ContainerCustomizer{
277+
testcontainers.WithExposedPorts(registryPort),
278+
testcontainers.WithEnv(map[string]string{
280279
// convenient for testing
281280
"REGISTRY_STORAGE_DELETE_ENABLED": "true",
282-
},
283-
WaitingFor: wait.ForHTTP("/").
284-
WithPort(registryPort).
285-
WithStartupTimeout(10 * time.Second),
281+
}),
282+
testcontainers.WithWaitStrategy(
283+
wait.ForHTTP("/").
284+
WithPort(registryPort).
285+
WithStartupTimeout(10 * time.Second),
286+
),
286287
}
287288

288-
genericContainerReq := testcontainers.GenericContainerRequest{
289-
ContainerRequest: req,
290-
Started: true,
291-
}
292-
293-
for _, opt := range opts {
294-
if err := opt.Customize(&genericContainerReq); err != nil {
295-
return nil, err
296-
}
297-
}
298-
299-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
289+
container, err := testcontainers.Run(ctx, img, append(moduleOpts, opts...)...)
300290
var c *RegistryContainer
301291
if container != nil {
302292
c = &RegistryContainer{Container: container}
303293
}
304294
if err != nil {
305-
return c, fmt.Errorf("generic container: %w", err)
295+
return c, fmt.Errorf("run registry: %w", err)
306296
}
307297

308298
address, err := c.Address(ctx)

0 commit comments

Comments
 (0)