Skip to content

Commit

Permalink
Use random exposed ports for testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
bzablocki committed Jul 9, 2024
1 parent c3bd8ca commit d2912c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
package org.apache.beam.sdk.io.solace.it;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Container.ExecResult;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.solace.Service;
import org.testcontainers.solace.SolaceContainer;
Expand All @@ -36,30 +38,34 @@ public class SolaceContainerManager {
public static final String TOPIC_NAME = "test_topic";
private static final Logger LOG = LoggerFactory.getLogger(SolaceContainerManager.class);
private final SolaceContainer container;
int jcsmpPortMapped = findAvailablePort();
int sempPortMapped = findAvailablePort();

public SolaceContainerManager() {
public SolaceContainerManager() throws IOException {
this.container =
new SolaceContainer(DockerImageName.parse("solace/solace-pubsub-standard:10.7")) {
{
addFixedExposedPort(55555, 55555);
addFixedExposedPort(9000, 9000);
addFixedExposedPort(8080, 8080);
addFixedExposedPort(80, 80);
addFixedExposedPort(jcsmpPortMapped, 55555);
addFixedExposedPort(sempPortMapped, 8080);
}
}.withVpn(VPN_NAME)
.withCredentials(USERNAME, PASSWORD)
// .withExposedPorts(Service.SMF.getPort());
.withTopic(TOPIC_NAME, Service.SMF)
.withLogConsumer(new Slf4jLogConsumer(LOG));
container.addExposedPort(8080);
container.addExposedPort(55555);
}

public void start() {
container.start();
}

void createQueueWithSubscriptionTopic(String queueName) {
executeCommand(
"curl",
"http://localhost:8080/SEMP/v2/config/msgVpns/" + VPN_NAME + "/topicEndpoints",
"-X",
"GET",
"-u",
"admin:admin");
executeCommand(
"curl",
"http://localhost:8080/SEMP/v2/config/msgVpns/" + VPN_NAME + "/topicEndpoints",
Expand Down Expand Up @@ -105,8 +111,7 @@ void createQueueWithSubscriptionTopic(String queueName) {

private void executeCommand(String... command) {
try {
org.testcontainers.containers.Container.ExecResult execResult =
container.execInContainer(command);
ExecResult execResult = container.execInContainer(command);
if (execResult.getExitCode() != 0) {
logCommandError(execResult.getStderr(), command);
} else {
Expand Down Expand Up @@ -165,4 +170,19 @@ public void sendToTopic(String payload, List<String> additionalHeaders) {

executeCommand(command.toArray(new String[0]));
}

private static int findAvailablePort() throws IOException {
ServerSocket s = new ServerSocket(0);
try {
return s.getLocalPort();
} finally {
s.close();
try {
// Some systems don't free the port for future use immediately.
Thread.sleep(100);
} catch (InterruptedException exn) {
// ignore
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.io.solace.SolaceIO;
import org.apache.beam.sdk.io.solace.broker.BasicAuthJcsmpSessionServiceFactory;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class SolaceIOIT {
@Rule public final TestPipeline readPipeline = TestPipeline.fromOptions(readPipelineOptions);

@BeforeClass
public static void setup() {
public static void setup() throws IOException {
solaceContainerManager = new SolaceContainerManager();
solaceContainerManager.start();
}
Expand Down Expand Up @@ -89,14 +90,14 @@ public void testRead() {
.withMaxNumConnections(1)
.withSempClientFactory(
BasicAuthSempClientFactory.builder()
.host("http://localhost:8080")
.host("http://localhost:" + solaceContainerManager.sempPortMapped)
.username("admin")
.password("admin")
.vpnName(SolaceContainerManager.VPN_NAME)
.build())
.withSessionServiceFactory(
BasicAuthJcsmpSessionServiceFactory.builder()
.host("localhost")
.host("localhost:" + solaceContainerManager.jcsmpPortMapped)
.username(SolaceContainerManager.USERNAME)
.password(SolaceContainerManager.PASSWORD)
.vpnName(SolaceContainerManager.VPN_NAME)
Expand Down

0 comments on commit d2912c4

Please sign in to comment.