Skip to content

Commit 1c64a46

Browse files
mdelapenyaclaude
andauthored
chore(toxiproxy): use Run function (#3435)
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 62a65a4 commit 1c64a46

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

modules/toxiproxy/toxiproxy.go

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,21 @@ func (c *Container) URI(ctx context.Context) (string, error) {
5454

5555
// Run creates an instance of the Toxiproxy container type
5656
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
57-
req := testcontainers.ContainerRequest{
58-
Image: img,
59-
ExposedPorts: []string{ControlPort},
60-
WaitingFor: wait.ForHTTP("/version").WithPort(ControlPort).WithStatusCodeMatcher(func(status int) bool {
61-
return status == http.StatusOK
62-
}),
63-
}
64-
65-
genericContainerReq := testcontainers.GenericContainerRequest{
66-
ContainerRequest: req,
67-
Started: true,
68-
}
69-
57+
// Process custom options first
7058
settings := defaultOptions()
7159
for _, opt := range opts {
7260
if apply, ok := opt.(Option); ok {
7361
if err := apply(&settings); err != nil {
74-
return nil, fmt.Errorf("apply: %w", err)
62+
return nil, fmt.Errorf("apply option: %w", err)
7563
}
7664
}
77-
if err := opt.Customize(&genericContainerReq); err != nil {
78-
return nil, fmt.Errorf("customize: %w", err)
79-
}
65+
}
66+
67+
moduleOpts := []testcontainers.ContainerCustomizer{
68+
testcontainers.WithExposedPorts(ControlPort),
69+
testcontainers.WithWaitStrategy(wait.ForHTTP("/version").WithPort(ControlPort).WithStatusCodeMatcher(func(status int) bool {
70+
return status == http.StatusOK
71+
})),
8072
}
8173

8274
// Expose the ports for the proxies, starting from the first proxied port
@@ -87,39 +79,44 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
8779
proxy.Listen = fmt.Sprintf("0.0.0.0:%d", proxiedPort)
8880
portsInRange = append(portsInRange, fmt.Sprintf("%d/tcp", proxiedPort))
8981
}
90-
genericContainerReq.ExposedPorts = append(genericContainerReq.ExposedPorts, portsInRange...)
82+
83+
if len(portsInRange) > 0 {
84+
moduleOpts = append(moduleOpts, testcontainers.WithExposedPorts(portsInRange...))
85+
}
9186

9287
// Render the config file
9388
jsonData, err := json.MarshalIndent(settings.proxies, "", " ")
9489
if err != nil {
95-
return nil, fmt.Errorf("marshal: %w", err)
90+
return nil, fmt.Errorf("marshal config: %w", err)
9691
}
9792

9893
// Apply the config file to the container with the proxies.
9994
if len(settings.proxies) > 0 {
100-
genericContainerReq.Files = append(genericContainerReq.Files, testcontainers.ContainerFile{
101-
Reader: bytes.NewReader(jsonData),
102-
ContainerFilePath: "/tmp/tc-toxiproxy.json",
103-
FileMode: 0o644,
104-
})
105-
genericContainerReq.Cmd = append(genericContainerReq.Cmd, "-host=0.0.0.0", "-config=/tmp/tc-toxiproxy.json")
95+
moduleOpts = append(moduleOpts,
96+
testcontainers.WithFiles(testcontainers.ContainerFile{
97+
Reader: bytes.NewReader(jsonData),
98+
ContainerFilePath: "/tmp/tc-toxiproxy.json",
99+
FileMode: 0o644,
100+
}),
101+
testcontainers.WithCmd("-host=0.0.0.0", "-config=/tmp/tc-toxiproxy.json"),
102+
)
106103
}
107104

108-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
105+
ctr, err := testcontainers.Run(ctx, img, append(moduleOpts, opts...)...)
109106
var c *Container
110-
if container != nil {
111-
c = &Container{Container: container, proxiedEndpoints: make(map[int]string)}
107+
if ctr != nil {
108+
c = &Container{Container: ctr, proxiedEndpoints: make(map[int]string)}
112109
}
113110

114111
if err != nil {
115-
return c, fmt.Errorf("generic container: %w", err)
112+
return c, fmt.Errorf("run toxiproxy: %w", err)
116113
}
117114

118115
// Map the ports of the proxies to the container, so that we can use them in the tests
119116
for _, proxy := range settings.proxies {
120117
err := proxy.sanitize()
121118
if err != nil {
122-
return c, fmt.Errorf("sanitize: %w", err)
119+
return c, fmt.Errorf("sanitize proxy: %w", err)
123120
}
124121

125122
endpoint, err := c.PortEndpoint(ctx, nat.Port(fmt.Sprintf("%d/tcp", proxy.listenPort)), "")

0 commit comments

Comments
 (0)