|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 4 | +import org.testcontainers.utility.DockerImageName; |
| 5 | + |
| 6 | +import java.time.Duration; |
| 7 | +import java.time.temporal.ChronoUnit; |
| 8 | + |
| 9 | +/** |
| 10 | + * Testcontainers implementation for MinIO. |
| 11 | + * <p> |
| 12 | + * Supported image: {@code minio/minio} |
| 13 | + * <p> |
| 14 | + * Exposed ports: |
| 15 | + * <ul> |
| 16 | + * <li>S3: 9000</li> |
| 17 | + * <li>Console: 9001</li> |
| 18 | + * </ul> |
| 19 | + */ |
| 20 | +public class MinIOContainer extends GenericContainer<MinIOContainer> { |
| 21 | + |
| 22 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("minio/minio"); |
| 23 | + |
| 24 | + private static final int MINIO_S3_PORT = 9000; |
| 25 | + |
| 26 | + private static final int MINIO_UI_PORT = 9001; |
| 27 | + |
| 28 | + private static final String DEFAULT_USER = "minioadmin"; |
| 29 | + |
| 30 | + private static final String DEFAULT_PASSWORD = "minioadmin"; |
| 31 | + |
| 32 | + private String userName; |
| 33 | + |
| 34 | + private String password; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructs a MinIO container from the dockerImageName |
| 38 | + * @param dockerImageName the full image name to use |
| 39 | + */ |
| 40 | + public MinIOContainer(final String dockerImageName) { |
| 41 | + this(DockerImageName.parse(dockerImageName)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Constructs a MinIO container from the dockerImageName |
| 46 | + * @param dockerImageName the full image name to use |
| 47 | + */ |
| 48 | + public MinIOContainer(final DockerImageName dockerImageName) { |
| 49 | + super(dockerImageName); |
| 50 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Overrides the DEFAULT_USER |
| 55 | + * @param userName the Root user to override |
| 56 | + * @return this |
| 57 | + */ |
| 58 | + public MinIOContainer withUserName(String userName) { |
| 59 | + this.userName = userName; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Overrides the DEFAULT_PASSWORD |
| 65 | + * @param password the Root user's password to override |
| 66 | + * @return this |
| 67 | + */ |
| 68 | + public MinIOContainer withPassword(String password) { |
| 69 | + this.password = password; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Configures the MinIO container |
| 75 | + */ |
| 76 | + @Override |
| 77 | + public void configure() { |
| 78 | + withExposedPorts(MinIOContainer.MINIO_S3_PORT, MinIOContainer.MINIO_UI_PORT); |
| 79 | + |
| 80 | + if (this.userName != null) { |
| 81 | + addEnv("MINIO_ROOT_USER", this.userName); |
| 82 | + } else { |
| 83 | + this.userName = DEFAULT_USER; |
| 84 | + } |
| 85 | + if (this.password != null) { |
| 86 | + addEnv("MINIO_ROOT_PASSWORD", this.password); |
| 87 | + } else { |
| 88 | + this.password = DEFAULT_PASSWORD; |
| 89 | + } |
| 90 | + |
| 91 | + withCommand("server", "--console-address", ":" + MINIO_UI_PORT, "/data"); |
| 92 | + |
| 93 | + waitingFor( |
| 94 | + Wait |
| 95 | + .forLogMessage(".*Status: 1 Online, 0 Offline..*", 1) |
| 96 | + .withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS)) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return the URL to upload/download objects from |
| 102 | + */ |
| 103 | + public String getS3URL() { |
| 104 | + return String.format("http://%s:%s", this.getHost(), getMappedPort(MINIO_S3_PORT)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return the Username for the Root user |
| 109 | + */ |
| 110 | + public String getUserName() { |
| 111 | + return this.userName; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return the password for the Root user |
| 116 | + */ |
| 117 | + public String getPassword() { |
| 118 | + return this.password; |
| 119 | + } |
| 120 | +} |
0 commit comments