|
| 1 | +package org.testcontainers.rabbitmq; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import org.testcontainers.containers.GenericContainer; |
| 5 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 6 | +import org.testcontainers.utility.DockerImageName; |
| 7 | +import org.testcontainers.utility.MountableFile; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * Testcontainers implementation for RabbitMQ. |
| 15 | + * <p> |
| 16 | + * Supported image: {@code rabbitmq} |
| 17 | + * <p> |
| 18 | + * Exposed ports: |
| 19 | + * <ul> |
| 20 | + * <li>5671 (AMQPS)</li> |
| 21 | + * <li>5672 (AMQP)</li> |
| 22 | + * <li>15671 (HTTPS)</li> |
| 23 | + * <li>15672 (HTTP)</li> |
| 24 | + * </ul> |
| 25 | + */ |
| 26 | +public class RabbitMQContainer extends GenericContainer<RabbitMQContainer> { |
| 27 | + |
| 28 | + /** |
| 29 | + * The image defaults to the official RabbitMQ image: <a href="https://hub.docker.com/_/rabbitmq/">RabbitMQ</a>. |
| 30 | + */ |
| 31 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("rabbitmq"); |
| 32 | + |
| 33 | + private static final int DEFAULT_AMQP_PORT = 5672; |
| 34 | + |
| 35 | + private static final int DEFAULT_AMQPS_PORT = 5671; |
| 36 | + |
| 37 | + private static final int DEFAULT_HTTPS_PORT = 15671; |
| 38 | + |
| 39 | + private static final int DEFAULT_HTTP_PORT = 15672; |
| 40 | + |
| 41 | + private String adminPassword = "guest"; |
| 42 | + |
| 43 | + private String adminUsername = "guest"; |
| 44 | + |
| 45 | + private final List<List<String>> values = new ArrayList<>(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a RabbitMQ container using a specific docker image. |
| 49 | + * |
| 50 | + * @param dockerImageName The docker image to use. |
| 51 | + */ |
| 52 | + public RabbitMQContainer(String dockerImageName) { |
| 53 | + this(DockerImageName.parse(dockerImageName)); |
| 54 | + } |
| 55 | + |
| 56 | + public RabbitMQContainer(DockerImageName dockerImageName) { |
| 57 | + super(dockerImageName); |
| 58 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 59 | + |
| 60 | + addExposedPorts(DEFAULT_AMQP_PORT, DEFAULT_AMQPS_PORT, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT); |
| 61 | + |
| 62 | + waitingFor(Wait.forLogMessage(".*Server startup complete.*", 1)); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void configure() { |
| 67 | + if (this.adminUsername != null) { |
| 68 | + addEnv("RABBITMQ_DEFAULT_USER", this.adminUsername); |
| 69 | + } |
| 70 | + if (this.adminPassword != null) { |
| 71 | + addEnv("RABBITMQ_DEFAULT_PASS", this.adminPassword); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 77 | + values.forEach(command -> { |
| 78 | + try { |
| 79 | + ExecResult execResult = execInContainer(command.toArray(new String[0])); |
| 80 | + if (execResult.getExitCode() != 0) { |
| 81 | + logger().error("Could not execute command {}: {}", command, execResult.getStderr()); |
| 82 | + } |
| 83 | + } catch (IOException | InterruptedException e) { |
| 84 | + logger().error("Could not execute command {}: {}", command, e.getMessage()); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return The admin password for the <code>admin</code> account |
| 91 | + */ |
| 92 | + public String getAdminPassword() { |
| 93 | + return this.adminPassword; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return The admin user for the <code>admin</code> account |
| 98 | + */ |
| 99 | + public String getAdminUsername() { |
| 100 | + return this.adminUsername; |
| 101 | + } |
| 102 | + |
| 103 | + public Integer getAmqpPort() { |
| 104 | + return getMappedPort(DEFAULT_AMQP_PORT); |
| 105 | + } |
| 106 | + |
| 107 | + public Integer getAmqpsPort() { |
| 108 | + return getMappedPort(DEFAULT_AMQPS_PORT); |
| 109 | + } |
| 110 | + |
| 111 | + public Integer getHttpsPort() { |
| 112 | + return getMappedPort(DEFAULT_HTTPS_PORT); |
| 113 | + } |
| 114 | + |
| 115 | + public Integer getHttpPort() { |
| 116 | + return getMappedPort(DEFAULT_HTTP_PORT); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return AMQP URL for use with AMQP clients. |
| 121 | + */ |
| 122 | + public String getAmqpUrl() { |
| 123 | + return "amqp://" + getHost() + ":" + getAmqpPort(); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @return AMQPS URL for use with AMQPS clients. |
| 128 | + */ |
| 129 | + public String getAmqpsUrl() { |
| 130 | + return "amqps://" + getHost() + ":" + getAmqpsPort(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @return URL of the HTTP management endpoint. |
| 135 | + */ |
| 136 | + public String getHttpUrl() { |
| 137 | + return "http://" + getHost() + ":" + getHttpPort(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return URL of the HTTPS management endpoint. |
| 142 | + */ |
| 143 | + public String getHttpsUrl() { |
| 144 | + return "https://" + getHost() + ":" + getHttpsPort(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the user for the admin (default is <pre>guest</pre>) |
| 149 | + * |
| 150 | + * @param adminUsername The admin user. |
| 151 | + * @return This container. |
| 152 | + */ |
| 153 | + public RabbitMQContainer withAdminUser(final String adminUsername) { |
| 154 | + this.adminUsername = adminUsername; |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Sets the password for the admin (default is <pre>guest</pre>) |
| 160 | + * |
| 161 | + * @param adminPassword The admin password. |
| 162 | + * @return This container. |
| 163 | + */ |
| 164 | + public RabbitMQContainer withAdminPassword(final String adminPassword) { |
| 165 | + this.adminPassword = adminPassword; |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Overwrites the default RabbitMQ configuration file with the supplied one. |
| 171 | + * |
| 172 | + * @param rabbitMQConf The rabbitmq.conf file to use (in sysctl format, don't forget empty line in the end of file) |
| 173 | + * @return This container. |
| 174 | + */ |
| 175 | + public RabbitMQContainer withRabbitMQConfig(MountableFile rabbitMQConf) { |
| 176 | + return withRabbitMQConfigSysctl(rabbitMQConf); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Overwrites the default RabbitMQ configuration file with the supplied one. |
| 181 | + * |
| 182 | + * This function doesn't work with RabbitMQ < 3.7. |
| 183 | + * |
| 184 | + * This function and the Sysctl format is recommended for RabbitMQ >= 3.7 |
| 185 | + * |
| 186 | + * @param rabbitMQConf The rabbitmq.config file to use (in sysctl format, don't forget empty line in the end of file) |
| 187 | + * @return This container. |
| 188 | + */ |
| 189 | + public RabbitMQContainer withRabbitMQConfigSysctl(MountableFile rabbitMQConf) { |
| 190 | + withEnv("RABBITMQ_CONFIG_FILE", "/etc/rabbitmq/rabbitmq-custom.conf"); |
| 191 | + return withCopyFileToContainer(rabbitMQConf, "/etc/rabbitmq/rabbitmq-custom.conf"); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Overwrites the default RabbitMQ configuration file with the supplied one. |
| 196 | + * |
| 197 | + * @param rabbitMQConf The rabbitmq.config file to use (in erlang format) |
| 198 | + * @return This container. |
| 199 | + */ |
| 200 | + public RabbitMQContainer withRabbitMQConfigErlang(MountableFile rabbitMQConf) { |
| 201 | + withEnv("RABBITMQ_CONFIG_FILE", "/etc/rabbitmq/rabbitmq-custom.config"); |
| 202 | + return withCopyFileToContainer(rabbitMQConf, "/etc/rabbitmq/rabbitmq-custom.config"); |
| 203 | + } |
| 204 | +} |
0 commit comments