11package org .testcontainers .containers ;
22
3+ import com .github .dockerjava .api .command .InspectContainerResponse ;
34import org .testcontainers .containers .wait .strategy .Wait ;
45import org .testcontainers .utility .DockerImageName ;
56
7+ import java .io .IOException ;
68import java .time .Duration ;
79import java .time .temporal .ChronoUnit ;
10+ import java .util .ArrayList ;
11+ import java .util .Arrays ;
12+ import java .util .List ;
13+ import java .util .stream .Collectors ;
14+
15+ import static java .lang .String .format ;
816
917/**
1018 * Testcontainers implementation for MinIO.
@@ -29,12 +37,15 @@ public class MinIOContainer extends GenericContainer<MinIOContainer> {
2937
3038 private static final String DEFAULT_PASSWORD = "minioadmin" ;
3139
40+ private final List <String > buckets = new ArrayList <>();
41+
3242 private String userName ;
3343
3444 private String password ;
3545
3646 /**
3747 * Constructs a MinIO container from the dockerImageName
48+ *
3849 * @param dockerImageName the full image name to use
3950 */
4051 public MinIOContainer (final String dockerImageName ) {
@@ -43,6 +54,7 @@ public MinIOContainer(final String dockerImageName) {
4354
4455 /**
4556 * Constructs a MinIO container from the dockerImageName
57+ *
4658 * @param dockerImageName the full image name to use
4759 */
4860 public MinIOContainer (final DockerImageName dockerImageName ) {
@@ -60,6 +72,7 @@ public MinIOContainer(final DockerImageName dockerImageName) {
6072
6173 /**
6274 * Overrides the DEFAULT_USER
75+ *
6376 * @param userName the Root user to override
6477 * @return this
6578 */
@@ -70,6 +83,7 @@ public MinIOContainer withUserName(String userName) {
7083
7184 /**
7285 * Overrides the DEFAULT_PASSWORD
86+ *
7387 * @param password the Root user's password to override
7488 * @return this
7589 */
@@ -78,6 +92,17 @@ public MinIOContainer withPassword(String password) {
7892 return this ;
7993 }
8094
95+ /**
96+ * Create buckets after container started
97+ *
98+ * @param buckets bucket list
99+ * @return this
100+ */
101+ public MinIOContainer withBuckets (String ... buckets ) {
102+ this .buckets .addAll (Arrays .stream (buckets ).collect (Collectors .toList ()));
103+ return this ;
104+ }
105+
81106 /**
82107 * Configures the MinIO container
83108 */
@@ -99,7 +124,7 @@ public void configure() {
99124 * @return the URL to upload/download objects from
100125 */
101126 public String getS3URL () {
102- return String . format ("http://%s:%s" , this .getHost (), getMappedPort (MINIO_S3_PORT ));
127+ return format ("http://%s:%s" , this .getHost (), getMappedPort (MINIO_S3_PORT ));
103128 }
104129
105130 /**
@@ -115,4 +140,28 @@ public String getUserName() {
115140 public String getPassword () {
116141 return this .password ;
117142 }
143+
144+ @ Override
145+ protected void containerIsStarted (InspectContainerResponse containerInfo ) {
146+ if (!buckets .isEmpty ()) {
147+ try {
148+ ExecResult setAliasExecResult = execInContainer ("mc" , "alias" , "set" , "local" , format ("http://localhost:%s" , MINIO_S3_PORT ), userName , password );
149+ if (setAliasExecResult .getExitCode () == 0 ) {
150+ for (String bucket : buckets ) {
151+ ExecResult createBucketExecResult = execInContainer ("mc" , "mb" , format ("local/%s" , bucket ));
152+ if (createBucketExecResult .getExitCode () == 0 ) {
153+ logger ().info ("Create bucket {}" , bucket );
154+ } else {
155+ logger ().error ("Cannot create bucket {}: {}" , bucket , createBucketExecResult .getStderr ());
156+ }
157+ }
158+ } else {
159+ logger ().error ("Cannot set local alias {}" , setAliasExecResult .getStderr ());
160+ }
161+ } catch (IOException | InterruptedException e ) {
162+ logger ().error ("Cannot create buckets {}" , buckets , e );
163+ }
164+ }
165+ }
166+
118167}
0 commit comments