@@ -11,6 +11,7 @@ import (
1111 tc "github.com/testcontainers/testcontainers-go"
1212 tcwait "github.com/testcontainers/testcontainers-go/wait"
1313 "math/rand"
14+ "os"
1415 "strconv"
1516)
1617
@@ -31,6 +32,7 @@ type Minio struct {
3132 secretKey string
3233 bucket string
3334 region string
35+ keep bool
3436}
3537
3638func (m Minio ) GetSecretKey () string {
@@ -45,16 +47,12 @@ func (m Minio) GetBucket() string {
4547 return m .bucket
4648}
4749
48- func (m Minio ) GetURL () string {
49- return fmt .Sprintf ("http://%s:%d" , m .host , m .port )
50- }
51-
5250func (m Minio ) GetConsoleURL () string {
5351 return fmt .Sprintf ("http://%s:%d" , m .host , m .consolePort )
5452}
5553
5654func (m Minio ) GetEndpoint () string {
57- return fmt .Sprintf ("%s:%d" , m .host , m .consolePort )
55+ return fmt .Sprintf ("%s:%d" , m .host , m .port )
5856}
5957
6058func (m Minio ) GetRegion () string {
@@ -69,34 +67,59 @@ func NewMinioFactory() ProviderFactory {
6967 return MinioFactory {}
7068}
7169
72- func (mf MinioFactory ) NewProvider (options ... Option ) (Provider , error ) {
70+ func (mf MinioFactory ) New (options ... Option ) (Provider , error ) {
7371 m := & Minio {
7472 port : DefaultPort ,
7573 consolePort : DefaultConsolePort ,
7674 accessKey : randomStr (20 ),
7775 secretKey : randomStr (40 ),
7876 bucket : DefaultBucket ,
7977 region : DefaultRegion ,
78+ keep : false ,
8079 }
8180
8281 for _ , opt := range options {
8382 opt (m )
8483 }
8584
85+ var tcRyukDisabled string
86+ if m .keep {
87+ // store original env var to value
88+ tcRyukDisabled = os .Getenv ("TESTCONTAINERS_RYUK_DISABLED" )
89+ err := os .Setenv ("TESTCONTAINERS_RYUK_DISABLED" , "true" )
90+ if err != nil {
91+ return nil , err
92+ }
93+ }
94+
8695 ctx := context .Background ()
8796 containerName := framework .DefaultTCName (DefaultName )
8897 bindPort := fmt .Sprintf ("%d/tcp" , m .port )
8998 bindConsolePort := fmt .Sprintf ("%d/tcp" , m .consolePort )
99+ networks := []string {"compose_default" }
100+ networkAliases := map [string ][]string {
101+ "compose_default" : {DefaultName },
102+ }
103+
104+ if len (framework .DefaultNetworkName ) == 0 {
105+ // attach default ctf network if initiated
106+ networks = append (networks , framework .DefaultNetworkName )
107+ networkAliases [framework .DefaultNetworkName ] = []string {
108+ containerName ,
109+ DefaultName ,
110+ }
111+ }
90112
91113 req := tc.ContainerRequest {
92- Name : containerName ,
93- Image : DefaultImage ,
94- Labels : framework .DefaultTCLabels (),
95- Networks : []string {framework .DefaultNetworkName },
96- NetworkAliases : map [string ][]string {
97- framework .DefaultNetworkName : {containerName },
114+ Name : containerName ,
115+ Image : DefaultImage ,
116+ Labels : framework .DefaultTCLabels (),
117+ Networks : networks ,
118+ NetworkAliases : networkAliases ,
119+ ExposedPorts : []string {
120+ bindPort ,
121+ bindConsolePort ,
98122 },
99- ExposedPorts : []string {bindPort , bindConsolePort },
100123 Env : map [string ]string {
101124 "MINIO_ROOT_USER" : m .accessKey ,
102125 "MINIO_ROOT_PASSWORD" : m .secretKey ,
@@ -105,40 +128,39 @@ func (mf MinioFactory) NewProvider(options ...Option) (Provider, error) {
105128 Entrypoint : []string {
106129 "minio" ,
107130 "server" ,
108- "data" ,
131+ "/ data" ,
109132 "--address" ,
110133 fmt .Sprintf (":%d" , m .port ),
111134 "--console-address" ,
112135 fmt .Sprintf (":%d" , m .consolePort ),
113136 },
114137 HostConfigModifier : func (h * container.HostConfig ) {
115138 framework .NoDNS (true , h )
116- h .PortBindings = framework .MapTheSamePort (bindPort )
139+ h .PortBindings = nat.PortMap {
140+ nat .Port (bindPort ): []nat.PortBinding {
141+ {
142+ HostIP : "0.0.0.0" ,
143+ HostPort : strconv .Itoa (m .port ),
144+ },
145+ },
146+ nat .Port (bindConsolePort ): []nat.PortBinding {
147+ {
148+ HostIP : "0.0.0.0" ,
149+ HostPort : strconv .Itoa (m .consolePort ),
150+ },
151+ },
152+ }
117153 },
118154 WaitingFor : tcwait .ForAll (
119- tcwait .ForListeningPort (nat .Port (fmt .Sprintf ("%d/tcp" , m .port ))),
155+ tcwait .ForListeningPort (nat .Port (bindPort )),
156+ tcwait .ForListeningPort (nat .Port (bindConsolePort )),
120157 ),
121158 }
122- req .HostConfigModifier = func (h * container.HostConfig ) {
123- h .PortBindings = nat.PortMap {
124- nat .Port (bindPort ): []nat.PortBinding {
125- {
126- HostIP : "0.0.0.0" ,
127- HostPort : strconv .Itoa (m .port ),
128- },
129- },
130- nat .Port (bindPort ): []nat.PortBinding {
131- {
132- HostIP : "0.0.0.0" ,
133- HostPort : strconv .Itoa (m .consolePort ),
134- },
135- },
136- }
137- }
138159
139160 c , err := tc .GenericContainer (ctx , tc.GenericContainerRequest {
140161 ContainerRequest : req ,
141162 Started : true ,
163+ Reuse : m .keep ,
142164 })
143165 if err != nil {
144166 return nil , err
@@ -156,15 +178,25 @@ func (mf MinioFactory) NewProvider(options ...Option) (Provider, error) {
156178 Secure : false ,
157179 })
158180 if err != nil {
181+ framework .L .Warn ().Str ("error" , err .Error ()).Msg ("failed to create minio client" )
159182 return nil , err
160183 }
161184
162185 // Initialize default bucket
163186 err = minioClient .MakeBucket (ctx , m .GetBucket (), minio.MakeBucketOptions {Region : m .GetRegion ()})
164187 if err != nil {
188+ framework .L .Warn ().Str ("error" , err .Error ()).Msg ("failed to create minio bucket" )
165189 return nil , err
166190 }
167191
192+ if m .keep {
193+ // reverse env var to prev. value
194+ err := os .Setenv ("TESTCONTAINERS_RYUK_DISABLED" , tcRyukDisabled )
195+ if err != nil {
196+ return nil , err
197+ }
198+ }
199+
168200 return m , nil
169201}
170202
@@ -180,6 +212,24 @@ func WithConsolePort(consolePort int) Option {
180212 }
181213}
182214
215+ func WithKeep () Option {
216+ return func (m * Minio ) {
217+ m .keep = true
218+ }
219+ }
220+
221+ func WithAccessKey (accessKey string ) Option {
222+ return func (m * Minio ) {
223+ m .accessKey = accessKey
224+ }
225+ }
226+
227+ func WithSecretKey (secretKey string ) Option {
228+ return func (m * Minio ) {
229+ m .secretKey = secretKey
230+ }
231+ }
232+
183233const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
184234
185235func randomStr (n int ) string {
0 commit comments