Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set RABBITMQ_DEFAULT_USER env var with withAdminUser #9571

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -105,11 +106,14 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
* @return The admin password for the <code>admin</code> account
*/
public String getAdminPassword() {
return adminPassword;
return this.adminPassword;
}

/**
* @return The admin user for the <code>admin</code> account
*/
public String getAdminUsername() {
return adminUsername;
return this.adminUsername;
}

public Integer getAmqpPort() {
Expand Down Expand Up @@ -156,6 +160,17 @@ public String getHttpsUrl() {
return "https://" + getHost() + ":" + getHttpsPort();
}

/**
* Sets the user for the admin (default is <pre>guest</pre>)
*
* @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 <pre>guest</pre>)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
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;
import java.security.NoSuchAlgorithmException;
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;
Expand All @@ -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(
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -283,15 +291,15 @@ 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");
kmf.init(ks, "password".toCharArray());

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");
Expand All @@ -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);
}
}
}
Loading