55 "fmt"
66 "math/rand"
77 "net"
8- "os"
98 "strconv"
109
10+ "dario.cat/mergo"
1111 "github.com/docker/docker/api/types/container"
1212 "github.com/docker/go-connections/nat"
1313 "github.com/minio/minio-go/v7"
@@ -23,6 +23,7 @@ const (
2323 DefaultName = "minio"
2424 DefaultBucket = "test-bucket"
2525 DefaultRegion = "us-east-1"
26+ DefaultHost = "minio"
2627 DefaultPort = 9000
2728 DefaultConsolePort = 9001
2829
@@ -31,38 +32,66 @@ const (
3132)
3233
3334type Minio struct {
34- host string
35- port int
36- consolePort int
37- accessKey string
38- secretKey string
39- bucket string
40- region string
41- keep bool
35+ Host string `toml:"host"`
36+ Port int `toml:"port"`
37+ ConsolePort int `toml:"console_port"`
38+ AccessKey string `toml:"access_key"`
39+ SecretKey string `toml:"secret_key"`
40+ Bucket string `toml:"bucket"`
41+ Region string `toml:"region"`
42+ }
43+
44+ type Input = Minio
45+
46+ type Output struct {
47+ SecretKey string `toml:"secret_key"`
48+ AccessKey string `toml:"access_key"`
49+ Bucket string `toml:"bucket"`
50+ ConsoleURL string `toml:"console_url"`
51+ Endpoint string `toml:"endpoint"`
52+ DockerEndpoint string `toml:"docker_endpoint"`
53+ Region string `toml:"region"`
54+ UseCache bool `toml:"use_cache"`
55+ }
56+
57+ func (m Minio ) GetOutput () * Output {
58+ return & Output {
59+ AccessKey : m .GetAccessKey (),
60+ SecretKey : m .GetSecretKey (),
61+ Bucket : m .GetBucket (),
62+ ConsoleURL : m .GetConsoleURL (),
63+ Endpoint : m .GetEndpoint (),
64+ DockerEndpoint : m .GetDockerEndpoint (),
65+ Region : m .GetRegion (),
66+ }
4267}
4368
4469func (m Minio ) GetSecretKey () string {
45- return m .secretKey
70+ return m .SecretKey
4671}
4772
4873func (m Minio ) GetAccessKey () string {
49- return m .accessKey
74+ return m .AccessKey
5075}
5176
5277func (m Minio ) GetBucket () string {
53- return m .bucket
78+ return m .Bucket
5479}
5580
5681func (m Minio ) GetConsoleURL () string {
57- return fmt .Sprintf ("http://%s" , net .JoinHostPort (m .host , strconv .Itoa (m .consolePort )))
82+ return fmt .Sprintf ("http://%s" , net .JoinHostPort (m .Host , strconv .Itoa (m .ConsolePort )))
5883}
5984
6085func (m Minio ) GetEndpoint () string {
61- return fmt .Sprintf ("%s:%d" , m .host , m .port )
86+ return fmt .Sprintf ("%s:%d" , m .Host , m .Port )
87+ }
88+
89+ func (m Minio ) GetDockerEndpoint () string {
90+ return fmt .Sprintf ("%s:%d" , DefaultHost , m .Port )
6291}
6392
6493func (m Minio ) GetRegion () string {
65- return m .region
94+ return m .Region
6695}
6796
6897type Option func (* Minio )
@@ -73,40 +102,49 @@ func NewMinioFactory() ProviderFactory {
73102 return MinioFactory {}
74103}
75104
76- func (mf MinioFactory ) New (options ... Option ) (Provider , error ) {
77- m := & Minio {
78- port : DefaultPort ,
79- consolePort : DefaultConsolePort ,
80- accessKey : randomStr (accessKeyLength ),
81- secretKey : randomStr (secretKeyLength ),
82- bucket : DefaultBucket ,
83- region : DefaultRegion ,
84- keep : false ,
105+ func (mf MinioFactory ) NewFrom (input * Input ) (* Output , error ) {
106+ // Fill in defaults on empty
107+ err := mergo .Merge (input , DefaultMinio ())
108+ if err != nil {
109+ return nil , err
85110 }
86111
87- for _ , opt := range options {
88- opt (m )
112+ provider , err := mf .run (input )
113+ if err != nil {
114+ return nil , err
89115 }
116+ return provider .GetOutput (), nil
117+ }
90118
91- var (
92- tcRyukDisabled string
93- err error
94- )
119+ func DefaultMinio () * Minio {
120+ return & Minio {
121+ Host : DefaultHost ,
122+ Port : DefaultPort ,
123+ ConsolePort : DefaultConsolePort ,
124+ AccessKey : randomStr (accessKeyLength ),
125+ SecretKey : randomStr (secretKeyLength ),
126+ Bucket : DefaultBucket ,
127+ Region : DefaultRegion ,
128+ }
129+ }
95130
96- if m .keep {
97- // store original env var to value
98- tcRyukDisabled = os .Getenv ("TESTCONTAINERS_RYUK_DISABLED" )
99- err = os .Setenv ("TESTCONTAINERS_RYUK_DISABLED" , "true" )
131+ func (mf MinioFactory ) New (options ... Option ) (Provider , error ) {
132+ m := DefaultMinio ()
100133
101- if err != nil {
102- return nil , err
103- }
134+ for _ , opt := range options {
135+ opt (m )
104136 }
105137
138+ return mf .run (m )
139+ }
140+
141+ func (mf MinioFactory ) run (m * Minio ) (Provider , error ) {
142+ var err error
143+
106144 ctx := context .Background ()
107145 containerName := framework .DefaultTCName (DefaultName )
108- bindPort := fmt .Sprintf ("%d/tcp" , m .port )
109- bindConsolePort := fmt .Sprintf ("%d/tcp" , m .consolePort )
146+ bindPort := fmt .Sprintf ("%d/tcp" , m .Port )
147+ bindConsolePort := fmt .Sprintf ("%d/tcp" , m .ConsolePort )
110148 networks := []string {"compose_default" }
111149 networkAliases := map [string ][]string {
112150 "compose_default" : {DefaultName },
@@ -132,32 +170,32 @@ func (mf MinioFactory) New(options ...Option) (Provider, error) {
132170 bindConsolePort ,
133171 },
134172 Env : map [string ]string {
135- "MINIO_ROOT_USER" : m .accessKey ,
136- "MINIO_ROOT_PASSWORD" : m .secretKey ,
137- "MINIO_BUCKET" : DefaultBucket ,
173+ "MINIO_ROOT_USER" : m .AccessKey ,
174+ "MINIO_ROOT_PASSWORD" : m .SecretKey ,
175+ "MINIO_BUCKET" : m . Bucket ,
138176 },
139177 Entrypoint : []string {
140178 "minio" ,
141179 "server" ,
142180 "/data" ,
143181 "--address" ,
144- fmt .Sprintf (":%d" , m .port ),
182+ fmt .Sprintf (":%d" , m .Port ),
145183 "--console-address" ,
146- fmt .Sprintf (":%d" , m .consolePort ),
184+ fmt .Sprintf (":%d" , m .ConsolePort ),
147185 },
148186 HostConfigModifier : func (h * container.HostConfig ) {
149187 framework .NoDNS (true , h )
150188 h .PortBindings = nat.PortMap {
151189 nat .Port (bindPort ): []nat.PortBinding {
152190 {
153191 HostIP : "0.0.0.0" ,
154- HostPort : strconv .Itoa (m .port ),
192+ HostPort : strconv .Itoa (m .Port ),
155193 },
156194 },
157195 nat .Port (bindConsolePort ): []nat.PortBinding {
158196 {
159197 HostIP : "0.0.0.0" ,
160- HostPort : strconv .Itoa (m .consolePort ),
198+ HostPort : strconv .Itoa (m .ConsolePort ),
161199 },
162200 },
163201 }
@@ -171,13 +209,12 @@ func (mf MinioFactory) New(options ...Option) (Provider, error) {
171209 c , err := tc .GenericContainer (ctx , tc.GenericContainerRequest {
172210 ContainerRequest : req ,
173211 Started : true ,
174- Reuse : m .keep ,
175212 })
176213 if err != nil {
177214 return nil , err
178215 }
179216
180- m .host , err = framework .GetHost (c )
217+ m .Host , err = framework .GetHost (c )
181218 if err != nil {
182219 return nil , err
183220 }
@@ -201,44 +238,30 @@ func (mf MinioFactory) New(options ...Option) (Provider, error) {
201238 return nil , err
202239 }
203240
204- if m .keep {
205- // reverse env var to prev. value
206- err := os .Setenv ("TESTCONTAINERS_RYUK_DISABLED" , tcRyukDisabled )
207- if err != nil {
208- return nil , err
209- }
210- }
211-
212241 return m , nil
213242}
214243
215244func WithPort (port int ) Option {
216245 return func (m * Minio ) {
217- m .port = port
246+ m .Port = port
218247 }
219248}
220249
221250func WithConsolePort (consolePort int ) Option {
222251 return func (m * Minio ) {
223- m .consolePort = consolePort
224- }
225- }
226-
227- func WithKeep () Option {
228- return func (m * Minio ) {
229- m .keep = true
252+ m .ConsolePort = consolePort
230253 }
231254}
232255
233256func WithAccessKey (accessKey string ) Option {
234257 return func (m * Minio ) {
235- m .accessKey = accessKey
258+ m .AccessKey = accessKey
236259 }
237260}
238261
239262func WithSecretKey (secretKey string ) Option {
240263 return func (m * Minio ) {
241- m .secretKey = secretKey
264+ m .SecretKey = secretKey
242265 }
243266}
244267
0 commit comments