@@ -54,29 +54,21 @@ func (c *Container) URI(ctx context.Context) (string, error) {
54
54
55
55
// Run creates an instance of the Toxiproxy container type
56
56
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
70
58
settings := defaultOptions ()
71
59
for _ , opt := range opts {
72
60
if apply , ok := opt .(Option ); ok {
73
61
if err := apply (& settings ); err != nil {
74
- return nil , fmt .Errorf ("apply: %w" , err )
62
+ return nil , fmt .Errorf ("apply option : %w" , err )
75
63
}
76
64
}
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
+ })),
80
72
}
81
73
82
74
// 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
87
79
proxy .Listen = fmt .Sprintf ("0.0.0.0:%d" , proxiedPort )
88
80
portsInRange = append (portsInRange , fmt .Sprintf ("%d/tcp" , proxiedPort ))
89
81
}
90
- genericContainerReq .ExposedPorts = append (genericContainerReq .ExposedPorts , portsInRange ... )
82
+
83
+ if len (portsInRange ) > 0 {
84
+ moduleOpts = append (moduleOpts , testcontainers .WithExposedPorts (portsInRange ... ))
85
+ }
91
86
92
87
// Render the config file
93
88
jsonData , err := json .MarshalIndent (settings .proxies , "" , " " )
94
89
if err != nil {
95
- return nil , fmt .Errorf ("marshal: %w" , err )
90
+ return nil , fmt .Errorf ("marshal config : %w" , err )
96
91
}
97
92
98
93
// Apply the config file to the container with the proxies.
99
94
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
+ )
106
103
}
107
104
108
- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
105
+ ctr , err := testcontainers .Run (ctx , img , append ( moduleOpts , opts ... ) ... )
109
106
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 )}
112
109
}
113
110
114
111
if err != nil {
115
- return c , fmt .Errorf ("generic container : %w" , err )
112
+ return c , fmt .Errorf ("run toxiproxy : %w" , err )
116
113
}
117
114
118
115
// Map the ports of the proxies to the container, so that we can use them in the tests
119
116
for _ , proxy := range settings .proxies {
120
117
err := proxy .sanitize ()
121
118
if err != nil {
122
- return c , fmt .Errorf ("sanitize: %w" , err )
119
+ return c , fmt .Errorf ("sanitize proxy : %w" , err )
123
120
}
124
121
125
122
endpoint , err := c .PortEndpoint (ctx , nat .Port (fmt .Sprintf ("%d/tcp" , proxy .listenPort )), "" )
0 commit comments