From c50d51e2257604c62409be27613ef3cfd68d3cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 26 Nov 2024 11:06:56 -0600 Subject: [PATCH] Set `RABBITMQ_DEFAULT_USER` env var with `withAdminUser` Currently, `RabbitMQContainer` allow to customize the admin password. This commit allows to customize the user. Fixes #9570 --- .../containers/RabbitMQContainer.java | 29 +++++-- .../containers/RabbitMQContainerTest.java | 75 +++++++++++++------ 2 files changed, 75 insertions(+), 29 deletions(-) diff --git a/modules/rabbitmq/src/main/java/org/testcontainers/containers/RabbitMQContainer.java b/modules/rabbitmq/src/main/java/org/testcontainers/containers/RabbitMQContainer.java index bac998b85ac..9eff3fa83f2 100644 --- a/modules/rabbitmq/src/main/java/org/testcontainers/containers/RabbitMQContainer.java +++ b/modules/rabbitmq/src/main/java/org/testcontainers/containers/RabbitMQContainer.java @@ -9,7 +9,6 @@ import org.testcontainers.utility.MountableFile; import java.io.IOException; -import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -76,14 +75,16 @@ public RabbitMQContainer(final DockerImageName dockerImageName) { addExposedPorts(DEFAULT_AMQP_PORT, DEFAULT_AMQPS_PORT, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT); - this.waitStrategy = - Wait.forLogMessage(".*Server startup complete.*", 1).withStartupTimeout(Duration.ofSeconds(60)); + waitingFor(Wait.forLogMessage(".*Server startup complete.*", 1)); } @Override protected void configure() { - if (adminPassword != null) { - addEnv("RABBITMQ_DEFAULT_PASS", adminPassword); + if (this.adminUsername != null) { + addEnv("RABBITMQ_DEFAULT_USER", this.adminUsername); + } + if (this.adminPassword != null) { + addEnv("RABBITMQ_DEFAULT_PASS", this.adminPassword); } } @@ -105,11 +106,14 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) { * @return The admin password for the admin account */ public String getAdminPassword() { - return adminPassword; + return this.adminPassword; } + /** + * @return The admin user for the admin account + */ public String getAdminUsername() { - return adminUsername; + return this.adminUsername; } public Integer getAmqpPort() { @@ -156,6 +160,17 @@ public String getHttpsUrl() { return "https://" + getHost() + ":" + getHttpsPort(); } + /** + * Sets the user for the admin (default is
guest
) + * + * @param adminUsername The admin user. + * @return This container. + */ + public RabbitMQContainer withAdminUser(final String adminUsername) { + this.adminUsername = adminUsername; + return this; + } + /** * Sets the password for the admin (default is
guest
) * diff --git a/modules/rabbitmq/src/test/java/org/testcontainers/containers/RabbitMQContainerTest.java b/modules/rabbitmq/src/test/java/org/testcontainers/containers/RabbitMQContainerTest.java index d26b4d6e924..728b2622b17 100644 --- a/modules/rabbitmq/src/test/java/org/testcontainers/containers/RabbitMQContainerTest.java +++ b/modules/rabbitmq/src/test/java/org/testcontainers/containers/RabbitMQContainerTest.java @@ -5,13 +5,15 @@ import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DeliverCallback; import org.junit.Test; import org.testcontainers.containers.RabbitMQContainer.SslVerification; import org.testcontainers.utility.MountableFile; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; @@ -19,6 +21,7 @@ import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.Collections; +import java.util.concurrent.TimeoutException; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; @@ -40,32 +43,19 @@ public class RabbitMQContainerTest { @Test public void shouldCreateRabbitMQContainer() { try (RabbitMQContainer container = new RabbitMQContainer(RabbitMQTestImages.RABBITMQ_IMAGE)) { + container.start(); + assertThat(container.getAdminPassword()).isEqualTo("guest"); assertThat(container.getAdminUsername()).isEqualTo("guest"); - container.start(); - assertThat(container.getAmqpsUrl()) - .isEqualTo( - String.format("amqps://%s:%d", container.getHost(), container.getMappedPort(DEFAULT_AMQPS_PORT)) - ); + .isEqualTo(String.format("amqps://%s:%d", container.getHost(), container.getAmqpsPort())); assertThat(container.getAmqpUrl()) - .isEqualTo( - String.format("amqp://%s:%d", container.getHost(), container.getMappedPort(DEFAULT_AMQP_PORT)) - ); + .isEqualTo(String.format("amqp://%s:%d", container.getHost(), container.getAmqpPort())); assertThat(container.getHttpsUrl()) - .isEqualTo( - String.format("https://%s:%d", container.getHost(), container.getMappedPort(DEFAULT_HTTPS_PORT)) - ); + .isEqualTo(String.format("https://%s:%d", container.getHost(), container.getHttpsPort())); assertThat(container.getHttpUrl()) - .isEqualTo( - String.format("http://%s:%d", container.getHost(), container.getMappedPort(DEFAULT_HTTP_PORT)) - ); - - assertThat(container.getHttpsPort()).isEqualTo(container.getMappedPort(DEFAULT_HTTPS_PORT)); - assertThat(container.getHttpPort()).isEqualTo(container.getMappedPort(DEFAULT_HTTP_PORT)); - assertThat(container.getAmqpsPort()).isEqualTo(container.getMappedPort(DEFAULT_AMQPS_PORT)); - assertThat(container.getAmqpPort()).isEqualTo(container.getMappedPort(DEFAULT_AMQP_PORT)); + .isEqualTo(String.format("http://%s:%d", container.getHost(), container.getHttpPort())); assertThat(container.getLivenessCheckPortNumbers()) .containsExactlyInAnyOrder( @@ -74,6 +64,24 @@ public void shouldCreateRabbitMQContainer() { container.getMappedPort(DEFAULT_HTTP_PORT), container.getMappedPort(DEFAULT_HTTPS_PORT) ); + + assertFunctionality(container); + } + } + + @Test + public void shouldCreateRabbitMQContainerWithCustomCredentials() { + try ( + RabbitMQContainer container = new RabbitMQContainer(RabbitMQTestImages.RABBITMQ_IMAGE) + .withAdminUser("admin") + .withAdminPassword("admin") + ) { + container.start(); + + assertThat(container.getAdminPassword()).isEqualTo("admin"); + assertThat(container.getAdminUsername()).isEqualTo("admin"); + + assertFunctionality(container); } } @@ -283,7 +291,7 @@ private SSLContext createSslContext( KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load( - new FileInputStream(new File(classLoader.getResource(keystoreFile).getFile())), + Files.newInputStream(new File(classLoader.getResource(keystoreFile).getFile()).toPath()), keystorePassword.toCharArray() ); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); @@ -291,7 +299,7 @@ private SSLContext createSslContext( KeyStore trustStore = KeyStore.getInstance("PKCS12"); trustStore.load( - new FileInputStream(new File(classLoader.getResource(truststoreFile).getFile())), + Files.newInputStream(new File(classLoader.getResource(truststoreFile).getFile()).toPath()), truststorePassword.toCharArray() ); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); @@ -301,4 +309,27 @@ private SSLContext createSslContext( c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); return c; } + + private void assertFunctionality(RabbitMQContainer container) { + String queueName = "test-queue"; + String text = "Hello World!"; + + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost(container.getHost()); + factory.setPort(container.getAmqpPort()); + factory.setUsername(container.getAdminUsername()); + factory.setPassword(container.getAdminPassword()); + try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { + channel.queueDeclare(queueName, false, false, false, null); + channel.basicPublish("", queueName, null, text.getBytes()); + + DeliverCallback deliverCallback = (consumerTag, delivery) -> { + String message = new String(delivery.getBody(), StandardCharsets.UTF_8); + assertThat(message).isEqualTo(text); + }; + channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {}); + } catch (IOException | TimeoutException e) { + throw new RuntimeException(e); + } + } }